-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
88 lines (73 loc) · 2.51 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
import platform
import subprocess
# from django.test import TestCase
from unittest import TestCase
from set_environment import DeployEnv
print("temp_config/tests.py")
class TempConfigTestCase(TestCase):
"""
Unit tests for temp_config repo.
Tests set_environment.py, set_env_vars.sh,
and set_env_vars.bat for any errors, then
validate to see env vars are set.
"""
def __init__(self):
self.batch_filename = "set_env_vars.bat"
self.shell_filename = "set_env_vars.sh"
self.test_env_filename = "local_outside.env"
def setUp(self):
"""
Set up a default test scenario for temp_config
"""
print("setUp")
def test_set_environment(self):
"""
Runs load_deployment_environment() function from
set_environment.py and checks for errors.
"""
runtime_env = DeployEnv()
func_response = runtime_env.load_deployment_environment()
if '.env' in func_response:
# todo: more robust test, like checking if an env var is set
print("set_environment test passed! Using {} env file".format(func_response))
return True
else:
print("set_enviornemtn test failed... Using {} env file".format(func_response))
return False
def test_scripts(self):
"""
Tests set_env_vars, .sh or .bat, depending on platform
code is running on (windows or unix)
"""
os_type = platform.system()
print("system platform: ".format(os_type))
try:
if os_type == "Windows":
return self.eval_test_scripts(self.batch_filename, self.test_env_filename, os_type)
elif os_type == "Linux":
return self.eval_test_scripts(self.shell_filename, self.test_env_filename, os_type)
else:
# raise hell, only testing for above two cases (sorry, mac)
print("Test failed, os_type {} undetermined...".format(os_type))
return False
except Exception as e:
print("test_scripts test failed... exception: {}".format(e))
return False
def eval_test_scripts(self, script_filename, env_filename, os_type):
"""
Function for test_scripts, does subprocess.call for running
script file depending on OS platform
"""
try:
print("testing subprocess.call for {} script and {} env var file".format(script_filename, env_filename))
p = subprocess.call([script_filename, env_filename])
except Exception as e:
print("Test failed at eval_test_scripts making subprocess.call")
return False
print("returned value from subprocess.call: {}".format(p))
if p == 0:
print("set_env_vars test for {} passed!".format(os_type))
return True
else:
print("set_env_vars test for {} failed...".format(os_type))
return False