/
test_init.py
175 lines (125 loc) · 5.98 KB
/
test_init.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright (c) 2015-2017 Cisco Systems, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import os
import uuid
import pytest
from molecule import util
from molecule.command import init
def test_process_templates(temp_dir):
template_dir = os.path.join(
os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'resources',
'templates')
repo_name = str(uuid.uuid4())
context = {'repo_name': repo_name}
init._process_templates(template_dir, context, temp_dir.strpath)
expected_file = os.path.join(temp_dir.strpath, repo_name, 'template.yml')
expected_contents = '- value: foo'
with util.open_file(expected_file) as stream:
for line in stream.readlines():
assert line.strip() in expected_contents
def test_resolve_template_dir_relative():
result = init._resolve_template_dir('foo')
parts = pytest.helpers.os_split(result)
assert ('..', 'cookiecutter', 'foo') == parts[-3:]
def test_resolve_template_dir_absolute():
result = init._resolve_template_dir('/foo/bar')
parts = pytest.helpers.os_split(result)
assert ('foo', 'bar') == parts[-2:]
@pytest.fixture
def init_new_role_command_args():
return {
'dependency_name': 'galaxy',
'driver_name': 'docker',
'lint_name': 'ansible-lint',
'provisioner_name': 'ansible',
'role_name': 'test-role',
'scenario_name': 'default',
'subcommand': __name__,
'verifier_name': 'testinfra'
}
def test_init_new_role(temp_dir, init_new_role_command_args,
patched_logger_info, patched_logger_success):
init._init_new_role(init_new_role_command_args)
msg = 'Initializing new role test-role...'
patched_logger_info.assert_called_once_with(msg)
assert os.path.isdir('./test-role')
assert os.path.isdir('./test-role/molecule/default')
assert os.path.isdir('./test-role/molecule/default/tests')
role_directory = os.path.join(temp_dir.strpath, 'test-role')
msg = 'Initialized role in {} successfully.'.format(role_directory)
patched_logger_success.assert_called_once_with(msg)
def test_init_new_role_already_exists(temp_dir, init_new_role_command_args,
patched_logger_critical):
init._init_new_role(init_new_role_command_args)
with pytest.raises(SystemExit) as e:
init._init_new_role(init_new_role_command_args)
assert 1 == e.value.code
msg = 'The directory test-role exists. Cannot create new role.'
patched_logger_critical.assert_called_once_with(msg)
@pytest.fixture
def init_new_scenario_command_args():
return {
'driver_name': 'docker',
'role_name': 'test-role',
'scenario_name': 'test-scenario',
'subcommand': __name__,
'verifier_name': 'testinfra'
}
def test_init_new_scenario(temp_dir, init_new_scenario_command_args,
patched_logger_info, patched_logger_success):
init._init_new_scenario(init_new_scenario_command_args)
msg = 'Initializing new scenario test-scenario...'
patched_logger_info.assert_called_once_with(msg)
assert os.path.isdir('./molecule/test-scenario')
assert os.path.isdir('./molecule/test-scenario/tests')
scenario_directory = os.path.join(temp_dir.strpath, 'molecule',
'test-scenario')
msg = 'Initialized scenario in {} successfully.'.format(scenario_directory)
patched_logger_success.assert_called_once_with(msg)
def test_init_new_scenario_already_exists(
temp_dir, init_new_scenario_command_args, patched_logger_critical):
init._init_new_scenario(init_new_scenario_command_args)
with pytest.raises(SystemExit) as e:
init._init_new_scenario(init_new_scenario_command_args)
assert 1 == e.value.code
msg = ('The directory molecule/test-scenario exists. '
'Cannot create new scenario.')
patched_logger_critical.assert_called_once_with(msg)
def test_init_template(temp_dir, patched_logger_info, patched_logger_success):
url = 'https://github.com/retr0h/cookiecutter-molecule.git'
no_input = True
command_args = {'role_name': 'foo', }
init._init_template(url, command_args, no_input)
assert os.path.isdir('./foo')
msg = 'Initializing new role foo...'
patched_logger_info.assert_called_once_with(msg)
role_directory = os.path.join(temp_dir.strpath, 'foo')
msg = 'Initialized role in {} successfully.'.format(role_directory)
patched_logger_success.assert_called_once_with(msg)
def test_init_template_role_already_exists(temp_dir, patched_logger_critical):
url = 'https://github.com/retr0h/cookiecutter-molecule.git'
no_input = True
command_args = {'role_name': 'foo', }
init._init_template(url, command_args, no_input)
with pytest.raises(SystemExit) as e:
init._init_template(url, command_args, no_input)
assert 1 == e.value.code
msg = ('The directory foo exists. Cannot create new role.')
patched_logger_critical.assert_called_once_with(msg)