require File.dirname(__FILE__) + '/../spec_helper.rb' describe Map do before(:each) do Fabric.options[:map_root] = File.join(File.dirname(__FILE__), '..') @map = Map.create end describe "when created" do it "should be valid when created" do @map.should be_a Map end it "should have a blank collection attribute for roles" do @map.roles.should == [] end it "should have a blank collection attribute for users" do @map.users.should == [] end it "should default the key repository to 'keys'" do @map.key_repository.should == 'keys' end end describe "when read" do it "should return true" do @map.read!.should be_true end end describe "'draw' called with a block" do it "should return the map" do map = Map.draw do |map| 1 end map.should be_a Map end end describe "when passed a non-existent directory as key repository" do it "should raise a LoadError" do lambda { @map.key_repository('blah') }.should raise_error LoadError end end describe "with a mock key repository" do it "should update the key repository without raising an error" do lambda { @map.key_repository('support/mock_key_repo') }.should_not raise_error LoadError end it "should know the full path of the repository" do @map.key_repository 'support/mock_key_repo' @map.expanded_key_repository_path.should == File.join(File.dirname(__FILE__), '..','support', 'mock_key_repo') end end describe "when given a role" do it "should add that role to the role list" do @map.role 'test' @map.roles.first(:name => 'test').should be_a Role end describe "with host(s)" do it "should add a server object when passed one host" do @map.role 'test', '127.0.0.1' @map.roles.first(:name =>'test').servers.first.should be_a Server end it "should add multiple server objects when passed multiple hosts" do hosts = [] (rand(4) + 1).times { hosts << '127.0.0.1' } @map.role 'test', *hosts servers = @map.roles.first(:name => 'test').servers servers.count.should == hosts.count servers.each do |server| server.should be_a Server end end end end describe "with user(s)" do it "should add user to the user list" do @map.user 'test' @map.users.first(:name => 'test').should be_a User end describe "with a user with an ssh key in the mock key repository" do before(:each) do @map.key_repository 'support/mock_key_repo' @key_file = File.join(@map.expanded_key_repository_path, 'fabric_test_user.pub') File.open(@key_file, 'w+') do |f| f.write('test key') end end after(:each) do File.delete(@key_file) end it "should add the user's ssh key from the key repository" do @map.user 'fabric_test_user' @map.users.first(:name => 'fabric_test_user').keys.first.public_key.should == 'test key' end end it "should add multiple users to the user list" do users = [] (rand(4) + 1).times { |i| users << "user_#{i}" } @map.user *users @map.users.count.should == users.count @map.users.all.each do |user| user.should be_a User end end end describe "when given a grant" do before(:each) do @map.role 'test', '127.0.0.1' @map.user 'test_user' end it "should add the map's users to the role" do @map.roles.first(:name => 'test').users.count.should == 0 @map.grant 'test_user', 'test' @map.roles.first(:name => 'test').users.count.should == 1 end describe "for multiple users" do before(:each) do @map.user 'another test' end it "should add multiple users to the role" do @map.grant ['test_user', 'another test'], 'test' @map.roles.first(:name => 'test').users.count.should == 2 end it "should add all the users to the role when passed :all" do @map.grant :all, 'test' @map.roles.first(:name => 'test').users.count.should == 2 end end describe "for all roles" do before(:each) do @map.role 'test again', '127.0.0.1' end it "should add the user to multiple roles" do @map.grant 'test_user', ['test', 'test again'] @map.roles.each do |role| role.users.count.should == 1 end end it "should add the user to all the roles when passed :all" do @map.grant 'test_user', :all @map.roles.each do |role| role.users.count.should == 1 end end end it "should raise a Object Not Found if an invalid user identifier is passed" do lambda { @map.grant 'fake_user', 'test' }.should raise_error(DataMapper::ObjectNotFoundError) end it "should raise a Object Not Found if an invalid role identifier is passed" do lambda { @map.grant 'user', 'fake_test' }.should raise_error(DataMapper::ObjectNotFoundError) end end describe "with a namespace" do it "should be an instance of Map when created" do namespace = @map.namespace namespace.should be_a Map end it "should know its parent when created" do namespace = @map.namespace namespace.parent.should == @map end describe "with users" do before(:each) do @map.users.create(:name => 'test') @namespace = @map.namespace end it "should inherit its parents users" do @namespace.users.should == @map.users end it "should add users without adding them to the parent" do @namespace.users.create(:name => 'test two') @namespace.users.length.should == 2 @map.users.length.should == 1 end end end end