blob: dc71d91c276e9e87dd67cd05a4a6a233692b1b07 [file] [log] [blame]
garciaale08395032021-01-15 13:04:05 -03001# -*- 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
25from osm_common.sol004_package import SOL004Package, SOL004PackageException
26import unittest
27
28
29class SOL004ValidatorTest(unittest.TestCase):
30 def test_get_package_file_hash_algorithm_from_manifest_with_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +010031 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")
garciaale08395032021-01-15 13:04:05 -030038
39 def test_get_package_file_hash_algorithm_from_manifest_without_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +010040 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")
garciaale08395032021-01-15 13:04:05 -030047
48 def test_get_package_file_hash_algorithm_from_manifest_on_non_existent_file(self):
garciadeblas2644b762021-03-24 09:21:01 +010049 package = SOL004Package(
50 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
51 )
garciaale08395032021-01-15 13:04:05 -030052 with self.assertRaises(SOL004PackageException):
garciadeblas2644b762021-03-24 09:21:01 +010053 package.get_package_file_hash_algorithm_from_manifest("Non/Existing/file")
garciaale08395032021-01-15 13:04:05 -030054
55 def test_get_package_file_hash_digest_from_manifest_with_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +010056 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 )
garciaale08395032021-01-15 13:04:05 -030065
66 def test_get_package_file_hash_digest_from_manifest_without_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +010067 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 )
garciaale08395032021-01-15 13:04:05 -030076
77 def test_get_package_file_hash_digest_from_manifest_on_non_existent_file(self):
garciadeblas2644b762021-03-24 09:21:01 +010078 package = SOL004Package(
79 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
80 )
garciaale08395032021-01-15 13:04:05 -030081 with self.assertRaises(SOL004PackageException):
garciadeblas2644b762021-03-24 09:21:01 +010082 package.get_package_file_hash_digest_from_manifest("Non/Existing/file")
garciaale08395032021-01-15 13:04:05 -030083
garciadeblas2644b762021-03-24 09:21:01 +010084 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")
garciaale08395032021-01-15 13:04:05 -030088 with self.assertRaises(SOL004PackageException):
garciadeblas2644b762021-03-24 09:21:01 +010089 package.get_package_file_hash_digest_from_manifest(
90 "Scripts/charms/simple/hooks/upgrade-charm"
91 )
garciaale08395032021-01-15 13:04:05 -030092
93 def test_validate_package_file_hash_with_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +010094 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")
garciaale08395032021-01-15 13:04:05 -030098
99 def test_validate_package_file_hash_without_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +0100100 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")
garciaale08395032021-01-15 13:04:05 -0300104
105 def test_validate_package_file_hash_on_non_existing_file(self):
garciadeblas2644b762021-03-24 09:21:01 +0100106 package = SOL004Package(
107 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
108 )
garciaale08395032021-01-15 13:04:05 -0300109 with self.assertRaises(SOL004PackageException):
garciadeblas2644b762021-03-24 09:21:01 +0100110 package.validate_package_file_hash("Non/Existing/file")
garciaale08395032021-01-15 13:04:05 -0300111
112 def test_validate_package_file_hash_on_wrong_manifest_hash(self):
garciadeblas2644b762021-03-24 09:21:01 +0100113 package = SOL004Package("osm_common/tests/packages/invalid_package_vnf")
garciaale08395032021-01-15 13:04:05 -0300114 with self.assertRaises(SOL004PackageException):
garciadeblas2644b762021-03-24 09:21:01 +0100115 package.validate_package_file_hash("Scripts/charms/simple/hooks/start")
garciaale08395032021-01-15 13:04:05 -0300116
117 def test_validate_package_file_hash_on_unsupported_hash_algorithm(self):
garciadeblas2644b762021-03-24 09:21:01 +0100118 package = SOL004Package("osm_common/tests/packages/invalid_package_vnf")
garciaale08395032021-01-15 13:04:05 -0300119 with self.assertRaises(SOL004PackageException):
garciadeblas2644b762021-03-24 09:21:01 +0100120 package.validate_package_file_hash("Scripts/charms/simple/src/charm.py")
garciaale08395032021-01-15 13:04:05 -0300121
122 def test_validate_package_hashes_with_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +0100123 package = SOL004Package(
124 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
125 )
garciaale08395032021-01-15 13:04:05 -0300126 package.validate_package_hashes()
127
128 def test_validate_package_hashes_without_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +0100129 package = SOL004Package(
130 "osm_common/tests/packages/native_charm_without_metadata_dir_vnf"
131 )
garciaale08395032021-01-15 13:04:05 -0300132 package.validate_package_hashes()
133
134 def test_validate_package_hashes_on_invalid_package(self):
garciadeblas2644b762021-03-24 09:21:01 +0100135 package = SOL004Package("osm_common/tests/packages/invalid_package_vnf")
garciaale08395032021-01-15 13:04:05 -0300136 with self.assertRaises(SOL004PackageException):
137 package.validate_package_hashes()
138
139 def test_get_descriptor_location_with_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +0100140 package = SOL004Package(
141 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
142 )
garciaale08395032021-01-15 13:04:05 -0300143 descriptor_path = package.get_descriptor_location()
garciadeblas2644b762021-03-24 09:21:01 +0100144 self.assertEqual(descriptor_path, "Definitions/native_charm_vnfd.yaml")
garciaale08395032021-01-15 13:04:05 -0300145
146 def test_get_descriptor_location_without_metadata_dir(self):
garciadeblas2644b762021-03-24 09:21:01 +0100147 package = SOL004Package(
148 "osm_common/tests/packages/native_charm_without_metadata_dir_vnf"
149 )
garciaale08395032021-01-15 13:04:05 -0300150 descriptor_path = package.get_descriptor_location()
garciadeblas2644b762021-03-24 09:21:01 +0100151 self.assertEqual(descriptor_path, "native_charm_vnfd.yaml")