push addional code
[sdc.git] / openecomp-be / tools / build / scripts / action_library_client / test / test_action_library_client.py
1 import unittest
2 import os
3 import tempfile
4 import ConfigParser
5 import action_library_client as ALC
6
7
8 class D(dict):
9
10     def __init__(self, *args, **kwargs):
11         super(D, self).__init__(*args, **kwargs)
12         self.__dict__ = self
13
14
15 class UnitTest(unittest.TestCase):
16
17     def __write_config_file(self, map):
18         with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
19             config = ConfigParser.ConfigParser()
20             config.add_section("action_library_client")
21             for k, v in map.items():
22                 section = config.set("action_library_client", k, v)
23             config.write(tmp)
24             tmp.flush()
25             return tmp.name
26
27     def test_argument_parser(self):
28         # nothing = ALC.ArgumentParser().parse_args([])
29         # self.assertEquals(nothing.help, None)
30         # self.assertEquals(nothing.version, None)
31         # self.assertEquals(nothing.verbose, None)
32         #
33         # help = ALC.ArgumentParser().parse_args(["--help"])
34         # self.assertEquals(help.help, True)
35
36         uuidx = ALC.ArgumentParser().parse_args(["--uuid", "abc"])
37         self.assertEquals(uuidx.uuid, "abc")
38
39
40     def test_settings_get(self):
41
42         os.environ["a"] = "aa"
43         os.environ["b"] = "WILL_BE_OVERRIDDEN"
44
45         section = dict()
46         section['ALC_HTTP_USER'] = "batman"
47         section['ECOMP_INSTANCE_ID'] = "acdc"
48         section['b'] = "bb"
49         filename = self.__write_config_file(section)
50
51         # with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
52         #     config = configparser.ConfigParser()
53         #     config.add_section("action_library_client")
54         #     section = config["action_library_client"]
55         #     config.write(tmp)
56         #     tmp.flush()
57
58         settings = ALC.Settings(ALC.Runner.parse_args(["--config", filename]))
59         self.assertEquals("aa", settings.get("a"))
60         self.assertEquals("bb", settings.get("b"))
61         self.assertEquals("batman", settings.get("ALC_HTTP_USER"))
62         self.assertEquals("batman", settings.get(ALC.Constants.ENV_HTTP_USER))
63         self.assertEquals("ALC_ECOMP_INSTANCE_ID", settings.get("c", ALC.Constants.ENV_ECOMP_INSTANCE_ID))
64
65         os.remove(filename)
66
67     def test_parse_args(self):
68         c1 = ALC.Runner.parse_args(["--version"])
69         with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
70             config = ConfigParser.ConfigParser()
71             config.add_section("action_library_client")
72             config.set("action_library_client", "ALC_HTTP_USER", "batman")
73             config.write(tmp)
74             tmp.flush()
75         self.assertEquals(c1.version, True)
76
77     def test_get_http_insecure(self):
78         c = ALC.DryRunRESTClient(ALC.Runner.parse_args([]))
79         self.assertEquals(False, c.get_http_insecure())
80
81     def test_get_http_cafile(self):
82         c1 = ALC.DryRunRESTClient(ALC.Runner.parse_args([]))
83         self.assertEquals(False, c1.get_http_insecure())
84         self.assertIsNone(c1.get_http_cafile())
85
86         filename = self.__write_config_file({"ALC_HTTP_CAFILE": "/tmp/x"})
87         c2 = ALC.DryRunRESTClient(ALC.Runner.parse_args(["--config", filename]))
88         self.assertEquals(False, c2.get_http_insecure())
89         self.assertEquals("/tmp/x", c2.get_http_cafile())
90
91     def test_get_timeout_seconds(self):
92         args = ALC.Runner.parse_args(["--version"])
93         self.assertEquals(30, ALC.DryRunRESTClient(args).get_timeout_seconds())
94
95     def test_get_basic_credentials(self):
96         try:
97             saved_user = os.environ["ALC_HTTP_USER"]
98             saved_pass = os.environ["ALC_HTTP_PASS"]
99         except KeyError:
100             saved_user = ""
101             saved_pass = ""
102         try:
103             os.environ["ALC_HTTP_USER"] = "AUTH-DELETE"
104             os.environ["ALC_HTTP_PASS"] = "test"
105             c = ALC.DryRunRESTClient(ALC.Runner.parse_args([]))
106             c1 = c.get_basic_credentials()
107             self.assertEqual(c1, "QVVUSC1ERUxFVEU6dGVzdA==")
108             os.environ["ALC_HTTP_USER"] = "AUTH-DELETE"
109             os.environ["ALC_HTTP_PASS"] = "death"
110             c2 = c.get_basic_credentials()
111             self.assertNotEqual(c2, "QVVUSC1ERUxFVEU6dGVzdA==")
112         finally:
113             os.environ["ALC_HTTP_USER"] = saved_user
114             os.environ["ALC_HTTP_PASS"] = saved_pass
115
116     def test_get_rest_client(self):
117         uuid = ALC.IRESTClient.new_uuid()
118         c1 = ALC.Runner.get_rest_client(ALC.Runner.parse_args(["--dryrun"]))
119         self.assertTrue(isinstance(c1, ALC.DryRunRESTClient))
120         c2 = ALC.Runner.get_rest_client(ALC.Runner.parse_args(["--curl"]))
121         self.assertTrue(isinstance(c2, ALC.CURLRESTClient))
122         c3 = ALC.Runner.get_rest_client(ALC.Runner.parse_args(["--uuid", uuid]))
123         self.assertTrue(isinstance(c3, ALC.NativeRESTClient))
124
125     def test_get_logger(self):
126         logger = ALC.Runner.get_logger()
127         logger.info("idotlogger")
128
129     def test_new_uuid(self):
130         uuid = ALC.IRESTClient.new_uuid()
131         self.assertEqual(len(uuid), 36)
132
133     def test_make_service_url(self):
134         uuid = ALC.IRESTClient.new_uuid()
135
136         args1 = ALC.Runner.parse_args(["--url", "http://banana"])
137         client1 = ALC.DryRunRESTClient(args1)
138         self.assertEqual(client1.make_service_url(),
139                          "http://banana/onboarding-api/workflow/v1.0/actions")
140
141         args2 = ALC.Runner.parse_args(["--url", "http://banana/"])
142         client2 = ALC.DryRunRESTClient(args2)
143         self.assertEqual(client2.make_service_url(),
144                          "http://banana/onboarding-api/workflow/v1.0/actions")
145
146         args3 = ["--url", "http://banana/onboarding-api/workflow/v1.1/actions", "--uuid", uuid]
147         client3 = ALC.DryRunRESTClient(ALC.Runner.parse_args(args3))
148         self.assertEqual(client3.make_service_url(),
149                          "http://banana/onboarding-api/workflow/v1.1/actions/{}".format(uuid))
150
151     def test_debug_curl_cmd(self):
152         cmd = ["curl", "--header", "banana", "http://something/somewhere"]
153         debug = ALC.CURLRESTClient.debug_curl_cmd(cmd)
154         self.assertEqual("curl --header \"banana\" \"http://something/somewhere\" ", debug)