dc71d91c276e9e87dd67cd05a4a6a233692b1b07
[osm/common.git] / osm_common / tests / test_sol004_package.py
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(
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")
38
39 def test_get_package_file_hash_algorithm_from_manifest_without_metadata_dir(self):
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")
47
48 def test_get_package_file_hash_algorithm_from_manifest_on_non_existent_file(self):
49 package = SOL004Package(
50 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
51 )
52 with self.assertRaises(SOL004PackageException):
53 package.get_package_file_hash_algorithm_from_manifest("Non/Existing/file")
54
55 def test_get_package_file_hash_digest_from_manifest_with_metadata_dir(self):
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 )
65
66 def test_get_package_file_hash_digest_from_manifest_without_metadata_dir(self):
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 )
76
77 def test_get_package_file_hash_digest_from_manifest_on_non_existent_file(self):
78 package = SOL004Package(
79 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
80 )
81 with self.assertRaises(SOL004PackageException):
82 package.get_package_file_hash_digest_from_manifest("Non/Existing/file")
83
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")
88 with self.assertRaises(SOL004PackageException):
89 package.get_package_file_hash_digest_from_manifest(
90 "Scripts/charms/simple/hooks/upgrade-charm"
91 )
92
93 def test_validate_package_file_hash_with_metadata_dir(self):
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")
98
99 def test_validate_package_file_hash_without_metadata_dir(self):
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")
104
105 def test_validate_package_file_hash_on_non_existing_file(self):
106 package = SOL004Package(
107 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
108 )
109 with self.assertRaises(SOL004PackageException):
110 package.validate_package_file_hash("Non/Existing/file")
111
112 def test_validate_package_file_hash_on_wrong_manifest_hash(self):
113 package = SOL004Package("osm_common/tests/packages/invalid_package_vnf")
114 with self.assertRaises(SOL004PackageException):
115 package.validate_package_file_hash("Scripts/charms/simple/hooks/start")
116
117 def test_validate_package_file_hash_on_unsupported_hash_algorithm(self):
118 package = SOL004Package("osm_common/tests/packages/invalid_package_vnf")
119 with self.assertRaises(SOL004PackageException):
120 package.validate_package_file_hash("Scripts/charms/simple/src/charm.py")
121
122 def test_validate_package_hashes_with_metadata_dir(self):
123 package = SOL004Package(
124 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
125 )
126 package.validate_package_hashes()
127
128 def test_validate_package_hashes_without_metadata_dir(self):
129 package = SOL004Package(
130 "osm_common/tests/packages/native_charm_without_metadata_dir_vnf"
131 )
132 package.validate_package_hashes()
133
134 def test_validate_package_hashes_on_invalid_package(self):
135 package = SOL004Package("osm_common/tests/packages/invalid_package_vnf")
136 with self.assertRaises(SOL004PackageException):
137 package.validate_package_hashes()
138
139 def test_get_descriptor_location_with_metadata_dir(self):
140 package = SOL004Package(
141 "osm_common/tests/packages/native_charm_with_metadata_dir_vnf"
142 )
143 descriptor_path = package.get_descriptor_location()
144 self.assertEqual(descriptor_path, "Definitions/native_charm_vnfd.yaml")
145
146 def test_get_descriptor_location_without_metadata_dir(self):
147 package = SOL004Package(
148 "osm_common/tests/packages/native_charm_without_metadata_dir_vnf"
149 )
150 descriptor_path = package.get_descriptor_location()
151 self.assertEqual(descriptor_path, "native_charm_vnfd.yaml")