~jamesodhunt/upstart/test-quiesce-cleanup

« back to all changes in this revision

Viewing changes to scripts/tests/test_pyupstart_system_init.py

  • Committer: Scott James Remnant
  • Date: 2009-07-08 19:43:16 UTC
  • Revision ID: scott@netsplit.com-20090708194316-t6rw4e8auuza6qju
* conf/control-alt-delete.conf: Default job for Control-Alt-Delete
* conf/rc-sysinit.conf: Default job for system initialisation
* conf/rc.conf: A fully wacky instance job that runs the rc script
for runlevel changes
* conf/rcS.conf: And a job for single-user-mode, which calls back
to rc-sysinit
* conf/Makefile.am (dist_init_DATA): Install the default files
into the /etc/init directory
* configure.ac (AC_CONFIG_FILES): Create conf/Makefile
* Makefile.am (SUBDIRS): Recurse into the conf directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python3
2
 
# -*- coding: utf-8 -*-
3
 
#---------------------------------------------------------------------
4
 
# Copyright © 2013 Canonical Ltd.
5
 
#
6
 
# Author: James Hunt <james.hunt@canonical.com>
7
 
#
8
 
# This program is free software; you can redistribute it and/or modify
9
 
# it under the terms of the GNU General Public License version 2, as
10
 
# published by the Free Software Foundation.
11
 
#
12
 
# This program is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
# GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License along
18
 
# with this program; if not, write to the Free Software Foundation, Inc.,
19
 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 
#---------------------------------------------------------------------
21
 
 
22
 
#---------------------------------------------------------------------
23
 
# Description: System-level Upstart tests for the pyupstart module.
24
 
#
25
 
# Notes: Can only be run as the root user.
26
 
#---------------------------------------------------------------------
27
 
 
28
 
import os
29
 
import sys
30
 
 
31
 
base_dir = os.path.abspath(os.path.dirname(__file__))
32
 
module_dir = os.path.normpath(os.path.realpath(base_dir + os.sep + '..'))
33
 
 
34
 
# top-level unpacked source directory
35
 
top_srcdir = os.path.normpath(os.path.realpath(module_dir + os.sep + '..'))
36
 
 
37
 
# Tell Python where the uninstalled module lives in the source tree
38
 
sys.path.append(module_dir)
39
 
from pyupstart import *
40
 
 
41
 
import unittest
42
 
 
43
 
class TestSystemUpstart(unittest.TestCase):
44
 
    def setUp(self):
45
 
        if os.geteuid():
46
 
            raise unittest.SkipTest('Need root for System-level Upstart tests')
47
 
 
48
 
        # Tests must not operate within a session
49
 
        self.assertEqual(None, os.environ.get('UPSTART_SESSION', None))
50
 
 
51
 
        self.upstart = SystemInit()
52
 
 
53
 
    def tearDown(self):
54
 
        # Ensure no state file exists
55
 
        state_file = '{}{}{}'.format(SYSTEM_LOG_DIR, os.sep, UPSTART_STATE_FILE)
56
 
        self.assertFalse(os.path.exists(state_file))
57
 
 
58
 
class TestSystemInitReExec(TestSystemUpstart):
59
 
 
60
 
    def test_pid1_reexec(self):
61
 
      version = self.upstart.version()
62
 
      self.assertTrue(version)
63
 
 
64
 
      # create a job and start it, marking it such that the .conf file
65
 
      # will be retained when object becomes unusable (after re-exec).
66
 
      job = self.upstart.job_create('sleeper', 'exec sleep 123')
67
 
      self.assertTrue(job)
68
 
 
69
 
      inst = job.start()
70
 
      self.assertTrue(inst)
71
 
      pids = job.pids()
72
 
      self.assertEqual(len(pids), 1)
73
 
      pid = pids['main']
74
 
 
75
 
      self.upstart.reexec()
76
 
 
77
 
      # PID 1 Upstart is now in the process of starting, but we need to
78
 
      # reconnect to it via D-Bus since it cannot yet retain client
79
 
      # connections. However, since the re-exec won't be instantaneous,
80
 
      # try a few times.
81
 
      self.upstart.polling_connect(force=True)
82
 
 
83
 
      # check that we can still operate on the re-exec'd Upstart
84
 
      version_postexec = self.upstart.version()
85
 
      self.assertTrue(version_postexec)
86
 
      self.assertEqual(version, version_postexec)
87
 
 
88
 
      # Ensure the job is still running with the same PID
89
 
      os.kill(pid, 0)
90
 
 
91
 
      self.assertTrue(job.running('_'))
92
 
 
93
 
      pids = job.pids()
94
 
      self.assertEqual(len(pids), 1)
95
 
      self.assertTrue(pids['main'])
96
 
 
97
 
      # Ensure pid remains the same
98
 
      self.assertEqual(pid, pids['main'])
99
 
 
100
 
      # Exceptions will be caught by the unittest framework
101
 
      inst.stop()
102
 
 
103
 
      job.start()
104
 
 
105
 
      pids = job.pids()
106
 
      self.assertEqual(len(pids), 1)
107
 
      self.assertTrue(pids['main'])
108
 
 
109
 
      os.kill(pids['main'], 0)
110
 
      self.assertTrue(job.running('_'))
111
 
 
112
 
      # The pid should have changed after a restart
113
 
      self.assertNotEqual(pid, pids['main'])
114
 
 
115
 
      job.stop()
116
 
 
117
 
      # Clean up
118
 
      self.upstart.destroy()
119
 
 
120
 
class TestSystemInitChrootSession(TestSystemUpstart):
121
 
    CHROOT_ENVVAR = 'UPSTART_TEST_CHROOT_PATH'
122
 
 
123
 
    def test_chroot_session_reexec(self):
124
 
        chroot_path = os.environ.get(self.CHROOT_ENVVAR, None)
125
 
 
126
 
        if not chroot_path:
127
 
            raise unittest.SkipTest('{} variable not set'.format(self.CHROOT_ENVVAR))
128
 
 
129
 
        # Ensure the chroot exists
130
 
        self.assertTrue(os.path.exists(chroot_path))
131
 
 
132
 
        # Ensure Upstart is installed in the chroot
133
 
        chroot_initctl = '{}{}{}'.format(chroot_path, os.sep, INITCTL)
134
 
        self.assertTrue(os.path.exists(chroot_initctl))
135
 
 
136
 
        # No sessions should exist before the test starts
137
 
        self.assertFalse(self.upstart.sessions_exist())
138
 
 
139
 
        # Create an Upstart chroot session by talking from the chroot
140
 
        # back to PID 1.
141
 
        ret = subprocess.call(['chroot', chroot_path, INITCTL, 'list'])
142
 
        self.assertEqual(0, ret)
143
 
 
144
 
        # Ensure a session now exists
145
 
        self.assertTrue(self.upstart.sessions_exist())
146
 
 
147
 
        # Restart
148
 
        self.upstart.reexec()
149
 
 
150
 
        # Ensure Upstart responds
151
 
        self.upstart.polling_connect(force=True)
152
 
        self.assertTrue(self.upstart.version())
153
 
 
154
 
def main():
155
 
    kwargs = {}
156
 
    format =             \
157
 
        '%(asctime)s:'   \
158
 
        '%(filename)s:'  \
159
 
        '%(name)s:'      \
160
 
        '%(funcName)s:'  \
161
 
        '%(levelname)s:' \
162
 
        '%(message)s'
163
 
 
164
 
    kwargs['format'] = format
165
 
 
166
 
    # We want to see what's happening
167
 
    kwargs['level'] = logging.DEBUG
168
 
 
169
 
    logging.basicConfig(**kwargs)
170
 
 
171
 
    unittest.main(
172
 
        testRunner=unittest.TextTestRunner(
173
 
            stream=sys.stdout,
174
 
            verbosity=2
175
 
        )
176
 
    )
177
 
 
178
 
    sys.exit(0)
179
 
 
180
 
if __name__ == '__main__':
181
 
    main()