forked from kapicorp/kapitan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_remote_inventory.py
228 lines (203 loc) · 8.08 KB
/
test_remote_inventory.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import json
import os
import sys
import tempfile
import unittest
from distutils.dir_util import copy_tree
from shutil import copytree, rmtree
import yaml
from kapitan.cached import reset_cache
from kapitan.cli import main
from kapitan.dependency_manager.base import (
fetch_git_dependency,
fetch_git_source,
fetch_http_dependency,
fetch_http_source,
)
from kapitan.resources import yaml_load
class RemoteInventoryTest(unittest.TestCase):
def setUp(self):
os.chdir(
os.path.join(
os.path.abspath(os.path.dirname(__file__)), "test_remote_inventory", "environment_one"
)
)
def test_fetch_git_inventory(self):
"""
test fetching of git inventories
"""
temp_dir = tempfile.mkdtemp()
repo_dir = os.path.join(temp_dir, "7a8f3940kapitan.git")
git_source = "https://github.com/kapicorp/kapitan.git"
fetch_git_source(git_source, repo_dir, item_type="Inventory")
self.assertTrue(os.path.isfile(os.path.join(repo_dir, "README.md")))
rmtree(temp_dir)
def test_clone_inv_subdir(self):
"""
test clonning of inventories from git and storing it in the output path
"""
temp_dir = tempfile.mkdtemp()
output_dir = tempfile.mkdtemp()
git_source = "https://github.com/kapicorp/kapitan.git"
inv = [{"output_path": os.path.join(output_dir, "subdir"), "ref": "master", "subdir": "tests"}]
fetch_git_dependency((git_source, inv), temp_dir, force=False, item_type="inventory")
self.assertTrue(os.path.isdir(os.path.join(output_dir, "subdir")))
rmtree(output_dir)
rmtree(temp_dir)
def test_fetch_http_inventory(self):
"""
test fetching of inventories over http
"""
temp_dir = tempfile.mkdtemp()
http_sources = [
("https://github.com/kapicorp/kapitan/archive/master.zip", "009a21cfmaster.zip"),
(
"https://raw.githubusercontent.com/kapicorp/kapitan/master/tests/test_resources/inventory/classes/common.yml",
"eac6ceb7common.yml",
),
(
"https://github.com/kapicorp/kapitan/raw/master/tests/test_remote_inventory/zipped_inventories/inventory.zip",
"47c29a3binventory.zip",
),
]
for source, path_hash in http_sources:
fetch_http_source(source, os.path.join(temp_dir, path_hash), item_type="Inventory")
self.assertTrue(os.path.isfile(os.path.join(temp_dir, "009a21cfmaster.zip")))
self.assertTrue(os.path.isfile(os.path.join(temp_dir, "eac6ceb7common.yml")))
self.assertTrue(os.path.isfile(os.path.join(temp_dir, "47c29a3binventory.zip")))
rmtree(temp_dir)
def test_unpack_http_inv(self):
"""
test unpacking of remote inventories after downloading it over http
"""
temp_dir = tempfile.mkdtemp()
output_dir = tempfile.mkdtemp()
# tar.gz file
http_source = "https://github.com/kapicorp/kapitan/raw/master/tests/test_remote_inventory/zipped_inventories/inventory.tar.gz"
inv = [{"output_path": os.path.join(output_dir, "subdir"), "unpack": "True"}]
fetch_http_dependency((http_source, inv), temp_dir, force=False, item_type="inventory")
self.assertTrue(os.path.isdir(os.path.join(output_dir, "subdir", "targets")))
rmtree(temp_dir)
rmtree(output_dir)
def test_compile_fetch(self):
"""Run $ kapitan compile --force-fetch --output-path=some/dir/ --inventory-path=another/dir --targets remoteinv-example remoteinv-nginx zippedinv
were some/dir/ & another/dir/ are directories chosen by the user
Recursively force fetch inventories and compile
"""
temp_output = tempfile.mkdtemp()
temp_inv = tempfile.mkdtemp()
copy_tree(
os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"test_remote_inventory",
"environment_one",
"inventory",
),
temp_inv,
)
sys.argv = [
"kapitan",
"compile",
"--force-fetch",
"--output-path",
temp_output,
"--inventory-path",
temp_inv,
"--targets",
"remoteinv-example",
"remoteinv-nginx",
"zippedinv",
]
main()
self.assertTrue(os.path.isfile(os.path.join(temp_inv, "targets", "remoteinv-nginx.yml")))
self.assertTrue(os.path.isfile(os.path.join(temp_inv, "targets", "nginx.yml")))
self.assertTrue(os.path.isfile(os.path.join(temp_inv, "targets", "nginx-dev.yml")))
self.assertTrue(os.path.isdir(os.path.join(temp_output, "compiled", "remoteinv-nginx")))
self.assertTrue(os.path.isdir(os.path.join(temp_output, "compiled", "zippedinv")))
rmtree(temp_inv)
rmtree(temp_output)
def test_compile_fetch_classes_that_doesnot_exist_yet(self):
"""
runs $ kapitan compile --fetch --search-paths temp_dir --output-path temp_dir --inventory-path temp_dir/inventory -t nginx
The `test-objects` class of target `nginx` does not exist initially, it is fetched and then compiled
"""
os.chdir(os.path.join(os.getcwd(), "..", "environment_three"))
temp_dir = tempfile.mkdtemp()
copy_tree(".", temp_dir) # copying the test environment to search path
sys.argv = [
"kapitan",
"compile",
"--fetch",
"--search-path",
temp_dir,
"--output-path",
temp_dir,
"--inventory-path",
os.path.join(temp_dir, "inventory"),
"-t",
"nginx",
]
main()
self.assertTrue(os.path.exists(os.path.join(temp_dir, "compiled", "nginx")))
rmtree(temp_dir)
def test_force_fetch(self):
"""Test overwriting inventory items while using the --force-fetch flag
runs $ kapitan compile --fetch --cache --output-path=temp_output --inventory-pat=temp_inv --targets remoteinv-example
runs $ kapitan compile --force-fetch --cache --output-path=temp_output --inventory-pat=temp_inv --targets remoteinv-example
"""
temp_output = tempfile.mkdtemp()
temp_inv = tempfile.mkdtemp()
copy_tree(
os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"test_remote_inventory",
"environment_one",
"inventory",
),
temp_inv,
)
sys.argv = [
"kapitan",
"compile",
"--fetch",
"--cache",
"--output-path",
temp_output,
"--inventory-path",
temp_inv,
"--targets",
"remoteinv-example",
]
main()
fname = os.path.join(temp_inv, "targets", "zippedinv.yml")
data = json.loads(yaml_load([os.path.dirname(fname)], os.path.basename(fname)))
data["parameters"]["compression_type"] = "test"
with open(fname, "w") as yaml_file:
yaml_file.write(yaml.dump(data, default_flow_style=False))
reset_cache()
main()
# no change in zippedinv.yml
new_data = json.loads(yaml_load([os.path.dirname(fname)], os.path.basename(fname)))
reset_cache()
sys.argv = [
"kapitan",
"compile",
"--force-fetch",
"--cache",
"--output-path",
temp_output,
"--inventory-path",
temp_inv,
"--targets",
"remoteinv-example",
]
main()
# zippedinv.yml overwritten
force_fetched_data = json.loads(yaml_load([os.path.dirname(fname)], os.path.basename(fname)))
self.assertEqual(new_data["parameters"]["compression_type"], "test")
self.assertEqual(force_fetched_data["parameters"]["compression_type"], "gzip")
rmtree(temp_inv)
rmtree(temp_output)
def tearDown(self):
os.chdir("../../../")
reset_cache()