blob: 821714651c4a6a4ea6c51b55926705aebbced266 [file] [log] [blame]
Krzysztof Frukacz84185682017-01-31 08:03:29 +01001#
2# Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15#
Lianhao Luc2fc0782017-09-14 15:48:04 +080016import collections
Krzysztof Frukacz84185682017-01-31 08:03:29 +010017import filecmp
Lianhao Luc2fc0782017-09-14 15:48:04 +080018import os
Krzysztof Frukacz84185682017-01-31 08:03:29 +010019import tempfile
20import shutil
Lianhao Lu7db9cd92018-08-29 13:55:41 +080021import pytest
22
Lianhao Lu7676ca52018-03-22 20:39:04 +080023from vnfsdk_pkgtools.packager import csar
Bogumil Zebeka7308fa2019-02-25 11:36:37 +010024from vnfsdk_pkgtools import util
Krzysztof Frukacz84185682017-01-31 08:03:29 +010025
Bogumil Zebeka7308fa2019-02-25 11:36:37 +010026
27ROOT_DIR = util.get_project_root()
28
29CSAR_RESOURCE_DIR = os.path.join(ROOT_DIR, 'tests', 'resources', 'csar')
Lianhao Luc2fc0782017-09-14 15:48:04 +080030CSAR_ENTRY_FILE = 'test_entry.yaml'
31CSAR_OUTPUT_FILE = 'output.csar'
32
33Args = collections.namedtuple('Args',
Lianhao Lu0efadc12018-08-27 17:11:10 +080034 ['source', 'entry', 'manifest', 'history', 'tests',
35 'licenses', 'digest', 'certificate', 'privkey'])
36
Lianhao Luc2fc0782017-09-14 15:48:04 +080037
38ARGS_MANIFEST = {
39 'source': CSAR_RESOURCE_DIR,
40 'entry': CSAR_ENTRY_FILE,
41 'manifest': 'test_entry.mf',
42 'history': 'ChangeLog.txt',
43 'tests': 'Tests',
44 'licenses': 'Licenses',
Lianhao Lu0efadc12018-08-27 17:11:10 +080045 'digest': None,
46 'certificate': None,
47 'privkey': None,
Lianhao Luc2fc0782017-09-14 15:48:04 +080048 }
49
Lianhao Lucd02d1f2018-03-26 13:35:22 +080050ARGS_MANIFEST_DIGEST = {
51 'source': CSAR_RESOURCE_DIR,
52 'entry': CSAR_ENTRY_FILE,
53 'manifest': 'test_entry.mf',
54 'history': 'ChangeLog.txt',
55 'tests': 'Tests',
56 'licenses': 'Licenses',
Lianhao Lu0efadc12018-08-27 17:11:10 +080057 'digest': 'sha256',
58 'certificate': None,
59 'privkey': None,
Lianhao Lucd02d1f2018-03-26 13:35:22 +080060 }
61
Lianhao Lu0efadc12018-08-27 17:11:10 +080062ARGS_MANIFEST_DIGEST_CERT = {
63 'source': CSAR_RESOURCE_DIR,
64 'entry': CSAR_ENTRY_FILE,
65 'manifest': 'test_entry.mf',
66 'history': 'ChangeLog.txt',
67 'tests': 'Tests',
68 'licenses': 'Licenses',
69 'digest': 'sha256',
70 'certificate': 'test.crt',
Bogumil Zebeka7308fa2019-02-25 11:36:37 +010071 'privkey': os.path.join(ROOT_DIR, 'tests', 'resources', 'signature', 'test.key')
Lianhao Lu0efadc12018-08-27 17:11:10 +080072 }
Lianhao Lucd02d1f2018-03-26 13:35:22 +080073
Lianhao Luc2fc0782017-09-14 15:48:04 +080074ARGS_NO_MANIFEST = {
75 'source': CSAR_RESOURCE_DIR,
76 'entry': CSAR_ENTRY_FILE,
77 'manifest': None,
78 'history': None,
79 'tests': None,
80 'licenses': None,
Lianhao Lucd02d1f2018-03-26 13:35:22 +080081 'digest': None,
Lianhao Lu0efadc12018-08-27 17:11:10 +080082 'certificate': None,
83 'privkey': None,
Lianhao Luc2fc0782017-09-14 15:48:04 +080084 }
85
Lianhao Lu7db9cd92018-08-29 13:55:41 +080086INVALID_ARGS_NO_MANIFEST = {
87 'source': CSAR_RESOURCE_DIR,
88 'entry': CSAR_ENTRY_FILE,
89 'manifest': None,
90 'history': None,
91 'tests': None,
92 'licenses': None,
93 'digest': 'sha256',
94 'certificate': None,
95 'privkey': None,
96 }
97
98INVALID_ARGS_NO_PRIVKEY = {
99 'source': CSAR_RESOURCE_DIR,
100 'entry': CSAR_ENTRY_FILE,
101 'manifest': 'test_entry.mf',
102 'history': None,
103 'tests': None,
104 'licenses': None,
105 'digest': None,
106 'certificate': 'test.crt',
107 'privkey': None,
108 }
109
Lianhao Luc2fc0782017-09-14 15:48:04 +0800110
111def csar_write_test(args):
Krzysztof Frukacz84185682017-01-31 08:03:29 +0100112 csar_target_dir = tempfile.mkdtemp()
113 csar_extract_dir = tempfile.mkdtemp()
114 try:
Lianhao Lu1de1b092018-08-02 16:16:24 +0800115 csar.write(args.source, args.entry, csar_target_dir + '/' + CSAR_OUTPUT_FILE, args)
Lianhao Lu0efadc12018-08-27 17:11:10 +0800116 csar.read(csar_target_dir + '/' + CSAR_OUTPUT_FILE, csar_extract_dir, True)
Lianhao Luc2fc0782017-09-14 15:48:04 +0800117 assert filecmp.cmp(args.source + '/' + args.entry, csar_extract_dir + '/' + args.entry)
Lianhao Lucd02d1f2018-03-26 13:35:22 +0800118 if(args.manifest and not args.digest):
Lianhao Luc2fc0782017-09-14 15:48:04 +0800119 assert filecmp.cmp(args.source + '/' + args.manifest,
120 csar_extract_dir + '/' + args.manifest)
121 if(args.history):
122 assert filecmp.cmp(args.source + '/' + args.history,
123 csar_extract_dir + '/' + args.history)
Krzysztof Frukacz84185682017-01-31 08:03:29 +0100124 finally:
125 shutil.rmtree(csar_target_dir, ignore_errors=True)
126 shutil.rmtree(csar_extract_dir, ignore_errors=True)
127
128
Lianhao Luc2fc0782017-09-14 15:48:04 +0800129def test_CSARWrite():
130 csar_write_test(Args(**ARGS_NO_MANIFEST))
131
132
133def test_CSARWrite_manifest():
134 # Because git can not store emptry directory, we need to create manually here
135 license_path = ARGS_MANIFEST['source'] + '/' + ARGS_MANIFEST['licenses']
136 if not os.path.exists(license_path):
137 os.makedirs(license_path)
138 csar_write_test(Args(**ARGS_MANIFEST))
Lianhao Lucd02d1f2018-03-26 13:35:22 +0800139
140
141def test_CSARWrite_manifest_digest():
142 # Because git can not store emptry directory, we need to create manually here
143 license_path = ARGS_MANIFEST['source'] + '/' + ARGS_MANIFEST['licenses']
144 if not os.path.exists(license_path):
145 os.makedirs(license_path)
146 csar_write_test(Args(**ARGS_MANIFEST_DIGEST))
Lianhao Lu0efadc12018-08-27 17:11:10 +0800147
Lianhao Lu7db9cd92018-08-29 13:55:41 +0800148
Lianhao Lu0efadc12018-08-27 17:11:10 +0800149def test_CSARWrite_manifest_digest_cert():
150 # Because git can not store emptry directory, we need to create manually here
151 license_path = ARGS_MANIFEST['source'] + '/' + ARGS_MANIFEST['licenses']
152 if not os.path.exists(license_path):
153 os.makedirs(license_path)
154 csar_write_test(Args(**ARGS_MANIFEST_DIGEST_CERT))
Lianhao Lu7db9cd92018-08-29 13:55:41 +0800155
156
157def test_CSARWrite_invalid_arg():
158 with pytest.raises(ValueError) as excinfo:
159 csar_write_test(Args(**INVALID_ARGS_NO_MANIFEST))
160 excinfo.match(r"Must specify manifest file")
161
162 with pytest.raises(ValueError) as excinfo:
163 csar_write_test(Args(**INVALID_ARGS_NO_PRIVKEY))
164 excinfo.match(r"Need private key file")