forked from Katello/katello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts_and_hostgroups_helper_test.rb
247 lines (211 loc) · 10 KB
/
hosts_and_hostgroups_helper_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require 'test_helper'
require 'katello_test_helper'
class HostsAndHostGroupsHelperTestBase < ActionView::TestCase
include ::Katello::HostsAndHostgroupsHelper
include ApplicationHelper
attr_accessor :params
end
class HostAndHostGroupsHelperLifecycleEnvironmentTests < HostsAndHostGroupsHelperTestBase
def setup
User.current = User.anonymous_api_admin
@library = katello_environments(:library)
@host = FactoryGirl.build(:host, :with_content, :with_subscription,
:content_view => katello_content_views(:library_dev_view),
:lifecycle_environment => katello_environments(:library), :id => 343)
@host.organization = taxonomies(:organization1)
@group = FactoryGirl.build(:hostgroup)
@smart_proxy = FactoryGirl.create(:smart_proxy, :features => [FactoryGirl.create(:feature, name: 'Pulp')])
end
def test_accessible_lifecycle_environments
envs = accessible_lifecycle_environments(@library.organization, @host)
assert_includes(envs, @library)
end
def test_accessible_lifecycle_environments_limited
User.current = FactoryGirl.create(:user)
envs = accessible_lifecycle_environments(@library.organization, @host)
assert_equal([@host.content_facet.lifecycle_environment], envs)
end
def test_relevant_organizations
org = Organization.new
Organization.stubs(:my_organizations).returns([org])
assert_equal [@host.organization], relevant_organizations(@host)
@host.organization = nil
assert_equal [org], relevant_organizations(@host)
end
def test_accessible_content_proxies
assert_includes accessible_content_proxies(@host), @smart_proxy
assert_includes accessible_content_proxies(@group), @smart_proxy
end
def test_accessible_content_proxies_no_perms
User.current = FactoryGirl.create(:user)
FactoryGirl.create(:smart_proxy, :features => [FactoryGirl.create(:feature, name: 'Pulp')])
@host.content_facet.content_source = @smart_proxy
assert_equal [@smart_proxy], accessible_content_proxies(@host)
end
end
class HostsAndHostGroupsHelperKickstartRepositoryOptionsTest < HostsAndHostGroupsHelperTestBase
def setup
@repo_with_distro = katello_repositories(:fedora_17_x86_64)
@os = ::Redhat.create_operating_system('RedHat', '9', '0')
@content_source = FactoryGirl.create(:smart_proxy, :name => "foobar", :url => "http://capsule.com/")
@arch = architectures(:x86_64)
@cv = @repo_with_distro.content_view
@env = @repo_with_distro.environment
end
test "kickstart repository options should handle os - selected call with no params" do
self.params = {}
assert_empty kickstart_repository_options(nil)
end
test "kickstart_repository_options should handle os - selected call with all params" do
self.params = {"host" => {
"operatingsystem_id" => @os.id,
"content_view_id" => @cv.id,
"lifecycle_environment_id" => @env.id,
"content_source_id" => @content_source.id,
"architecture_id" => @arch.id
}}.with_indifferent_access
ret = [{:name => "boo" }]
::Operatingsystem.expects(:find).with(@os.id).returns(@os).at_least_once
@os.expects(:kickstart_repos).returns(ret).with do |host|
host.must_be_kind_of(::Host::Managed)
host.os.must_equal(@os)
host.content_facet.lifecycle_environment.must_equal(@env)
host.content_facet.content_view.must_equal(@cv)
host.content_source.must_equal(@content_source)
host.architecture.must_equal(@arch)
end
options = kickstart_repository_options(nil)
refute_empty options
assert_equal ret.first[:name], options.first.name
end
test "kickstart_repository_options should provide options for a populated host" do
host = ::Host.new(:architecture => @arch, :operatingsystem => @os,
:content_facet_attributes => {:lifecycle_environment_id => @env.id,
:content_view_id => @cv.id,
:content_source_id => @content_source.id})
ret = [{:name => "boo" }]
@os.expects(:kickstart_repos).returns(ret).with do |param_host|
param_host.must_be_kind_of(::Host::Managed)
param_host.os.must_equal(@os)
param_host.content_facet.lifecycle_environment.must_equal(@env)
param_host.content_facet.content_view.must_equal(@cv)
param_host.content_source.must_equal(@content_source)
param_host.architecture.must_equal(@arch)
end
options = kickstart_repository_options(host)
refute_empty options
assert_equal ret.first[:name], options.first.name
end
test "kickstart_repository_options should_handle_non_redhat_host" do
hostgroup = Hostgroup.new(:operatingsystem => @os)
host = ::Host.new(:architecture => @arch, :operatingsystem => operatingsystems(:opensuse), :hostgroup => hostgroup,
:content_facet_attributes => {:lifecycle_environment_id => @env.id,
:content_view_id => @cv.id,
:content_source_id => @content_source.id})
options = kickstart_repository_options(host, :selected_host_group => hostgroup)
assert_empty options
end
test "kickstart_repository_options should provide options for a populated hostgroup" do
self.params = {}
hostgroup = ::Hostgroup.new(:lifecycle_environment_id => @env.id,
:content_view_id => @cv.id)
hostgroup.architecture = @arch
hostgroup.operatingsystem = @os
hostgroup.content_source = @content_source
ret = [{:name => "boo" }]
@os.expects(:kickstart_repos).returns(ret).with do |param_host|
param_host.must_be_kind_of(::Host::Managed)
param_host.os.must_equal(@os)
param_host.content_facet.lifecycle_environment.must_equal(@env)
param_host.content_facet.content_view.must_equal(@cv)
param_host.content_source.must_equal(@content_source)
param_host.architecture.must_equal(@arch)
end
options = kickstart_repository_options(hostgroup)
refute_empty options
assert_equal ret.first[:name], options.first.name
end
test "kickstart_repository_options should provide options for a populated host with a selected_host_group" do
host = ::Host.new
hostgroup = ::Hostgroup.new(:lifecycle_environment_id => @env.id,
:content_view_id => @cv.id)
hostgroup.architecture = @arch
hostgroup.operatingsystem = @os
hostgroup.content_source = @content_source
ret = [{:name => "boo" }]
@os.expects(:kickstart_repos).returns(ret).with do |param_host|
param_host.must_be_kind_of(::Host::Managed)
param_host.os.must_equal(@os)
param_host.content_facet.lifecycle_environment.must_equal(@env)
param_host.content_facet.content_view.must_equal(@cv)
param_host.content_source.must_equal(@content_source)
param_host.architecture.must_equal(@arch)
end
options = kickstart_repository_options(host, :selected_host_group => hostgroup)
refute_empty options
assert_equal ret.first[:name], options.first.name
end
end
class HostsAndHostGroupsHelperKickstartRepositoryIDTest < HostsAndHostGroupsHelperTestBase
def setup
@repo_with_distro = katello_repositories(:fedora_17_x86_64)
@os = ::Redhat.create_operating_system('RedHat', '9', '0')
@content_source = FactoryGirl.create(:smart_proxy, :name => "foobar", :url => "http://capsule.com/")
@arch = architectures(:x86_64)
@cv = @repo_with_distro.content_view
@env = @repo_with_distro.environment
@hostgroup = ::Hostgroup.new(:lifecycle_environment_id => @env.id,
:content_view_id => @cv.id)
@hostgroup.architecture = @arch
@hostgroup.operatingsystem = @os
@hostgroup.content_source = @content_source
@host = ::Host.new(:architecture => @arch, :operatingsystem => @os,
:content_facet_attributes => {:lifecycle_environment_id => @env.id,
:content_view_id => @cv.id,
:content_source_id => @content_source.id})
end
test "must return host or host group kickstart id" do
repo_id = 1000
@hostgroup.kickstart_repository_id = repo_id
assert_equal repo_id, kickstart_repository_id(@hostgroup)
@host.content_facet.kickstart_repository_id = repo_id
assert_equal repo_id, kickstart_repository_id(@host)
end
test "must handle hosts or host groups with medium_id but no kickstart_repository_id" do
@hostgroup.kickstart_repository_id = nil
@hostgroup.medium_id = 1000
assert_nil kickstart_repository_id(@hostgroup)
@host.content_facet.kickstart_repository_id = nil
@host.medium_id = 1000
assert_nil kickstart_repository_id(@host)
end
test "must handle hosts or host groups with no medium_id or kickstart_repository_id" do
id = 100
option = mock
option.expects(:id).returns(id).at_least_once
@hostgroup.kickstart_repository_id = nil
@hostgroup.medium_id = nil
expects(:kickstart_repository_options).with(@hostgroup, {}).returns([option])
assert_equal id, kickstart_repository_id(@hostgroup)
@host.content_facet.kickstart_repository_id = nil
@host.medium_id = nil
expects(:kickstart_repository_options).with(@host, {}).returns([option])
assert_equal id, kickstart_repository_id(@host)
end
test "must handle nil hosts or host groups" do
expects(:kickstart_repository_options).with(nil, {}).returns([])
assert_nil kickstart_repository_id(nil)
end
test "must handle selected hosts or host groups" do
id = 100
@hostgroup.kickstart_repository_id = id
host = ::Host.new
expects(:kickstart_repository_options).at_least_once.
returns([OpenStruct.new(:id => id)])
assert_equal id, kickstart_repository_id(host, :selected_host_group => @hostgroup)
#if the host group had a medium_id lets make sure that gets used.
@hostgroup.kickstart_repository_id = nil
@hostgroup.medium_id = id
assert_nil kickstart_repository_id(::Host.new, :selected_host_group => @hostgroup)
end
end