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
|
#!/usr/bin/env python
"""This module tests the deployment with constraints."""
from __future__ import print_function
import argparse
import logging
import os
import sys
import re
import yaml
from deploy_stack import (
BootstrapManager,
)
from jujucharm import (
Charm,
local_charm_path,
)
from utility import (
add_basic_testing_arguments,
configure_logging,
JujuAssertionError,
temp_dir,
)
__metaclass__ = type
log = logging.getLogger("assess_constraints")
VIRT_TYPES = ['lxd']
INSTANCE_TYPES = {
'azure': [],
'ec2': ['t2.micro'],
'gce': [],
'joyent': [],
'openstack': [],
}
# This assumes instances are unique accross providers.
def get_instance_spec(instance_type):
"""Get the specifications of a given instance type."""
return {
't2.micro': {'mem': '1G', 'cpu-power': '10', 'cores': '1'},
}[instance_type]
def mem_to_int(size):
"""Convert an argument size into a number of megabytes."""
if not re.match(re.compile('^[0123456789]+[MGTP]?$'), size):
raise JujuAssertionError('Not a size format:', size)
if size[-1] in 'MGTP':
val = int(size[0:-1])
unit = size[-1]
return val * (1024 ** 'MGTP'.find(unit))
else:
return int(size)
class Constraints:
"""Class that represents a set of contraints."""
@staticmethod
def _list_to_str(constraints_list):
parts = ['{}={}'.format(name, value) for (name, value) in
constraints_list if value is not None]
return ' '.join(parts)
def __init__(self, mem=None, cores=None, virt_type=None,
instance_type=None, root_disk=None, cpu_power=None,
arch=None):
"""Create a new constraints instance from individual constraints."""
self.mem = mem
self.cores = cores
self.virt_type = virt_type
self.instance_type = instance_type
self.root_disk = root_disk
self.cpu_power = cpu_power
self.arch = arch
def __str__(self):
"""Convert the instance constraint values into an argument string."""
return Constraints._list_to_str(
[('mem', self.mem), ('cores', self.cores),
('virt-type', self.virt_type),
('instance-type', self.instance_type),
('root-disk', self.root_disk), ('cpu-power', self.cpu_power),
('arch', self.arch),
])
def __eq__(self, other):
return (self.mem == other.mem and self.cores == other.cores and
self.virt_type == other.virt_type and
self.instance_type == other.instance_type and
self.root_disk == other.root_disk and
self.cpu_power == other.cpu_power and
self.arch == other.arch
)
@staticmethod
def _meets_string(constraint, actual):
if constraint is None:
return True
return constraint == actual
@staticmethod
def _meets_min_int(constraint, actual):
if constraint is None:
return True
return int(constraint) <= int(actual)
@staticmethod
def _meets_min_mem(constraint, actual):
if constraint is None:
return True
return mem_to_int(constraint) <= mem_to_int(actual)
def meets_root_disk(self, actual_root_disk):
"""Check to see if a given value meets the root_disk constraint."""
return self._meets_min_mem(self.root_disk, actual_root_disk)
def meets_cores(self, actual_cores):
"""Check to see if a given value meets the cores constraint."""
return self._meets_min_int(self.cores, actual_cores)
def meets_cpu_power(self, actual_cpu_power):
"""Check to see if a given value meets the cpu_power constraint."""
return self._meets_min_int(self.cpu_power, actual_cpu_power)
def meets_arch(self, actual_arch):
"""Check to see if a given value meets the arch constraint."""
return self._meets_string(self.arch, actual_arch)
def meets_instance_type(self, actual_data):
"""Check to see if a given value meets the instance_type constraint.
Currently there is no direct way to check for it, so we 'fingerprint'
each instance_type in a dictionary."""
instance_data = get_instance_spec(self.instance_type)
for (key, value) in instance_data.iteritems():
# Temperary fix until cpu-cores -> cores switch is finished.
if key is 'cores' and 'cpu-cores' in actual_data:
key = 'cpu-cores'
if key not in actual_data:
raise JujuAssertionError('Missing data:', key)
elif key in ['mem', 'root-disk']:
if mem_to_int(value) != mem_to_int(actual_data[key]):
return False
elif value != actual_data[key]:
return False
else:
return True
def deploy_constraint(client, constraints, charm, series, charm_repo):
"""Test deploying charm with constraints."""
client.deploy(charm, series=series, repository=charm_repo,
constraints=str(constraints))
client.wait_for_workloads()
def deploy_charm_constraint(client, constraints, charm_name, charm_series,
charm_dir):
"""Create a charm with constraints and test deploying it."""
constraints_charm = Charm(charm_name,
'Test charm for constraints',
series=[charm_series])
charm_root = constraints_charm.to_repo_dir(charm_dir)
platform = 'ubuntu'
charm = local_charm_path(charm=charm_name,
juju_ver=client.version,
series=charm_series,
repository=os.path.dirname(charm_root),
platform=platform)
deploy_constraint(client, constraints, charm,
charm_series, charm_dir)
def juju_show_machine_hardware(client, machine):
"""Uses juju show-machine and returns information about the hardware."""
raw = client.get_juju_output('show-machine', machine, '--format', 'yaml')
raw_yaml = yaml.load(raw)
try:
hardware = raw_yaml['machines'][machine]['hardware']
except KeyError as error:
raise KeyError(error.args, raw_yaml)
data = {}
for kvp in hardware.split(' '):
(key, value) = kvp.split('=')
data[key] = value
return data
def assess_virt_type(client, virt_type):
"""Assess the virt-type option for constraints"""
if virt_type not in VIRT_TYPES:
raise JujuAssertionError(virt_type)
constraints = Constraints(virt_type=virt_type)
charm_name = 'virt-type-' + virt_type
charm_series = 'xenial'
with temp_dir() as charm_dir:
deploy_charm_constraint(client, constraints, charm_name,
charm_series, charm_dir)
def assess_virt_type_constraints(client, test_kvm=False):
"""Assess deployment with virt-type constraints."""
if test_kvm:
VIRT_TYPES.append("kvm")
for virt_type in VIRT_TYPES:
assess_virt_type(client, virt_type)
try:
assess_virt_type(client, 'aws')
except JujuAssertionError:
log.info("Correctly rejected virt-type aws")
else:
raise JujuAssertionError("FAIL: Client deployed with virt-type aws")
if test_kvm:
VIRT_TYPES.remove("kvm")
def assess_instance_type(client, provider, instance_type):
"""Assess the instance-type option for constraints"""
if instance_type not in INSTANCE_TYPES[provider]:
raise JujuAssertionError(instance_type)
constraints = Constraints(instance_type=instance_type)
charm_name = 'instance-type-' + instance_type.replace('.', '-')
charm_series = 'xenial'
with temp_dir() as charm_dir:
deploy_charm_constraint(client, constraints, charm_name,
charm_series, charm_dir)
client.wait_for_started()
data = juju_show_machine_hardware(client, '0')
if not constraints.meets_instance_type(data):
raise ValueError('Test failed', charm_name)
def assess_instance_type_constraints(client, provider=None):
"""Assess deployment with instance-type constraints."""
if provider is None:
provider = client.env.config.get('type')
if provider not in INSTANCE_TYPES:
return
for instance_type in INSTANCE_TYPES[provider]:
assess_instance_type(client, provider, instance_type)
def assess_constraints(client, test_kvm=False):
"""Assess deployment with constraints."""
provider = client.env.config.get('type')
if 'lxd' == provider:
assess_virt_type_constraints(client, test_kvm)
elif 'ec2' == provider:
assess_instance_type_constraints(client, provider)
def parse_args(argv):
"""Parse all arguments."""
parser = argparse.ArgumentParser(description="Test constraints")
add_basic_testing_arguments(parser)
return parser.parse_args(argv)
def main(argv=None):
args = parse_args(argv)
configure_logging(args.verbose)
bs_manager = BootstrapManager.from_args(args)
test_kvm = '--with-virttype-kvm' in args
with bs_manager.booted_context(args.upload_tools):
assess_constraints(bs_manager.client, test_kvm)
return 0
if __name__ == '__main__':
sys.exit(main())
|