From 2ac89832b7e065df4490d81b3080b2b570a172ad Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Mon, 9 Jul 2018 17:15:39 -0700 Subject: Initial host import from puppet#ee7cf4d28077be7d1bdbbe934ea012d41d33deff --- ..._2289_should_not_destroy_data_when_malformed.rb | 32 ++++++++++++++++++++ spec/acceptance/tests/should_create.rb | 23 +++++++++++++++ spec/acceptance/tests/should_create_aliases.rb | 24 +++++++++++++++ spec/acceptance/tests/should_destroy.rb | 27 +++++++++++++++++ spec/acceptance/tests/should_modify_alias.rb | 27 +++++++++++++++++ spec/acceptance/tests/should_modify_ip.rb | 27 +++++++++++++++++ .../acceptance/tests/should_not_create_existing.rb | 24 +++++++++++++++ spec/acceptance/tests/should_query_all.rb | 34 ++++++++++++++++++++++ .../ticket_4131_should_not_create_without_ip.rb | 29 ++++++++++++++++++ 9 files changed, 247 insertions(+) create mode 100644 spec/acceptance/tests/pup_2289_should_not_destroy_data_when_malformed.rb create mode 100644 spec/acceptance/tests/should_create.rb create mode 100644 spec/acceptance/tests/should_create_aliases.rb create mode 100644 spec/acceptance/tests/should_destroy.rb create mode 100644 spec/acceptance/tests/should_modify_alias.rb create mode 100644 spec/acceptance/tests/should_modify_ip.rb create mode 100644 spec/acceptance/tests/should_not_create_existing.rb create mode 100644 spec/acceptance/tests/should_query_all.rb create mode 100644 spec/acceptance/tests/ticket_4131_should_not_create_without_ip.rb (limited to 'spec/acceptance') diff --git a/spec/acceptance/tests/pup_2289_should_not_destroy_data_when_malformed.rb b/spec/acceptance/tests/pup_2289_should_not_destroy_data_when_malformed.rb new file mode 100644 index 0000000..670651d --- /dev/null +++ b/spec/acceptance/tests/pup_2289_should_not_destroy_data_when_malformed.rb @@ -0,0 +1,32 @@ +test_name "should not delete data when existing content is malformed" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +agents.each do |agent| + file = agent.tmpfile('host-not-delete-data') + + teardown do + on(agent, "rm -f #{file}", :acceptable_exit_codes => (0..255)) + end + + step "(setup) populate test file with host information" + on(agent, "printf '127.0.0.2 existing alias\n' > #{file}") + + step "(setup) populate test file with a malformed line" + on(agent, "printf '==\n' >> #{file}") + + step "tell puppet to add another host entry" + on(agent, puppet_resource('host', 'test', "target=#{file}", + 'ensure=present', 'ip=127.0.0.3', 'host_aliases=foo')) + + step "verify that the initial host entry was not deleted" + on(agent, "cat #{file}") do |res| + fail_test "existing host data was deleted" unless + res.stdout.include? 'existing' + end + +end diff --git a/spec/acceptance/tests/should_create.rb b/spec/acceptance/tests/should_create.rb new file mode 100644 index 0000000..b590f85 --- /dev/null +++ b/spec/acceptance/tests/should_create.rb @@ -0,0 +1,23 @@ +test_name "host should create" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +agents.each do |agent| + target = agent.tmpfile('host-create') + + step "clean up for the test" + on agent, "rm -f #{target}" + + step "create the host record" + on(agent, puppet_resource("host", "test", "ensure=present", + "ip=127.0.0.1", "target=#{target}")) + + step "verify that the record was created" + on(agent, "cat #{target} ; rm -f #{target}") do + fail_test "record was not present" unless stdout =~ /^127\.0\.0\.1[[:space:]]+test/ + end +end diff --git a/spec/acceptance/tests/should_create_aliases.rb b/spec/acceptance/tests/should_create_aliases.rb new file mode 100644 index 0000000..be4a134 --- /dev/null +++ b/spec/acceptance/tests/should_create_aliases.rb @@ -0,0 +1,24 @@ +test_name "host should create aliases" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +agents.each do |agent| + target = agent.tmpfile('host-create-aliases') + + step "clean up the system for testing" + on agent, "rm -f #{target}" + + step "create the record" + on(agent, puppet_resource('host', 'test', "ensure=present", + "ip=127.0.0.7", "target=#{target}", "host_aliases=alias")) + + step "verify that the aliases were added" + on(agent, "cat #{target} ; rm -f #{target}") do + fail_test "alias was missing" unless + stdout =~ /^127\.0\.0\.7[[:space:]]+test[[:space:]]alias/ + end +end diff --git a/spec/acceptance/tests/should_destroy.rb b/spec/acceptance/tests/should_destroy.rb new file mode 100644 index 0000000..97f1477 --- /dev/null +++ b/spec/acceptance/tests/should_destroy.rb @@ -0,0 +1,27 @@ +test_name "should be able to remove a host record" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +agents.each do |agent| + file = agent.tmpfile('host-destroy') + line = "127.0.0.7 test1" + + step "set up files for the test" + on agent, "printf '#{line}\n' > #{file}" + + step "delete the resource from the file" + on(agent, puppet_resource('host', 'test1', "target=#{file}", + 'ensure=absent', 'ip=127.0.0.7')) + + step "verify that the content was removed" + on(agent, "cat #{file}; rm -f #{file}") do + fail_test "the content was still present" if stdout.include? line + end + + step "clean up after the test" + on agent, "rm -f #{file}" +end diff --git a/spec/acceptance/tests/should_modify_alias.rb b/spec/acceptance/tests/should_modify_alias.rb new file mode 100644 index 0000000..07198b1 --- /dev/null +++ b/spec/acceptance/tests/should_modify_alias.rb @@ -0,0 +1,27 @@ +test_name "should be able to modify a host alias" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +agents.each do |agent| + file = agent.tmpfile('host-modify-alias') + + step "set up files for the test" + on agent, "printf '127.0.0.8 test alias\n' > #{file}" + + step "modify the resource" + on(agent, puppet_resource('host', 'test', "target=#{file}", + 'ensure=present', 'ip=127.0.0.8', 'host_aliases=banzai')) + + step "verify that the content was updated" + on(agent, "cat #{file}; rm -f #{file}") do + fail_test "the alias was not updated" unless + stdout =~ /^127\.0\.0\.8[[:space:]]+test[[:space:]]+banzai[[:space:]]*$/ + end + + step "clean up after the test" + on agent, "rm -f #{file}" +end diff --git a/spec/acceptance/tests/should_modify_ip.rb b/spec/acceptance/tests/should_modify_ip.rb new file mode 100644 index 0000000..10c5adf --- /dev/null +++ b/spec/acceptance/tests/should_modify_ip.rb @@ -0,0 +1,27 @@ +test_name "should be able to modify a host address" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +agents.each do |agent| + file = agent.tmpfile('host-modify-ip') + + step "set up files for the test" + on agent, "printf '127.0.0.9 test alias\n' > #{file}" + + step "modify the resource" + on(agent, puppet_resource('host', 'test', "target=#{file}", + 'ensure=present', 'ip=127.0.0.10', 'host_aliases=alias')) + + step "verify that the content was updated" + on(agent, "cat #{file}; rm -f #{file}") do + fail_test "the address was not updated" unless + stdout =~ /^127\.0\.0\.10[[:space:]]+test[[:space:]]+alias[[:space:]]*$/ + end + + step "clean up after the test" + on agent, "rm -f #{file}" +end diff --git a/spec/acceptance/tests/should_not_create_existing.rb b/spec/acceptance/tests/should_not_create_existing.rb new file mode 100644 index 0000000..0cdc87b --- /dev/null +++ b/spec/acceptance/tests/should_not_create_existing.rb @@ -0,0 +1,24 @@ +test_name "should not create host if it exists" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +agents.each do |agent| + file = agent.tmpfile('host-not-create-existing') + + step "set up the system for the test" + on agent, "printf '127.0.0.2 test alias\n' > #{file}" + + step "tell puppet to ensure the host exists" + on(agent, puppet_resource('host', 'test', "target=#{file}", + 'ensure=present', 'ip=127.0.0.2', 'host_aliases=alias')) do + fail_test "darn, we created the host record" if + stdout.include? '/Host[test1]/ensure: created' + end + + step "clean up after we created things" + on agent, "rm -f #{file}" +end diff --git a/spec/acceptance/tests/should_query_all.rb b/spec/acceptance/tests/should_query_all.rb new file mode 100644 index 0000000..e60756d --- /dev/null +++ b/spec/acceptance/tests/should_query_all.rb @@ -0,0 +1,34 @@ +test_name "should query all hosts from hosts file" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +content = %q{127.0.0.1 test1 test1.local +127.0.0.2 test2 test2.local +127.0.0.3 test3 test3.local +127.0.0.4 test4 test4.local +} + +agents.each do |agent| + backup = agent.tmpfile('host-query-all') + + step "configure the system for testing (including file backups)" + on agent, "cp /etc/hosts #{backup}" + on agent, "cat > /etc/hosts", :stdin => content + + step "query all host records using puppet" + on(agent, puppet_resource('host')) do + found = stdout.scan(/host \{ '([^']+)'/).flatten.sort + fail_test "the list of returned hosts was wrong: #{found.join(', ')}" unless + found == %w{test1 test2 test3 test4} + + count = stdout.scan(/ensure\s+=>\s+'present'/).length + fail_test "found #{count} records, wanted 4" unless count == 4 + end + + step "clean up the system afterwards" + on agent, "cat #{backup} > /etc/hosts && rm -f #{backup}" +end diff --git a/spec/acceptance/tests/ticket_4131_should_not_create_without_ip.rb b/spec/acceptance/tests/ticket_4131_should_not_create_without_ip.rb new file mode 100644 index 0000000..867ff6c --- /dev/null +++ b/spec/acceptance/tests/ticket_4131_should_not_create_without_ip.rb @@ -0,0 +1,29 @@ +test_name "#4131: should not create host without IP attribute" + +tag 'audit:low', + 'audit:refactor', # Use block style `test_name` + 'audit:acceptance' # Could be done at the integration (or unit) layer though + # actual changing of resources could irreparably damage a + # host running this, or require special permissions. + +agents.each do |agent| + file = agent.tmpfile('4131-require-ip') + + step "configure the target system for the test" + on agent, "rm -rf #{file} ; touch #{file}" + + step "try to create the host, which should fail" + # REVISIT: This step should properly need to handle the non-zero exit code, + # and #5668 has been filed to record that. When it is fixed this test will + # start to fail, and this comment will tell you why. --daniel 2010-12-24 + on(agent, puppet_resource('host', 'test', "target=#{file}", + "host_aliases=alias")) do + fail_test "puppet didn't complain about the missing attribute" unless + stderr.include? 'ip is a required attribute for hosts' unless agent['locale'] == 'ja' + end + + step "verify that the host was not added to the file" + on(agent, "cat #{file} ; rm -f #{file}") do + fail_test "the host was apparently added to the file" if stdout.include? 'test' + end +end -- cgit v1.2.3