aboutsummaryrefslogtreecommitdiffstats
path: root/tests/packager/test_toscameta.py
blob: be6a173e03fad327c3f59279db1e0ac3ce51b6f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#
# Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved.
#
# 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.
#
import copy
import os
import pytest
import shutil
import tempfile

from vnfsdk_pkgtools.packager import toscameta
from vnfsdk_pkgtools import util


ROOT_DIR = util.get_project_root()

CSAR_RESOURCE_DIR = os.path.join(ROOT_DIR, 'tests', 'resources', 'csar')
CSAR_ENTRY_FILE = 'test_entry.yaml'
CSAR_OUTPUT_FILE = 'output.csar'

ARGS_MANIFEST = {
            'base_dir': CSAR_RESOURCE_DIR,
            'entry': CSAR_ENTRY_FILE,
            'manifest': 'test_entry.mf',
            'changelog': 'ChangeLog.txt',
            'licenses': 'Licenses',
            'tests': 'Tests',
            'certificate': None,
        }

ARGS_MANIFEST_CERTIFICATE = {
            'base_dir': CSAR_RESOURCE_DIR,
            'entry': CSAR_ENTRY_FILE,
            'manifest': 'test_entry.mf',
            'changelog': 'ChangeLog.txt',
            'licenses': 'Licenses',
            'tests': 'Tests',
            'certificate': 'test.crt',
        }

ARGS_NO_MANIFEST = {
            'base_dir': CSAR_RESOURCE_DIR,
            'entry': CSAR_ENTRY_FILE,
            'manifest': None,
            'changelog': None,
            'licenses': None,
            'tests': None,
            'certificate': None,
        }


def _validate_metadata(cls, expected):
    metadata = cls(**expected)
    assert toscameta.META_CREATED_BY_VALUE == metadata.created_by
    assert toscameta.META_CSAR_VERSION_VALUE == metadata.csar_version
    assert toscameta.META_FILE_VERSION_VALUE == metadata.meta_file_version
    assert expected['entry'] == metadata.entry_definitions
    assert expected['manifest'] == metadata.entry_manifest_file
    assert expected['changelog'] == metadata.entry_history_file
    assert expected['licenses'] == metadata.entry_licenses_dir
    assert expected['tests'] == metadata.entry_tests_dir
    assert expected['certificate'] == metadata.entry_certificate_file
    return metadata


def test_261():
    metadata = _validate_metadata(toscameta.ToscaMeta261, ARGS_MANIFEST)
    assert "ETSI-Entry-Change-Log: ChangeLog.txt\n" in metadata.dump_as_string()


def test_241():
    metadata = _validate_metadata(toscameta.ToscaMeta241, ARGS_MANIFEST_CERTIFICATE)
    assert "Entry-Certificate: test.crt\n" in metadata.dump_as_string()


def test_261_no_manifest():
    with pytest.raises(ValueError):
        toscameta.ToscaMeta261(**ARGS_NO_MANIFEST)


def test_241_no_manifest():
    metadata = _validate_metadata(toscameta.ToscaMeta241, ARGS_NO_MANIFEST)
    assert "Entry-Definitions: test_entry.yaml\n" in metadata.dump_as_string()


def test_invalid_entry():
    args = copy.copy(ARGS_MANIFEST)
    args['entry'] = 'test_entry.mf'
    with pytest.raises(ValueError):
        toscameta.ToscaMeta261(**args)


def test_invalid_csar_version():
    args = copy.copy(ARGS_MANIFEST)
    args['meta_csar_version'] = '1.2'
    with pytest.raises(ValueError):
        toscameta.ToscaMeta241(**args)


FROM_FILE_CASES = ['TOSCA.meta.sol261', 'TOSCA.meta.sol241']

def _prepare(target, metafile_path):
    shutil.copytree(CSAR_RESOURCE_DIR, target)
    os.mkdir(os.path.join(target, 'TOSCA-Metadata'))
    shutil.copy(metafile_path, os.path.join(target,
                                            'TOSCA-Metadata',
                                            'TOSCA.meta'))

def test_create_from_file():
    for case in FROM_FILE_CASES:
        target = tempfile.mkdtemp()
        base_dir = os.path.join(target, 'mytest')
        _prepare(base_dir,
                 os.path.join(ROOT_DIR,
                              'tests',
                              'resources',
                              case))
        try:
            toscameta.create_from_file(base_dir)
        finally:
            shutil.rmtree(target, ignore_errors=True)