| israelad | e2051cc | 2019-11-21 16:46:28 +0100 | [diff] [blame^] | 1 | # |
| 2 | # Test our constraints parser |
| 3 | # |
| 4 | |
| 5 | import unittest |
| 6 | |
| 7 | from juju import constraints |
| 8 | |
| 9 | |
| 10 | class TestConstraints(unittest.TestCase): |
| 11 | |
| 12 | def test_mem_regex(self): |
| 13 | m = constraints.MEM |
| 14 | self.assertTrue(m.match("10G")) |
| 15 | self.assertTrue(m.match("1G")) |
| 16 | self.assertFalse(m.match("1Gb")) |
| 17 | self.assertFalse(m.match("a1G")) |
| 18 | self.assertFalse(m.match("1000")) |
| 19 | |
| 20 | def test_normalize_key(self): |
| 21 | _ = constraints.normalize_key |
| 22 | |
| 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") |
| 28 | |
| 29 | def test_normalize_val(self): |
| 30 | _ = constraints.normalize_value |
| 31 | |
| 32 | self.assertEqual(_("10G"), 10 * 1024) |
| 33 | self.assertEqual(_("10M"), 10) |
| 34 | self.assertEqual(_("10"), 10) |
| 35 | self.assertEqual(_("foo,bar"), "foo,bar") |
| 36 | |
| 37 | def test_normalize_list_val(self): |
| 38 | _ = constraints.normalize_list_value |
| 39 | |
| 40 | self.assertEqual(_("foo"), ["foo"]) |
| 41 | self.assertEqual(_("foo,bar"), ["foo", "bar"]) |
| 42 | |
| 43 | def test_parse_constraints(self): |
| 44 | _ = constraints.parse |
| 45 | |
| 46 | self.assertEqual( |
| 47 | _("mem=10G"), |
| 48 | {"mem": 10 * 1024} |
| 49 | ) |
| 50 | |
| 51 | self.assertEqual( |
| 52 | _("mem=10G foo=bar,baz tags=tag1 spaces=space1,space2"), |
| 53 | {"mem": 10 * 1024, |
| 54 | "foo": "bar,baz", |
| 55 | "tags": ["tag1"], |
| 56 | "spaces": ["space1", "space2"]} |
| 57 | ) |