-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest_sprout.py
executable file
·331 lines (275 loc) · 11.5 KB
/
unittest_sprout.py
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python3
import mock
import uuid
import yaml
import unittest
from time import sleep
from pprint import pprint
import googleapiclient.discovery as googleapi
from googleapiclient.errors import HttpError
from oauth2client.client import GoogleCredentials
from sprout import parse_args
from sprout import TerraformDeployment
from sprout import ComputeOperator
class TestTerraformDeployment(unittest.TestCase):
@mock.patch('sprout.call')
def test_basic_tf_plan_call(self, mock_call):
name = 'development'
var_files = ['test.tfvars']
state_file = 'tfstate-files/test.tfstate'
#variables = {'version': '0.1'}
basic_plan_call = [
"terraform",
"plan",
"-var-file={}".format(var_files[0]),
"-state={}".format(state_file)]
deployment = TerraformDeployment(
name = name,
var_files = var_files,
state_file = state_file)
deployment.plan(dry_run = False)
mock_call.assert_called_with(basic_plan_call)
def test_read_yaml_config(self):
""" Test formatting of sprout config file.
"""
config_file = 'sprout_unittest.yaml'
with open(config_file) as config_fh:
config = yaml.load(config_fh)
self.assertTrue(len(config['terraform_sets']) == 1)
dev_set = config['terraform_sets'][0]
self.assertTrue(dev_set['name'] == 'development')
self.assertTrue(dev_set['var-file'] == 'development.tfvars')
self.assertTrue(dev_set['state-file'] == 'tfstate-files/development.tfstate')
class TestGimsDeployment(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(__class__, self).__init__(*args, **kwargs)
self.project = 'gbsc-gcp-project-scgs-dev'
self.zone = 'us-central1-a'
self.machine_type = 'n1-standard-1'
self.name = 'sprout-test-instance'
self.image_name = 'sprout-test-image'
self.dummy_image = 'dummy-image'
self.credentials = GoogleCredentials.get_application_default()
self.api_service = googleapi.build(
'compute',
'v1',
credentials = self.credentials)
def set_up(self, compute, project, zone, machine_type, name):
""" Create basic compute instance in SCGS dev project.
Run to set up environment to test GIMS deployment functions.
Dev status: Done.
compute (obj): Google compute API service client
project (str): Google project ID
zone (str): Compute zone ("us-west1-a")
machine_type (str): Compute instance machine type
name (str): Compute instance name
"""
# Get the latest Debian Jessie image.
image_response = compute.images().getFromFamily(
project='debian-cloud', family='debian-8').execute()
source_disk_image = image_response['selfLink']
# Configure instance settings
machine_url = "zones/{}/machineTypes/{}".format(
zone,
machine_type)
config = {
"name": name,
"machineType": machine_url,
# Specify the boot disk and the image to use as a source.
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': source_disk_image,
}
}
],
# Specify a network interface with NAT to access the public
# internet.
'networkInterfaces': [{
'network': 'global/networks/default',
'accessConfigs': [
{
'type': 'ONE_TO_ONE_NAT',
'name': 'External NAT'
}
]
}]
}
# Delete an existing disk image
request_id = str(uuid.uuid4())
request = self.compute.images().delete(
project = self.project,
image = self.image_name,
requestId = request_id)
try:
response = request.execute()
wait_for_status(request, response, 'DONE', 60)
except HttpError as err:
if err.resp.status in [404]:
pprint("Skipping delete: image does not exist")
pass
else:
raise
# Delete an existing instance
request_id = str(uuid.uuid4())
request = self.compute.instances().delete(
project = self.project,
zone = self.zone,
instance = self.name,
requestId = request_id)
try:
response = request.execute()
wait_for_status(request, response, 'DONE', 300)
except HttpError as err:
if err.resp.status in [404]:
pprint("Skipping delete: instance does not exist")
pass
else:
raise
# Create new instance
request_id = str(uuid.uuid4())
request = self.compute.instances().insert(
project = self.project,
zone = self.zone,
body = config,
requestId = request_id)
response = request.execute()
wait_for_status(request, response, 'DONE', 300)
pprint("Setup complete.")
pprint("=================")
def test_stop_instance(self):
""" Stop compute instance.
Dev status: Done.
"""
pprint("Setting up compute instance.")
self.set_up(
self.api_service,
self.project,
self.zone,
self.machine_type,
self.name)
pprint("Setup complete.")
compute = ComputeOperator(
self.project,
self.zone)
compute.stop_instance(self.name)
# Check that instance has been stopped
request = self.api_service.instances().get(
project = self.project,
zone = self.zone,
instance = self.name)
response = request.execute()
self.assertTrue(response['status'] == 'TERMINATED')
def test_delete_instance(self):
""" Delete compute instance.
Dev status: Done.
"""
pprint("Setting up compute instance.")
self.set_up(
self.api_service,
self.project,
self.zone,
self.machine_type,
self.name)
pprint("Setup complete.")
compute = ComputeOperator(
self.project,
self.zone)
compute.delete_instance(self.name)
# Get list of comute instances
request = self.api_service.instances().list(
project = self.project,
zone = self.zone)
response = request.execute()
# Determine whether instance is in list
for instance in response['items']:
self.assertFalse(instance['name'] == self.name)
def test_create_image(self):
""" Stop instance and create image from it.
Dev status: Done.
"""
source_disk = "zones/{}/disks/{}".format(
self.zone,
self.name)
force = True
pprint("Setting up compute instance.")
self.set_up(
self.api_service,
self.project,
self.zone,
self.machine_type,
self.name)
compute = ComputeOperator(
self.project,
self.zone)
compute.stop_instance(self.name)
compute.create_image(
image_name = self.image_name,
source_disk = source_disk,
force = True)
# Test whether you can get image
request = self.api_service.images().get(
project = self.project,
image = self.image_name)
try:
response = request.execute()
except HttpError as err:
pprint(response)
def test_delete_image(self):
""" Delete disk image.
Dev status: In-progress.
"""
compute = ComputeOperator(
self.project,
self.zone)
compute.delete_image(image_name = self.image_name)
# Check that image does not exist
request = self.api_service.images().get(
project = self.project,
image = self.image_name)
try:
response = request.execute()
except HttpError as err:
if err.code == 404:
pass
else:
raise
class ParseArgsTestCase(unittest.TestCase):
def test_config_arg(self):
args = parse_args(['--config', 'sprout_unittest.yaml'])
self.assertTrue(args.config_file == "sprout_unittest.yaml")
def wait_for_status(request, response, status, timeout):
""" Wait for Google Cloud API request to complete.
Possible status are PENDING, RUNNING, or DONE.
"""
sleep_interval = 5
timeout_cycles = int(timeout)/sleep_interval
valid_statuses = ['PENDING', 'RUNNING', 'DONE']
if not status in valid_statuses:
raise ValueError(
'{} '.format(status) +
'is not a valid status. ' +
'{}'.format(valid_statuses))
# TODO: Test custom error and fully integrate status/timeout
# variables into function.
n = 0
while response['status'] != status:
if n >= timeout_cycles:
raise TimeoutError("Operation exceeded timeout period. " +
"{}: {}".format(op_kind, op_type))
sleep(sleep_interval)
response = request.execute()
op_kind = response['kind']
op_type = response['operationType']
pprint("Waiting for operation. {}: {}".format(
op_kind,
op_type))
pprint("=================")
pprint("Operation complete. {}: {}.".format(
op_kind,
op_type))
pprint("=================")
if __name__ == '__main__':
unittest.main()