Skip to content

Commit

Permalink
Merge branch 'master' into revert-python-libjuju
Browse files Browse the repository at this point in the history
  • Loading branch information
simskij authored Apr 25, 2022
2 parents 70c7555 + ac9fe69 commit 5af9477
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/tox.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.5, 3.6, 3.7, 3.8, 3.9]
python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "3.10"
steps:
- name: Check out code
uses: actions/checkout@v2
Expand All @@ -25,7 +31,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.5, 3.6, 3.7, 3.8, 3.9]
python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "3.10"
steps:
- uses: actions/checkout@v2
- name: Setup Python
Expand Down
40 changes: 32 additions & 8 deletions juju/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@
"Y": 1024 ** 6
}

# List of supported constraint keys, see
# http://github.com/cderici/juju/blob/2.9/core/constraints/constraints.go#L20-L39
SUPPORTED_KEYS = [
"arch",
"container",
"cpu_cores",
"cores",
"cpu_power",
"mem",
"root_disk",
"root_disk_source",
"tags",
"instance_role",
"instance_type",
"spaces",
"virt_type",
"zones",
"allocate_public_ip"]

LIST_KEYS = {'tags', 'spaces'}

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

constraints = {
normalize_key(k): (
normalize_list_value(v) if k in LIST_KEYS else
normalize_value(v)
) for k, v in [s.split("=") for s in constraints.split(" ")]}
normalized_constraints = {}
for s in constraints.split(" "):
if "=" not in s:
raise Exception("malformed constraint %s" % s)

k, v = s.split("=")
normalized_constraints[normalize_key(k)] = normalize_list_value(v) if\
k in LIST_KEYS else normalize_value(v)

return constraints
return normalized_constraints


def normalize_key(key):
key = key.strip()
def normalize_key(orig_key):
key = orig_key.strip()

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

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

if key not in SUPPORTED_KEYS:
raise Exception("unknown constraint in %s" % orig_key)
return key


Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
'pyyaml>=5.1.2,<=6.0',
'theblues>=0.5.1,<1.0',
'websockets>=7.0,<8.0 ; python_version<"3.9"',
'websockets>=8.0,<9.0 ; python_version>="3.9"',
'websockets>=8.0,<9.0 ; python_version=="3.9"',
'websockets>=9.0; python_version>"3.9"',
'paramiko>=2.4.0,<3.0.0',
'pyasn1>=0.4.4',
'toposort>=1.5,<2',
Expand All @@ -60,6 +61,7 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
entry_points={
'console_scripts': [
Expand Down
19 changes: 12 additions & 7 deletions tests/unit/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ def test_mem_regex(self):
def test_normalize_key(self):
_ = constraints.normalize_key

self.assertEqual(_("test-key"), "test_key")
self.assertEqual(_("test-key "), "test_key")
self.assertEqual(_(" test-key"), "test_key")
self.assertEqual(_("TestKey"), "test_key")
self.assertEqual(_("testKey"), "test_key")
self.assertEqual(_("root-disk"), "root_disk")
self.assertEqual(_("root-disk "), "root_disk")
self.assertEqual(_(" root-disk"), "root_disk")
self.assertEqual(_("RootDisk"), "root_disk")
self.assertEqual(_("rootDisk"), "root_disk")

self.assertRaises(Exception, lambda: _("not-one-of-the-supported-keys"))

def test_normalize_val(self):
_ = constraints.normalize_value
Expand Down Expand Up @@ -53,13 +55,16 @@ def test_parse_constraints(self):
)

self.assertEqual(
_("mem=10G foo=bar,baz tags=tag1 spaces=space1,space2"),
_("mem=10G zones=bar,baz tags=tag1 spaces=space1,space2"),
{"mem": 10 * 1024,
"foo": "bar,baz",
"zones": "bar,baz",
"tags": ["tag1"],
"spaces": ["space1", "space2"]}
)

self.assertRaises(Exception, lambda: _("root-disk>16G"))
self.assertRaises(Exception, lambda: _("root-disk>=16G"))

def test_parse_storage_constraint(self):
_ = constraints.parse_storage_constraint

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = lint,py3,py35,py36,py37,py38,py39
envlist = lint,py3,py35,py36,py37,py38,py39,py310
skipsdist=True

[pytest]
Expand Down

0 comments on commit 5af9477

Please sign in to comment.