-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
132 lines (105 loc) · 3.41 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
#!/usr/bin python3.7
import os
from unittest import TestCase, main
import classes
class TestCollector(TestCase):
"""Testing functionality of the Collector class.
Takes in 'file' as a directory path to the file
to be parsed and collected via the
inputs() : method
File written with collected values via the
outputs() : method
"""
def setUp(self):
"""
Creates two(2) instances of the Collector class from test files
stored within the tests directory.
"""
self.test_dir = os.path.join(classes.FILE_DIR, 'test_file_templates')
gunicorn_file = os.path.join(
self.test_dir,
'template-gunicorn.service' # Gunicorn test file
)
self.gunicorn_collector = classes.Collector(gunicorn_file)
nginx_file = os.path.join(
self.test_dir,
'template-sites-available' # Nginx test files
)
self.nginx_collector = classes.Collector(nginx_file)
false_file = '/file/that/does/not/exist'
self.false_collector = classes.Collector(false_file)
no_format = os.path.join(
self.test_dir,
'no-format.txt',
)
self.no_format = classes.Collector(no_format)
def test_pull_vars(self):
"""
Tests the values returned from the pull_vars() method
on 2 pre-determined files. Tests values against constants
pulled from the files
"""
nginx_vars = self.nginx_collector.pull_vars(raw=True)
nginx_vars_constant = [
#'{$ project_name, $PROJECT_NAME }',
'{$ port, 80 }',
'{$ domains, None, True }',
'{$ root_dir, $PWD }',
'{$ sock_path, $SOCK_DIR }'
]
gunicorn_vars = self.gunicorn_collector.pull_vars(raw=True)
gunicorn_vars_constant = [
'{$ user, $USER }',
'{$ group, www-data }',
'{$ root_dir, $PWD }',
'{$ path_to_env, $ENV_DIR }',
'{$ sock_path, $SOCK_DIR }',
'{$ project_name, $PROJECT_NAME }'
]
self.assertListEqual(nginx_vars, nginx_vars_constant)
self.assertListEqual(gunicorn_vars, gunicorn_vars_constant)
self.assertRaises(TypeError, self.no_format.pull_vars) # Test file with no formatting
def test_outputs(self) -> None:
user = os.getlogin()
nginx_inputs_constants = {
'project_name': 'Project_Name',
'port': '80',
'domains': 'domain.com www.domain.com',
'root_dir': self.test_dir,
'sock_path': f'/home/jsyme/projects/pycharm/django_deploy/django_deploy.sock'
}
gunicorn_inputs_constants = {
'user': user,
'group': 'www-data',
'root_dir': self.test_dir,
'path_to_env': classes.default_env_dir(self.test_dir),
'sock_path': f'/home/jsyme/projects/pycharm/django_deploy/django_deploy.sock',
'project_name': 'django_deploy'
}
print(gunicorn_inputs_constants)
g = self.gunicorn_collector.outputs(gunicorn_inputs_constants, os.path.join(self.test_dir, 'new-template-gunicorn.service')
)
n = self.nginx_collector.outputs(nginx_inputs_constants, os.path.join(self.test_dir, 'new-sites-available'))
exists = self.nginx_collector.outputs(
nginx_inputs_constants,
os.path.join(
self.test_dir,
'no-format.txt'
)
)
# Test when file exists
self.assertEqual(g, True)
self.assertEqual(n, True)
self.assertFalse(exists)
def tearDown(self) -> None:
g_file = os.path.join(self.test_dir, 'new-template-gunicorn.service')
n_file = os.path.join(self.test_dir, 'new-sites-available')
while os.path.isfile(g_file) or os.path.isfile(n_file):
os.remove(g_file)
print(f'{g_file} removed')
os.remove(n_file)
print(f'{n_file} removed')
class TestDeploy(TestCase):
pass
if __name__ == '__main__':
main()