Krzysztof Frukacz | 8418568 | 2017-01-31 08:03:29 +0100 | [diff] [blame] | 1 | # |
| 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 Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 16 | import collections |
Krzysztof Frukacz | 8418568 | 2017-01-31 08:03:29 +0100 | [diff] [blame] | 17 | import filecmp |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 18 | import os |
Krzysztof Frukacz | 8418568 | 2017-01-31 08:03:29 +0100 | [diff] [blame] | 19 | import tempfile |
| 20 | import shutil |
Lianhao Lu | 7db9cd9 | 2018-08-29 13:55:41 +0800 | [diff] [blame] | 21 | import pytest |
| 22 | |
Lianhao Lu | 7676ca5 | 2018-03-22 20:39:04 +0800 | [diff] [blame] | 23 | from vnfsdk_pkgtools.packager import csar |
Bogumil Zebek | a7308fa | 2019-02-25 11:36:37 +0100 | [diff] [blame] | 24 | from vnfsdk_pkgtools import util |
Krzysztof Frukacz | 8418568 | 2017-01-31 08:03:29 +0100 | [diff] [blame] | 25 | |
Bogumil Zebek | a7308fa | 2019-02-25 11:36:37 +0100 | [diff] [blame] | 26 | |
| 27 | ROOT_DIR = util.get_project_root() |
| 28 | |
| 29 | CSAR_RESOURCE_DIR = os.path.join(ROOT_DIR, 'tests', 'resources', 'csar') |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 30 | CSAR_ENTRY_FILE = 'test_entry.yaml' |
| 31 | CSAR_OUTPUT_FILE = 'output.csar' |
| 32 | |
| 33 | Args = collections.namedtuple('Args', |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 34 | ['source', 'entry', 'manifest', 'history', 'tests', |
| 35 | 'licenses', 'digest', 'certificate', 'privkey']) |
| 36 | |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 37 | |
| 38 | ARGS_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 Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 45 | 'digest': None, |
| 46 | 'certificate': None, |
| 47 | 'privkey': None, |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Lianhao Lu | cd02d1f | 2018-03-26 13:35:22 +0800 | [diff] [blame] | 50 | ARGS_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 Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 57 | 'digest': 'sha256', |
| 58 | 'certificate': None, |
| 59 | 'privkey': None, |
Lianhao Lu | cd02d1f | 2018-03-26 13:35:22 +0800 | [diff] [blame] | 60 | } |
| 61 | |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 62 | ARGS_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 Zebek | a7308fa | 2019-02-25 11:36:37 +0100 | [diff] [blame] | 71 | 'privkey': os.path.join(ROOT_DIR, 'tests', 'resources', 'signature', 'test.key') |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 72 | } |
Lianhao Lu | cd02d1f | 2018-03-26 13:35:22 +0800 | [diff] [blame] | 73 | |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 74 | ARGS_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 Lu | cd02d1f | 2018-03-26 13:35:22 +0800 | [diff] [blame] | 81 | 'digest': None, |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 82 | 'certificate': None, |
| 83 | 'privkey': None, |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 84 | } |
| 85 | |
Lianhao Lu | 7db9cd9 | 2018-08-29 13:55:41 +0800 | [diff] [blame] | 86 | INVALID_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 | |
| 98 | INVALID_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 Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 110 | |
| 111 | def csar_write_test(args): |
Krzysztof Frukacz | 8418568 | 2017-01-31 08:03:29 +0100 | [diff] [blame] | 112 | csar_target_dir = tempfile.mkdtemp() |
| 113 | csar_extract_dir = tempfile.mkdtemp() |
| 114 | try: |
Lianhao Lu | 1de1b09 | 2018-08-02 16:16:24 +0800 | [diff] [blame] | 115 | csar.write(args.source, args.entry, csar_target_dir + '/' + CSAR_OUTPUT_FILE, args) |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 116 | csar.read(csar_target_dir + '/' + CSAR_OUTPUT_FILE, csar_extract_dir, True) |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 117 | assert filecmp.cmp(args.source + '/' + args.entry, csar_extract_dir + '/' + args.entry) |
Lianhao Lu | cd02d1f | 2018-03-26 13:35:22 +0800 | [diff] [blame] | 118 | if(args.manifest and not args.digest): |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 119 | 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 Frukacz | 8418568 | 2017-01-31 08:03:29 +0100 | [diff] [blame] | 124 | finally: |
| 125 | shutil.rmtree(csar_target_dir, ignore_errors=True) |
| 126 | shutil.rmtree(csar_extract_dir, ignore_errors=True) |
| 127 | |
| 128 | |
Lianhao Lu | c2fc078 | 2017-09-14 15:48:04 +0800 | [diff] [blame] | 129 | def test_CSARWrite(): |
| 130 | csar_write_test(Args(**ARGS_NO_MANIFEST)) |
| 131 | |
| 132 | |
| 133 | def 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 Lu | cd02d1f | 2018-03-26 13:35:22 +0800 | [diff] [blame] | 139 | |
| 140 | |
| 141 | def 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 Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 147 | |
Lianhao Lu | 7db9cd9 | 2018-08-29 13:55:41 +0800 | [diff] [blame] | 148 | |
Lianhao Lu | 0efadc1 | 2018-08-27 17:11:10 +0800 | [diff] [blame] | 149 | def 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 Lu | 7db9cd9 | 2018-08-29 13:55:41 +0800 | [diff] [blame] | 155 | |
| 156 | |
| 157 | def 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") |