forked from Normation/rudder-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tif
executable file
·202 lines (160 loc) · 5.96 KB
/
tif
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
#!/usr/bin/python
# commands :
# - run <test> <platform>
# - platform up <platform>
# - platform destroy <platform>
import argparse
import json
import pprint
import os
import copy
import re
class Host:
def __init__(self, host_info):
self.info = host_info
def get_command(self, test_info):
# fill environment variables
env = "TEST_SYSTEM='" + self.info['system'] + "'"
env = env + " TEST_TYPE='" + self.info['rudder-setup'] + "'"
command_line = env + " sudo -E ./" + test_info['command']
command_line = "cd /vagrant/commands; " + command_line
return self.get_command_line(command_line)
class Vagrant(Host):
def __init__(self, host_info):
Host.__init__(self, host_info)
self.hostid = self.info['rudder-setup'] + "_" + self.info['system']
# find the host matching description and set it up
def start(self):
os.system("vagrant up " + self.hostid)
# find the host matching description and stop it
def stop(self):
os.system("vagrant destroy " + self.hostid)
# get the full command to run it on this host
def get_command_line(self, command):
return "vagrant ssh " + self.hostid + " -c \"" + command + "\""
host_types = { 'vagrant': Vagrant }
class Platform:
def __init__(self, platform_info):
self.hosts = {}
# manage default values
default = platform_info['default']
for host in platform_info.keys():
if host == "default":
continue
host_info = copy.deepcopy(default)
host_info.update(platform_info[host])
class_name = host_info['run-with']
self.hosts[host] = host_types[class_name](host_info) # new Vagrant/AWS/... object
# startup the full platform
def setup(self):
for host in self.hosts.keys():
self.hosts[host].start()
# stop the full platform
def teardown(self):
for host in self.hosts.keys():
self.hosts[host].stop()
# get the full command to run the given test on its remote host
def test_command(self, test_info):
host = test_info['host']
cmd = ""
if host == "all-nodes":
for host in self.hosts.keys():
if re.match("node", self.hosts[host].info['rudder-setup']):
cmd = cmd + self.hosts[host].get_command(test_info) + "\n"
elif host == "all-servers":
for host in self.hosts.keys():
if re.match("server", self.hosts[host].info['rudder-setup']):
cmd = cmd + self.hosts[host].get_command(test_info) + "\n"
elif host == "all":
for host in self.hosts.keys():
cmd = cmd + self.hosts[host].get_command(test_info) + "\n"
else:
cmd = self.hosts[host].get_command(test_info) + "\n"
return cmd
# Load a commented json
def load_json(filename):
# read json from file
file = open(filename, 'r')
data = file.read()
file.close()
data = re.sub("\\/\\/.*", "", data)
return json.loads(data)
# Get a platform object given it file name
_platform = None
def get_platform(name):
global _platform
if _platform is not None:
return _platform
platform_description = load_json("platforms/" + name + ".json", 'r')
_platform = Platform(platform_description)
return _platform
############
# COMMANDS #
############
# run one scenario
def run(args):
# extract test json
file = open("tests/" + args.test + ".json", 'r')
test_description = json.loads(file.read())
file.close()
# setup platform
platform_setup(args)
platform = get_platform(args.platform)
# write tests commands to a single script
file = open("harness", "w")
file.write("#!/bin/bash\n\n")
file.write("cd commands\n\n")
for test_item in test_description['checks']:
file.write(platform.test_command(test_item))
file.close()
# run tests
# we can prefix the command with "prove "
os.system("bash ./harness")
# manage platform
def platform(args):
if args.command == "setup":
platform_setup(args)
elif args.command == "destroy":
platform_destroy(args)
# prepare a platform
def platform_setup(args):
platform = get_platform(args.platform)
platform.setup()
# finalize a platform
def platform_destroy(args):
platform = get_platform(args.platform)
platform.teardown()
# MAIN, argument parser and help
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser(prog='testing')
subparsers = parser.add_subparsers(help='sub-commands help')
run_parser = subparsers.add_parser('run', help='run help')
run_parser.add_argument('test', type=str, help='run test help')
run_parser.add_argument('platform', type=str, help='run test help')
run_parser.set_defaults(func=run)
platform_parser = subparsers.add_parser('pf', help='up help')
platform_parser.add_argument('command', type=str, help='pf help')
platform_parser.add_argument('platform', type=str, help='pf help')
platform_parser.set_defaults(func=platform)
args = parser.parse_args()
args.func(args)
print("Done")
# test scenario {
# infra: { type: amazon; force-new:true; }
# server-os: { type: debian7; force-new:true; }
# server: { type: simple; version: last; force-new:true; }
# client-os: { type: debian4; force-new:true; }
# client: { force-new:true; }
# tests: [
# { action: setup-technique ..., check: user valid ...; reset-after-test: true }
# { action: upgrade-os ..., check: user valid ... reset-after-test: false }
# { action: setup-technique ..., check: user valid ... }
# { action: setup-technique ..., check: user valid ... }
# ]
# }
# tester-db: {
# infra: { type: amazon; status: installed; connect-info: {...} }
# server: { type: simple; version: 2.10; linked-to: infra,amazon,..; connect-info: {...} }
# client: { version: 2.10; linked-to: infra,amazon,..; connect-info: {...} }
# }