|
| 1 | +# Copyright 2016: Mirantis Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +import copy |
| 17 | +import json |
| 18 | + |
| 19 | +import elasticsearch |
| 20 | +import mock |
| 21 | + |
| 22 | +from tests.unit.api.v1 import base |
| 23 | + |
| 24 | + |
| 25 | +class RunbookTestCase(base.APITestCase): |
| 26 | + api_type = "reader" |
| 27 | + |
| 28 | + correct_runbook = { |
| 29 | + "name": "test", |
| 30 | + "description": "test", |
| 31 | + "type": "bash", |
| 32 | + "runbook": "echo", |
| 33 | + } |
| 34 | + |
| 35 | + def test_get_no_region(self): |
| 36 | + resp = self.client.get("/api/v1/region/region_zero/runbooks") |
| 37 | + self.assertEqual(404, resp.status_code) |
| 38 | + |
| 39 | + resp = self.client.post("/api/v1/region/region_zero/runbooks") |
| 40 | + self.assertEqual(405, resp.status_code) |
| 41 | + |
| 42 | + @mock.patch.object(elasticsearch.Elasticsearch, "search") |
| 43 | + def test_get_runbooks(self, es_search): |
| 44 | + es_search.return_value = { |
| 45 | + "hits": {"hits": [ |
| 46 | + { |
| 47 | + "_id": "121", |
| 48 | + "_source": self.correct_runbook, |
| 49 | + }, |
| 50 | + { |
| 51 | + "_id": "122", |
| 52 | + "_source": self.correct_runbook, |
| 53 | + }, |
| 54 | + { |
| 55 | + "_id": "123", |
| 56 | + "_source": self.correct_runbook, |
| 57 | + }, |
| 58 | + ]}, |
| 59 | + } |
| 60 | + resp = self.client.get("/api/v1/region/region_one/runbooks", |
| 61 | + content_type="application/json") |
| 62 | + self.assertEqual(200, resp.status_code) |
| 63 | + resp_json = json.loads(resp.data.decode()) |
| 64 | + expected = [] |
| 65 | + for book_id in ["121", "122", "123"]: |
| 66 | + data = copy.copy(self.correct_runbook) |
| 67 | + data["_id"] = book_id |
| 68 | + expected.append(data) |
| 69 | + self.assertEqual(expected, resp_json) |
| 70 | + |
| 71 | + es_search.assert_called_with(index="ms_runbooks_region_one", |
| 72 | + doc_type="runbook") |
| 73 | + |
| 74 | + @mock.patch.object(elasticsearch.Elasticsearch, "get") |
| 75 | + def test_get_single_runbook_bad_id(self, es_get): |
| 76 | + es_get.side_effect = elasticsearch.NotFoundError |
| 77 | + resp = self.client.get("/api/v1/region/region_one/runbooks/123", |
| 78 | + content_type="application/json") |
| 79 | + self.assertEqual(404, resp.status_code) |
| 80 | + |
| 81 | + es_get.assert_called_with(index="ms_runbooks_region_one", |
| 82 | + doc_type="runbook", |
| 83 | + id="123") |
| 84 | + |
| 85 | + @mock.patch.object(elasticsearch.Elasticsearch, "get") |
| 86 | + def test_get_single_runbook(self, es_get): |
| 87 | + es_get.return_value = { |
| 88 | + "_source": self.correct_runbook, |
| 89 | + "_id": "123", |
| 90 | + } |
| 91 | + resp = self.client.get("/api/v1/region/region_one/runbooks/123", |
| 92 | + content_type="application/json") |
| 93 | + self.assertEqual(200, resp.status_code) |
| 94 | + |
| 95 | + resp_json = json.loads(resp.data.decode()) |
| 96 | + expected = copy.copy(self.correct_runbook) |
| 97 | + expected["_id"] = "123" |
| 98 | + self.assertEqual(expected, resp_json) |
| 99 | + |
| 100 | + es_get.assert_called_with(index="ms_runbooks_region_one", |
| 101 | + doc_type="runbook", |
| 102 | + id="123") |
| 103 | + |
| 104 | + @mock.patch.object(elasticsearch.Elasticsearch, "get") |
| 105 | + def test_get_single_runbook_reg_two(self, es_get): |
| 106 | + es_get.return_value = { |
| 107 | + "_source": self.correct_runbook, |
| 108 | + "_id": "123", |
| 109 | + } |
| 110 | + resp = self.client.get("/api/v1/region/region_two/runbooks/123", |
| 111 | + content_type="application/json") |
| 112 | + self.assertEqual(200, resp.status_code) |
| 113 | + |
| 114 | + resp_json = json.loads(resp.data.decode()) |
| 115 | + expected = copy.copy(self.correct_runbook) |
| 116 | + expected["_id"] = "123" |
| 117 | + self.assertEqual(expected, resp_json) |
| 118 | + |
| 119 | + es_get.assert_called_with(index="ms_runbooks_region_two", |
| 120 | + doc_type="runbook", |
| 121 | + id="123") |
0 commit comments