~benji/charmworld/fix-generation-19

« back to all changes in this revision

Viewing changes to charmworld/scripts/tests/test_remove_server_start_time.py

  • Committer: Benji York
  • Date: 2013-11-15 16:58:32 UTC
  • mto: (461.3.1 remove-charm)
  • mto: This revision was merged to the branch mainline in revision 462.
  • Revision ID: benji@benjiyork.com-20131115165832-c43u4obyfzv9fope
extract the remove server script into a new scripts directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from cStringIO import StringIO
 
2
import os
 
3
import time
 
4
 
 
5
import mock
 
6
 
 
7
from charmworld.scripts.remove_server_start_time import (
 
8
    remove_server_start_time_entries,
 
9
)
 
10
from charmworld.testing import MongoTestBase
 
11
from charmworld.utils import (
 
12
    project_root,
 
13
    record_server_start_time,
 
14
    SERVER_STARTED,
 
15
)
 
16
 
 
17
 
 
18
class RemoveServerStartTimeScriptTests(MongoTestBase):
 
19
 
 
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):
 
24
            def uname():
 
25
                return (None, hostname)
 
26
            return uname
 
27
 
 
28
        with mock.patch.object(
 
29
                os, 'uname', new_callable=make_uname,
 
30
                hostname='foo'):
 
31
            before = time.time()
 
32
            record_server_start_time(self.db)
 
33
            after = time.time()
 
34
        foo_start_time = self.db.server_status.find_one(SERVER_STARTED)['foo']
 
35
        self.assertTrue(before <= foo_start_time <= after)
 
36
 
 
37
        # Updating a server's start time retains already recorded data.
 
38
        with mock.patch.object(
 
39
                os, 'uname', new_callable=make_uname,
 
40
                hostname='bar'):
 
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))
 
44
 
 
45
        # Existing records are updated.
 
46
        with mock.patch.object(
 
47
                os, 'uname', new_callable=make_uname,
 
48
                hostname='foo'):
 
49
            before = time.time()
 
50
            record_server_start_time(self.db)
 
51
            after = time.time()
 
52
        recorded = self.db.server_status.find_one(SERVER_STARTED)
 
53
        self.assertTrue(recorded['foo'] > foo_start_time)
 
54
 
 
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,
 
62
            'foo': 123.456,
 
63
            'bar': 234.567,
 
64
        })
 
65
        output = StringIO()
 
66
        self.assertEqual(
 
67
            0, remove_server_start_time_entries(['', 'foo'], output))
 
68
        start_times = self.db.server_status.find_one({'_id': SERVER_STARTED})
 
69
        self.assertEqual(
 
70
            {
 
71
                '_id': SERVER_STARTED,
 
72
                'bar': 234.567,
 
73
            },
 
74
            start_times)
 
75
        self.assertEqual('', output.getvalue())
 
76
 
 
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.
 
80
        output = StringIO()
 
81
        self.assertEqual(
 
82
            1, remove_server_start_time_entries([''], output))
 
83
        self.assertEqual(
 
84
            'At least one server hostname must be specified.\n',
 
85
            output.getvalue())
 
86
 
 
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
 
89
        # informs the user.
 
90
        output = StringIO()
 
91
        self.assertEqual(
 
92
            1, remove_server_start_time_entries(['', 'foo'], output))
 
93
        self.assertEqual(
 
94
            'No server start times are yet recorded.\n', output.getvalue())
 
95
 
 
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,
 
101
            'foo': 123.456,
 
102
        })
 
103
        output = StringIO()
 
104
        self.assertEqual(
 
105
            1, remove_server_start_time_entries(['', 'bar'], output))
 
106
        self.assertEqual(
 
107
            'hostname bar not found.\n', output.getvalue())
 
108
 
 
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()
 
113
        script_config = (
 
114
            'remove-start-time-entry = '
 
115
            'charmworld.scripts.remove_server_start_time:entry_point\n')
 
116
        self.assertIn(script_config, setup_py)