7
from unittest import TestCase
16
def make_machine(state='running', hours=2):
17
then = datetime.utcnow() - timedelta(hours=hours)
21
'created': then.strftime(ISO_8601_FORMAT)}
24
def fake_list_machines(machine):
25
def list_machines(machine_id=None):
33
class JoyentTestCase(TestCase):
35
def test_parse_args(self):
37
['-d', '-v', '-u', 'sdc_url', '-a', 'account', '-k', 'key_id',
38
'-p', 'key/path', 'list-machines'])
39
self.assertEqual('sdc_url', args.sdc_url)
40
self.assertEqual('account', args.account)
41
self.assertEqual('key_id', args.key_id)
42
self.assertEqual('key/path', args.key_path)
43
self.assertTrue(args.dry_run)
44
self.assertTrue(args.verbose)
47
class ClientTestCase(TestCase):
51
'sdc_url', 'account', 'key_id', './key', 'manta_url',
52
dry_run=True, verbose=True)
53
self.assertEqual('sdc_url', client.sdc_url)
54
self.assertEqual('account', client.account)
55
self.assertEqual('key_id', client.key_id)
56
self.assertEqual('./key', client.key_path)
57
self.assertEqual(3, client.pause)
58
self.assertTrue(client.dry_run)
59
self.assertTrue(client.verbose)
61
def test_list_machine_tags(self):
63
'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
65
content = json.dumps({'env': 'foo'})
66
with patch.object(client, '_request', autospec=True,
67
return_value=(headers, content)) as mock:
68
tags = client._list_machine_tags('bar')
69
mock.assert_called_once_with('/machines/bar/tags')
70
self.assertEqual({'env': 'foo'}, tags)
72
def test_delete_old_machines(self):
73
machine = make_machine('stopped')
75
'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
76
with patch.object(client, '_list_machines',
77
side_effect=fake_list_machines(machine)) as lm_mock:
78
with patch.object(client, '_list_machine_tags', autospec=True,
79
return_value={}) as lmt_mock:
80
with patch.object(client, '_delete_running_machine',
81
autospec=True) as drm_mock:
82
with patch.object(client, 'attempt_deletion',
83
autospec=True) as rd_mock:
84
client.delete_old_machines(1)
85
lm_mock.assert_called_once_with()
86
lmt_mock.assert_called_once_with('id')
87
drm_mock.assert_called_once_with('id')
88
self.assertEqual(0, rd_mock.call_count)
90
def test_delete_old_machines_stuck_provisioning(self):
91
machine = make_machine('provisioning')
93
'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
94
with patch.object(client, '_list_machines', autospec=True,
95
side_effect=fake_list_machines(machine)):
96
with patch.object(client, '_list_machine_tags', autospec=True):
97
with patch.object(client, '_delete_running_machine',
98
autospec=True) as drm_mock:
99
with patch.object(client, 'attempt_deletion',
100
autospec=True) as rd_mock:
101
client.delete_old_machines(1)
102
self.assertEqual(0, drm_mock.call_count)
103
rd_mock.assert_called_once_with([machine])
105
def test_delete_old_machines_permanent(self):
106
machine = make_machine('provisioning')
108
'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
109
with patch.object(client, '_list_machines', autospec=True,
110
side_effect=fake_list_machines(machine)):
111
with patch.object(client, '_list_machine_tags', autospec=True,
112
return_value={'permanent': 'true'}) as lmt_mock:
113
with patch.object(client, '_delete_running_machine',
114
autospec=True) as drm_mock:
115
with patch.object(client, 'attempt_deletion',
116
autospec=True) as rd_mock:
117
client.delete_old_machines(1)
118
lmt_mock.assert_called_once_with('id')
119
self.assertEqual(0, drm_mock.call_count)
120
self.assertEqual(0, rd_mock.call_count)
122
def test_attempt_deletion(self):
124
'sdc_url', 'account', 'key_id', './key', 'manta_url', pause=0)
125
with patch.object(client, 'delete_machine', autospec=True) as dm_func:
126
all_success = client.attempt_deletion(['a', 'b'])
127
self.assertIs(True, all_success)
128
dm_func.assert_any_call('a')
129
dm_func.assert_any_call('b')
130
with patch.object(client, 'delete_machine', autospec=True,
131
side_effect=[Exception, None]) as dm_func:
132
all_success = client.attempt_deletion(['a', 'b'])
133
self.assertIs(False, all_success)
134
dm_func.assert_any_call('a')
135
dm_func.assert_any_call('b')