netsted template validate type error
[parser.git] / tosca2heat / tosca-parser / toscaparser / tests / test_prereq.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 import os
14 import shutil
15 import zipfile
16
17 from toscaparser.common.exception import URLException
18 from toscaparser.common.exception import ValidationError
19 from toscaparser.prereq.csar import CSAR
20 from toscaparser.tests.base import TestCase
21 import toscaparser.utils
22 from toscaparser.utils.gettextutils import _
23
24
25 class CSARPrereqTest(TestCase):
26
27     base_path = os.path.dirname(os.path.abspath(__file__))
28
29     def test_file_exists(self):
30         path = os.path.join(self.base_path, "data/CSAR/csar_not_there.zip")
31         csar = CSAR(path)
32         error = self.assertRaises(ValidationError, csar.validate)
33         self.assertEqual(_('"%s" does not exist.') % path, str(error))
34         self.assertTrue(csar.temp_dir is None or
35                         not os.path.exists(csar.temp_dir))
36
37     def test_file_is_zip(self):
38         path = os.path.join(self.base_path, "data/CSAR/csar_not_zip.zip")
39         csar = CSAR(path)
40         error = self.assertRaises(ValidationError, csar.validate)
41         self.assertEqual(_('"%s" is not a valid zip file.') % path, str(error))
42
43     def test_url_is_zip(self):
44         path = "https://github.com/openstack/tosca-parser/raw/master/" \
45                "toscaparser/tests/data/CSAR/csar_not_zip.zip"
46         csar = CSAR(path, False)
47         error = self.assertRaises(ValidationError, csar.validate)
48         self.assertEqual(_('"%s" is not a valid zip file.') % path, str(error))
49         self.assertTrue(csar.temp_dir is None or
50                         not os.path.exists(csar.temp_dir))
51
52     def test_metadata_file_exists(self):
53         path = os.path.join(self.base_path,
54                             "data/CSAR/csar_no_metadata_file.zip")
55         csar = CSAR(path)
56         error = self.assertRaises(ValidationError, csar.validate)
57         self.assertEqual(_('"%s" is not a valid CSAR as it does not contain '
58                            'the required file "TOSCA.meta" in the folder '
59                            '"TOSCA-Metadata".') % path, str(error))
60         self.assertTrue(csar.temp_dir is None or
61                         not os.path.exists(csar.temp_dir))
62
63     def test_valid_metadata_file_exists(self):
64         path = os.path.join(self.base_path,
65                             "data/CSAR/csar_wrong_metadata_file.zip")
66         csar = CSAR(path)
67         error = self.assertRaises(ValidationError, csar.validate)
68         self.assertEqual(_('"%s" is not a valid CSAR as it does not contain '
69                            'the required file "TOSCA.meta" in the folder '
70                            '"TOSCA-Metadata".') % path, str(error))
71         self.assertTrue(csar.temp_dir is None or
72                         not os.path.exists(csar.temp_dir))
73
74     def test_metadata_is_yaml(self):
75         path = os.path.join(self.base_path,
76                             "data/CSAR/csar_metadata_not_yaml.zip")
77         csar = CSAR(path)
78         error = self.assertRaises(ValidationError, csar.validate)
79         self.assertEqual(_('The file "TOSCA-Metadata/TOSCA.meta" in the CSAR '
80                            '"%s" does not contain valid YAML content.') % path,
81                          str(error))
82         self.assertTrue(csar.temp_dir is None or
83                         not os.path.exists(csar.temp_dir))
84
85     def test_metadata_exists(self):
86         path = os.path.join(self.base_path,
87                             "data/CSAR/csar_missing_metadata.zip")
88         csar = CSAR(path)
89         error = self.assertRaises(ValidationError, csar.validate)
90         self.assertEqual(_('The CSAR "%s" is missing the required metadata '
91                            '"Entry-Definitions" in '
92                            '"TOSCA-Metadata/TOSCA.meta".') % path, str(error))
93         self.assertTrue(csar.temp_dir is None or
94                         not os.path.exists(csar.temp_dir))
95
96     def test_entry_def_exists(self):
97         path = os.path.join(self.base_path,
98                             "data/CSAR/csar_invalid_entry_def.zip")
99         csar = CSAR(path)
100         error = self.assertRaises(ValidationError, csar.validate)
101         self.assertEqual(_('The "Entry-Definitions" file defined in the CSAR '
102                            '"%s" does not exist.') % path, str(error))
103         self.assertTrue(csar.temp_dir is None or
104                         not os.path.exists(csar.temp_dir))
105
106     def test_csar_invalid_import_path(self):
107         path = os.path.join(self.base_path,
108                             "data/CSAR/csar_wordpress_invalid_import_path.zip")
109         csar = CSAR(path)
110         error = self.assertRaises(ImportError, csar.validate)
111         self.assertEqual(_('Import "Invalid_import_path/wordpress.yaml" is'
112                            ' not valid.'), str(error))
113         self.assertTrue(csar.temp_dir is None or
114                         not os.path.exists(csar.temp_dir))
115
116     def test_csar_invalid_import_url(self):
117         path = os.path.join(self.base_path,
118                             "data/CSAR/csar_wordpress_invalid_import_url.zip")
119         csar = CSAR(path)
120         error = self.assertRaises(URLException, csar.validate)
121         self.assertEqual(_('Failed to reach server '
122                            '"https://raw.githubusercontent.com/openstack/'
123                            'tosca-parser/master/toscaparser/tests/data/CSAR/'
124                            'tosca_single_instance_wordpress/Definitions/'
125                            'wordpress1.yaml". Reason is: Not Found.'),
126                          str(error))
127         self.assertTrue(csar.temp_dir is None or
128                         not os.path.exists(csar.temp_dir))
129
130     def test_csar_invalid_script_path(self):
131         path = os.path.join(self.base_path,
132                             "data/CSAR/csar_wordpress_invalid_script_path.zip")
133         csar = CSAR(path)
134         error = self.assertRaises(ValueError, csar.validate)
135         self.assertTrue(
136             str(error) == _('The resource "Scripts/WordPress/install.sh" does '
137                             'not exist.') or
138             str(error) == _('The resource "Scripts/WordPress/configure.sh" '
139                             'does not exist.'))
140         self.assertTrue(csar.temp_dir is None or
141                         not os.path.exists(csar.temp_dir))
142
143     def test_csar_invalid_script_url(self):
144         path = os.path.join(self.base_path,
145                             "data/CSAR/csar_wordpress_invalid_script_url.zip")
146         csar = CSAR(path)
147         error = self.assertRaises(URLException, csar.validate)
148         self.assertEqual(_('The resource at '
149                            '"https://raw.githubusercontent.com/openstack/'
150                            'tosca-parser/master/toscaparser/tests/data/CSAR/'
151                            'tosca_single_instance_wordpress/Scripts/WordPress/'
152                            'install1.sh" cannot be accessed.'),
153                          str(error))
154         self.assertTrue(csar.temp_dir is None or
155                         not os.path.exists(csar.temp_dir))
156
157     def test_valid_csar(self):
158         path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
159         csar = CSAR(path)
160         self.assertTrue(csar.validate())
161         self.assertTrue(csar.temp_dir is None or
162                         not os.path.exists(csar.temp_dir))
163
164     def test_valid_csar_with_url_import_and_script(self):
165         path = os.path.join(self.base_path, "data/CSAR/csar_wordpress_with_url"
166                             "_import_and_script.zip")
167         csar = CSAR(path)
168         self.assertTrue(csar.validate())
169         self.assertTrue(csar.temp_dir is None or
170                         not os.path.exists(csar.temp_dir))
171
172     def test_metadata_invalid_csar(self):
173         path = os.path.join(self.base_path,
174                             "data/CSAR/csar_metadata_not_yaml.zip")
175         csar = CSAR(path)
176         error = self.assertRaises(ValidationError, csar.get_author)
177         self.assertEqual(_('The file "TOSCA-Metadata/TOSCA.meta" in the CSAR '
178                            '"%s" does not contain valid YAML content.') % path,
179                          str(error))
180         self.assertTrue(csar.temp_dir is None or
181                         not os.path.exists(csar.temp_dir))
182
183     def test_metadata_valid_csar(self):
184         path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
185         csar = CSAR(path)
186         expected_meta = {'TOSCA-Meta-File-Version': 1.0,
187                          'CSAR-Version': 1.1,
188                          'Created-By': 'OASIS TOSCA TC',
189                          'Entry-Definitions': 'tosca_helloworld.yaml'}
190         self.assertEqual(expected_meta, csar.get_metadata(),
191                          'The extracted metadata of the CSAR %(csar)s does '
192                          'not match the expected metadata %(meta)s'
193                          % {'csar': path, 'meta': expected_meta.__str__()})
194         self.assertEqual(1.1, csar.get_version())
195         self.assertEqual('OASIS TOSCA TC', csar.get_author())
196         self.assertEqual('tosca_helloworld.yaml', csar.get_main_template())
197         self.assertEqual('Template for deploying a single server with '
198                          'predefined properties.', csar.get_description())
199         self.assertTrue(csar.temp_dir is None or
200                         not os.path.exists(csar.temp_dir))
201
202     def test_csar_main_template(self):
203         path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
204         csar = CSAR(path)
205         yaml_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
206                                  "data/tosca_helloworld.yaml")
207         expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file)
208         self.assertEqual(expected_yaml, csar.get_main_template_yaml())
209         self.assertTrue(csar.temp_dir is None or
210                         not os.path.exists(csar.temp_dir))
211
212     def test_decompress(self):
213         path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
214         csar = CSAR(path)
215         csar.decompress()
216         zf = zipfile.ZipFile(path)
217         for name in zf.namelist():
218             tmp_path = os.path.join(csar.temp_dir, name)
219             self.assertTrue(os.path.isdir(tmp_path) or
220                             os.path.isfile(tmp_path))
221         shutil.rmtree(csar.temp_dir)
222         self.assertTrue(csar.temp_dir is None or
223                         not os.path.exists(csar.temp_dir))
224
225     def test_alternate_csar_extension(self):
226         path = os.path.join(self.base_path, "data/CSAR/csar_elk.csar")
227         csar = CSAR(path)
228         self.assertTrue(csar.validate())
229         self.assertTrue(csar.temp_dir is None or
230                         not os.path.exists(csar.temp_dir))