def test_coalescelist(self):
        input_str = 'coalescelist(["a", "b"], ["c", "d"])'
        expected = ['a', 'b']
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'coalescelist([], ["c", "d"])'
        expected = ["c", "d"]
        self.assertEqual(expected, evaluate_terraform(input_str))
    def test_concat(self):
        input_str = 'concat(["a", ""], ["b", "c"])'
        expected = ['a', '', 'b', 'c']
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'concat([\'postgresql-tcp\'],[],[\'\'])'
        expected = ['postgresql-tcp', '']
        self.assertEqual(expected, evaluate_terraform(input_str))
    def test_format(self):
        input_str = '"format("Hello, %s!", "Ander")"'
        expected = 'Hello, Ander!'
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = '"format("There are %d lights", 4)"'
        expected = 'There are 4 lights'
        self.assertEqual(expected, evaluate_terraform(input_str))
    def test_join(self):
        input_str = 'join(", ", ["foo", "bar", "baz"])'
        expected = 'foo, bar, baz'
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'join(", ", ["foo"])'
        expected = 'foo'
        self.assertEqual(expected, evaluate_terraform(input_str))
    def test_upper(self):
        input_str = 'upper("hello")'
        expected = 'HELLO'
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'upper("алло!")'
        expected = 'АЛЛО!'
        self.assertEqual(expected, evaluate_terraform(input_str))
    def test_map_access(self):
        input_str = '{\'module-input-bucket\':\'mapped-bucket-name\'}[module-input-bucket]-works-yay'
        expected = 'mapped-bucket-name-works-yay'
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = '{"module-input-bucket":"mapped-bucket-name"}[module-input-bucket]-works-yay'
        expected = 'mapped-bucket-name-works-yay'
        self.assertEqual(expected, evaluate_terraform(input_str))
Exemple #7
0
    def test_concat_dictionaries(self):
        input_str = "concat([{'key':'a','value':'a'},{'key':'b','value':'b'}, \"{'key':'d','value':'d'}\"],,[{'key':'c','value':'c'}],)"
        expected = [{'key':'a','value':'a'},{'key':'b','value':'b'},"{'key':'d','value':'d'}",{'key':'c','value':'c'}]
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'concat([\'postgresql-tcp\'],[],[\'\'])'
        expected = ['postgresql-tcp', '']
        self.assertEqual(expected, evaluate_terraform(input_str))
    def test_flatten(self):
        input_str = 'flatten([["a", "b"], [], ["c"]])'
        expected = ['a', 'b', 'c']
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'flatten([[["a", "b"], []], ["c"]])'
        expected = ['a', 'b', 'c']
        self.assertEqual(expected, evaluate_terraform(input_str))
    def test_condition(self):
        input_str = '"2 > 0 ? bigger : smaller"'
        expected = 'bigger'
        self.assertEqual(expected, evaluate_terraform(input_str).strip())

        input_str = '"2 > 5 ? bigger : smaller"'
        expected = 'smaller'
        self.assertEqual(expected, evaluate_terraform(input_str).strip())
Exemple #10
0
 def test_block_file_write(self):
     temp_file_path = "/tmp/file_shouldnt_create"
     input_str = "[x for x in {}.__class__.__bases__[0].__subclasses__() if x.__name__ == 'catch_warnings'][0]()._module.__builtins__['__import__']('os').system('date >> /tmp/file_shouldnt_create')"
     try:
         evaluate_terraform(input_str)
         self.fail("expected to raise BuiltinError")
     except BuiltinError as e:
         print(e)
         self.assertFalse(os.path.exists(temp_file_path))
         pass
    def test_coalese(self):
        input_str = 'coalesce("a", "b")'
        expected = 'a'
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'coalesce("", "b")'
        expected = 'b'
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'coalesce(1, 2)'
        expected = 1
        self.assertEqual(expected, evaluate_terraform(input_str))
    def test_regexall(self):
        input_str = 'regexall("[a-z]+", "1234abcd5678efgh9")'
        expected = ["abcd", "efgh"]
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'length(regexall("[a-z]+", "1234abcd5678efgh9"))'
        expected = 2
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'length(regexall("[a-z]+", "123456789")) > 0'
        expected = False
        self.assertEqual(expected, evaluate_terraform(input_str))
Exemple #13
0
 def test_matchkeys(self):
     input_str = 'matchkeys(["i-123", "i-abc", "i-def"], ["us-west", "us-east", "us-east"], ["us-east"])'
     expected = ["i-abc", "i-def"]
     actual = evaluate_terraform(input_str)
     for elem in actual:
         if elem not in expected:
             self.fail(f'expected to find {elem} in {expected}. Got {actual}')
 def test_formatlist(self):
     input_str = '"formatlist("Hello, %s!", ["Valentina", "Ander", "Olivia", "Sam"])"'
     expected = [
         'Hello, Valentina!', 'Hello, Ander!', 'Hello, Olivia!',
         'Hello, Sam!'
     ]
     self.assertEqual(expected, evaluate_terraform(input_str))
Exemple #15
0
    def test_jsonencode(self):
        cases = [
            ("jsonencode(['a', 42, true, null])", ["a", 42, True, None]),
            ("jsonencode({'a': 'b'})", {
                "a": "b"
            }),
            ("jsonencode({'a' = 'b'})", {
                "a": "b"
            }),
            ("jsonencode({'a' = 42})", {
                "a": 42
            }),
            ("jsonencode({'a' = true})", {
                "a": True
            }),
            ("jsonencode({'a' = false})", {
                "a": False
            }),
            ("jsonencode({'a' = null})", {
                "a": None
            }),
            ("jsonencode({'a' = ['b', 'c']})", {
                "a": ["b", "c"]
            }),
            ("jsonencode({'a' = jsonencode(['b', 'c'])})", {
                "a": ["b", "c"]
            }),
        ]

        for input_str, expected in cases:
            with self.subTest(input_str):
                assert evaluate_terraform(input_str) == expected
    def test_regex(self):
        input_str = 'regex("[a-z]+", "53453453.345345aaabbbccc23454")'
        expected = 'aaabbbccc'
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'regex("[a-z]+", "53453453.34534523454")'
        expected = ''
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'regex("^(?:(?P<scheme>[^:/?#]+):)?(?://(?P<authority>[^/?#]*))?", "https://terraform.io/docs/")'
        expected = {"authority": "terraform.io", "scheme": "https"}
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'regex("(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)", "2019-02-01")'
        expected = ["2019", "02", "01"]
        self.assertEqual(expected, evaluate_terraform(input_str))

        input_str = 'regex("[a-z]+", "53453453.34534523454")'
        expected = ''
        self.assertEqual(expected, evaluate_terraform(input_str))
Exemple #17
0
 def evaluate_vertices_attributes(self) -> None:
     for vertex in self.local_graph.vertices:
         decoded_attributes = vertex.get_decoded_attribute_dict()
         for attr in decoded_attributes:
             if attr in vertex.changed_attributes:
                 continue
             origin_value = decoded_attributes[attr]
             if not isinstance(origin_value, str):
                 continue
             evaluated_attribute_value = evaluate_terraform(origin_value)
             if origin_value != evaluated_attribute_value:
                 vertex.update_inner_attribute(attr, vertex.attributes, evaluated_attribute_value)
Exemple #18
0
    def test_block_segmentation_fault(self):
        # in this test, the following code is causing segmentation fault if evaluated
        input_str = """
(lambda fc=(
    lambda n: [
        c for c in
            ().__class__.__bases__[0].__subclasses__()
            if c.__name__ == n
        ][0]
    ):
    fc("function")(
        fc("code")(
            0,0,0,0,0,b'test',(),(),(),"","",0,b'test'
        ),{}
    )()
)()
"""
        try:
            evaluate_terraform(input_str)
            self.fail("expected to raise BuiltinError")
        except BuiltinError as e:
            print(e)
            pass
Exemple #19
0
 def test_complex_merge(self):
     cases = [
         ("merge(local.one, local.two)",
          "merge(local.one, local.two)"),
         ("merge({\"Tag4\" = \"four\"}, {\"Tag5\" = \"five\"})",
          {"Tag4" : "four", "Tag5" : "five"}),
         ("merge({\"a\"=\"b\"}, {\"b\"=[1,2], \"c\"=\"z\"}, {\"d\"=3})",
          {"a":"b", "b":[1,2], "c":"z", "d":3}),
         ('merge({\'a\': \'}, evil\'})',
          {"a": '}, evil'}),
         ('merge(local.common_tags,,{\'Tag4\': \'four\'},,{\'Tag2\': \'Dev\'},)',
          'merge(local.common_tags,{\'Tag4\': \'four\'},{\'Tag2\': \'Dev\'},)')
     ]
     for case in cases:
         input_str = case[0]
         expected = input_str if case[1] is None else case[1]
         actual = evaluate_terraform(input_str)
         assert actual == expected, f"Case \"{input_str}\" failed. Expected: {expected}  Actual: {actual}"
Exemple #20
0
 def evaluate_value(self, val: Any) -> Any:
     if type(val) not in [str, list, set, dict]:
         evaluated_val = val
     elif isinstance(val, str):
         evaluated_val = evaluate_terraform(val, keep_interpolations=False)
     elif isinstance(val, list):
         evaluated_val = []
         for v in val:
             evaluated_val.append(self.evaluate_value(v))
     elif isinstance(val, set):
         evaluated_val = set()
         for v in val:
             evaluated_val.add(self.evaluate_value(v))
     else:
         evaluated_val = {}
         for k, v in val.items():
             evaluated_key = self.evaluate_value(k)
             evaluated_val[evaluated_key] = self.evaluate_value(v)
     return evaluated_val
    def test_block_segmentation_fault(self):
        # in this test, the following code is causing segmentation fault if evaluated
        input_str = """
(lambda fc=(
    lambda n: [
        c for c in
            ().__class__.__bases__[0].__subclasses__()
            if c.__name__ == n
        ][0]
    ):
    fc("function")(
        fc("code")(
            0,0,0,0,0,b'test',(),(),(),"","",0,b'test'
        ),{}
    )()
)()
"""
        evaluated = evaluate_terraform(input_str)
        self.assertEqual(input_str.replace("\n", ""), evaluated)
Exemple #22
0
 def update_evaluated_value(
     self,
     changed_attribute_key: str,
     changed_attribute_value: Union[str, List[str]],
     vertex: int,
     change_origin_id: int,
     attribute_at_dest: Optional[Union[str, List[str]]] = None,
 ) -> None:
     """
     The function updates the value of changed_attribute_key with changed_attribute_value for vertex
     """
     str_to_evaluate = (str(changed_attribute_value)
                        if changed_attribute_key in ATTRIBUTES_NO_EVAL else
                        f'"{str(changed_attribute_value)}"')
     str_to_evaluate = str_to_evaluate.replace("\\\\", "\\")
     evaluated_attribute_value = (str_to_evaluate if changed_attribute_key
                                  in ATTRIBUTES_NO_EVAL else
                                  evaluate_terraform(str_to_evaluate))
     self.local_graph.update_vertex_attribute(vertex, changed_attribute_key,
                                              evaluated_attribute_value,
                                              change_origin_id,
                                              attribute_at_dest)
 def test_reverse(self):
     input_str = 'reverse([1, 2, 3])'
     expected = [3, 2, 1]
     self.assertEqual(expected, evaluate_terraform(input_str))
 def test_trim(self):
     input_str = 'trim("?!hello?!", "!?")'
     expected = 'hello'
     self.assertEqual(expected, evaluate_terraform(input_str))
 def test_trimprefix(self):
     input_str = 'trimprefix("helloworld", "hello")'
     expected = 'world'
     self.assertEqual(expected, evaluate_terraform(input_str))
 def test_substr(self):
     input_str = 'substr("hello world", 1, 4)'
     expected = 'ello'
     self.assertEqual(expected, evaluate_terraform(input_str))
 def test_replace(self):
     input_str = 'replace("1 + 2 + 3", "+", "-")'
     expected = -4
     self.assertEqual(expected, evaluate_terraform(input_str))
 def test_directive(self):
     input_str = '"Hello, %{ if "d" != "" }named%{ else }unnamed%{ endif }!"'
     expected = 'Hello, named!'
     self.assertEqual(expected, evaluate_terraform(input_str))
 def test_sort(self):
     input_str = 'sort(compact(distinct(concat([\'postgresql-tcp\'],[],[\'\']))))'
     expected = ['postgresql-tcp']
     self.assertEqual(expected, evaluate_terraform(input_str))
 def test_condition2(self):
     input_str = 'us-west-2 == "something to produce false" ? true : false'
     expected = 'false'
     self.assertEqual(expected, evaluate_terraform(input_str).strip())