Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
Merge pull request #24 from galthaus/pull-req-master-e6102275bad5d958…
Browse files Browse the repository at this point in the history
…0a39bc697d78ada8c287fc79

Unit tests for more components [9/17]
  • Loading branch information
Rob Hirschfeld committed Oct 23, 2012
2 parents a216ba0 + 5d672cd commit 01694b9
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 21 deletions.
40 changes: 19 additions & 21 deletions crowbar_framework/app/models/mysql_service.rb
Expand Up @@ -15,15 +15,12 @@

class MysqlService < ServiceObject

def create_proposal
def create_proposal(name)
@logger.debug("Mysql create_proposal: entering")
base = super
base = super(name)

nodes = Node.all
nodes.delete_if { |n| n.nil? or n.is_admin? }
if nodes.size >= 1
add_role_to_instance_and_node(nodes[0].name, inst, "mysql-server")
end
node = Node.first(:conditions => [ "admin = ?", false])
add_role_to_instance_and_node(node.name, base.name, "mysql-server") if node

@logger.debug("Mysql create_proposal: exiting")
base
Expand All @@ -37,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
Expand Down
144 changes: 144 additions & 0 deletions crowbar_framework/spec/models/mysql_service_object_spec.rb
@@ -0,0 +1,144 @@
# Copyright 2012, Dell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

describe "MysqlServiceObject" do

before(:each) do
@barclamp = Barclamp.find_by_name("mysql")
@service_object = @barclamp.operations
end

#
# Create proposal is an overriden routine. Parameters are already validated.
#
describe "Create Proposal" do
it "should create a proposal with no nodes" do
answer = @service_object.create_proposal("fred")
answer.should be_an_instance_of Proposal
node_roles = NodeRole.find_all_by_proposal_config_id(answer.current_config.id)
node_roles.length.should be 0
end

it "should create a proposal with no nodes if the only node is an admin node" do
n = Node.new
n.name = "admin.dell.com"
n.admin = true
n.save!

answer = @service_object.create_proposal("fred")
answer.should be_an_instance_of Proposal
node_roles = NodeRole.find_all_by_proposal_config_id(answer.current_config.id)
node_roles.length.should be 0
end

it "should create a proposal with one node" do
n = Node.new
n.name = "admin.dell.com"
n.admin = true
n.save!
n = Node.new
n.name = "other.dell.com"
n.save!

answer = @service_object.create_proposal("fred")
answer.should be_an_instance_of Proposal
node_roles = NodeRole.find_all_by_proposal_config_id(answer.current_config.id)
node_roles.length.should be 1
node_roles[0].node.id.should be n.id
end

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 01694b9

Please sign in to comment.