| 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): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 31 | package = SOL004Package( |
| 32 | "osm_common/tests/packages/native_charm_with_metadata_dir_vnf" |
| 33 | ) |
| 34 | algorithm = package.get_package_file_hash_algorithm_from_manifest( |
| 35 | "Scripts/charms/simple/src/charm.py" |
| 36 | ) |
| 37 | self.assertEqual(algorithm, "SHA-256") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 38 | |
| 39 | def test_get_package_file_hash_algorithm_from_manifest_without_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 40 | package = SOL004Package( |
| 41 | "osm_common/tests/packages/native_charm_without_metadata_dir_vnf" |
| 42 | ) |
| 43 | algorithm = package.get_package_file_hash_algorithm_from_manifest( |
| 44 | "Scripts/charms/simple/src/charm.py" |
| 45 | ) |
| 46 | self.assertEqual(algorithm, "SHA-256") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 47 | |
| 48 | def test_get_package_file_hash_algorithm_from_manifest_on_non_existent_file(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 49 | package = SOL004Package( |
| 50 | "osm_common/tests/packages/native_charm_with_metadata_dir_vnf" |
| 51 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 52 | with self.assertRaises(SOL004PackageException): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 53 | package.get_package_file_hash_algorithm_from_manifest("Non/Existing/file") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 54 | |
| 55 | def test_get_package_file_hash_digest_from_manifest_with_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 56 | package = SOL004Package( |
| 57 | "osm_common/tests/packages/native_charm_with_metadata_dir_vnf" |
| 58 | ) |
| 59 | digest = package.get_package_file_hash_digest_from_manifest( |
| 60 | "Scripts/charms/simple/src/charm.py" |
| 61 | ) |
| 62 | self.assertEqual( |
| 63 | digest, "ea72f897a966e6174ed9164fabc3c500df5a2f712eb6b22ab2408afb07d04d14" |
| 64 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 65 | |
| 66 | def test_get_package_file_hash_digest_from_manifest_without_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 67 | package = SOL004Package( |
| 68 | "osm_common/tests/packages/native_charm_without_metadata_dir_vnf" |
| 69 | ) |
| 70 | digest = package.get_package_file_hash_digest_from_manifest( |
| 71 | "Scripts/charms/simple/src/charm.py" |
| 72 | ) |
| 73 | self.assertEqual( |
| 74 | digest, "ea72f897a966e6174ed9164fabc3c500df5a2f712eb6b22ab2408afb07d04d14" |
| 75 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 76 | |
| 77 | def test_get_package_file_hash_digest_from_manifest_on_non_existent_file(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 78 | package = SOL004Package( |
| 79 | "osm_common/tests/packages/native_charm_with_metadata_dir_vnf" |
| 80 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 81 | with self.assertRaises(SOL004PackageException): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 82 | package.get_package_file_hash_digest_from_manifest("Non/Existing/file") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 83 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 84 | def test_get_package_file_hash_digest_from_manifest_on_non_existing_hash_entry( |
| 85 | self, |
| 86 | ): |
| 87 | package = SOL004Package("osm_common/tests/packages/invalid_package_vnf") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 88 | with self.assertRaises(SOL004PackageException): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 89 | package.get_package_file_hash_digest_from_manifest( |
| 90 | "Scripts/charms/simple/hooks/upgrade-charm" |
| 91 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 92 | |
| 93 | def test_validate_package_file_hash_with_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 94 | package = SOL004Package( |
| 95 | "osm_common/tests/packages/native_charm_with_metadata_dir_vnf" |
| 96 | ) |
| 97 | package.validate_package_file_hash("Scripts/charms/simple/src/charm.py") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 98 | |
| 99 | def test_validate_package_file_hash_without_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 100 | package = SOL004Package( |
| 101 | "osm_common/tests/packages/native_charm_without_metadata_dir_vnf" |
| 102 | ) |
| 103 | package.validate_package_file_hash("Scripts/charms/simple/src/charm.py") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 104 | |
| 105 | def test_validate_package_file_hash_on_non_existing_file(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 106 | package = SOL004Package( |
| 107 | "osm_common/tests/packages/native_charm_with_metadata_dir_vnf" |
| 108 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 109 | with self.assertRaises(SOL004PackageException): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 110 | package.validate_package_file_hash("Non/Existing/file") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 111 | |
| 112 | def test_validate_package_file_hash_on_wrong_manifest_hash(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 113 | package = SOL004Package("osm_common/tests/packages/invalid_package_vnf") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 114 | with self.assertRaises(SOL004PackageException): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 115 | package.validate_package_file_hash("Scripts/charms/simple/hooks/start") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 116 | |
| 117 | def test_validate_package_file_hash_on_unsupported_hash_algorithm(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 118 | package = SOL004Package("osm_common/tests/packages/invalid_package_vnf") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 119 | with self.assertRaises(SOL004PackageException): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 120 | package.validate_package_file_hash("Scripts/charms/simple/src/charm.py") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 121 | |
| 122 | def test_validate_package_hashes_with_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 123 | package = SOL004Package( |
| 124 | "osm_common/tests/packages/native_charm_with_metadata_dir_vnf" |
| 125 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 126 | package.validate_package_hashes() |
| 127 | |
| 128 | def test_validate_package_hashes_without_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 129 | package = SOL004Package( |
| 130 | "osm_common/tests/packages/native_charm_without_metadata_dir_vnf" |
| 131 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 132 | package.validate_package_hashes() |
| 133 | |
| 134 | def test_validate_package_hashes_on_invalid_package(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 135 | package = SOL004Package("osm_common/tests/packages/invalid_package_vnf") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 136 | with self.assertRaises(SOL004PackageException): |
| 137 | package.validate_package_hashes() |
| 138 | |
| 139 | def test_get_descriptor_location_with_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 140 | package = SOL004Package( |
| 141 | "osm_common/tests/packages/native_charm_with_metadata_dir_vnf" |
| 142 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 143 | descriptor_path = package.get_descriptor_location() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 144 | self.assertEqual(descriptor_path, "Definitions/native_charm_vnfd.yaml") |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 145 | |
| 146 | def test_get_descriptor_location_without_metadata_dir(self): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 147 | package = SOL004Package( |
| 148 | "osm_common/tests/packages/native_charm_without_metadata_dir_vnf" |
| 149 | ) |
| garciaale | 0839503 | 2021-01-15 13:04:05 -0300 | [diff] [blame] | 150 | descriptor_path = package.get_descriptor_location() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame^] | 151 | self.assertEqual(descriptor_path, "native_charm_vnfd.yaml") |