1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import requests
import shutil
from unittest import mock
import urllib
import zipfile
from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import URLException
from toscaparser.common.exception import ValidationError
from toscaparser.prereq.csar import CSAR
from toscaparser.tests.base import MockTestClass
from toscaparser.tests.base import TestCase
from toscaparser.tests import utils
import toscaparser.utils
from toscaparser.utils.gettextutils import _
from toscaparser.utils.urlutils import UrlUtils
class CSARPrereqTest(TestCase):
base_path = utils.get_sample_test_dir()
def setUp(self):
super(CSARPrereqTest, self).setUp()
ExceptionCollector.clear()
ExceptionCollector.stop()
def test_file_exists(self):
path = os.path.join(self.base_path, "data/CSAR/csar_not_there.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('"%s" does not exist.') % path, str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_file_is_zip(self):
path = os.path.join(self.base_path, "data/CSAR/csar_not_zip.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('"%s" is not a valid zip file.') % path, str(error))
@mock.patch.object(requests, 'get')
def test_url_is_zip(self, mock_requests_get):
path = "https://example.com/csar_not_zip.zip"
class TestResponse:
content: bytes
response = TestResponse()
file_path = utils.get_sample_test_path("data/CSAR/csar_not_zip.zip")
with open(file_path, 'br') as f:
response.content = f.read()
mock_requests_get.return_value = response
csar = CSAR(path, False)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('"%s" is not a valid zip file.') % path, str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_metadata_file_exists(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_no_metadata_file.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('"%s" is not a valid CSAR as it does not contain '
'the required file "TOSCA.meta" in the folder '
'"TOSCA-Metadata".') % path, str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_valid_metadata_file_exists(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_wrong_metadata_file.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('"%s" is not a valid CSAR as it does not contain '
'the required file "TOSCA.meta" in the folder '
'"TOSCA-Metadata".') % path, str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_metadata_is_yaml(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_metadata_not_yaml.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('The file "TOSCA-Metadata/TOSCA.meta" in the CSAR '
'"%s" does not contain valid YAML content.') % path,
str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_metadata_exists(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_missing_metadata.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('The CSAR "%s" is missing the required metadata '
'"Entry-Definitions" in '
'"TOSCA-Metadata/TOSCA.meta".') % path, str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_entry_def_exists(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_invalid_entry_def.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('The "Entry-Definitions" file defined in the CSAR '
'"%s" does not exist.') % path, str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_invalid_import_path(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_wordpress_invalid_import_path.zip")
csar = CSAR(path)
error = self.assertRaises(ImportError, csar.validate)
self.assertEqual(_('Import "Invalid_import_path/wordpress.yaml" is'
' not valid.'), str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_invalid_import_url(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_wordpress_invalid_import_url.zip")
csar = CSAR(path)
error = self.assertRaises(URLException, csar.validate)
self.assertEqual(_('Failed to reach server '
'"https://raw.githubusercontent.com/openstack/'
'tosca-parser/master/toscaparser/tests/data/CSAR/'
'tosca_single_instance_wordpress/Definitions/'
'wordpress1.yaml". Reason is: Not Found.'),
str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_invalid_script_path(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_wordpress_invalid_script_path.zip")
csar = CSAR(path)
error = self.assertRaises(ValueError, csar.validate)
self.assertTrue(
str(error) == _('The resource "Scripts/WordPress/install.sh" does '
'not exist.') or
str(error) == _('The resource "Scripts/WordPress/configure.sh" '
'does not exist.'))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_invalid_script_url(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_wordpress_invalid_script_url.zip")
csar = CSAR(path)
error = self.assertRaises(URLException, csar.validate)
self.assertEqual(_('The resource at '
'"https://raw.githubusercontent.com/openstack/'
'tosca-parser/master/toscaparser/tests/data/CSAR/'
'tosca_single_instance_wordpress/Scripts/WordPress/'
'install1.sh" cannot be accessed.'),
str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_valid_csar(self):
path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
csar = CSAR(path)
self.assertTrue(csar.validate())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
@mock.patch.object(urllib.request, 'urlopen')
@mock.patch.object(UrlUtils, 'url_accessible')
def test_valid_csar_with_url_import_and_script(
self, mock_url_accessible, mock_urlopen):
path = os.path.join(self.base_path, "data/CSAR/csar_wordpress_with_url"
"_import_and_script.zip")
mock_path = "https://example.com/wordpress.yaml"
mockclass = MockTestClass()
mockclass.comp_urldict = {mock_path: utils.get_sample_test_path(
"data/custom_types/wordpress.yaml")}
mock_urlopen.side_effect = mockclass.mock_urlopen_method
mock_url_accessible.return_value = True
csar = CSAR(path)
self.assertTrue(csar.validate())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_metadata_invalid_csar(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_metadata_not_yaml.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.get_author)
self.assertEqual(_('The file "TOSCA-Metadata/TOSCA.meta" in the CSAR '
'"%s" does not contain valid YAML content.') % path,
str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_metadata_valid_csar(self):
path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
csar = CSAR(path)
expected_meta = {'TOSCA-Meta-File-Version': 1.0,
'CSAR-Version': 1.1,
'Created-By': 'OASIS TOSCA TC',
'Entry-Definitions': 'tosca_helloworld.yaml'}
self.assertEqual(expected_meta, csar.get_metadata(),
'The extracted metadata of the CSAR %(csar)s does '
'not match the expected metadata %(meta)s'
% {'csar': path, 'meta': expected_meta.__str__()})
self.assertEqual(1.1, csar.get_version())
self.assertEqual('OASIS TOSCA TC', csar.get_author())
self.assertEqual('tosca_helloworld.yaml', csar.get_main_template())
self.assertEqual('Template for deploying a single server with '
'predefined properties.', csar.get_description())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_main_template(self):
path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
csar = CSAR(path)
yaml_file = utils.get_sample_test_path("data/tosca_helloworld.yaml")
expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file)
self.assertEqual(expected_yaml, csar.get_main_template_yaml())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_decompress(self):
path = os.path.join(self.base_path, "data/CSAR/csar_hello_world.zip")
csar = CSAR(path)
csar.decompress()
zf = zipfile.ZipFile(path)
for name in zf.namelist():
tmp_path = os.path.join(csar.temp_dir, name)
self.assertTrue(os.path.isdir(tmp_path) or
os.path.isfile(tmp_path))
shutil.rmtree(csar.temp_dir)
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_alternate_csar_extension(self):
path = os.path.join(self.base_path, "data/CSAR/csar_elk.csar")
csar = CSAR(path)
self.assertTrue(csar.validate())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_with_root_level_yaml(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_root_level_yaml.zip")
csar = CSAR(path)
yaml_file = utils.get_sample_test_path(
"data/CSAR/root_level_file.yaml")
expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file)
self.assertEqual(expected_yaml, csar.get_main_template_yaml())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_with_multiple_root_level_yaml_files_error(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_two_root_level_yaml.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('CSAR file should contain only one root level'
' yaml file. Found "2" yaml file(s).'), str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_with_root_level_yaml_and_tosca_metadata(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_root_level_"
"yaml_and_tosca_metadata.zip")
csar = CSAR(path)
yaml_file = utils.get_sample_test_path(
"data/CSAR/tosca_meta_file.yaml")
expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file)
self.assertEqual(expected_yaml, csar.get_main_template_yaml())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_root_yaml_with_tosca_definition_1_0_error(self):
path = os.path.join(self.base_path, "data/CSAR/csar_root_yaml"
"_with_tosca_definition1_0.zip")
csar = CSAR(path)
error = self.assertRaises(ValidationError, csar.validate)
self.assertEqual(_('"%s" is not a valid CSAR as it does not contain'
' the required file "TOSCA.meta" in the folder '
'"TOSCA-Metadata".') % path, str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_with_multilevel_imports_valid(self):
path = os.path.join(
self.base_path,
"data/CSAR/csar_valid_multilevel_imports_validation.zip")
csar = CSAR(path)
yaml_file = utils.get_sample_test_path(
"data/CSAR/multi_level_imports_response.yaml")
expected_yaml = toscaparser.utils.yamlparser.load_yaml(yaml_file)
self.assertEqual(expected_yaml, csar.get_main_template_yaml())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_with_multilevel_imports_invalid(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_invalid_multilevel"
"_imports_validation.zip")
csar = CSAR(path)
error = self.assertRaises(ValueError, csar.validate)
self.assertEqual(_(
'The resource "%s" does '
'not exist.') % 'Files/images/'
'cirros-0.4.0-x86_64-disk.img', str(error))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_valid_artifact(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_wordpress_valid_artifact.zip")
csar = CSAR(path)
self.assertTrue(csar.validate())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_invalid_artifact(self):
path = os.path.join(self.base_path,
"data/CSAR/csar_wordpress_invalid_artifact.zip")
csar = CSAR(path)
error = self.assertRaises(ValueError, csar.validate)
self.assertTrue(
str(error) == _('The resource "Scripts/WordPress/configure.sh" '
'does not exist.'))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_valid_artifact_multi(self):
path = os.path.join(
self.base_path,
"data/CSAR/csar_wordpress_valid_artifact_multi.zip")
csar = CSAR(path)
self.assertTrue(csar.validate())
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
def test_csar_invalid_artifact_multi(self):
path = os.path.join(
self.base_path,
"data/CSAR/csar_wordpress_invalid_artifact_multi.zip")
csar = CSAR(path)
error = self.assertRaises(ValueError, csar.validate)
self.assertTrue(
str(error) == _('The resource "dummy-wordpress" '
'does not exist.'))
self.assertTrue(csar.temp_dir is None or
not os.path.exists(csar.temp_dir))
|