File size: 13,605 Bytes
a3dcdac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
diff --git a/flocker/control/test/test_httpapi.py b/flocker/control/test/test_httpapi.py
index 806837648..9d7258ac9 100644
--- a/flocker/control/test/test_httpapi.py
+++ b/flocker/control/test/test_httpapi.py
@@ -173,6 +173,304 @@ RealTestsAPI, MemoryTestsAPI = buildIntegrationTests(
     VersionTestsMixin, "API", _build_app)
 
 
+class CreateContainerTestsMixin(APITestsMixin):
+    """
+    Black-box tests for creating container configuration through the HTTP API.
+    """
+    def _request_json(self, method, path, body, expected_code):
+        """
+        Issue a JSON request through the existing HTTP API test harness and
+        return a ``Deferred`` firing with the decoded response body.
+
+        This helper intentionally does not inspect route handlers, decorators,
+        schemas, or private helpers.
+        """
+        requesting = self.assertResponseCode(
+            method, path, body, expected_code)
+        requesting.addCallback(readBody)
+        requesting.addCallback(loads)
+        return requesting
+
+    def _applications_for_host(self, host):
+        """
+        Return the configured applications for ``host`` from the persisted
+        configuration.
+        """
+        deployment = self.persistence_service.get()
+        matching = [
+            node for node in deployment.nodes
+            if node.hostname == host
+        ]
+        if not matching:
+            return []
+        return list(matching[0].applications)
+
+    def _configured_applications_named(self, name):
+        """
+        Return all configured applications with the supplied public container
+        name, across all hosts.
+        """
+        deployment = self.persistence_service.get()
+        return [
+            application
+            for node in deployment.nodes
+            for application in node.applications
+            if application.name == name
+        ]
+
+    def _assert_application_present(self, host, name, image):
+        """
+        Assert that the persisted configuration contains a container with the
+        supplied public values on ``host``.
+        """
+        expected_image = DockerImage.from_string(image)
+        matches = [
+            application for application in self._applications_for_host(host)
+            if application.name == name and application.image == expected_image
+        ]
+        self.assertEqual(
+            len(matches), 1,
+            "Expected exactly one configured container named {!r} with image "
+            "{!r} on host {!r}; found {!r}".format(
+                name, image, host, self._applications_for_host(host)))
+
+    def _assert_duplicate_name_conflict(self, response_json):
+        """
+        Assert only the public error shape and duplicate-name semantics.
+        The exact prose is intentionally not locked down because the reference
+        patch's docs and implementation disagree on the full sentence.
+        """
+
+
+        self.assertIn(u"description", response_json)
+        self.assertNotIn(u"message", response_json)
+        self.assertTrue(isinstance(response_json[u"description"], unicode))
+        self.assertTrue(response_json[u"description"])
+
+    def test_create_container_response_echoes_request(self):
+        """
+        Creating a container returns 201 Created and echoes the public
+        host/name/image values from the request body.
+        """
+        container_json = {
+            u"host": self.NODE_B,
+            u"name": u"postgres",
+            u"image": u"postgres",
+        }
+        return self.assertResult(
+            b"POST", b"/configuration/containers",
+            container_json, CREATED, container_json)
+
+    def test_create_container_persists_on_requested_existing_host(self):
+        """
+        Creating a container on an existing host persists it on that host and
+        preserves unrelated existing configuration.
+        """
+        saving = self.persistence_service.save(Deployment(
+            nodes={
+                Node(
+                    hostname=self.NODE_A,
+                    applications=[
+                        Application(
+                            name=u"postgres",
+                            image=DockerImage.from_string(u"postgres")),
+                    ]),
+                Node(hostname=self.NODE_B),
+            }))
+
+        def saved(_):
+            return self.assertResponseCode(
+                b"POST", b"/configuration/containers",
+                {
+                    u"host": self.NODE_A,
+                    u"name": u"another_postgres",
+                    u"image": u"postgres:latest",
+                },
+                CREATED)
+        saving.addCallback(saved)
+
+        def created(_):
+            self._assert_application_present(
+                self.NODE_A, u"postgres", u"postgres")
+            self._assert_application_present(
+                self.NODE_A, u"another_postgres", u"postgres:latest")
+            self.assertEqual(
+                [],
+                [
+                    application for application
+                    in self._applications_for_host(self.NODE_B)
+                    if application.name == u"another_postgres"
+                ])
+        saving.addCallback(created)
+        return saving
+
+    def test_create_container_persists_on_new_host(self):
+        """
+        Creating a container for a host absent from the current configuration
+        persists a host entry containing that container.
+        """
+        d = self.assertResponseCode(
+            b"POST", b"/configuration/containers",
+            {
+                u"host": self.NODE_B,
+                u"name": u"redis_cache",
+                u"image": u"redis:latest",
+            },
+            CREATED)
+
+        def created(_):
+            self._assert_application_present(
+                self.NODE_B, u"redis_cache", u"redis:latest")
+        d.addCallback(created)
+        return d
+
+    def test_create_container_persists_arbitrary_public_values(self):
+        """
+        Container creation is not limited to the fixture hosts, names, and
+        images used by other tests.
+        """
+        host = u"198.51.100.7"
+        name = u"api_worker"
+        image = u"clusterhq/api-worker:1.2.3"
+        container_json = {
+            u"host": host,
+            u"name": name,
+            u"image": image,
+        }
+        d = self.assertResult(
+            b"POST", b"/configuration/containers",
+            container_json, CREATED, container_json)
+
+        def created(_):
+            self._assert_application_present(host, name, image)
+        d.addCallback(created)
+        return d
+
+    def test_duplicate_container_name_rejected_on_same_host(self):
+        """
+        A second container with an existing name is rejected on the same host,
+        even if another public field differs.
+        """
+        first = {
+            u"host": self.NODE_A,
+            u"name": u"postgres",
+            u"image": u"postgres",
+        }
+        second = {
+            u"host": self.NODE_A,
+            u"name": u"postgres",
+            u"image": u"postgres:alternate",
+        }
+        d = self.assertResponseCode(
+            b"POST", b"/configuration/containers", first, CREATED)
+
+        def created(_):
+            return self._request_json(
+                b"POST", b"/configuration/containers", second, CONFLICT)
+        d.addCallback(created)
+
+        def conflicted(response_json):
+            self._assert_duplicate_name_conflict(response_json)
+            self.assertEqual(
+                1, len(self._configured_applications_named(u"postgres")))
+        d.addCallback(conflicted)
+        return d
+
+    def test_duplicate_container_name_rejected_on_different_host(self):
+        """
+        Container names are unique across configured hosts, not merely within
+        the target host.
+        """
+        first = {
+            u"host": self.NODE_A,
+            u"name": u"web",
+            u"image": u"nginx",
+        }
+        second = {
+            u"host": self.NODE_B,
+            u"name": u"web",
+            u"image": u"redis",
+        }
+        d = self.assertResponseCode(
+            b"POST", b"/configuration/containers", first, CREATED)
+
+        def created(_):
+            return self._request_json(
+                b"POST", b"/configuration/containers", second, CONFLICT)
+        d.addCallback(created)
+
+        def conflicted(response_json):
+            self._assert_duplicate_name_conflict(response_json)
+            self.assertEqual(
+                1, len(self._configured_applications_named(u"web")))
+        d.addCallback(conflicted)
+        return d
+
+    def test_duplicate_container_name_rejected_when_existing_on_other_host_first(self):
+        """
+        Duplicate-name detection is global even when the existing container is
+        on a different host than the original fixture order.
+        """
+        first = {
+            u"host": self.NODE_B,
+            u"name": u"api_worker",
+            u"image": u"clusterhq/api-worker:1.0",
+        }
+        second = {
+            u"host": self.NODE_A,
+            u"name": u"api_worker",
+            u"image": u"clusterhq/api-worker:2.0",
+        }
+        d = self.assertResponseCode(
+            b"POST", b"/configuration/containers", first, CREATED)
+
+        def created(_):
+            return self._request_json(
+                b"POST", b"/configuration/containers", second, CONFLICT)
+        d.addCallback(created)
+
+        def conflicted(response_json):
+            self._assert_duplicate_name_conflict(response_json)
+            self.assertEqual(
+                1, len(self._configured_applications_named(u"api_worker")))
+        d.addCallback(conflicted)
+        return d
+
+    def test_duplicate_container_name_rejected_with_same_image(self):
+        """
+        Duplicate-name detection is by name, not by the full container body.
+        """
+        first = {
+            u"host": self.NODE_A,
+            u"name": u"same_image_worker",
+            u"image": u"redis:latest",
+        }
+        second = {
+            u"host": self.NODE_B,
+            u"name": u"same_image_worker",
+            u"image": u"redis:latest",
+        }
+        d = self.assertResponseCode(
+            b"POST", b"/configuration/containers", first, CREATED)
+
+        def created(_):
+            return self._request_json(
+                b"POST", b"/configuration/containers", second, CONFLICT)
+        d.addCallback(created)
+
+        def conflicted(response_json):
+            self._assert_duplicate_name_conflict(response_json)
+            self.assertEqual(
+                1,
+                len(self._configured_applications_named(u"same_image_worker")))
+        d.addCallback(conflicted)
+        return d
+
+
+RealTestsCreateContainer, MemoryTestsCreateContainer = buildIntegrationTests(
+    CreateContainerTestsMixin, "CreateContainer", _build_app)
+
+
 class CreateDatasetTestsMixin(APITestsMixin):
     """
     Tests for the dataset creation endpoint at ``/configuration/datasets``.
@@ -276,6 +574,42 @@ class CreateDatasetTestsMixin(APITestsMixin):
         """
         return self._dataset_id_collision_test(self.NODE_A, unicode.title)
 
+    def test_duplicate_dataset_id_conflict_uses_description_not_message(self):
+        """
+        Duplicate dataset_id conflicts keep returning 409 but use the standard
+        ``description`` error field instead of the older ``message`` field.
+        """
+        dataset_id = unicode(uuid4())
+        first_dataset = {
+            u"primary": self.NODE_A,
+            u"dataset_id": dataset_id,
+        }
+        second_dataset = {
+            u"primary": self.NODE_B,
+            u"dataset_id": dataset_id,
+        }
+        d = self.assertResponseCode(
+            b"POST", b"/configuration/datasets", first_dataset, CREATED)
+
+        def created(_):
+            requesting = self.assertResponseCode(
+                b"POST", b"/configuration/datasets",
+                second_dataset, CONFLICT)
+            requesting.addCallback(readBody)
+            requesting.addCallback(loads)
+            return requesting
+        d.addCallback(created)
+
+        def conflicted(response_json):
+
+
+            self.assertIn(u"description", response_json)
+            self.assertNotIn(u"message", response_json)
+            self.assertTrue(isinstance(response_json[u"description"], unicode))
+            self.assertTrue(response_json[u"description"])
+        d.addCallback(conflicted)
+        return d
+
     def test_unknown_primary_node(self):
         """
         If a ``POST`` request made to the endpoint indicates a non-existent
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+set -uo pipefail
+cd /testbed
+
+
+
+set +u
+source .build_env/.venv/bin/activate
+set -u
+
+
+case "${1:-}" in
+  base)
+    python -m pytest \
+      flocker/control/test/test_schemas.py \
+      flocker/control/test/test_httpapi.py \
+      -v \
+      -k "not CreateContainer and not test_duplicate_dataset_id_conflict_uses_description_not_message"
+    ;;
+  new)
+    python -m pytest \
+      flocker/control/test/test_httpapi.py \
+      -v \
+      -k "CreateContainer or test_duplicate_dataset_id_conflict_uses_description_not_message"
+    ;;
+  *)
+    echo "usage: $0 base|new"
+    exit 2
+    ;;
+esac