hammer_cli_foreman_puppet 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +5 -0
  3. data/README.md +20 -0
  4. data/config/foreman_puppet.yml +2 -0
  5. data/lib/hammer_cli_foreman_puppet.rb +57 -0
  6. data/lib/hammer_cli_foreman_puppet/associating_commands.rb +1 -0
  7. data/lib/hammer_cli_foreman_puppet/associating_commands/associating_commands.rb +40 -0
  8. data/lib/hammer_cli_foreman_puppet/class.rb +54 -0
  9. data/lib/hammer_cli_foreman_puppet/combination.rb +14 -0
  10. data/lib/hammer_cli_foreman_puppet/command_extensions.rb +2 -0
  11. data/lib/hammer_cli_foreman_puppet/command_extensions/discovery.rb +8 -0
  12. data/lib/hammer_cli_foreman_puppet/command_extensions/environment.rb +24 -0
  13. data/lib/hammer_cli_foreman_puppet/command_extensions/environments.rb +24 -0
  14. data/lib/hammer_cli_foreman_puppet/command_extensions/host.rb +41 -0
  15. data/lib/hammer_cli_foreman_puppet/command_extensions/hostgroup.rb +47 -0
  16. data/lib/hammer_cli_foreman_puppet/command_extensions/location.rb +9 -0
  17. data/lib/hammer_cli_foreman_puppet/command_extensions/organization.rb +9 -0
  18. data/lib/hammer_cli_foreman_puppet/commands.rb +76 -0
  19. data/lib/hammer_cli_foreman_puppet/config_group.rb +45 -0
  20. data/lib/hammer_cli_foreman_puppet/discovery.rb +11 -0
  21. data/lib/hammer_cli_foreman_puppet/environment.rb +59 -0
  22. data/lib/hammer_cli_foreman_puppet/environment_name_mapping.rb +20 -0
  23. data/lib/hammer_cli_foreman_puppet/host.rb +70 -0
  24. data/lib/hammer_cli_foreman_puppet/hostgroup.rb +76 -0
  25. data/lib/hammer_cli_foreman_puppet/id_resolver.rb +73 -0
  26. data/lib/hammer_cli_foreman_puppet/location.rb +35 -0
  27. data/lib/hammer_cli_foreman_puppet/option_sources.rb +1 -0
  28. data/lib/hammer_cli_foreman_puppet/option_sources/puppet_environment_params.rb +60 -0
  29. data/lib/hammer_cli_foreman_puppet/organization.rb +35 -0
  30. data/lib/hammer_cli_foreman_puppet/puppet_references.rb +20 -0
  31. data/lib/hammer_cli_foreman_puppet/references.rb +22 -0
  32. data/lib/hammer_cli_foreman_puppet/smart_class_parameter.rb +182 -0
  33. data/lib/hammer_cli_foreman_puppet/smart_proxy.rb +58 -0
  34. data/lib/hammer_cli_foreman_puppet/version.rb +5 -0
  35. data/test/data/2.1/foreman_api.json +1 -0
  36. data/test/data/3.0/foreman_api.json +1 -0
  37. data/test/data/README.md +27 -0
  38. data/test/functional/config_group_test.rb +28 -0
  39. data/test/functional/host/create_test.rb +164 -0
  40. data/test/functional/host/update_test.rb +97 -0
  41. data/test/functional/hostgroup/create_test.rb +149 -0
  42. data/test/functional/hostgroup/update_test.rb +97 -0
  43. data/test/functional/proxy_test.rb +86 -0
  44. data/test/functional/smart_class_parameter_test.rb +97 -0
  45. data/test/functional/template_test.rb +38 -0
  46. data/test/functional/test_helper.rb +7 -0
  47. data/test/test_helper.rb +29 -0
  48. data/test/unit/apipie_resource_mock.rb +186 -0
  49. data/test/unit/config_group_test.rb +81 -0
  50. data/test/unit/helpers/command.rb +163 -0
  51. data/test/unit/helpers/fake_searchables.rb +19 -0
  52. data/test/unit/helpers/resource_disabled.rb +24 -0
  53. data/test/unit/puppet_class_test.rb +72 -0
  54. data/test/unit/puppet_environment_test.rb +116 -0
  55. data/test/unit/smart_class_parameter_test.rb +114 -0
  56. data/test/unit/test_helper.rb +18 -0
  57. data/test/unit/test_output_adapter.rb +22 -0
  58. metadata +143 -0
@@ -0,0 +1,38 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ describe 'template' do
4
+ describe 'combinations' do
5
+ before do
6
+ @cmd = %w[template combination]
7
+ end
8
+
9
+ it 'should create new combination' do
10
+ params = ['create', '--provisioning-template-id=10', '--hostgroup-id=1', '--puppet-environment-id=1']
11
+ expected_result = success_result("Template combination created.\n")
12
+ api_expects(:template_combinations, :create, 'Create template combination') do |p|
13
+ p['provisioning_template_id'] == 10 &&
14
+ p['hostgroup_id'] == 1 &&
15
+ p['environment_id'] == 1 &&
16
+ p['template_combination'] == { 'environment_id' => 1, 'hostgroup_id' => 1 }
17
+ end
18
+
19
+ result = run_cmd(@cmd + params)
20
+ assert_cmd(expected_result, result)
21
+ end
22
+
23
+ it 'should update combination' do
24
+ params = ['update', '--id=3', '--provisioning-template-id=10', '--hostgroup-id=1', '--puppet-environment-id=1']
25
+ expected_result = success_result("Template combination updated.\n")
26
+ api_expects(:template_combinations, :update, 'Update template combination') do |p|
27
+ p['id'] == '3' &&
28
+ p['provisioning_template_id'] == 10 &&
29
+ p['hostgroup_id'] == 1 &&
30
+ p['environment_id'] == 1 &&
31
+ p['template_combination'] == { 'environment_id' => 1, 'hostgroup_id' => 1 }
32
+ end
33
+
34
+ result = run_cmd(@cmd + params)
35
+ assert_cmd(expected_result, result)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ require File.join(File.dirname(__FILE__), '../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
@@ -0,0 +1,29 @@
1
+ require 'simplecov'
2
+ require 'pathname'
3
+
4
+ SimpleCov.use_merging true
5
+ SimpleCov.start do
6
+ command_name 'MiniTest'
7
+ add_filter 'test'
8
+ end
9
+ SimpleCov.root Pathname.new(File.dirname(__FILE__) + "../../../")
10
+
11
+
12
+ require 'minitest/autorun'
13
+ require 'minitest/spec'
14
+ require "minitest-spec-context"
15
+ require "mocha/minitest"
16
+
17
+ require 'hammer_cli'
18
+ require 'hammer_cli/testing/command_assertions'
19
+ require 'hammer_cli_foreman/testing/api_expectations'
20
+ FOREMAN_VERSION = Gem::Version.new(ENV['TEST_API_VERSION'] || '3.0')
21
+
22
+ include HammerCLI::Testing::CommandAssertions
23
+ include HammerCLIForeman::Testing::APIExpectations
24
+ HammerCLI.context[:api_connection].create('foreman') do
25
+ api_connection({}, FOREMAN_VERSION)
26
+ end
27
+
28
+ require 'hammer_cli_foreman'
29
+ require 'hammer_cli_foreman_puppet'
@@ -0,0 +1,186 @@
1
+ module ResourceMocks
2
+
3
+ def self.mock_action_call(resource, action, value, params=:default)
4
+ response = ApipieBindings::Example.new('GET', '/', '', 200, JSON.dump(value))
5
+ @mocks ||= {}
6
+ @mocks[[resource, action]] ||= {}
7
+ @mocks[[resource, action]][params] = response
8
+ ApipieBindings::API.any_instance.stubs(:fake_responses).returns(@mocks)
9
+ end
10
+
11
+ def self.clear_mocks
12
+ @mocks = {}
13
+ ApipieBindings::API.any_instance.stubs(:fake_responses).returns(@mocks)
14
+ end
15
+
16
+ def self.mock_action_calls(*calls)
17
+ calls.each do |(resource, action, value, params)|
18
+ mock_action_call(resource, action, value, (params || :default))
19
+ end
20
+ end
21
+
22
+ def self.smart_class_parameters_index
23
+ ResourceMocks.mock_action_call(:smart_class_parameters, :index,
24
+ { "results" => [ { 'parameter' => 'config', 'id' => '1'} ] })
25
+ end
26
+
27
+ def self.smart_class_parameters_show
28
+ ResourceMocks.mock_action_call(:smart_class_parameters, :show, { 'smart_class_parameter' => { 'override_value_order' => '', 'environments' => [] }})
29
+ end
30
+
31
+ def self.smart_variables_index
32
+ ResourceMocks.mock_action_call(:smart_variables, :index,
33
+ { "results" => [ { 'variable' => 'var', 'id' => '1'} ] })
34
+ end
35
+
36
+ def self.smart_variables_show
37
+ ResourceMocks.mock_action_call(:smart_variables, :show, { "id" => 1, "override_value_order" => "fqdn" })
38
+ end
39
+
40
+ def self.organizations_index
41
+ ResourceMocks.mock_action_call(:organizations, :index, {
42
+ "results" => [
43
+ {
44
+ "label" => "Default_Organization",
45
+ "id" => 1,
46
+ "name" => "Default_Organization",
47
+ "title" => "Default_Organization"
48
+ }
49
+ ]})
50
+ end
51
+
52
+ def self.hosts_show
53
+ ResourceMocks.mock_action_call(:hosts, :show, {
54
+ "ip" => "192.168.122.51",
55
+ "ip6" => nil,
56
+ "environment_id" => 1,
57
+ "environment_name" => "production",
58
+ "last_report" => "2016-10-24 12:06:31 UTC",
59
+ "mac" => "52:54:00:ce:b2:b9",
60
+ "realm_id" => nil,
61
+ "realm_name" => nil,
62
+ "sp_mac" => nil,
63
+ "sp_ip" => nil,
64
+ "sp_name" => nil,
65
+ "domain_id" => 1,
66
+ "domain_name" => "tstrachota.usersys.redhat.com",
67
+ "architecture_id" => 1,
68
+ "architecture_name" => "x86_64",
69
+ "operatingsystem_id" => 1,
70
+ "operatingsystem_name" => "CentOS 7.2.1511",
71
+ "build" => false,
72
+ "model_id" => 1,
73
+ "hostgroup_id" => nil,
74
+ "owner_id" => nil,
75
+ "owner_type" => nil,
76
+ "enabled" => true,
77
+ "managed" => false,
78
+ "use_image" => nil,
79
+ "image_file" => "",
80
+ "uuid" => nil,
81
+ "compute_resource_id" => nil,
82
+ "compute_resource_name" => nil,
83
+ "compute_profile_id" => nil,
84
+ "compute_profile_name" => nil,
85
+ "capabilities" => ["build"],
86
+ "provision_method" => "build",
87
+ "certname" => "foreman.example.com",
88
+ "image_id" => nil,
89
+ "image_name" => nil,
90
+ "created_at" => "2016-10-24 08:36:43 UTC",
91
+ "updated_at" => "2016-10-24 12:06:46 UTC",
92
+ "last_compile" => "2016-10-24 12:06:41 UTC",
93
+ "global_status" => 0,
94
+ "global_status_label" => "Warning",
95
+ "organization_id" => nil,
96
+ "organization_name" => nil,
97
+ "location_id" => nil,
98
+ "location_name" => nil,
99
+ "puppet_status" => 0,
100
+ "model_name" => "KVM",
101
+ "configuration_status" => 0,
102
+ "configuration_status_label" => "No reports",
103
+ "name" => "foreman.example.com",
104
+ "id" => 1,
105
+ "puppet_proxy_id" => 1,
106
+ "puppet_proxy_name" => "foreman.example.com",
107
+ "puppet_ca_proxy_id" => 1,
108
+ "puppet_ca_proxy_name" => "foreman.example.com",
109
+ "puppet_proxy" => {
110
+ "name" => "foreman.example.com",
111
+ "id" => 1,
112
+ "url" => "https://foreman.example.com:9090"
113
+ },
114
+ "puppet_ca_proxy" => {
115
+ "name" => "foreman.example.com",
116
+ "id" => 1,
117
+ "url" => "https://foreman.example.com:9090"
118
+ },
119
+ "hostgroup_name" => nil,
120
+ "hostgroup_title" => nil,
121
+ "parameters" => [],
122
+ "all_parameters" => [],
123
+ "interfaces" => [{
124
+ "id" => 1,
125
+ "name" => "foreman.example.com",
126
+ "ip" => "192.168.122.51",
127
+ "mac" => "52:54:00:ce:b2:b9",
128
+ "identifier" => "eth0",
129
+ "primary" => true,
130
+ "provision" => true,
131
+ "type" => "interface"
132
+ },
133
+ {
134
+ "id" => 2,
135
+ "name" => nil,
136
+ "ip" => "10.34.130.105",
137
+ "mac" => "52:54:00:f5:1b:57",
138
+ "identifier" => "eth1",
139
+ "primary" => false,
140
+ "provision" => false,
141
+ "type" => "interface"
142
+ }],
143
+ "puppetclasses" => [],
144
+ "config_groups" => [],
145
+ "all_puppetclasses" => []
146
+ })
147
+ end
148
+
149
+
150
+ def self.organizations_show
151
+ ResourceMocks.mock_action_calls(
152
+ [:organizations, :index, [{ "id" => 2, "name" => "ACME" }]],
153
+ [:organizations, :show, { "id" => 2, "name" => "ACME" }]
154
+ )
155
+ end
156
+
157
+ def self.locations_index
158
+ ResourceMocks.mock_action_call(:locations, :index, {
159
+ "results" => [
160
+ {
161
+ "ancestry" => nil,
162
+ "created_at" => "2014-07-17T17:21:49+02:00",
163
+ "updated_at" => "2015-06-17T13:18:10+02:00",
164
+ "id" => 2,
165
+ "name" => "Default_Location",
166
+ "title" => "Default_Location"
167
+ }
168
+ ]})
169
+ end
170
+
171
+ def self.locations_show
172
+ ResourceMocks.mock_action_calls(
173
+ [:locations, :index, [{ "id" => 2, "name" => "Rack" }]],
174
+ [:locations, :show, { "id" => 2, "name" => "Rack" }]
175
+ )
176
+ end
177
+
178
+ def self.config_groups_index
179
+ ResourceMocks.mock_action_call(:config_groups, :index, [{
180
+ id: 15,
181
+ name: "test config group",
182
+ puppetclasses: [ { name: "My puppetclass" } ]
183
+ }])
184
+ end
185
+
186
+ end
@@ -0,0 +1,81 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require File.join(File.dirname(__FILE__), 'apipie_resource_mock')
3
+
4
+ require 'hammer_cli_foreman_puppet/config_group'
5
+
6
+ describe HammerCLIForemanPuppet::ConfigGroup do
7
+ include CommandTestHelper
8
+
9
+ context "ListCommand" do
10
+ let(:cmd) { HammerCLIForemanPuppet::ConfigGroup::ListCommand.new("", ctx) }
11
+
12
+ before :each do
13
+ ResourceMocks.config_groups_index
14
+ end
15
+
16
+ context "parameters" do
17
+ it_should_accept "no arguments"
18
+ it_should_accept_search_params
19
+ it_should_accept 'organization', ['--organization-id=1']
20
+ it_should_accept 'location', ['--location-id=1']
21
+ end
22
+
23
+ context "output" do
24
+ let(:expected_record_count) { cmd.resource.call(:index).length }
25
+ it_should_print_n_records
26
+ it_should_print_columns ["ID", "Name"]
27
+ end
28
+ end
29
+
30
+ context "InfoCommand" do
31
+ let(:cmd) { HammerCLIForemanPuppet::ConfigGroup::InfoCommand.new("", ctx) }
32
+
33
+ context "parameters" do
34
+ it_should_accept "id", ["--id=1"]
35
+ it_should_accept "name", ["--name=group_x"]
36
+ it_should_accept 'organization', %w[--id=1 --organization-id=1]
37
+ it_should_accept 'location', %w[--id=1 --location-id=1]
38
+ end
39
+
40
+ context "output" do
41
+ with_params ["--id=1"] do
42
+ it_should_print_n_records 1
43
+ it_should_print_column "Name"
44
+ it_should_print_column "ID"
45
+ it_should_print_column "Puppetclasses"
46
+ end
47
+ end
48
+ end
49
+
50
+ context "CreateCommand" do
51
+ let(:cmd) { HammerCLIForemanPuppet::ConfigGroup::CreateCommand.new("", ctx) }
52
+
53
+ context "parameters" do
54
+ it_should_accept 'name, puppetclass ids, location, organization',
55
+ %w[--name=first_group --puppet-class-ids=1,2 --location-id=1 --organization-id=1]
56
+ end
57
+ end
58
+
59
+ context "DeleteCommand" do
60
+ let(:cmd) { HammerCLIForemanPuppet::ConfigGroup::DeleteCommand.new("", ctx) }
61
+
62
+ context "parameters" do
63
+ it_should_accept "name", ["--name=group_x"]
64
+ it_should_accept "id", ["--id=1"]
65
+ it_should_accept 'organization', %w[--id=1 --organization-id=1]
66
+ it_should_accept 'location', %w[--id=1 --location-id=1]
67
+ end
68
+ end
69
+
70
+ context "UpdateCommand" do
71
+
72
+ let(:cmd) { HammerCLIForemanPuppet::ConfigGroup::UpdateCommand.new("", ctx) }
73
+
74
+ context "parameters" do
75
+ it_should_accept "name", ["--name=group_x"]
76
+ it_should_accept "id", ["--id=1"]
77
+ it_should_accept 'organization', %w[--id=1 --organization-id=1]
78
+ it_should_accept 'location', %w[--id=1 --location-id=1]
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,163 @@
1
+ require File.join(File.dirname(__FILE__), '../test_output_adapter')
2
+
3
+ class IdResolverTestProxy
4
+
5
+ attr_reader :api
6
+
7
+ def initialize(original_resolver)
8
+ @original_resolver = original_resolver
9
+ define_id_finders
10
+ end
11
+
12
+ def scoped_options(scope, options, mode = nil)
13
+ @original_resolver.scoped_options(scope, options, mode)
14
+ end
15
+
16
+ def searchables(resource)
17
+ @original_resolver.searchables(resource)
18
+ end
19
+
20
+ def puppet_environment_id(options)
21
+ environment_id(options)
22
+ end
23
+
24
+ def puppet_environment_ids(options)
25
+ environment_ids(options)
26
+ end
27
+
28
+ protected
29
+
30
+ def define_id_finders
31
+ @original_resolver.api.resources.each do |resource|
32
+ method_name = "#{resource.singular_name}_id"
33
+ self.class.send(:define_method, method_name) do |options|
34
+ value = options[HammerCLI.option_accessor_name("id")]
35
+ value ||= HammerCLI::NilValue if searchables(resource).any? do |s|
36
+ options[HammerCLI.option_accessor_name(s.name)] == HammerCLI::NilValue
37
+ end
38
+ value ||= 1 if searchables(resource).any? do |s|
39
+ !options[HammerCLI.option_accessor_name(s.name)].nil?
40
+ end
41
+ value
42
+ end
43
+
44
+ method_name = "#{resource.singular_name}_ids"
45
+ self.class.send(:define_method, method_name) do |options|
46
+ options["option_#{resource.singular_name}_ids"].nil? ? nil : [1]
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+
54
+ module CommandTestHelper
55
+
56
+ def self.included(base)
57
+ base.extend(ClassMethods)
58
+
59
+ base.before :each do
60
+ resolver = cmd.resolver
61
+ cmd.stubs(:resolver).returns(IdResolverTestProxy.new(resolver))
62
+ end
63
+ end
64
+
65
+ def count_records(data)
66
+ HammerCLIForeman.collection_to_common_format(data['results']).count
67
+ end
68
+
69
+ module ClassMethods
70
+
71
+ def with_params(params, &block)
72
+ context "with params "+params.to_s do
73
+ let(:with_params) { params }
74
+ self.instance_eval &block
75
+ end
76
+ end
77
+
78
+ def it_should_call_action(action, params, headers={})
79
+ it "should call action "+action.to_s do
80
+ arguments ||= respond_to?(:with_params) ? with_params : []
81
+ ApipieBindings::API.any_instance.expects(:call).with() do |r,a,p,h,o|
82
+ (r == cmd.resource.name && a == action && p == params && h == headers)
83
+ end
84
+ cmd.run(arguments)
85
+ end
86
+ end
87
+
88
+ def it_should_call_action_and_test_params(action, &block)
89
+ it "should call action "+action.to_s do
90
+ arguments ||= respond_to?(:with_params) ? with_params : []
91
+ ApipieBindings::API.any_instance.expects(:call).with() do |r,a,p,h,o|
92
+ (r == cmd.resource.name && a == action && yield(p))
93
+ end
94
+ cmd.run(arguments)
95
+ end
96
+ end
97
+
98
+ def it_should_fail_with(message, arguments=[])
99
+ it "should fail with " + message.to_s do
100
+ _(cmd.run(arguments)).must_equal HammerCLI::EX_USAGE
101
+ end
102
+ end
103
+
104
+ def it_should_accept(message, arguments=[])
105
+ it "should accept " + message.to_s do
106
+ out, err = capture_io do
107
+ _(cmd.run(arguments)).must_equal HammerCLI::EX_OK
108
+ end
109
+ end
110
+ end
111
+
112
+ def it_should_output(message, adapter=:base)
113
+ it "should output '" + message.to_s + "'" do
114
+ arguments ||= respond_to?(:with_params) ? with_params : []
115
+ cmd.stubs(:context).returns(ctx.update(:adapter => adapter))
116
+ out, err = capture_io do
117
+ cmd.run(arguments)
118
+ end
119
+ _(out).must_include message
120
+ end
121
+ end
122
+
123
+ def it_should_print_column(column_name, arguments=nil)
124
+ it "should print column " + column_name do
125
+ arguments ||= respond_to?(:with_params) ? with_params : []
126
+
127
+ cmd.stubs(:context).returns(ctx.update(:adapter => :test))
128
+ out, err = capture_io do
129
+ cmd.run(arguments)
130
+ end
131
+
132
+ _(out.split("\n")[0]).must_match /.*##{column_name}#.*/
133
+ end
134
+ end
135
+
136
+ def it_should_print_columns(column_names, arguments=nil)
137
+ column_names.each do |name|
138
+ it_should_print_column name, arguments
139
+ end
140
+ end
141
+
142
+ def it_should_print_n_records(count=nil, arguments=nil)
143
+ it "should print correct count of records" do
144
+ arguments ||= respond_to?(:with_params) ? with_params : []
145
+
146
+ cmd.stubs(:context).returns(ctx.update(:adapter => :test))
147
+ count ||= expected_record_count rescue 0
148
+ out, err = capture_io do
149
+ cmd.run(arguments)
150
+ end
151
+ _(out.split(/\n/).length).must_equal count+1 # plus 1 for line with column headers
152
+ end
153
+ end
154
+
155
+ def it_should_accept_search_params
156
+ it_should_accept "search", ["--search=some_search"]
157
+ it_should_accept "per page", ["--per-page=1"]
158
+ it_should_accept "page", ["--page=2"]
159
+ it_should_accept "order", ["--order=order"]
160
+ end
161
+ end
162
+
163
+ end