require'rubygems' require'rspec' require 'carioca' $debug = true describe Carioca do before :all do FileUtils.rm_rf("/tmp/log.file") $carioca = Carioca::Services::Registry.init :file => 'spec/config/services.registry', :debug => $debug end subject { Carioca } it { should be_an_instance_of Module} context "Carioca::Services" do subject { Carioca::Services } it { should be_an_instance_of Module } end context "Carioca::Services::Registry" do subject { $carioca } context "#init" do it { should be_an_instance_of Carioca::Services::Registry } it { $carioca.list.keys.should include "logger" } it "should log Registry starting and logger init if debug mode", :if => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: Registry started, service logger preloaded/).size.should eq 1 end it "should not log Registry starting and logger init if not debug mode", :unless => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: Registry started, service logger preloaded/).size.should eq 0 end it "should be possible to log with this primary service" do $logger = subject.get_service :name => 'logger' $logger.info("test").should eq true open('/tmp/log.file').grep(/INFO -- : test/).size.should eq 1 $logger.warn("test").should eq true open('/tmp/log.file').grep(/WARN -- : test/).size.should eq 1 $logger.error("test").should eq true open('/tmp/log.file').grep(/ERROR -- : test/).size.should eq 1 $logger.fatal("test").should eq true open('/tmp/log.file').grep(/FATAL -- : test/).size.should eq 1 $logger.info("Program") { "running" } open('/tmp/log.file').grep(/INFO -- Program: running/).size.should eq 1 end end context "attributs" do context "#debug (RW)" do it { should respond_to :debug } it { should respond_to :debug= } it "should be true if debug mode", :if => $debug do subject.debug.should eq true end it "should be false if not debug mode", :unless => $debug do subject.debug.should eq false end end context "#list (RO)" do it { should respond_to :list } it { should_not respond_to :list= } it { subject.list.should be_an_instance_of Hash } it { $carioca.list.keys.sort.should eq ["configuration", "debug", "dummy", "logger", "uuid"] } end context "#loaded_services (RO)" do it { should respond_to :loaded_services } it { should_not respond_to :loaded_services= } it { subject.loaded_services.should be_an_instance_of Hash } it { $carioca.loaded_services.keys.should eq ["logger"] } end end context "#start_service" do it { should respond_to :start_service } it { should respond_to :get_service } context "Builtin services" do context "Logger service" do it "should be possible to get the logger service" do $logger = subject.get_service :name => 'logger' end it "should log if debug mode", :if => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: getting service logger/).size.should >= 1 end it "should not log if debug mode", :unless => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: getting service logger/).size.should eq 0 end end context "Debug Proxy Service" do it "should start the registry with a service debug" do myservice = subject.start_service :name => 'debug' , :params => {:service => 'configuration'} myservice.test 'titi' end it { subject.list.keys.should include "debug" } it "should log a proxy service log sequence" do open('/tmp/log.file').grep(/DEBUG -- ProxyDebug: BEGIN CALL for mapped service configuration/).size.should eq 1 open('/tmp/log.file').grep(/DEBUG -- ProxyDebug: called: test/).size.should eq 1 open('/tmp/log.file').grep(/DEBUG -- ProxyDebug: args : titi/).size.should eq 1 open('/tmp/log.file').grep(/DEBUG -- ProxyDebug: => returned: toto/).size.should eq 1 open('/tmp/log.file').grep(/DEBUG -- ProxyDebug: END CALL/).size.should eq 1 end end end context "External services" do it "should start a dummy service precised in registry YAML configuration" do $dummy = subject.start_service :name => 'dummy' $dummy.should be_an_instance_of ExternalServices::Dummy end it { subject.list.keys.should include "dummy" } it "should be possible to use test method of the dummy service, and return 'OK'" do $dummy.test.should eq 'OK' end it "should start a gem service precised in registry YAML configuration" do $uuid = subject.start_service :name => 'uuid' $uuid.should be_an_instance_of UUID end it { subject.list.keys.should include "uuid" } it "should be possible to execute a method with the gem service" do $uuid.generate.should be_a_kind_of String end end end context "#stop_service" do it { should respond_to :stop_service } it "should raise ArgumentError if the option hash argument passed not include :name" do lambda { subject.stop_service }.should raise_error ArgumentError lambda { subject.stop_service(:notname => 'debug') }.should raise_error ArgumentError end it "should return true if service really stop" do subject.stop_service({:name => 'dummy'}).should eq true end it "should log if debug mode", :if => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: Service dummy stopped/).size.should >= 1 end it "should not log if debug mode", :unless => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: Service dummy stopped/).size.should = 0 end it "should return false if service not already running" do subject.stop_service({:name => 'dum'}).should eq false end it "should log if debug mode and service not loaded", :if => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: Service dum not loaded/).size.should >= 1 end it "should not log if debug mode and service not loaded", :unless => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: Service dum not loaded/).size.should = 0 end end context "#restart_service" do pending end context "#discover_builtin" do pending end context "#register_service" do pending end context "#unregister_service" do pending end context "#save!" do it { should respond_to :save! } it "should save the config" do subject.save!.should eq true end end context "#close" do context "Closing the Carioca registry" do it { should respond_to :close } it "should close" do subject.close.should eq true end it "should log a registry closing notification if debug mode", :if => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: closing Registry .../).size.should eq 1 end it "should not log a registry closing notification if not debug mode", :unless => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: closing Registry .../).size.should eq 0 end it "should log a stopping notification for each services if debug mode", :if => $debug do ['debug','uuid','configuration'].each do |service| open('/tmp/log.file').grep(/DEBUG -- Carioca: Stopping service #{service} .../).size.should eq 1 end end it "should not log a stopping notification for each services if not debug mode", :unless => $debug do ['debug','uuid','configuration'].each do |service| open('/tmp/log.file').grep(/DEBUG -- Carioca: Stopping service #{service} .../).size.should eq 0 end end it "should log a registry closing confirmation if debug mode", :if => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: Registry services closed, logger will be closed asynchronously/).size.should eq 1 end it "should not log a registry closing confirmation if not debug mode", :unless => $debug do open('/tmp/log.file').grep(/DEBUG -- Carioca: Registry services closed, logger will be closed asynchronously/).size.should eq 0 end it "should loaded services empty" do subject.loaded_services.empty?.should eq true end end end end end