1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
19 from unittest
.mock
import Mock
, patch
29 from osm_ng_ro
.ns
import Ns
, NsException
32 class TestNs(unittest
.TestCase
):
36 @patch("jinja2.Environment.__init__")
37 def test__parse_jinja2_undefined_error(self
, env_mock
: Mock
):
38 cloud_init_content
= None
42 env_mock
.side_effect
= UndefinedError("UndefinedError occurred.")
44 with self
.assertRaises(NsException
):
45 self
.ns
._parse
_jinja
2(
46 cloud_init_content
=cloud_init_content
, params
=params
, context
=context
49 @patch("jinja2.Environment.__init__")
50 def test__parse_jinja2_template_error(self
, env_mock
: Mock
):
51 cloud_init_content
= None
55 env_mock
.side_effect
= TemplateError("TemplateError occurred.")
57 with self
.assertRaises(NsException
):
58 self
.ns
._parse
_jinja
2(
59 cloud_init_content
=cloud_init_content
, params
=params
, context
=context
62 @patch("jinja2.Environment.__init__")
63 def test__parse_jinja2_template_not_found(self
, env_mock
: Mock
):
64 cloud_init_content
= None
68 env_mock
.side_effect
= TemplateNotFound("TemplateNotFound occurred.")
70 with self
.assertRaises(NsException
):
71 self
.ns
._parse
_jinja
2(
72 cloud_init_content
=cloud_init_content
, params
=params
, context
=context
75 def test_rendering_jinja2_temp_without_special_characters(self
):
76 cloud_init_content
= """
81 overwrite: {{is_override}}
84 - [ sh, -xc, "echo $(date) '{{command}}'" ]
88 "is_override": "False",
89 "command": "; mkdir abc",
91 context
= "cloud-init for VM"
100 - [ sh, -xc, "echo $(date) '; mkdir abc'" ]
102 result
= self
.ns
._parse
_jinja
2(
103 cloud_init_content
=cloud_init_content
, params
=params
, context
=context
105 self
.assertEqual(result
, expected_result
)
107 def test_rendering_jinja2_temp_with_special_characters(self
):
108 cloud_init_content
= """
113 overwrite: {{is_override}}
116 - [ sh, -xc, "echo $(date) '{{command}}'" ]
120 "is_override": "False",
121 "command": "& rm -rf",
123 context
= "cloud-init for VM"
124 expected_result
= """
132 - [ sh, -xc, "echo $(date) '& rm -rf /'" ]
134 result
= self
.ns
._parse
_jinja
2(
135 cloud_init_content
=cloud_init_content
, params
=params
, context
=context
137 self
.assertNotEqual(result
, expected_result
)
139 def test_rendering_jinja2_temp_with_special_characters_autoescape_is_false(self
):
140 with
patch("osm_ng_ro.ns.Environment") as mock_environment
:
141 mock_environment
.return_value
= Environment(
142 undefined
=StrictUndefined
,
143 autoescape
=select_autoescape(default_for_string
=False, default
=False),
145 cloud_init_content
= """
150 overwrite: {{is_override}}
153 - [ sh, -xc, "echo $(date) '{{command}}'" ]
157 "is_override": "False",
158 "command": "& rm -rf /",
160 context
= "cloud-init for VM"
161 expected_result
= """
169 - [ sh, -xc, "echo $(date) '& rm -rf /'" ]
171 result
= self
.ns
._parse
_jinja
2(
172 cloud_init_content
=cloud_init_content
,
176 self
.assertEqual(result
, expected_result
)