5 import action_library_client as ALC
10 def __init__(self, *args, **kwargs):
11 super(D, self).__init__(*args, **kwargs)
15 class UnitTest(unittest.TestCase):
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)
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)
33 # help = ALC.ArgumentParser().parse_args(["--help"])
34 # self.assertEquals(help.help, True)
36 uuidx = ALC.ArgumentParser().parse_args(["--uuid", "abc"])
37 self.assertEquals(uuidx.uuid, "abc")
40 def test_settings_get(self):
42 os.environ["a"] = "aa"
43 os.environ["b"] = "WILL_BE_OVERRIDDEN"
46 section['ALC_HTTP_USER'] = "batman"
47 section['ECOMP_INSTANCE_ID'] = "acdc"
49 filename = self.__write_config_file(section)
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"]
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))
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")
75 self.assertEquals(c1.version, True)
77 def test_get_http_insecure(self):
78 c = ALC.DryRunRESTClient(ALC.Runner.parse_args([]))
79 self.assertEquals(False, c.get_http_insecure())
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())
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())
91 def test_get_timeout_seconds(self):
92 args = ALC.Runner.parse_args(["--version"])
93 self.assertEquals(30, ALC.DryRunRESTClient(args).get_timeout_seconds())
95 def test_get_basic_credentials(self):
97 saved_user = os.environ["ALC_HTTP_USER"]
98 saved_pass = os.environ["ALC_HTTP_PASS"]
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==")
113 os.environ["ALC_HTTP_USER"] = saved_user
114 os.environ["ALC_HTTP_PASS"] = saved_pass
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))
125 def test_get_logger(self):
126 logger = ALC.Runner.get_logger()
127 logger.info("idotlogger")
129 def test_new_uuid(self):
130 uuid = ALC.IRESTClient.new_uuid()
131 self.assertEqual(len(uuid), 36)
133 def test_make_service_url(self):
134 uuid = ALC.IRESTClient.new_uuid()
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")
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")
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))
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)