-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
185 lines (169 loc) · 6.57 KB
/
tests.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
import unittest
import logging
import tempfile
import os
import shutil
from random import Random
import string
from subprocess import call, Popen, PIPE
import tarfile
import application
import settings
import utils
import schema
from utils import deploy
try:
import simplejson as json
except ImportError:
import json
def get_random_string():
return ''.join(Random().sample(string.letters+string.digits, 16))
class CoreTestCase(unittest.TestCase):
def setUp(self):
self.client = application.app.test_client()
def test_index(self):
resp = self.client.get('/')
assert(resp.status_code == 200 or resp.status_code == 302)
def test_user_ops(self):
test_user = get_random_string()
test_role = get_random_string()
# create
assert utils.create_user(username=test_user, password='na', role=test_role)
assert utils.get_user(test_user) != None
# toggle
assert utils.toggle_user(test_user, False)
user_data = json.loads(utils.get_user(test_user))
assert user_data['enabled'] == False
assert utils.toggle_user(test_user, True)
user_data = json.loads(utils.get_user(test_user))
assert user_data['enabled'] == True
assert utils.toggle_user(test_user)
user_data = json.loads(utils.get_user(test_user))
assert user_data['enabled'] == False
# delete
assert utils.delete_user(test_user)
assert utils.get_user(test_user) == None
assert utils.delete_role(test_role)
def test_role_ops(self):
test_role = get_random_string()
assert utils.create_role(test_role)
assert utils.get_role(test_role) != None
assert utils.delete_role(test_role)
assert utils.get_role(test_role) == None
def tearDown(self):
pass
class DeployTestCase(unittest.TestCase):
def setUp(self):
pass
def test_deploy_app(self):
tmp_app_name = get_random_string()
tmp_app_file = tempfile.mktemp()
with open(tmp_app_file, 'w') as f:
f.write('testing')
# create temp manifest
tmp_manifest = tempfile.mktemp()
with open(tmp_manifest, 'w') as f:
data = {"application": tmp_app_name, "version": "test"}
f.write(json.dumps(data))
# create package
tmp_pkg = tempfile.mktemp()
tf = tarfile.open(tmp_pkg, 'w:gz')
tf.add(tmp_manifest, arcname='manifest.json')
tf.add(tmp_app_file, arcname='testfile')
tf.close()
# deploy
deploy.deploy_app(package=tmp_pkg, build_ve=False)
tmp_app_dir = os.path.join(settings.APPLICATION_BASE_DIR, tmp_app_name)
assert os.path.exists(os.path.join(settings.APPLICATION_BASE_DIR, tmp_app_name))
assert os.listdir(os.path.join(tmp_app_dir, tmp_app_name)) > 0
assert os.path.exists(os.path.join(os.path.join(tmp_app_dir, tmp_app_name), 'testfile'))
# cleanup
if os.path.exists(tmp_app_file):
os.remove(tmp_app_file)
if os.path.exists(tmp_manifest):
os.remove(tmp_manifest)
if os.path.exists(tmp_pkg):
os.remove(tmp_pkg)
if os.path.exists(tmp_app_dir):
shutil.rmtree(tmp_app_dir)
def test_install_virtualenv(self):
tmp_ve = get_random_string()
tmp_ve_path = os.path.join(settings.VIRTUALENV_BASE_DIR, tmp_ve)
tmp_reqs = tempfile.mktemp()
with open(tmp_reqs, 'w') as f:
f.write('flask')
pkgs = ['requests']
# test install
deploy.install_virtualenv(application=tmp_ve, packages=pkgs, requirements=tmp_reqs)
assert os.path.exists(tmp_ve_path)
assert 'python' in os.listdir(os.path.join(tmp_ve_path, 'bin'))
assert 'pip' in os.listdir(os.path.join(tmp_ve_path, 'bin'))
p = Popen([os.path.join(os.path.join(tmp_ve_path, 'bin'), 'pip'), 'freeze'], stdout=PIPE, stderr=PIPE)
p_out, p_err = p.stdout, p.stderr
pip_freeze = p_out.read().lower()
assert pip_freeze.find('requests') > -1
assert pip_freeze.find('flask') > -1
# cleanup
shutil.rmtree(tmp_ve_path)
os.remove(tmp_reqs)
def test_install_virtualenv_custom_runtime(self):
tmp_ve = get_random_string()
tmp_ve_path = os.path.join(settings.VIRTUALENV_BASE_DIR, tmp_ve)
tmp_reqs = tempfile.mktemp()
with open(tmp_reqs, 'w') as f:
f.write('flask')
pkgs = ['requests']
# test install
deploy.install_virtualenv(application=tmp_ve, packages=pkgs, requirements=tmp_reqs, runtime='python2.6')
assert os.path.exists(tmp_ve_path)
assert 'python2.6' in os.listdir(os.path.join(tmp_ve_path, 'bin'))
assert 'pip' in os.listdir(os.path.join(tmp_ve_path, 'bin'))
p = Popen([os.path.join(os.path.join(tmp_ve_path, 'bin'), 'pip'), 'freeze'], stdout=PIPE, stderr=PIPE)
p_out, p_err = p.stdout, p.stderr
pip_freeze = p_out.read().lower()
assert pip_freeze.find('requests') > -1
assert pip_freeze.find('flask') > -1
# cleanup
shutil.rmtree(tmp_ve_path)
os.remove(tmp_reqs)
def test_configure_supervisor(self):
tmp_app_name = get_random_string()
uwsgi_args = {
'buffer-size': '8192',
'processes': '6',
'master': '',
}
deploy.configure_supervisor(application=tmp_app_name, uwsgi_args=uwsgi_args)
uwsgi_config = os.path.join(settings.SUPERVISOR_CONF_DIR, 'uwsgi_{0}.conf'.format(tmp_app_name))
assert os.path.exists(uwsgi_config)
with open(uwsgi_config, 'r') as f:
cfg = f.read()
assert cfg.find('[program:uwsgi_{0}]'.format(tmp_app_name)) > -1
assert cfg.find('--buffer-size 8192') > -1
assert cfg.find('--processes 6') > -1
assert cfg.find('--master') > -1
# cleanup
os.remove(uwsgi_config)
def tearDown(self):
pass
class UtilsTestCase(unittest.TestCase):
def setUp(self):
self.db = application.get_db_connection()
pass
def test_reserve_app_port(self):
port = utils.get_next_application_port()
assert port != None
utils.reserve_application_port(port)
ports_key = schema.PORTS_KEY
ports = json.loads(self.db.get(ports_key))
assert port in ports
# cleanup
utils.release_application_port(port)
ports = self.db.get(ports_key)
if ports:
ports = json.loads(ports)
assert port not in ports
def tearDown(self):
pass
if __name__=="__main__":
unittest.main()