Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

Generate JSON in pretty form, and add ip address #360

Merged
merged 1 commit into from
Apr 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/knife-solo/node_config_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def generate_node_config
attributes = config[:json_attributes] || config[:first_boot_attributes] || {}
run_list = { :run_list => config[:run_list] || [] }
environment = config[:environment] ? { :environment => config[:environment] } : {}
f.print attributes.merge(run_list).merge(environment).to_json
automatic = host ? { :automatic => { :ipaddress => host } } : {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, what's the use case for the addition of automatic.ipaddress? I thought ohai would do that for you w/o having to specify anything in the node config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, although you then only have the IP inside of that node's run, and not on the others. We're using this for two purposes: First, to automate connecting to the server to use chef-solo (we don't need to keep track of IPs, they are stored in the node file). Second, to use other servers IP addresses inside a recipe (say, I want my web server to connect to the DB server on it's IP, so it loads the IP from the node file using a search).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah... interesting. Sounds worth the feature then.

f.print JSON.pretty_generate(attributes.merge(run_list).merge(environment).merge(automatic))
end
end
end
Expand Down
68 changes: 60 additions & 8 deletions test/node_config_command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def test_generates_a_node_config
cmd = command(@host)
cmd.generate_node_config
assert cmd.node_config.exist?
assert_match '{"run_list":[]}', cmd.node_config.read

assert_config_contains({"run_list" => []}, cmd)
end
end

Expand All @@ -72,7 +73,8 @@ def test_generates_a_node_config_with_specified_run_list
in_kitchen do
cmd = command(@host, "--run-list=role[base],recipe[foo]")
cmd.generate_node_config
assert_match '{"run_list":["role[base]","recipe[foo]"]}', cmd.node_config.read

assert_config_contains({"run_list" => ["role[base]","recipe[foo]"]}, cmd)
end
end

Expand All @@ -81,7 +83,13 @@ def test_generates_a_node_config_with_specified_attributes
foo_json = '"foo":{"bar":[1,2],"baz":"x"}'
cmd = command(@host, "--json-attributes={#{foo_json}}")
cmd.generate_node_config
assert_match "{#{foo_json},\"run_list\":[]}", cmd.node_config.read

expected_hash = {
"foo" => {"bar" => [1,2], "baz" => "x"},
"run_list" => []
}

assert_config_contains expected_hash, cmd
end
end

Expand All @@ -94,17 +102,28 @@ def test_generates_a_node_config_with_specified_json_attributes
cmd.config[:json_attributes] = JSON.parse("{#{foo_json}}")
cmd.config[:first_boot_attributes] = JSON.parse("{#{ignored_json}}")
cmd.generate_node_config
assert_match "{#{foo_json},\"run_list\":[]}", cmd.node_config.read

expected_hash = {
"foo" => 99,
"run_list" => []
}

assert_config_contains expected_hash, cmd
end
end

def test_generates_a_node_config_with_specified_first_boot_attributes
in_kitchen do
foo_json = '"foo":null'
cmd = command(@host)
cmd.config[:first_boot_attributes] = JSON.parse("{#{foo_json}}")
cmd.config[:first_boot_attributes] = {"foo"=>nil}
cmd.generate_node_config
assert_match "{#{foo_json},\"run_list\":[]}", cmd.node_config.read

expected_hash = {
"foo" => nil,
"run_list" => []
}

assert_config_contains expected_hash, cmd
end
end

Expand All @@ -114,10 +133,33 @@ def test_generates_a_node_config_with_specified_run_list_and_attributes
run_list = 'recipe[baz]'
cmd = command(@host, "--run-list=#{run_list}", "--json-attributes={#{foo_json}}")
cmd.generate_node_config
assert_match "{#{foo_json},\"run_list\":[\"#{run_list}\"]}", cmd.node_config.read

expected_hash = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeated pattern in the test has a bit of smell to it. Thinking we should have some sort of helper here that allows for testing only the part of the json we're interested in for a given test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt the same way. I'll refactor this. I was thinking something like

assert_config_contains expected_hash, cmd

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, you can see the updated code below

"foo" => "bar",
"run_list" => [run_list]
}

assert_config_contains expected_hash, cmd

end
end

def test_generates_a_node_config_with_the_ip_address
in_kitchen do
cmd = command(@host)
cmd.generate_node_config

expected_hash = {
"automatic" => { "ipaddress" => @host }
}

assert_config_contains expected_hash, cmd

end
end



def test_creates_the_nodes_directory_if_needed
outside_kitchen do
cmd = command(@host, "--node-name=mynode")
Expand All @@ -126,7 +168,17 @@ def test_creates_the_nodes_directory_if_needed
end
end

private

def command(*args)
knife_command(DummyNodeConfigCommand, *args)
end

def assert_config_contains expected_hash, cmd
config = JSON.parse(cmd.node_config.read)
expected_hash.each do |k, v|
assert_equal v, config[k]
end
end

end