summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/build/scripts/action_library_client/test/test_action_library_client_integration.py
blob: b6418e617ecdbe5c5b8d9cadcbc03a53e508f408 (plain)
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
import sys
import os
import unittest
import uuid
import json
import tempfile
import action_library_client

class IntegrationTest(unittest.TestCase):

    HTTP = "http://10.147.97.199:8080"
    HTTPS = "https://10.147.97.199:8443"

    def setUp(self):
        os.environ["ALC_HTTP_USER"] = "AUTH-DELETE"
        os.environ["ALC_HTTP_PASS"] = "test"

    def tearDown(self):
        os.environ["ALC_HTTP_INSECURE"] = ""
        os.environ["ALC_HTTP_USER"] = ""
        os.environ["ALC_HTTP_PASS"] = ""

    @staticmethod
    def __prepare(testcase, name):
        with open(testcase, 'r') as fin:
            jsonk = json.loads(fin.read())
            jsonk['name'] = name
            with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp:
                temp.write(json.dumps(jsonk))
                temp.flush()
                return temp.name

    @staticmethod
    def __get_sequence():
        with open(r'./seq.txt', 'r+') as f:
            value = int(f.read())
            f.seek(0)
            f.write(str(value + 1))
            return value

    def __print_separator(self):
        logger = action_library_client.Runner.get_logger()
        logger.info("==================================================")

    def __list(self, stdargs):
        logger = action_library_client.Runner.get_logger()
        list_response = action_library_client.execute(["--list"] + stdargs)
        logger.info("--list response: {}".format(list_response))
        self.assertTrue(isinstance(list_response, dict))
        return list_response

    def __get_action(self, list_response, ai_uuid):
        for action in list_response['actionList']:
            if action['actionInvariantUUID'] == ai_uuid:
                return action

    def __create_delete(self, extraargs):

        logger = action_library_client.Runner.get_logger()

        # Setup.

        seq = IntegrationTest.__get_sequence()
        name = "Backout{}".format(seq)
        path = IntegrationTest.__prepare("scenarios/Backout.json", name)
        stdargs = ["--url", self.HTTP, "--verbose"]
        if extraargs:
            stdargs.extend(extraargs)

        # List actions.

        self.__print_separator()
        list_response1 = self.__list(stdargs)
        self.assertTrue(isinstance(list_response1, dict))

        # CREATE action.

        self.__print_separator()
        create_response = action_library_client.execute(["--create", "--in", path] + stdargs)
        logger.info("--create response: {}".format(create_response))
        self.assertTrue(isinstance(create_response, dict))
        ai_uuid = create_response['actionInvariantUUID']
        self.assertTrue(ai_uuid)
        self.assertEquals(create_response['status'], 'Locked')
        self.assertEquals(create_response['version'], '0.1')

        # UPDATE action #1.

        self.__print_separator()
        update_response1 = action_library_client.execute(["--update", "--in", path, "--uuid", ai_uuid] + stdargs)
        logger.info("--update response: {}".format(update_response1))
        self.assertTrue(isinstance(update_response1, dict))

        # UPDATE action #2.

        self.__print_separator()
        update_response2 = action_library_client.execute(["--update", "--in", path, "--uuid", ai_uuid] + stdargs)
        logger.info("--update response: {}".format(update_response2))
        self.assertTrue(isinstance(update_response2, dict))

        # CHECKOUT action (usage unknown).

        self.__print_separator()
        try:
            action_library_client.execute(["--checkout", "--uuid", ai_uuid] + stdargs)
            self.fail("--checkout should fail")
        except Exception as err:
            print(err)

        # CHECKIN action.

        self.__print_separator()
        checkin_response = action_library_client.execute(["--checkin", "--in", path, "--uuid", ai_uuid] + stdargs)
        logger.info("--checkin response: {}".format(checkin_response))
        self.assertTrue(isinstance(checkin_response, dict))
        self.assertEquals(checkin_response['status'], 'Available')
        self.assertEquals(checkin_response['version'], '0.1')

        # SUBMIT action.

        self.__print_separator()
        submit_response = action_library_client.execute(["--submit", "--in", path, "--uuid", ai_uuid] + stdargs)
        logger.info("--submit response: {}".format(submit_response))
        self.assertTrue(isinstance(submit_response, dict))
        self.assertEquals(submit_response['status'], 'Final')
        self.assertEquals(submit_response['version'], '1.0')

        # LIST again

        self.__print_separator()
        list_response2 = self.__list(stdargs)
        action_found2 = self.__get_action(list_response2, ai_uuid)
        self.assertTrue(action_found2)

        # DELETE action.

        self.__print_separator()
        delete_response = action_library_client.execute(["--delete", "--uuid", ai_uuid] + stdargs)
        logger.info("--delete response: {}".format(delete_response))
        self.assertEqual(delete_response, action_library_client.ResponseCodes.OK)

        # LIST yet again

        self.__print_separator()
        list_response3 = self.__list(stdargs)
        action_found3 = self.__get_action(list_response3, ai_uuid)
        self.assertFalse(action_found3)

    def __create_undo(self, extraargs):

        # Setup

        logger = action_library_client.Runner.get_logger()
        seq = IntegrationTest.__get_sequence()
        name = "Backout{}".format(seq)
        path = IntegrationTest.__prepare("scenarios/Backout.json", name)
        stdargs = ["--url", self.HTTP, "--verbose"]

        # CREATE action.

        self.__print_separator()
        create_response = action_library_client.execute(["--create", "--in", path] + stdargs + extraargs)
        logger.info("--create response: {}".format(create_response))
        self.assertTrue(isinstance(create_response, dict))
        ai_uuid = create_response['actionInvariantUUID']
        self.assertTrue(ai_uuid)
        self.assertEquals(create_response['status'], 'Locked')
        self.assertEquals(create_response['version'], '0.1')

        # UNDOCHECKOUT action

        self.__print_separator()
        undocheckout_response = action_library_client.execute(["--undocheckout", "--uuid", ai_uuid] + stdargs + extraargs)
        self.assertTrue(isinstance(undocheckout_response, dict))

    def __create_list(self, extraargs):
        # Setup

        logger = action_library_client.Runner.get_logger()
        seq = IntegrationTest.__get_sequence()
        name = "Backout{}".format(seq)
        path = IntegrationTest.__prepare("scenarios/Backout.json", name)
        stdargs = ["--url", self.HTTP, "--verbose"]

        # CREATE action.

        self.__print_separator()
        create_response = action_library_client.execute(["--create", "--in", path] + stdargs + extraargs)
        logger.info("--create response: {}".format(create_response))
        self.assertTrue(isinstance(create_response, dict))
        ai_uuid = create_response['actionInvariantUUID']
        self.assertTrue(ai_uuid)
        self.assertEquals(create_response['status'], 'Locked')
        self.assertEquals(create_response['version'], '0.1')

        # CHECKIN action.

        self.__print_separator()
        checkin_response = action_library_client.execute(["--checkin", "--in", path, "--uuid", ai_uuid] +
                                                         stdargs + extraargs)
        logger.info("--checkin response: {}".format(checkin_response))
        self.assertTrue(isinstance(checkin_response, dict))
        self.assertEquals(checkin_response['status'], 'Available')
        self.assertEquals(checkin_response['version'], '0.1')

        try:
            # LIST.

            self.__print_separator()
            list_response1 = self.__list(stdargs + extraargs)
            action_found1 = self.__get_action(list_response1, ai_uuid)
            self.assertTrue(action_found1)

            # LIST with UUID.

            self.__print_separator()
            list_response2 = self.__list(stdargs + extraargs + ["--uuid", ai_uuid])
            self.assertFalse(hasattr(list_response2, 'actionList'))
            self.assertEquals(len(list_response2['versions']), 1)

            # LIST with bad UUID.

            self.__print_separator()
            list_response3 = action_library_client.execute(["--list"] + stdargs + extraargs +
                                                           ["--uuid", "where_the_wind_blows"])
            if isinstance(list_response3, int):
                self.assertEquals(action_library_client.ResponseCodes.HTTP_NOT_FOUND_ERROR, list_response3)
            else:
                self.assertEquals("ACT1045", list_response3["code"])

        finally:

            # DELETE action

            self.__print_separator()
            action_library_client.execute(["--delete", "--uuid", ai_uuid] + stdargs + extraargs)

    def __http_secure(self, extraargs):
        os.environ["ALC_HTTP_INSECURE"] = ""
        try:
            self.__list(["--url", self.HTTPS, "--verbose"] + extraargs)
            if not (sys.version_info[0] == 2 and sys.version_info[1] == 6):
                self.fail("Should fail (non-2.6) for TLS + secure")
        except Exception:
            pass

    def __http_insecure(self, extraargs):
        os.environ["ALC_HTTP_INSECURE"] = True
        self.__list(["--url", self.HTTPS, "--verbose"] + extraargs)

    def __no_credentials(self, extraargs):

        args = ["--url", self.HTTP] + extraargs
        self.__list(args)
        print("OK")

        os.environ["ALC_HTTP_USER"] = ""
        os.environ["ALC_HTTP_PASS"] = ""
        try:
            action_library_client.execute(["--list"] + args)
            self.fail("Should fail for missing credentials")
        except Exception as e:
            self.assertEquals("REST service credentials not found", e.message)

    def __bad_credentials(self, extraargs):

        args = ["--url", self.HTTP] + extraargs
        self.__list(args)

        os.environ["ALC_HTTP_USER"] = "wakey_wakey"
        os.environ["ALC_HTTP_PASS"] = "rise_and_shine"
        code = action_library_client.execute(["--list"] + args)
        self.assertEquals(action_library_client.ResponseCodes.HTTP_FORBIDDEN_ERROR, code)

    ################################################################################

    def test_https_insecure_local_fail(self):
        self.__http_secure([])

    def test_https_insecure_remote_fail(self):
        self.__http_secure(["--curl"])

    def test_https_native(self):
        self.__http_secure([])

    def test_https_curl(self):
        self.__http_secure(["--curl"])

    def test_undo_checkout_native(self):
        self.__create_undo([])

    def test_undo_checkout_curl(self):
        self.__create_undo(["--curl"])

    def test_create_delete_native(self):
        self.__create_delete([])

    def test_create_delete_curl(self):
        self.__create_delete(["--curl"])

    def test_create_list_native(self):
        self.__create_list([])

    def test_create_list_curl(self):
        self.__create_list(["--curl"])

    def test_bad_credentials_native(self):
        self.__bad_credentials([])

    def test_bad_credentials_curl(self):
        self.__bad_credentials(["--curl"])
    #
    def test_no_credentials_native(self):
        self.__no_credentials([])

    def test_no_credentials_curl(self):
        self.__no_credentials(["--curl"])

    def test_create_to_delete_dryrun(self):
        ai_uuid = str(uuid.uuid4())
        path = IntegrationTest.__prepare("scenarios/Backout.json", "Backout{}".format("001"))
        stdargs = ["--url", self.HTTP, "--verbose", "--dryrun"]
        action_library_client.execute(["--create", "--in", path] + stdargs)
        action_library_client.execute(["--update", "--in", path, "--uuid", ai_uuid] + stdargs)
        action_library_client.execute(["--checkout", "--uuid", ai_uuid] + stdargs)
        action_library_client.execute(["--undocheckout", "--uuid", ai_uuid] + stdargs)
        action_library_client.execute(["--checkin", "--uuid", ai_uuid] + stdargs)
        action_library_client.execute(["--submit", "--uuid", ai_uuid] + stdargs)
        action_library_client.execute(["--list"] + stdargs)