| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2020 Whitestack, LLC |
| 4 | # ************************************************************* |
| 5 | # |
| 6 | # This file is part of OSM common repository. |
| 7 | # All Rights Reserved to Whitestack, LLC |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. You may obtain |
| 11 | # a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | # License for the specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | # For those usages not covered by the Apache License, Version 2.0 please |
| 22 | # contact: agarcia@whitestack.com |
| 23 | ## |
| 24 | |
| 25 | from osm_common.sol004_package import SOL004Package, SOL004PackageException |
| 26 | import unittest |
| 27 | |
| 28 | |
| 29 | class SOL004ValidatorTest(unittest.TestCase): |
| 30 | def test_get_package_file_hash_algorithm_from_manifest_with_metadata_dir(self): |
| 31 | package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf') |
| 32 | algorithm = package.get_package_file_hash_algorithm_from_manifest('Scripts/charms/simple/src/charm.py') |
| 33 | self.assertEqual(algorithm, 'SHA-256') |
| 34 | |
| 35 | def test_get_package_file_hash_algorithm_from_manifest_without_metadata_dir(self): |
| 36 | package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf') |
| 37 | algorithm = package.get_package_file_hash_algorithm_from_manifest('Scripts/charms/simple/src/charm.py') |
| 38 | self.assertEqual(algorithm, 'SHA-256') |
| 39 | |
| 40 | def test_get_package_file_hash_algorithm_from_manifest_on_non_existent_file(self): |
| 41 | package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf') |
| 42 | with self.assertRaises(SOL004PackageException): |
| 43 | package.get_package_file_hash_algorithm_from_manifest('Non/Existing/file') |
| 44 | |
| 45 | def test_get_package_file_hash_digest_from_manifest_with_metadata_dir(self): |
| 46 | package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf') |
| 47 | digest = package.get_package_file_hash_digest_from_manifest('Scripts/charms/simple/src/charm.py') |
| 48 | self.assertEqual(digest, '7895f7b9e1b7ed5b5bcd64398950ca95b456d7fc973334351474eed466c2f480') |
| 49 | |
| 50 | def test_get_package_file_hash_digest_from_manifest_without_metadata_dir(self): |
| 51 | package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf') |
| 52 | digest = package.get_package_file_hash_digest_from_manifest('Scripts/charms/simple/src/charm.py') |
| 53 | self.assertEqual(digest, '7895f7b9e1b7ed5b5bcd64398950ca95b456d7fc973334351474eed466c2f480') |
| 54 | |
| 55 | def test_get_package_file_hash_digest_from_manifest_on_non_existent_file(self): |
| 56 | package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf') |
| 57 | with self.assertRaises(SOL004PackageException): |
| 58 | package.get_package_file_hash_digest_from_manifest('Non/Existing/file') |
| 59 | |
| 60 | def test_get_package_file_hash_digest_from_manifest_on_non_existing_hash_entry(self): |
| 61 | package = SOL004Package('osm_common/tests/packages/invalid_package_vnf') |
| 62 | with self.assertRaises(SOL004PackageException): |
| 63 | package.get_package_file_hash_digest_from_manifest('Scripts/charms/simple/hooks/upgrade-charm') |
| 64 | |
| 65 | def test_validate_package_file_hash_with_metadata_dir(self): |
| 66 | package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf') |
| 67 | package.validate_package_file_hash('Scripts/charms/simple/src/charm.py') |
| 68 | |
| 69 | def test_validate_package_file_hash_without_metadata_dir(self): |
| 70 | package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf') |
| 71 | package.validate_package_file_hash('Scripts/charms/simple/src/charm.py') |
| 72 | |
| 73 | def test_validate_package_file_hash_on_non_existing_file(self): |
| 74 | package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf') |
| 75 | with self.assertRaises(SOL004PackageException): |
| 76 | package.validate_package_file_hash('Non/Existing/file') |
| 77 | |
| 78 | def test_validate_package_file_hash_on_wrong_manifest_hash(self): |
| 79 | package = SOL004Package('osm_common/tests/packages/invalid_package_vnf') |
| 80 | with self.assertRaises(SOL004PackageException): |
| 81 | package.validate_package_file_hash('Scripts/charms/simple/hooks/start') |
| 82 | |
| 83 | def test_validate_package_file_hash_on_unsupported_hash_algorithm(self): |
| 84 | package = SOL004Package('osm_common/tests/packages/invalid_package_vnf') |
| 85 | with self.assertRaises(SOL004PackageException): |
| 86 | package.validate_package_file_hash('Scripts/charms/simple/src/charm.py') |
| 87 | |
| 88 | def test_validate_package_hashes_with_metadata_dir(self): |
| 89 | package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf') |
| 90 | package.validate_package_hashes() |
| 91 | |
| 92 | def test_validate_package_hashes_without_metadata_dir(self): |
| 93 | package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf') |
| 94 | package.validate_package_hashes() |
| 95 | |
| 96 | def test_validate_package_hashes_on_invalid_package(self): |
| 97 | package = SOL004Package('osm_common/tests/packages/invalid_package_vnf') |
| 98 | with self.assertRaises(SOL004PackageException): |
| 99 | package.validate_package_hashes() |
| 100 | |
| 101 | def test_get_descriptor_location_with_metadata_dir(self): |
| 102 | package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf') |
| 103 | descriptor_path = package.get_descriptor_location() |
| 104 | self.assertEqual(descriptor_path, 'Definitions/native_charm_vnfd.yaml') |
| 105 | |
| 106 | def test_get_descriptor_location_without_metadata_dir(self): |
| 107 | package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf') |
| 108 | descriptor_path = package.get_descriptor_location() |
| 109 | self.assertEqual(descriptor_path, 'native_charm_vnfd.yaml') |