This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
leak_vm_tests.py
111 lines (89 loc) · 3.34 KB
/
leak_vm_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
import tempfile
import unittest
import uuid
from cloudinitd.cb_iaas import iaas_get_con
from cloudinitd.user_api import CloudInitD
import os
import cloudinitd
import cloudinitd.cli.boot
import time
class LeakRepairTests(unittest.TestCase):
def setUp(self):
self.bkfab = None
self.bkssh = None
self.plan_basedir = cloudinitd.nosetests.g_plans_dir
if 'CLOUDINITD_TESTENV' in os.environ:
self.bkfab = os.environ['CLOUDINITD_FAB']
self.bkssh = os.environ['CLOUDINITD_SSH']
def tearDown(self):
if self.bkfab:
os.environ['CLOUDINITD_FAB'] = self.bkfab
os.environ['CLOUDINITD_SSH'] = self.bkssh
cloudinitd.close_log_handlers()
def _find_str(self, filename, needle):
file = open(filename, "r")
try:
found = False
while not found:
line = file.readline()
if not line:
return None
ndx = line.find(needle)
if ndx >= 0:
return line
finally:
file.close()
def _dump_output(self, filename):
file = open(filename, "r")
print file.readlines()
file.close()
def test_repair_leaks(self):
if 'CLOUDINITD_TESTENV' in os.environ:
#this wont work in fake mode
return
key = None
secret = None
url= None
try:
key = os.environ['CLOUDINITD_IAAS_ACCESS_KEY']
secret = os.environ['CLOUDINITD_IAAS_SECRET_KEY']
url = os.environ['CLOUDINITD_IAAS_URL']
except:
pass
# XXX this test may fail for nimbus
con = cloudinitd.cb_iaas.iaas_get_con(None, key=key, secret=secret, iaasurl=url)
i_list = con.get_all_instances()
initial_list = [i for i in i_list if i.get_state() == "running"]
(osf, outfile) = tempfile.mkstemp()
os.close(osf)
print "booting the bad plan"
rc = cloudinitd.cli.boot.main(["-O", outfile, "-v", "-v", "-v", "boot", "%s/reloadplan/badtop.conf" % (self.plan_basedir)])
self._dump_output(outfile)
n = "Starting up run"
line = self._find_str(outfile, n)
self.assertNotEqual(line, None)
runname = line[len(n):].strip()
print "run name is %s" % (runname)
self.assertNotEqual(rc, 0)
print "repair bad plan"
rc = cloudinitd.cli.boot.main(["repair", runname])
self.assertNotEqual(rc, 0)
print "reload a good plan"
rc = cloudinitd.cli.boot.main(["--name", runname, "reload", "%s/reloadplan/goodtop.conf" % (self.plan_basedir)])
self.assertEqual(rc, 0)
print "repair the good plan"
rc = cloudinitd.cli.boot.main(["repair", runname])
self.assertEqual(rc, 0)
print "terminate"
rc = cloudinitd.cli.boot.main(["terminate", runname])
self.assertEqual(rc, 0)
p_list = con.get_all_instances()
post_list = [i for i in p_list if i.get_state() == "running"]
self.assertEqual(len(post_list), len(initial_list))
def _get_runname(self, fname):
n = "Starting up run"
line = self._find_str(fname, n)
self.assertNotEqual(line, None)
runname = line[len(n):].strip()
print "run name is %s" % (runname)
return runname