def test_flatten_template_url_maxdepth(self):
        with self.assertRaises(Exception) as context:
            StackHelper.flatten_template_url(
                "{ one { two } { two { three { four { five { six { seven }}}}} }}"
            )

        self.assertTrue(
            'Template URL contains more than' in str(context.exception))
    def test_fn_findinmap_lookup(self):
        l_mappings = {
            "ami_lookup": {
                "us-east-1": {
                    "ami": "this_one",
                    "ami2": "that_one"
                },
                "us-east-2": {
                    "ami": "is_this_one",
                    "ami2": "is_that_one"
                },
                "us-west-1": {
                    "ami": "not_this_one",
                    "ami2": "not_that_one"
                }
            }
        }

        StackHelper.mappings = l_mappings

        mappings_map = "ami_lookup"
        first_key = "us-west-1"
        final_key = "ami2"

        result = StackHelper.find_in_map_lookup(mappings_map, first_key,
                                                final_key)

        self.assertEqual(result, 'not_that_one')
    def test_flatten_template_url(self):
        with open(
                "test/fixtures/templates/stackhelper/test.json") as test_file:
            self.tests = json.load(test_file)
            self.tests = self.tests['tests']

        total = len(self.tests)
        matched = 0

        for test in self.tests:
            cfn = self._load_template(test["input"]["master_template"])
            StackHelper.mappings = cfn.get("Mappings")
            if test["output"]["url_paths"] == StackHelper.flatten_template_url(
                    test["input"]["child_template"]):
                matched = matched + 1
        # print("matched {} total {}".format(matched, total))
        self.assertEqual(matched, total)
    def test_find_local_child_template(self):
        with open(
                "test/fixtures/templates/stackhelper/test.json") as test_file:
            self.tests = json.load(test_file)
            self.tests = self.tests['tests']

        total = 0
        matched = 0
        for test in self.tests:
            index = 0
            for url_path in test["output"]["url_paths"]:
                total = total + 1
                master_template = test["input"]["master_template"]
                result = StackHelper.find_local_child_template(
                    master_template, url_path)
                expected = test["output"]["local_paths"][index]
                if str(result) == str(expected):
                    matched = matched + 1
                index = index + 1

        # print("matched {} total {}".format(matched, total))
        self.assertEqual(matched, total)
    def test_flatten_template_url_exceptions_getatt(self):
        with self.assertRaises(Exception) as context:
            StackHelper.flatten_template_url("{'Fn::GetAtt'}")

        self.assertTrue('Fn::GetAtt: not supported' in str(context.exception))
        replacement = "test/fixtures/templates/stackhelper/"
        master_template_path_print = master_template_path_print.replace(
            original, replacement)
        if not printed:
            printed = True
            print("")
        print("{")
        print("\t\"input\": {")
        print("\t\t\"master_template\": \"" +
              "{}".format(master_template_path_print) + "\",")
        print("\t\t\"child_template\": \"{}".format(child_template_url) + "\"")
        print("\t},")

        StackHelper.mappings = cfn_mappings

        resolved_template_url_list = StackHelper.flatten_template_url(
            child_template_url)
        print("\t\"output\":{")
        print("\t\t\"url_paths\": {}".format(
            resolved_template_url_list).replace("'", '"') + ",")
        filepath_list = []
        for resolved_path in resolved_template_url_list:
            filepath = StackHelper.find_local_child_template(
                os.path.abspath(master_template_path), str(resolved_path))
            filepath = filepath.replace(original, replacement)
            filepath_list.append(filepath)

        filepath_list = list(dict.fromkeys(filepath_list))
        print("\t\t\"local_paths\": {} ".format(filepath_list).replace(
            "'", '"'))
        print("\t}")
        print("},")