1
from cStringIO import StringIO
7
from charmworld.scripts.remove_server_start_time import (
8
remove_server_start_time_entries,
10
from charmworld.testing import MongoTestBase
11
from charmworld.utils import (
13
record_server_start_time,
18
class RemoveServerStartTimeScriptTests(MongoTestBase):
20
def test_record_server_start_time(self):
21
# record_server_start_time() records the hostname and the current
22
# time in a MongoDB record.
23
def make_uname(hostname):
25
return (None, hostname)
28
with mock.patch.object(
29
os, 'uname', new_callable=make_uname,
32
record_server_start_time(self.db)
34
foo_start_time = self.db.server_status.find_one(SERVER_STARTED)['foo']
35
self.assertTrue(before <= foo_start_time <= after)
37
# Updating a server's start time retains already recorded data.
38
with mock.patch.object(
39
os, 'uname', new_callable=make_uname,
41
record_server_start_time(self.db)
42
recorded = self.db.server_status.find_one(SERVER_STARTED)
43
self.assertEqual(set(('_id', 'foo', 'bar')), set(recorded))
45
# Existing records are updated.
46
with mock.patch.object(
47
os, 'uname', new_callable=make_uname,
50
record_server_start_time(self.db)
52
recorded = self.db.server_status.find_one(SERVER_STARTED)
53
self.assertTrue(recorded['foo'] > foo_start_time)
55
def test_remove_server_start_time_entries(self):
56
# Each time a server starts it stores the current time in the DB.
57
# Stale entries can be removed by calling
58
# remove_server_start_time_entries() (used in the script
59
# remove-start-time-entry).
60
self.db.server_status.insert({
61
'_id': SERVER_STARTED,
67
0, remove_server_start_time_entries(['', 'foo'], output))
68
start_times = self.db.server_status.find_one({'_id': SERVER_STARTED})
71
'_id': SERVER_STARTED,
75
self.assertEqual('', output.getvalue())
77
def test_remove_server_start_time_entries_no_args(self):
78
# If the server start time script is run without arguments it
79
# generates an error message.
82
1, remove_server_start_time_entries([''], output))
84
'At least one server hostname must be specified.\n',
87
def test_remove_server_start_time_entries_no_db_record(self):
88
# If there are no server start times yet and the script is run, it
92
1, remove_server_start_time_entries(['', 'foo'], output))
94
'No server start times are yet recorded.\n', output.getvalue())
96
def test_remove_server_start_time_entries_invalid_server_name(self):
97
# If the script is asked to remove a server start time that does not
98
# exist, it generates an informative message.
99
self.db.server_status.insert({
100
'_id': SERVER_STARTED,
105
1, remove_server_start_time_entries(['', 'bar'], output))
107
'hostname bar not found.\n', output.getvalue())
109
def test_entry_point_exists(self):
110
# The entry point is wrapped in a script by setuptools. We want to be
111
# sure the entrypoint actually exists.
112
setup_py = open(os.path.join(project_root, 'setup.py')).read()
114
'remove-start-time-entry = '
115
'charmworld.scripts.remove_server_start_time:entry_point\n')
116
self.assertIn(script_config, setup_py)