Permalink
Browse files

Add unit tests and minor fixes for the apply_pre_chef call.

  • Loading branch information...
1 parent 252df5b commit 5d672cd92e6d8db9a662b9a9e37569d00d89a444 @galthaus galthaus committed Oct 22, 2012
@@ -34,33 +34,34 @@ def apply_role_pre_chef_call(old_config, new_config, all_nodes)
all_nodes.each do |node|
admin_address = node.address.addr
- chash = new_config.active_config.get_node_config_hash(node)
+ chash = new_config.get_node_config_hash(node)
chash[:mysql] = {} unless chash[:mysql]
chash[:mysql][:api_bind_host] = admin_address
- new_config.active_config.set_node_config_hash(node, chash)
+ new_config.set_node_config_hash(node, chash)
end
hash = new_config.config_hash
+ hash["mysql"] = {} unless hash["mysql"]
hash["mysql"]["server_debian_password"] = random_password if hash["mysql"]["server_debian_password"].nil?
hash["mysql"]["server_root_password"] = random_password if hash["mysql"]["server_root_password"].nil?
hash["mysql"]["server_repl_password"] = random_password if hash["mysql"]["server_repl_password"].nil?
hash["mysql"]["db_maker_password"] = random_password if hash["mysql"]["db_maker_password"].nil?
new_config.config_hash = hash
- #identify server node
- server_nodes = new_config.active_config.get_nodes_by_role("mysql-server")
- @logger.debug("Mysql mysql-server elements: #{server_nodes.inspect}")
- if server_nodes.size == 1
- server_name = server_nodes.first.name
- @logger.debug("Mysql found single server node: #{server_name}")
- # set mysql-server attribute for any mysql-client role nodes
- cnodes = new_config.active_config.get_nodes_by_role("mysql-client")
- @logger.debug("Mysql mysql-client elements: #{cnodes.inspect}")
- unless cnodes.nil? or cnodes.empty?
+ cnodes = new_config.get_nodes_by_role("mysql-client")
+ @logger.debug("Mysql mysql-client elements: #{cnodes.inspect}")
+ unless cnodes.nil? or cnodes.empty?
+ #identify server node
+ server_nodes = new_config.get_nodes_by_role("mysql-server")
+ @logger.debug("Mysql mysql-server elements: #{server_nodes.inspect}")
+ if server_nodes.size == 1
+ server_name = server_nodes.first.name
+ @logger.debug("Mysql found single server node: #{server_name}")
+ # set mysql-server attribute for any mysql-client role nodes
cnodes.each do |n|
- chash = new_config.active_config.get_node_config_hash(n)
+ chash = new_config.get_node_config_hash(n)
chash["mysql-server"] = server_name
- new_config.active_config.set_node_config_hash(n, chash)
+ new_config.set_node_config_hash(n, chash)
@logger.debug("Mysql assign node[:mysql-server] for #{n}")
end
end
@@ -63,6 +63,80 @@
end
+ describe "Apply Role Pre Chef Call" do
+ # It should never touch the old_config, always send nil in these tests
+
+ it "should do nothing if no nodes are provided" do
+ new_config = mock(Proposal)
+ new_config.should_receive(:active_config).exactly(0).times
+ new_config.should_receive(:config_hash).exactly(0).times
+ @service_object.apply_role_pre_chef_call(nil, new_config, [])
+ end
+
+ def setup_prop_config_mock()
+ addr1 = mock(IpAddress)
+ addr1.should_receive(:addr).exactly(1).times.and_return("1.1.1.1")
+ addr2 = mock(IpAddress)
+ addr2.should_receive(:addr).exactly(1).times.and_return("2.2.2.2")
+ n1 = mock(Node)
+ n1.should_receive(:address).exactly(1).times.and_return(addr1)
+ n2 = mock(Node)
+ n2.should_receive(:address).exactly(1).times.and_return(addr2)
+ nodes = [ n1, n2 ]
+
+ ac = mock(ProposalConfig)
+ ac.should_receive(:get_nodes_by_role).exactly(1).times.and_return([])
+ ac.should_receive(:get_node_config_hash).and_return({:mysql => {}}, {})
+ ac.should_receive(:set_node_config_hash).exactly(2).times do |arg1, arg2|
+ arg2[:mysql][:api_bind_host].should eq(arg1 == n1 ? "1.1.1.1" : "2.2.2.2")
+ end
+ [ac, nodes]
+ end
+
+ it "should set api_bind_host on all nodes to the nodes admin address" do
+ new_config, nodes = setup_prop_config_mock
+ new_config.should_receive(:config_hash).exactly(1).times.and_return({:mysql => {}})
+ new_config.should_receive(:config_hash=).exactly(1).times
+ @service_object.apply_role_pre_chef_call(nil, new_config, nodes)
+ end
+
+ it "should set passwords if unset" do
+ new_config, nodes = setup_prop_config_mock
+ new_config.should_receive(:config_hash).exactly(1).times.and_return({:mysql => {}})
+ new_config.should_receive(:config_hash=).exactly(1).times do |arg|
+ arg["mysql"]["server_debian_password"].should eq("fred1")
+ arg["mysql"]["server_root_password"].should eq("fred2")
+ arg["mysql"]["server_repl_password"].should eq("fred3")
+ arg["mysql"]["db_maker_password"].should eq("fred4")
+ end
+ @service_object.should_receive(:random_password).exactly(4).times.and_return("fred1", "fred2", "fred3", "fred4")
+
+ @service_object.apply_role_pre_chef_call(nil, new_config, nodes)
+ end
+
+ it "should not set passwords if set" do
+ new_config, nodes = setup_prop_config_mock
+ data = { "mysql" => {
+ "server_debian_password" => "greg1",
+ "server_root_password" => "greg2",
+ "server_repl_password" => "greg3",
+ "db_maker_password" => "greg4" } }
+ new_config.should_receive(:config_hash).exactly(1).times.and_return(data)
+ new_config.should_receive(:config_hash=).exactly(1).times do |arg|
+ arg["mysql"]["server_debian_password"].should eq("greg1")
+ arg["mysql"]["server_root_password"].should eq("greg2")
+ arg["mysql"]["server_repl_password"].should eq("greg3")
+ arg["mysql"]["db_maker_password"].should eq("greg4")
+ end
+ @service_object.should_receive(:random_password).exactly(0).times
+
+ @service_object.apply_role_pre_chef_call(nil, new_config, nodes)
+ end
+
+ # GREG: Add tests for mysql-client configs.
+
+ end
+
end

0 comments on commit 5d672cd

Please sign in to comment.