forked from shon/converge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
101 lines (78 loc) · 3.03 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
import os
import glob
import shutil
import settings
settings_dir = 'fortest/server1'
default_config = {'config': 'default'}
dev_config = {'config': 'dev'}
prod_config = {'config': 'prod'}
site_config = {'config': 'site'}
repo_dir = '/tmp/settings-repo'
git_settings_subdir = repo_dir + '/myapp1'
def setup_module():
cmds = ['mkdir -p %s' % git_settings_subdir,
'git init %s' % repo_dir,
'echo "PROD = True" > %s/prod_settings.py' % git_settings_subdir,
'echo "PROD = False" > %s/dev_settings.py' % git_settings_subdir
]
for cmd in cmds:
ret = os.system(cmd)
if ret != 0:
raise Exception('failed: %s' % cmd)
def create_config_lines(config):
lines = []
for kv in config.items():
lines.append('%s = "%s"' % kv)
return lines
def create_config_file(path, config):
open(path, 'w').writelines(create_config_lines(config))
def test_no_settings_dir():
assert settings.get('config') is None, settings.get('config')
create_config_file('default_settings.py', default_config)
settings.reload()
assert settings.get('config') == 'default', settings.get('config')
def test_rc():
rc_lines = [('SETTINGS_DIR = "%s"\n' % settings_dir), 'APP_MODE = "dev"\n']
open('.convergerc', 'w').writelines(rc_lines)
os.makedirs(settings_dir)
open(os.path.join(settings_dir, '__init__.py'), 'w').close()
open(os.path.join(settings_dir, '../', '__init__.py'), 'w').close()
config_path = os.path.join(settings_dir, 'default_settings.py')
create_config_file(config_path, default_config)
settings.reload()
assert settings.config == 'default'
config_path = os.path.join(settings_dir, 'dev_settings.py')
create_config_file(config_path, dev_config)
settings.reload()
assert settings.config == 'dev'
config_path = os.path.join(settings_dir, 'prod_settings.py')
create_config_file(config_path, prod_config)
settings.reload()
assert settings.config == 'dev'
config_path = os.path.join(settings_dir, 'site_settings.py')
create_config_file(config_path, site_config)
settings.reload()
assert settings.config == 'site'
def test_backward_compatibility():
from converge import settings
def test_git_settings():
rc_lines = [('SETTINGS_DIR = "%s"\n' % settings_dir),
'APP_MODE = "prod"\n',
('GIT_SETTINGS_REPO = "%s"\n' % repo_dir),
('GIT_SETTINGS_SUBDIR = "%s"\n' % git_settings_subdir)
]
open('.convergerc', 'w').writelines(rc_lines)
settings.reload()
assert settings.PROD is True
def teardown_module():
py_path = 'default_settings.py'
pyc_path = py_path + 'c'
for path in (py_path, pyc_path):
if os.path.exists(path):
os.remove(path)
if glob.glob(os.path.join(settings_dir, '__init__.py')): # playing safe
shutil.rmtree(settings_dir)
if os.path.exists('.convergerc'):
os.remove('.convergerc')
if repo_dir.startswith('/tmp'): # playing safe
shutil.rmtree(repo_dir)