-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
281 lines (224 loc) · 8.34 KB
/
test.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import os
import unittest
import time
import subprocess
import paramiko
import shutil
from anouman.templates import (
vagrant,
vagrant_bootstrap,
clean
)
for line in open("setup.py"):
if "VERSION" in line:
try:
VERSION = line.split("=")[1].strip().replace('"', '')
break
except:
pass
try: VERSION
except Exception as e:
raise Exception("VERSION NUMBER NOT FOUND")
VM1="10.0.1.21"
class TestBuild(unittest.TestCase):
def setUp(self):
"""
There were a lot of directory changes going on.
It was easier to alway's return to a common root directory
"""
# return to the rool level directory
os.chdir(os.path.dirname(__file__))
def test_1_remove_dist(self):
"""Make sure the build directory is gone"""
subprocess.call(['rm', '-rf', 'dist'])
self.assertFalse(
os.path.isfile(
'dist/anouman-%(version)s.tar.gz' % {'version':VERSION}
)
)
def test_2_uninstall_anouman(self):
subprocess.call(['pip', '-q', 'uninstall', 'anouman'])
try:
anouman = subprocess.check_output(['which', 'anouman'])
except subprocess.CalledProcessError:
anouman = False
self.assertFalse(anouman)
def test_3_sdist(self):
subprocess.call(['python', 'setup.py', '--quiet', 'sdist'])
self.assertTrue(
os.path.isfile(
'dist/anouman-%(version)s.tar.gz' % {'version':VERSION}
)
)
def test_4_install_anouman(self):
subprocess.call(['pip', 'install', 'dist/anouman-%(version)s.tar.gz' % {'version':VERSION}, '--upgrade'])
try:
anouman = subprocess.check_output(['which', 'anouman'])
except subprocess.CalledProcessError:
anouman = False
self.assertTrue(anouman)
def test_5_create_anouman_package(self):
# Pre test cleanup
subprocess.call(['rm', '-rf', 'tmp/*'])
subprocess.call(['rm', '-rf', 'site1.com.tar.gz'])
#create empty virtualenv
subprocess.call(['virtualenv', 'site1'])
os.system("source site1/bin/activate")
# build anouman package
results = subprocess.check_output(['anouman', '--django-project', 'test/django/site1', '--domainname', 'site1.com'])
# Mv package to tmp directory
subprocess.call(['mv', 'site1.com.tar.gz', 'tmp/'])
self.assertTrue(True)
# clean up and remove this virtualenv
def test_6_check_package_contents(self):
os.chdir('tmp/')
# unpack the package and check basic directory structure.
subprocess.call(['tar', 'xvfz', 'site1.com.tar.gz'])
dir_contents = os.listdir("site1.com")
self.assertEqual(
['pip_packages.txt', 'src'],
dir_contents
)
self.assertTrue(
os.path.isfile("site1.com/src/manage.py")
)
self.assertTrue(
os.path.isfile("site1.com/src/site1/settings.py")
)
self.assertTrue(
os.path.isfile("site1.com/src/site1/wsgi.py")
)
class TestVagrant(unittest.TestCase):
def setUp(self):
"""
There were a lot of directory changes going on.
It was easier to alway's return to a common root directory
"""
# return to the rool level directory
try:
os.chdir(os.path.dirname(__file__))
except:
os.chdir("/Users/jfurr/anouman/")
def connect_to_s1(self, transport=False):
"""
return a connection to server "s1"
This is a vagrant box. that expects to have already
had provisioning from bootstrap.sh done on it.
"""
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(
hostname=VM1,
username='anouman',
password='anouman',
timeout=5
)
stdin, stdout, stderr = client.exec_command('hostname')
return client
def scp(self, remotepath, localpath):
"""
scp a file to remote server s1.
"""
#TODO: Remove the hard coded scp call. In fact why not move this
# out of the test suite and make it a general purpose call.
transport = paramiko.Transport((VM1, 22))
transport.connect(
username='anouman',
password='anouman'
)
sftp = paramiko.SFTPClient.from_transport(transport)
try:
sftp.put(remotepath, localpath)
except Exception as e:
print os.listdir("./")
raise Exception("YOU FAIL: ", e)
sftp.close()
transport.close()
def exec_s1(self, cmd):
ssh = self.connect_to_s1()
return self.exec_command(ssh, cmd)
def exec_command(self, ssh, cmd):
stdin, stdout, stderr = ssh.exec_command(cmd)
out=''
for line in stdout:
out = out + line
return out
def test_1_create_new_vagrant_box(self):
print "test_1_create_new_vagrant_box"
try:
shutil.rmtree("test/vm/site3")
print "REMOVED"
except OSError as e:
print "EXCEPTION: ", e
pass
os.mkdir("test/vm/site3")
os.chdir("test/vm/site3")
vagrant.save(path="./Vagrantfile", context={
'NAME':'site3',
'PUBLIC':True
})
vagrant_bootstrap.save(path="./bootstrap.sh", context={
'NGINX':True,
'MYSQL':True,
})
clean.save(path="./clean.sh", context={
'DOMAINNAME':'site3.com'
})
print "YOU NEED TO ACTUALLY CHECK SOMETHIGN FOR THIS TO BE ATEST"
def test_2_bring_vagrant_up(self):
base_dir = os.path.dirname(__file__)
os.chdir(os.path.join(base_dir, "test/vm/site1"))
"""
Frist we try to connect to see if the vm is already up.
If that fails we then call vagrant up and try to connect
again after that finishes.
"""
try:
ssh = self.connect_to_s1()
except Exception as e:
print "\nCONNECTION FAILED. \nTrying to bring vagrant up...."
subprocess.call(['vagrant', 'up'])
print "Now retrying ssh connect"
ssh = self.connect_to_s1()
stdout = self.exec_command(ssh, 'hostname')
if 'precise64' not in stdout:
self.assertFalse("Incorrect hostname....so no this test fails")
def test_3_clean_server(self):
out = self.exec_s1("/usr/bin/yes | sh /vagrant/clean.sh")
# Confirm that the home directory has been cleaned out
out = self.exec_s1("cd /home/anouman; ls")
self.assertEqual(out.strip(), '')
# Make sure anouman has been uninstalled
out = self.exec_s1("anouman")
self.assertEqual(out, '')
# TODO There are probably several other checks could do here
def test_4_scp_anouman_to_s1(self):
# copy anouman to server
self.scp(
"dist/anouman-%(version)s.tar.gz" % {'version':VERSION},
"/home/anouman/anouman-0.0.4.0.tar.gz"
)
out = self.exec_s1("cd /home/anouman; ls")
self.assertEqual(
"anouman-%(version)s.tar.gz" % {'version':VERSION},
out.strip()
)
def test_5_install_anouman_with_pip(self):
out = self.exec_s1("sudo pip install /home/anouman/anouman-%(version)s.tar.gz --upgrade" % {'version':VERSION})
# Check that anouman is installed
out = self.exec_s1("anouman")
self.assertEqual(out.strip(), "anouman")
def test_6_scp_package_to_s1(self):
self.scp(
"tmp/site1.com.tar.gz",
"/home/anouman/site1.com.tar.gz"
)
out = self.exec_s1("cd /home/anouman; ls")
self.assertEqual(
"anouman-%(version)s.tar.gz\nsite1.com.tar.gz" % {'version':VERSION},
out.strip()
)
def test_7_install_site1(self):
print "cd /home/anouman; /usr/bin/yes | anouman --deploy site1.com.tar.gz"
out = self.exec_s1("cd /home/anouman; /usr/bin/yes | anouman --deploy site1.com.tar.gz")
print "out: ", out