|
13 | 13 | from deis import settings |
14 | 14 |
|
15 | 15 |
|
| 16 | +def get_allocations(container_dict): |
| 17 | + counts = {} |
| 18 | + for container in container_dict.values(): |
| 19 | + name, id = container.split(':') |
| 20 | + if name in counts: |
| 21 | + counts[name] += 1 |
| 22 | + else: |
| 23 | + counts[name] = 1 |
| 24 | + return sorted(counts.values()) |
| 25 | + |
| 26 | + |
16 | 27 | class ContainerTest(TestCase): |
17 | 28 |
|
18 | 29 | """Tests creation of containers on nodes""" |
@@ -100,6 +111,75 @@ def test_container_scale(self): |
100 | 111 | self.assertEqual(response.status_code, 200) |
101 | 112 | self.assertEqual(response.data['containers'], json.dumps(body)) |
102 | 113 |
|
| 114 | + def test_container_scale_allocation(self): |
| 115 | + url = '/api/formations' |
| 116 | + body = {'id': 'autotest'} |
| 117 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 118 | + self.assertEqual(response.status_code, 201) |
| 119 | + formation_id = response.data['id'] |
| 120 | + url = "/api/formations/{formation_id}/layers".format(**locals()) |
| 121 | + body = {'id': 'runtime', 'flavor': 'autotest', 'run_list': 'recipe[deis::runtime]'} |
| 122 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 123 | + self.assertEqual(response.status_code, 201) |
| 124 | + # With 4 nodes and 13 web containers |
| 125 | + url = "/api/formations/{formation_id}/scale/layers".format(**locals()) |
| 126 | + body = {'runtime': 4} |
| 127 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 128 | + self.assertEqual(response.status_code, 200) |
| 129 | + url = "/api/formations/{formation_id}/scale/containers".format(**locals()) |
| 130 | + body = {'web': 13} |
| 131 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 132 | + self.assertEqual(response.status_code, 200) |
| 133 | + # test that one node has 4 and 3 nodes have 3 containers |
| 134 | + self.assertEqual(get_allocations(response.data['containers']['web']), |
| 135 | + [3, 3, 3, 4]) |
| 136 | + # With 1 node |
| 137 | + url = "/api/formations/{formation_id}/scale/layers".format(**locals()) |
| 138 | + body = {'runtime': 1} |
| 139 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 140 | + self.assertEqual(response.status_code, 200) |
| 141 | + # test that the node has all 13 containers |
| 142 | + self.assertEqual(get_allocations(response.data['containers']['web']), |
| 143 | + [13,]) |
| 144 | + # With 2 nodes |
| 145 | + url = "/api/formations/{formation_id}/scale/layers".format(**locals()) |
| 146 | + body = {'runtime': 2} |
| 147 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 148 | + self.assertEqual(response.status_code, 200) |
| 149 | + # test that one has 6 and the other has 7 containers |
| 150 | + self.assertEqual(get_allocations(response.data['containers']['web']), |
| 151 | + [6, 7]) |
| 152 | + # With 8 containers |
| 153 | + url = "/api/formations/{formation_id}/scale/containers".format(**locals()) |
| 154 | + body = {'web': 8} |
| 155 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 156 | + self.assertEqual(response.status_code, 200) |
| 157 | + # test that both have 4 containers |
| 158 | + self.assertEqual(get_allocations(response.data['containers']['web']), |
| 159 | + [4, 4]) |
| 160 | + # With 0 nodes |
| 161 | + url = "/api/formations/{formation_id}/scale/layers".format(**locals()) |
| 162 | + body = {'runtime': 0} |
| 163 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 164 | + self.assertEqual(response.status_code, 200) |
| 165 | + # test that there are no containers |
| 166 | + self.assertNotIn('web', response.data['containers']) |
| 167 | + # With 5 containers |
| 168 | + url = "/api/formations/{formation_id}/scale/containers".format(**locals()) |
| 169 | + body = {'web': 5} |
| 170 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 171 | + # test that we get an error message about runtime nodes |
| 172 | + self.assertEqual(response.status_code, 400) |
| 173 | + self.assertIn('Must scale runtime nodes', response.data) |
| 174 | + # With 1 node |
| 175 | + url = "/api/formations/{formation_id}/scale/layers".format(**locals()) |
| 176 | + body = {'runtime': 1} |
| 177 | + response = self.client.post(url, json.dumps(body), content_type='application/json') |
| 178 | + self.assertEqual(response.status_code, 200) |
| 179 | + # test that it gets all 8 containers |
| 180 | + self.assertEqual(get_allocations(response.data['containers']['web']), |
| 181 | + [8,]) |
| 182 | + |
103 | 183 | def test_container_balance(self): |
104 | 184 | url = '/api/formations' |
105 | 185 | body = {'id': 'autotest'} |
|
0 commit comments