Adds SOL004 package validator with package examples and tests
[osm/common.git] / osm_common / tests / test_sol004_package.py
diff --git a/osm_common/tests/test_sol004_package.py b/osm_common/tests/test_sol004_package.py
new file mode 100644 (file)
index 0000000..b9f13af
--- /dev/null
@@ -0,0 +1,109 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2020 Whitestack, LLC
+# *************************************************************
+#
+# This file is part of OSM common repository.
+# All Rights Reserved to Whitestack, LLC
+#
+# 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.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: agarcia@whitestack.com
+##
+
+from osm_common.sol004_package import SOL004Package, SOL004PackageException
+import unittest
+
+
+class SOL004ValidatorTest(unittest.TestCase):
+    def test_get_package_file_hash_algorithm_from_manifest_with_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf')
+        algorithm = package.get_package_file_hash_algorithm_from_manifest('Scripts/charms/simple/src/charm.py')
+        self.assertEqual(algorithm, 'SHA-256')
+
+    def test_get_package_file_hash_algorithm_from_manifest_without_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf')
+        algorithm = package.get_package_file_hash_algorithm_from_manifest('Scripts/charms/simple/src/charm.py')
+        self.assertEqual(algorithm, 'SHA-256')
+
+    def test_get_package_file_hash_algorithm_from_manifest_on_non_existent_file(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf')
+        with self.assertRaises(SOL004PackageException):
+            package.get_package_file_hash_algorithm_from_manifest('Non/Existing/file')
+
+    def test_get_package_file_hash_digest_from_manifest_with_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf')
+        digest = package.get_package_file_hash_digest_from_manifest('Scripts/charms/simple/src/charm.py')
+        self.assertEqual(digest, '7895f7b9e1b7ed5b5bcd64398950ca95b456d7fc973334351474eed466c2f480')
+
+    def test_get_package_file_hash_digest_from_manifest_without_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf')
+        digest = package.get_package_file_hash_digest_from_manifest('Scripts/charms/simple/src/charm.py')
+        self.assertEqual(digest, '7895f7b9e1b7ed5b5bcd64398950ca95b456d7fc973334351474eed466c2f480')
+
+    def test_get_package_file_hash_digest_from_manifest_on_non_existent_file(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf')
+        with self.assertRaises(SOL004PackageException):
+            package.get_package_file_hash_digest_from_manifest('Non/Existing/file')
+
+    def test_get_package_file_hash_digest_from_manifest_on_non_existing_hash_entry(self):
+        package = SOL004Package('osm_common/tests/packages/invalid_package_vnf')
+        with self.assertRaises(SOL004PackageException):
+            package.get_package_file_hash_digest_from_manifest('Scripts/charms/simple/hooks/upgrade-charm')
+
+    def test_validate_package_file_hash_with_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf')
+        package.validate_package_file_hash('Scripts/charms/simple/src/charm.py')
+
+    def test_validate_package_file_hash_without_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf')
+        package.validate_package_file_hash('Scripts/charms/simple/src/charm.py')
+
+    def test_validate_package_file_hash_on_non_existing_file(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf')
+        with self.assertRaises(SOL004PackageException):
+            package.validate_package_file_hash('Non/Existing/file')
+
+    def test_validate_package_file_hash_on_wrong_manifest_hash(self):
+        package = SOL004Package('osm_common/tests/packages/invalid_package_vnf')
+        with self.assertRaises(SOL004PackageException):
+            package.validate_package_file_hash('Scripts/charms/simple/hooks/start')
+
+    def test_validate_package_file_hash_on_unsupported_hash_algorithm(self):
+        package = SOL004Package('osm_common/tests/packages/invalid_package_vnf')
+        with self.assertRaises(SOL004PackageException):
+            package.validate_package_file_hash('Scripts/charms/simple/src/charm.py')
+
+    def test_validate_package_hashes_with_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf')
+        package.validate_package_hashes()
+
+    def test_validate_package_hashes_without_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf')
+        package.validate_package_hashes()
+
+    def test_validate_package_hashes_on_invalid_package(self):
+        package = SOL004Package('osm_common/tests/packages/invalid_package_vnf')
+        with self.assertRaises(SOL004PackageException):
+            package.validate_package_hashes()
+
+    def test_get_descriptor_location_with_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_with_metadata_dir_vnf')
+        descriptor_path = package.get_descriptor_location()
+        self.assertEqual(descriptor_path, 'Definitions/native_charm_vnfd.yaml')
+
+    def test_get_descriptor_location_without_metadata_dir(self):
+        package = SOL004Package('osm_common/tests/packages/native_charm_without_metadata_dir_vnf')
+        descriptor_path = package.get_descriptor_location()
+        self.assertEqual(descriptor_path, 'native_charm_vnfd.yaml')