1
from cloudinit import util
2
from cloudinit.config import cc_rh_subscription
8
class GoodTests(unittest.TestCase):
10
super(GoodTests, self).setUp()
11
self.name = "cc_rh_subscription"
12
self.cloud_init = None
13
self.log = logging.getLogger("good_tests")
15
self.handle = cc_rh_subscription.handle
16
self.SM = cc_rh_subscription.SubscriptionManager
18
self.config = {'rh_subscription':
19
{'username': 'scooby@do.com',
20
'password': 'scooby-snacks'
22
self.config_full = {'rh_subscription':
23
{'username': 'scooby@do.com',
24
'password': 'scooby-snacks',
26
'service-level': 'self-support',
27
'add-pool': ['pool1', 'pool2', 'pool3'],
28
'enable-repo': ['repo1', 'repo2', 'repo3'],
29
'disable-repo': ['repo4', 'repo5']
32
def test_already_registered(self):
34
Emulates a system that is already registered. Ensure it gets
35
a non-ProcessExecution error from is_registered()
37
with mock.patch.object(cc_rh_subscription.SubscriptionManager,
38
'_sub_man_cli') as mockobj:
39
self.SM.log_success = mock.MagicMock()
40
self.handle(self.name, self.config, self.cloud_init,
42
self.assertEqual(self.SM.log_success.call_count, 1)
43
self.assertEqual(mockobj.call_count, 1)
45
def test_simple_registration(self):
47
Simple registration with username and password
49
self.SM.log_success = mock.MagicMock()
50
reg = "The system has been registered with ID:" \
51
" 12345678-abde-abcde-1234-1234567890abc"
52
self.SM._sub_man_cli = mock.MagicMock(
53
side_effect=[util.ProcessExecutionError, (reg, 'bar')])
54
self.handle(self.name, self.config, self.cloud_init,
56
self.assertIn(mock.call(['identity']),
57
self.SM._sub_man_cli.call_args_list)
58
self.assertIn(mock.call(['register', '--username=scooby@do.com',
59
'--password=scooby-snacks'],
61
self.SM._sub_man_cli.call_args_list)
63
self.assertEqual(self.SM.log_success.call_count, 1)
64
self.assertEqual(self.SM._sub_man_cli.call_count, 2)
66
def test_full_registration(self):
68
Registration with auto-attach, service-level, adding pools,
69
and enabling and disabling yum repos
72
call_lists.append(['attach', '--pool=pool1', '--pool=pool3'])
73
call_lists.append(['repos', '--enable=repo2', '--enable=repo3',
75
call_lists.append(['attach', '--auto', '--servicelevel=self-support'])
76
self.SM.log_success = mock.MagicMock()
77
reg = "The system has been registered with ID:" \
78
" 12345678-abde-abcde-1234-1234567890abc"
79
self.SM._sub_man_cli = mock.MagicMock(
80
side_effect=[util.ProcessExecutionError, (reg, 'bar'),
81
('Service level set to: self-support', ''),
82
('pool1\npool3\n', ''), ('pool2\n', ''), ('', ''),
83
('Repo ID: repo1\nRepo ID: repo5\n', ''),
84
('Repo ID: repo2\nRepo ID: repo3\nRepo ID: '
87
self.handle(self.name, self.config_full, self.cloud_init,
89
for call in call_lists:
90
self.assertIn(mock.call(call), self.SM._sub_man_cli.call_args_list)
91
self.assertEqual(self.SM.log_success.call_count, 1)
92
self.assertEqual(self.SM._sub_man_cli.call_count, 9)
95
class TestBadInput(unittest.TestCase):
96
name = "cc_rh_subscription"
98
log = logging.getLogger("bad_tests")
100
SM = cc_rh_subscription.SubscriptionManager
101
reg = "The system has been registered with ID:" \
102
" 12345678-abde-abcde-1234-1234567890abc"
104
config_no_password = {'rh_subscription':
105
{'username': 'scooby@do.com'
108
config_no_key = {'rh_subscription':
109
{'activation-key': '1234abcde',
112
config_service = {'rh_subscription':
113
{'username': 'scooby@do.com',
114
'password': 'scooby-snacks',
115
'service-level': 'self-support'
118
config_badpool = {'rh_subscription':
119
{'username': 'scooby@do.com',
120
'password': 'scooby-snacks',
121
'add-pool': 'not_a_list'
123
config_badrepo = {'rh_subscription':
124
{'username': 'scooby@do.com',
125
'password': 'scooby-snacks',
126
'enable-repo': 'not_a_list'
128
config_badkey = {'rh_subscription':
129
{'activation_key': 'abcdef1234',
134
super(TestBadInput, self).setUp()
135
self.handle = cc_rh_subscription.handle
137
def test_no_password(self):
139
Attempt to register without the password key/value
141
self.input_is_missing_data(self.config_no_password)
143
def test_no_org(self):
145
Attempt to register without the org key/value
147
self.input_is_missing_data(self.config_no_key)
149
def test_service_level_without_auto(self):
151
Attempt to register using service-level without the auto-attach key
153
self.SM.log_warn = mock.MagicMock()
154
self.SM._sub_man_cli = mock.MagicMock(
155
side_effect=[util.ProcessExecutionError, (self.reg, 'bar')])
156
self.handle(self.name, self.config_service, self.cloud_init,
158
self.assertEqual(self.SM._sub_man_cli.call_count, 1)
159
self.assertEqual(self.SM.log_warn.call_count, 2)
161
def test_pool_not_a_list(self):
163
Register with pools that are not in the format of a list
165
self.SM.log_warn = mock.MagicMock()
166
self.SM._sub_man_cli = mock.MagicMock(
167
side_effect=[util.ProcessExecutionError, (self.reg, 'bar')])
168
self.handle(self.name, self.config_badpool, self.cloud_init,
170
self.assertEqual(self.SM._sub_man_cli.call_count, 2)
171
self.assertEqual(self.SM.log_warn.call_count, 2)
173
def test_repo_not_a_list(self):
175
Register with repos that are not in the format of a list
177
self.SM.log_warn = mock.MagicMock()
178
self.SM._sub_man_cli = mock.MagicMock(
179
side_effect=[util.ProcessExecutionError, (self.reg, 'bar')])
180
self.handle(self.name, self.config_badrepo, self.cloud_init,
182
self.assertEqual(self.SM.log_warn.call_count, 3)
183
self.assertEqual(self.SM._sub_man_cli.call_count, 2)
185
def test_bad_key_value(self):
187
Attempt to register with a key that we don't know
189
self.SM.log_warn = mock.MagicMock()
190
self.SM._sub_man_cli = mock.MagicMock(
191
side_effect=[util.ProcessExecutionError, (self.reg, 'bar')])
192
self.handle(self.name, self.config_badkey, self.cloud_init,
194
self.assertEqual(self.SM.log_warn.call_count, 2)
195
self.assertEqual(self.SM._sub_man_cli.call_count, 1)
197
def input_is_missing_data(self, config):
199
Helper def for tests that having missing information
201
self.SM.log_warn = mock.MagicMock()
202
self.SM._sub_man_cli = mock.MagicMock(
203
side_effect=[util.ProcessExecutionError])
204
self.handle(self.name, config, self.cloud_init,
206
self.SM._sub_man_cli.assert_called_with(['identity'])
207
self.assertEqual(self.SM.log_warn.call_count, 4)
208
self.assertEqual(self.SM._sub_man_cli.call_count, 1)