hammer_cli_foreman_virt_who_configure 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +7 -0
  3. data/config/foreman_virt_who_configure.yml +2 -0
  4. data/lib/hammer_cli_foreman_virt_who_configure.rb +9 -0
  5. data/lib/hammer_cli_foreman_virt_who_configure/config.rb +223 -0
  6. data/lib/hammer_cli_foreman_virt_who_configure/i18n.rb +29 -0
  7. data/lib/hammer_cli_foreman_virt_who_configure/system_caller.rb +20 -0
  8. data/lib/hammer_cli_foreman_virt_who_configure/version.rb +5 -0
  9. data/locale/Makefile +66 -0
  10. data/locale/README.md +18 -0
  11. data/locale/ca/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  12. data/locale/ca/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  13. data/locale/ca/hammer-cli-foreman-virt-who-configure.po +132 -0
  14. data/locale/ca/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  15. data/locale/de/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  16. data/locale/de/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  17. data/locale/de/hammer-cli-foreman-virt-who-configure.po +132 -0
  18. data/locale/de/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  19. data/locale/en/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  20. data/locale/en/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  21. data/locale/en/hammer-cli-foreman-virt-who-configure.po +132 -0
  22. data/locale/en/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  23. data/locale/en_GB/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  24. data/locale/en_GB/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  25. data/locale/en_GB/hammer-cli-foreman-virt-who-configure.po +132 -0
  26. data/locale/en_GB/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  27. data/locale/es/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  28. data/locale/es/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  29. data/locale/es/hammer-cli-foreman-virt-who-configure.po +132 -0
  30. data/locale/es/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  31. data/locale/fr/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  32. data/locale/fr/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  33. data/locale/fr/hammer-cli-foreman-virt-who-configure.po +132 -0
  34. data/locale/fr/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  35. data/locale/hammer-cli-foreman-virt-who-configure.pot +177 -0
  36. data/locale/it/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  37. data/locale/it/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  38. data/locale/it/hammer-cli-foreman-virt-who-configure.po +132 -0
  39. data/locale/it/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  40. data/locale/ja/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  41. data/locale/ja/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  42. data/locale/ja/hammer-cli-foreman-virt-who-configure.po +132 -0
  43. data/locale/ja/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  44. data/locale/ko/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  45. data/locale/ko/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  46. data/locale/ko/hammer-cli-foreman-virt-who-configure.po +132 -0
  47. data/locale/ko/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  48. data/locale/pt_BR/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  49. data/locale/pt_BR/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  50. data/locale/pt_BR/hammer-cli-foreman-virt-who-configure.po +132 -0
  51. data/locale/pt_BR/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  52. data/locale/ru/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  53. data/locale/ru/hammer-cli-foreman-virt-who-configure.edit.po +171 -0
  54. data/locale/ru/hammer-cli-foreman-virt-who-configure.po +133 -0
  55. data/locale/ru/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  56. data/locale/zh_CN/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  57. data/locale/zh_CN/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  58. data/locale/zh_CN/hammer-cli-foreman-virt-who-configure.po +132 -0
  59. data/locale/zh_CN/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  60. data/locale/zh_TW/LC_MESSAGES/hammer-cli-foreman-virt-who-configure.mo +0 -0
  61. data/locale/zh_TW/hammer-cli-foreman-virt-who-configure.edit.po +170 -0
  62. data/locale/zh_TW/hammer-cli-foreman-virt-who-configure.po +132 -0
  63. data/locale/zh_TW/hammer-cli-foreman-virt-who-configure.po.time_stamp +0 -0
  64. data/test/data/1.15/foreman_api.json +1 -0
  65. data/test/functional/config/config_create_test.rb +93 -0
  66. data/test/functional/config/config_deploy_test.rb +56 -0
  67. data/test/functional/config/config_fetch_test.rb +60 -0
  68. data/test/functional/config/config_info_test.rb +147 -0
  69. data/test/functional/config/config_list_test.rb +53 -0
  70. data/test/functional/config/config_update_test.rb +46 -0
  71. data/test/functional/test_helper.rb +60 -0
  72. data/test/test_helper.rb +15 -0
  73. data/test/unit/system_caller_test.rb +26 -0
  74. metadata +154 -0
@@ -0,0 +1,56 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe "virt-who-config" do
4
+ describe "deploy" do
5
+ class FakeSystemCaller
6
+ def initialize(output, result=true)
7
+ @output = output
8
+ @result = result
9
+ end
10
+
11
+ def system(script)
12
+ puts @output
13
+ @result
14
+ end
15
+ end
16
+
17
+ before do
18
+ @cmd = ["virt-who-config", "deploy"]
19
+ @params = ['--name=test']
20
+ @script = 'echo BASH SCRIPT'
21
+ end
22
+
23
+ it "sends the script to #system" do
24
+ api_expects_search(:configs, :name => 'test').returns(index_response([config]))
25
+ api_expects(:configs, :deploy_script, 'Get config script').returns({'virt_who_config_script' => @script})
26
+
27
+ system_caller = mock
28
+ system_caller.expects(:system).with(@script).returns(true)
29
+
30
+ result = run_cmd(@cmd + @params, { :system_caller => system_caller })
31
+ assert_cmd(CommandExpectation.new, result)
32
+ end
33
+
34
+ it "prints output of the script to stdout" do
35
+ api_expects_search(:configs, :name => 'test').returns(index_response([config]))
36
+ api_expects(:configs, :deploy_script, 'Get config script').returns({'virt_who_config_script' => @script})
37
+
38
+ system_caller = FakeSystemCaller.new('Script output...')
39
+ expected_result = CommandExpectation.new('Script output...' + "\n")
40
+
41
+ result = run_cmd(@cmd + @params, { :system_caller => system_caller })
42
+ assert_cmd(expected_result, result)
43
+ end
44
+
45
+ it "returns HammerCLI::EX_SOFTWARE on config failure" do
46
+ api_expects_search(:configs, :name => 'test').returns(index_response([config]))
47
+ api_expects(:configs, :deploy_script, 'Get config script').returns({'virt_who_config_script' => @script})
48
+
49
+ system_caller = FakeSystemCaller.new('Script output...', false)
50
+ expected_result = CommandExpectation.new('Script output...' + "\n", '', HammerCLI::EX_SOFTWARE)
51
+
52
+ result = run_cmd(@cmd + @params, { :system_caller => system_caller })
53
+ assert_cmd(expected_result, result)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,60 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe "virt-who-config" do
4
+ describe "fetch" do
5
+ before do
6
+ @cmd = ["virt-who-config", "fetch"]
7
+ @script = 'echo BASH SCRIPT'
8
+ end
9
+
10
+ it "prints script to stdout by default" do
11
+ params = ['--name=test']
12
+
13
+ api_expects_search(:configs, :name => 'test').returns(index_response([config]))
14
+ api_expects(:configs, :deploy_script, 'Get config script').returns({'virt_who_config_script' => @script})
15
+
16
+ expected_result = CommandExpectation.new(@script + "\n")
17
+
18
+ result = run_cmd(@cmd + params)
19
+ assert_cmd(expected_result, result)
20
+ end
21
+
22
+ it "stores script into a file" do
23
+ file = Tempfile.new
24
+ file_path = file.path
25
+ file.unlink
26
+ begin
27
+ params = ['--name=test', '--output', file_path]
28
+
29
+ api_expects_search(:configs, :name => 'test').returns(index_response([config]))
30
+ api_expects(:configs, :deploy_script, 'Get config script').returns({'virt_who_config_script' => @script})
31
+
32
+ result = run_cmd(@cmd + params)
33
+
34
+ assert_cmd(CommandExpectation.new, result)
35
+ assert_equal(@script, File.read(file_path))
36
+ ensure
37
+ File.unlink(file_path)
38
+ end
39
+ end
40
+
41
+ it "refuses to store the script into an existing file" do
42
+ file = Tempfile.new
43
+ begin
44
+ params = ['--name=test', '--output', file.path]
45
+
46
+ api_expects_search(:configs, :name => 'test').returns(index_response([config]))
47
+ api_expects(:configs, :deploy_script, 'Get config script').returns({'virt_who_config_script' => @script})
48
+
49
+ result = run_cmd(@cmd + params)
50
+
51
+ expected_result = CommandExpectation.new('', /File at .* already exists, please specify a different path/, HammerCLI::EX_USAGE)
52
+
53
+ assert_cmd(expected_result, result)
54
+ assert_equal('', file.read)
55
+ ensure
56
+ file.unlink
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,147 @@
1
+ require_relative '../test_helper'
2
+
3
+ def expect_config_search(config)
4
+ api_expects_search(:configs, :name => config['name']).returns(index_response([config]))
5
+ api_expects(:configs, :show).with_params('id' => config['id']).returns(config)
6
+ end
7
+
8
+ def test_output_field(cmd, config_modification, output_label, output_value)
9
+ expect_config_search(config(config_modification))
10
+
11
+ output = FieldMatcher.new(output_label, output_value)
12
+
13
+ result = run_cmd(cmd)
14
+ assert_cmd(success_result(output), result)
15
+ end
16
+
17
+ def test_status_field(cmd, api_value, output_value)
18
+ test_output_field(cmd, { "status" => api_value }, 'Status', output_value)
19
+ end
20
+
21
+ def test_interval_field(cmd, api_value, output_value)
22
+ test_output_field(cmd, { "interval" => api_value }, 'Interval', output_value)
23
+ end
24
+
25
+ def test_filter_field(cmd, api_value, output_value)
26
+ test_output_field(cmd, { "filtering_mode" => api_value }, 'Filtering', output_value)
27
+ end
28
+
29
+ describe "virt-who-config" do
30
+ describe "info" do
31
+ before do
32
+ @cmd = ["virt-who-config", "info", '--name=test']
33
+ end
34
+
35
+ describe "interval formatting" do
36
+ it "formats 1 hour" do
37
+ test_interval_field(@cmd, 60, 'every hour')
38
+ end
39
+
40
+ it "formats 2 hours" do
41
+ test_interval_field(@cmd, 120, 'every 2 hours')
42
+ end
43
+ end
44
+
45
+ describe "status formatting" do
46
+ it "formats status unknown" do
47
+ test_status_field(@cmd, 'unknown', 'No Report Yet')
48
+ end
49
+
50
+ it "formats status ok" do
51
+ test_status_field(@cmd, 'ok', 'OK')
52
+ end
53
+
54
+ it "formats status out_of_date" do
55
+ test_status_field(@cmd, 'out_of_date', 'OK')
56
+ end
57
+
58
+ it "formats status error" do
59
+ test_status_field(@cmd, 'error', 'Error')
60
+ end
61
+
62
+ it "formats unknown status" do
63
+ test_status_field(@cmd, nil, 'Unknown configuration status')
64
+ end
65
+ end
66
+
67
+ describe "when filter is none" do
68
+ it "formats filter" do
69
+ test_filter_field(@cmd, 0, 'Unlimited')
70
+ end
71
+ it "hides whitelist field" do
72
+ expect_config_search(config("filtering_mode" => 0))
73
+ refute_match('Filtered hosts:', run_cmd(@cmd).out)
74
+ end
75
+
76
+ it "hides blacklist field" do
77
+ expect_config_search(config("filtering_mode" => 0))
78
+ refute_match('Excluded hosts:', run_cmd(@cmd).out)
79
+ end
80
+ end
81
+
82
+ describe "when filter is whitelist" do
83
+ it "formats filter" do
84
+ test_filter_field(@cmd, 1, 'Whitelist')
85
+ end
86
+
87
+ it "shows whitelist field" do
88
+ expect_config_search(config("filtering_mode" => 1, 'whitelist' => 'host1,hostb'))
89
+
90
+ output = FieldMatcher.new('Filtered hosts', 'host1,hostb')
91
+
92
+ result = run_cmd(@cmd)
93
+ assert_cmd(success_result(output), result)
94
+ end
95
+
96
+ it "shows empty whitelist field" do
97
+ expect_config_search(config("filtering_mode" => 1, 'whitelist' => nil))
98
+ assert_match('Filtered hosts:', run_cmd(@cmd).out)
99
+ end
100
+
101
+ it "hides blacklist field" do
102
+ expect_config_search(config("filtering_mode" => 1))
103
+ refute_match('Excluded hosts:', run_cmd(@cmd).out)
104
+ end
105
+ end
106
+
107
+ describe "when filter is blacklist" do
108
+ it "formats filter" do
109
+ test_filter_field(@cmd, 2, 'Blacklist')
110
+ end
111
+
112
+ it "shows blacklist field" do
113
+ expect_config_search(config("filtering_mode" => 2, 'blacklist' => 'host1,hostb'))
114
+
115
+ output = FieldMatcher.new('Excluded hosts', 'host1,hostb')
116
+
117
+ result = run_cmd(@cmd)
118
+ assert_cmd(success_result(output), result)
119
+ end
120
+
121
+ it "shows empty blacklist field" do
122
+ expect_config_search(config("filtering_mode" => 2, 'whitelist' => nil))
123
+ assert_match('Excluded hosts:', run_cmd(@cmd).out)
124
+ end
125
+
126
+ it "hides whitelist field" do
127
+ expect_config_search(config("filtering_mode" => 2))
128
+ refute_match('Filtered hosts:', run_cmd(@cmd).out)
129
+ end
130
+ end
131
+
132
+ describe "when filter is unknown value" do
133
+ it "formats filter" do
134
+ test_filter_field(@cmd, nil, 'Unknown listing mode')
135
+ end
136
+ it "hides whitelist field" do
137
+ expect_config_search(config("filtering_mode" => 0))
138
+ refute_match('Filtered hosts:', run_cmd(@cmd).out)
139
+ end
140
+
141
+ it "hides blacklist field" do
142
+ expect_config_search(config("filtering_mode" => 0))
143
+ refute_match('Excluded hosts:', run_cmd(@cmd).out)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,53 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe "virt-who-config" do
4
+ describe "list" do
5
+ before do
6
+ @cmd = ["virt-who-config", "list"]
7
+ end
8
+
9
+ it "formats interval and status" do
10
+ configs = [
11
+ config,
12
+ config('status' => 'ok', 'interval' => 120),
13
+ config('status' => 'out_of_date', 'interval' => 240),
14
+ config('status' => 'error', 'interval' => 480),
15
+ ]
16
+
17
+ api_expects(:configs, :index).returns(index_response(configs))
18
+
19
+ output = IndexMatcher.new([
20
+ ['ID', 'NAME', 'INTERVAL', 'STATUS', 'LAST REPORT AT'],
21
+ ['11', 'test', 'every hour', 'No Report Yet', ''],
22
+ ['11', 'test', 'every 2 hours', 'OK', ''],
23
+ ['11', 'test', 'every 4 hours', 'OK', ''],
24
+ ['11', 'test', 'every 8 hours', 'Error', '']
25
+ ])
26
+
27
+ result = run_cmd(@cmd)
28
+ assert_cmd(success_result(output), result)
29
+ end
30
+
31
+ it "supports pagination" do
32
+ params = ['--page=2', '--per-page=10', '--order', 'name ASC']
33
+
34
+ api_expects(:configs, :index).with_params(
35
+ 'page' => '2',
36
+ 'per_page' => '10',
37
+ 'order' => 'name ASC',
38
+ ).returns(index_response([]))
39
+
40
+ result = run_cmd(@cmd + params)
41
+ assert_cmd(success_result(/.*/), result)
42
+ end
43
+
44
+ it "supports search" do
45
+ params = ['--search', 'name ~ test']
46
+
47
+ api_expects(:configs, :index).with_params('search' => 'name ~ test').returns(index_response([]))
48
+
49
+ result = run_cmd(@cmd + params)
50
+ assert_cmd(success_result(/.*/), result)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,46 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe "virt-who-config" do
4
+ describe "update" do
5
+ before do
6
+ @cmd = ["virt-who-config", "update"]
7
+ @update_args = {
8
+ :name => 'test',
9
+ :new_name => 'test2',
10
+ :interval => 60,
11
+ :filtering_mode => 'blacklist',
12
+ :hypervisor_id => 'uuid',
13
+ :hypervisor_type => 'libvirt',
14
+ :hypervisor_server => 1,
15
+ :hypervisor_username => 1,
16
+ :satellite_url => 1,
17
+ :organization_id => 1
18
+ }
19
+ end
20
+
21
+ it "sends values to api" do
22
+ params = hash_to_opts(@update_args)
23
+
24
+ expected_result = success_result("Virt Who configuration [test] updated\n")
25
+
26
+ api_expects_search(:configs, :name => 'test').returns(index_response([config]))
27
+ api_expects(:configs, :update, 'Create configuration').with_params(
28
+ 'id' => 11,
29
+ 'foreman_virt_who_configure_config' => {
30
+ 'name' => 'test2',
31
+ 'interval' => '60',
32
+ 'filtering_mode' => 2,
33
+ 'hypervisor_id' => 'uuid',
34
+ 'hypervisor_type' => 'libvirt',
35
+ 'hypervisor_server' => '1',
36
+ 'hypervisor_username' => '1',
37
+ 'satellite_url' => '1',
38
+ 'organization_id' => 1
39
+ }
40
+ ).returns({:config => config})
41
+
42
+ result = run_cmd(@cmd + params)
43
+ assert_cmd(expected_result, result)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,60 @@
1
+ require_relative '../test_helper'
2
+
3
+ require 'hammer_cli/testing/command_assertions'
4
+ require 'hammer_cli/testing/output_matchers'
5
+
6
+ include HammerCLI::Testing::CommandAssertions
7
+ include HammerCLI::Testing::OutputMatchers
8
+
9
+ def missing_arguments_result(argument_name)
10
+ HammerCLI::Testing::CommandAssertions::CommandExpectation.new('', /Missing arguments for.*\[#{argument_name}\]/, HammerCLI::EX_USAGE)
11
+ end
12
+
13
+ def assert_requires_argument(cmd, params, required_argument_name)
14
+ expected_result = missing_arguments_result(required_argument_name)
15
+
16
+ api_expects_no_call
17
+ result = run_cmd(cmd + params)
18
+ assert_cmd(expected_result, result)
19
+ end
20
+
21
+ def assert_usage_error(cmd, params, error_msg)
22
+ expected_result = usage_error_result(cmd,
23
+ error_msg,
24
+ "Could not create the Virt Who configuration")
25
+
26
+ api_expects_no_call
27
+ result = run_cmd(cmd + params)
28
+ assert_cmd(expected_result, result)
29
+ end
30
+
31
+ def hash_to_opts(hash, options={})
32
+ hash.reject do |k|
33
+ k == options[:reject]
34
+ end.map do |key, value|
35
+ "--#{key.to_s.gsub('_', '-')}=#{value}"
36
+ end
37
+ end
38
+
39
+ def config(attrs = {})
40
+ {
41
+ "name" => "test",
42
+ "interval" => 60,
43
+ "organization_id" => 1,
44
+ "whitelist" => "",
45
+ "blacklist" => "",
46
+ "hypervisor_id" => "hostname",
47
+ "hypervisor_type" => "libvirt",
48
+ "hypervisor_server" => "libvirt.test.org",
49
+ "hypervisor_username" => "user",
50
+ "debug" => false,
51
+ "satellite_url" => "10.34.131.166",
52
+ "proxy" => "",
53
+ "no_proxy" => "",
54
+ "status" => "unknown",
55
+ "last_report_at" => nil,
56
+ "out_of_date_at" => nil,
57
+ "filtering_mode" => 0,
58
+ "id" => 11
59
+ }.merge(attrs)
60
+ end
@@ -0,0 +1,15 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/spec'
3
+ require "mocha/setup"
4
+
5
+ require 'hammer_cli'
6
+ require 'hammer_cli_foreman/testing/api_expectations'
7
+
8
+ FOREMAN_VERSION = Gem::Version.new(ENV['TEST_API_VERSION'] || '1.15')
9
+
10
+ include HammerCLIForeman::Testing::APIExpectations
11
+ HammerCLI.context[:api_connection].create('foreman') do
12
+ api_connection({}, FOREMAN_VERSION)
13
+ end
14
+
15
+ require 'hammer_cli_foreman_virt_who_configure'
@@ -0,0 +1,26 @@
1
+ require 'hammer_cli_foreman_virt_who_configure/system_caller'
2
+ require_relative '../test_helper'
3
+
4
+ describe "SystemCaller" do
5
+ class CaptureContentTempfile < Tempfile
6
+ attr_accessor :contents
7
+
8
+ def write(data)
9
+ @contents ||= []
10
+ @contents << data
11
+ super
12
+ end
13
+ end
14
+
15
+ let(:script) { 'echo "Test"' }
16
+
17
+ it 'uses tempfile for executing the script' do
18
+ tmp_file = CaptureContentTempfile.new
19
+ Kernel.expects(:system).with("/usr/bin/bash #{tmp_file.path}")
20
+
21
+ sys_caller = HammerCLIForemanVirtWhoConfigure::SystemCaller.new(tmp_file)
22
+ sys_caller.system(script)
23
+
24
+ assert_equal([script], tmp_file.contents)
25
+ end
26
+ end