Skip to content

Commit 5af9477

Browse files
authored
Merge branch 'master' into revert-python-libjuju
2 parents 70c7555 + ac9fe69 commit 5af9477

5 files changed

Lines changed: 62 additions & 19 deletions

File tree

.github/workflows/tox.yaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
python: [3.5, 3.6, 3.7, 3.8, 3.9]
11+
python:
12+
- "3.5"
13+
- "3.6"
14+
- "3.7"
15+
- "3.8"
16+
- "3.9"
17+
- "3.10"
1218
steps:
1319
- name: Check out code
1420
uses: actions/checkout@v2
@@ -25,7 +31,13 @@ jobs:
2531
runs-on: ubuntu-latest
2632
strategy:
2733
matrix:
28-
python: [3.5, 3.6, 3.7, 3.8, 3.9]
34+
python:
35+
- "3.5"
36+
- "3.6"
37+
- "3.7"
38+
- "3.8"
39+
- "3.9"
40+
- "3.10"
2941
steps:
3042
- uses: actions/checkout@v2
3143
- name: Setup Python

juju/constraints.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@
3232
"Y": 1024 ** 6
3333
}
3434

35+
# List of supported constraint keys, see
36+
# http://github.com/cderici/juju/blob/2.9/core/constraints/constraints.go#L20-L39
37+
SUPPORTED_KEYS = [
38+
"arch",
39+
"container",
40+
"cpu_cores",
41+
"cores",
42+
"cpu_power",
43+
"mem",
44+
"root_disk",
45+
"root_disk_source",
46+
"tags",
47+
"instance_role",
48+
"instance_type",
49+
"spaces",
50+
"virt_type",
51+
"zones",
52+
"allocate_public_ip"]
53+
3554
LIST_KEYS = {'tags', 'spaces'}
3655

3756
SNAKE1 = re.compile(r'(.)([A-Z][a-z]+)')
@@ -51,24 +70,29 @@ def parse(constraints):
5170
# Fowards compatibilty: already parsed
5271
return constraints
5372

54-
constraints = {
55-
normalize_key(k): (
56-
normalize_list_value(v) if k in LIST_KEYS else
57-
normalize_value(v)
58-
) for k, v in [s.split("=") for s in constraints.split(" ")]}
73+
normalized_constraints = {}
74+
for s in constraints.split(" "):
75+
if "=" not in s:
76+
raise Exception("malformed constraint %s" % s)
77+
78+
k, v = s.split("=")
79+
normalized_constraints[normalize_key(k)] = normalize_list_value(v) if\
80+
k in LIST_KEYS else normalize_value(v)
5981

60-
return constraints
82+
return normalized_constraints
6183

6284

63-
def normalize_key(key):
64-
key = key.strip()
85+
def normalize_key(orig_key):
86+
key = orig_key.strip()
6587

6688
key = key.replace("-", "_") # Our _client lib wants "_" in place of "-"
6789

6890
# Convert camelCase to snake_case
6991
key = SNAKE1.sub(r'\1_\2', key)
7092
key = SNAKE2.sub(r'\1_\2', key).lower()
7193

94+
if key not in SUPPORTED_KEYS:
95+
raise Exception("unknown constraint in %s" % orig_key)
7296
return key
7397

7498

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
'pyyaml>=5.1.2,<=6.0',
3737
'theblues>=0.5.1,<1.0',
3838
'websockets>=7.0,<8.0 ; python_version<"3.9"',
39-
'websockets>=8.0,<9.0 ; python_version>="3.9"',
39+
'websockets>=8.0,<9.0 ; python_version=="3.9"',
40+
'websockets>=9.0; python_version>"3.9"',
4041
'paramiko>=2.4.0,<3.0.0',
4142
'pyasn1>=0.4.4',
4243
'toposort>=1.5,<2',
@@ -60,6 +61,7 @@
6061
"Programming Language :: Python :: 3.7",
6162
"Programming Language :: Python :: 3.8",
6263
"Programming Language :: Python :: 3.9",
64+
"Programming Language :: Python :: 3.10",
6365
],
6466
entry_points={
6567
'console_scripts': [

tests/unit/test_constraints.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ def test_mem_regex(self):
2020
def test_normalize_key(self):
2121
_ = constraints.normalize_key
2222

23-
self.assertEqual(_("test-key"), "test_key")
24-
self.assertEqual(_("test-key "), "test_key")
25-
self.assertEqual(_(" test-key"), "test_key")
26-
self.assertEqual(_("TestKey"), "test_key")
27-
self.assertEqual(_("testKey"), "test_key")
23+
self.assertEqual(_("root-disk"), "root_disk")
24+
self.assertEqual(_("root-disk "), "root_disk")
25+
self.assertEqual(_(" root-disk"), "root_disk")
26+
self.assertEqual(_("RootDisk"), "root_disk")
27+
self.assertEqual(_("rootDisk"), "root_disk")
28+
29+
self.assertRaises(Exception, lambda: _("not-one-of-the-supported-keys"))
2830

2931
def test_normalize_val(self):
3032
_ = constraints.normalize_value
@@ -53,13 +55,16 @@ def test_parse_constraints(self):
5355
)
5456

5557
self.assertEqual(
56-
_("mem=10G foo=bar,baz tags=tag1 spaces=space1,space2"),
58+
_("mem=10G zones=bar,baz tags=tag1 spaces=space1,space2"),
5759
{"mem": 10 * 1024,
58-
"foo": "bar,baz",
60+
"zones": "bar,baz",
5961
"tags": ["tag1"],
6062
"spaces": ["space1", "space2"]}
6163
)
6264

65+
self.assertRaises(Exception, lambda: _("root-disk>16G"))
66+
self.assertRaises(Exception, lambda: _("root-disk>=16G"))
67+
6368
def test_parse_storage_constraint(self):
6469
_ = constraints.parse_storage_constraint
6570

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = lint,py3,py35,py36,py37,py38,py39
7+
envlist = lint,py3,py35,py36,py37,py38,py39,py310
88
skipsdist=True
99

1010
[pytest]

0 commit comments

Comments
 (0)