summaryrefslogtreecommitdiff
path: root/openstack/tester
blob: e4c57cd4ab33b3abc9b6c10110f1678e4763baee (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python
#
# Copyright 2014 Codethink Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

'''release-test

This script deploys the set of systems in the cluster morphology it is
instructed to read, to test that they work correctly.

'''

import cliapp
import os
import pipes
import shlex
import shutil
import socket
import tempfile
import time
import uuid

from novaclient import client

class TimeoutError(cliapp.AppException):

    """Error to be raised when a connection waits too long"""

    def __init__(self, msg):
        super(TimeoutError, self).__init__(msg)


class VMHost(object):

    def __init__(self, user, address):
        self.user = user
        self.address = address

    @property
    def ssh_host(self):
        return '{user}@{address}'.format(user=self.user, address=self.address)

    def runcmd(self, *args, **kwargs):
        cliapp.ssh_runcmd(self.ssh_host, *args, **kwargs)


class DeployedSystemInstance(object):

    def __init__(self, deployment, host_machine, ip_addr, hostname):
        self.deployment = deployment
        self.ip_address = ip_addr
        self.host_machine = host_machine
        self.hostname = hostname

    @property
    def ssh_host(self):
        # TODO: Stop assuming we ssh into test instances as root
        return 'root@{host}'.format(host=self.ip_address)

    def runcmd(self, argv, chdir='.', **kwargs):
        ssh_cmd = ['ssh', '-o', 'StrictHostKeyChecking=no',
                   '-o', 'UserKnownHostsFile=/dev/null', self.ssh_host]
        cmd = ['sh', '-c', 'cd "$1" && shift && exec "$@"', '-', chdir]
        cmd += argv
        ssh_cmd.append(' '.join(map(pipes.quote, cmd)))
        return cliapp.runcmd(ssh_cmd, **kwargs)

    def _wait_for_dhcp(self, timeout):
        '''Block until given hostname resolves successfully.

        Raises TimeoutError if the hostname has not appeared in 'timeout'
        seconds.

        '''
        start_time = time.time()
        while True:
            try:
                socket.gethostbyname(self.ip_address)
                return
            except socket.gaierror:
                pass
            if time.time() > start_time + timeout:
                raise TimeoutError("Host %s did not appear after %i seconds" %
                                   (self.ip_address, timeout))
            time.sleep(0.5)

    def _wait_for_ssh(self, timeout):
        """Wait until the deployed VM is responding via SSH"""
        start_time = time.time()
        while True:
            try:
                self.runcmd(['true'], stdin=None, stdout=None, stderr=None)
                return
            except cliapp.AppException:
                # TODO: Stop assuming the ssh part of the command is what failed
                if time.time() > start_time + timeout:
                    raise TimeoutError("%s sshd did not start after %i seconds"
                                       % (self.ip_address, timeout))
                time.sleep(0.5)

    def _wait_for_cloud_init(self, timeout):
        """Wait until cloud init has resized the disc"""
        start_time = time.time()
        while True:
            try:
                out = self.runcmd(['sh', '-c',
                              'test -e "$1" && echo exists || echo does not exist',
                              '-',
                              '/root/cloud-init-finished'])
            except:
                import traceback
                traceback.print_exc()
                raise
            if out.strip() == 'exists':
                return
            if time.time() > start_time + timeout:
                raise TimeoutError("Disc size not increased after %i seconds"
                                   % (timeout))
            time.sleep(3)

    def wait_until_online(self, timeout=120):
        self._wait_for_dhcp(timeout)
        self._wait_for_ssh(timeout)
        #self._wait_for_cloud_init(timeout)
        print "Test system %s ready to run tests." % (self.hostname)

    def delete(self):
        # Stop and remove VM
        print "Deleting %s test instance" % (self.hostname)
        try:
            cliapp.runcmd(['nova', 'delete', self.hostname])
        except cliapp.AppException as e:
            # TODO: Stop assuming that delete failed because the instance
            #       wasn't running
            print "- Failed"
            pass
        print "Deleting %s test disc image" % (self.hostname)
        try:
            cliapp.runcmd(['nova', 'image-delete', self.hostname])
        except cliapp.AppException as e:
            # TODO: Stop assuming that image-delete failed because it was
            #       already removed
            print "- Failed"
            pass


class Deployment(object):

    def __init__(self, host_machine, net_id, image_file, flavour):
        self.host_machine = host_machine
        self.net_id = net_id
        self.image_file = image_file
        self.flavour = flavour

    @staticmethod
    def _ssh_host_key_exists(hostname):
        """Check if an ssh host key exists in known_hosts"""
        filename = os.path.expanduser('~/.ssh/known_hosts')
        if not os.path.exists(filename):
            return False
        with open(filename, 'r') as known_hosts:
            return any(line.startswith(hostname) for line in known_hosts)

    def _update_known_hosts(self):
        if not self._ssh_host_key_exists(self.host_machine.address):
            filename = os.path.expanduser('~/.ssh/known_hosts')
            print("No known_hosts: ensuring path");
            if not os.path.exists(os.path.dirname(filename)):
                os.makedirs(os.path.dirname(filename))
            with open(filename, 'a') as known_hosts:
                print("Generating known_hosts");
                cliapp.runcmd(['ssh-keyscan', self.host_machine.address],
                              stdout=known_hosts)

    def deploy(self):
        self._update_known_hosts()

        hostname = str(uuid.uuid4())

        # Deploy the image to openstack
        # TODO: Ensure this is cleaned up when something raises an exception
        args = ['glance', 'image-create',
                '--name', hostname,
                '--disk-format', 'raw',
                '--container-format', 'bare',
                '--file', self.image_file]
        cliapp.runcmd(args, stdin=None, stdout=None, stderr=None)

        # Get a novaclient object
        nc = client.Client(2,
                           os.environ['OS_USERNAME'],
                           os.environ['OS_PASSWORD'],
                           os.environ['OS_TENANT_NAME'],
                           os.environ['OS_AUTH_URL'])

        # Boot an instance from the image
        # TODO: Ensure this is cleaned up when something raises an exception
        # TODO: use python-novaclient
        args = ['nova', 'boot',
                '--flavor', self.flavour,
                '--image', hostname,
                #'--user-data', '/usr/lib/mason/os-init-script',
                '--nic', "net-id=%s" % (self.net_id),
                hostname]
        output = cliapp.runcmd(args)

        # Print nova boot output, with adminPass line removed
        output_lines = output.split('\n')
        for line in output_lines:
            if line.find('adminPass') != -1:
                password_line = line
        output_lines.remove(password_line)
        output = '\n'.join(output_lines)
        print output

        # Sleep for a bit, or nova explodes when trying to assign IP address
        time.sleep(20)

        # Assign a floating IP address
        for retries in range(5, 0, -1):
            ip_list = nc.floating_ips.list()
            free_ip = None
            for ip in ip_list:
                if ip.instance_id == None:
                    free_ip = ip
                    break
            if free_ip != None:
                instance = nc.servers.find(name=hostname)
                # TODO: switch back to cli tool, as python
                #       approach gave error.
                instance.add_floating_ip(free_ip)
                ip_addr = free_ip.ip
                break
        else:
            raise cliapp.AppException('Could not get a floating IP')

        # Print the IP address
        print "IP address for instance %s: %s" % (hostname, ip_addr)

        return DeployedSystemInstance(self, self.host_machine,
                                      ip_addr, hostname)

class ReleaseApp(cliapp.Application):

    """Cliapp application which handles automatic builds and tests"""

    def add_settings(self):
        """Add the command line options needed"""
        group_main = 'Program Options'
        self.settings.string(['os-host'],
                              'HOST that VMs can be deployed to',
                              default=None,
                              group=group_main)
        self.settings.string(['net-id'],
                             'Openstack network ID',
                             default=None,
                             group=group_main)
        self.settings.string(['image-file'],
                             'Path to system image to test',
                             default=None,
                             group=group_main)
        self.settings.string(['flavour'],
                             'Name of flavour to use for instance',
                             default=None,
                             group=group_main)

    def run_tests(self, instance):
        instance.wait_until_online()

        tests = []

        def uname_test(instance):
            print('# uname test');
            instance.runcmd(['uname', '-a'], stdout=self.output)

        def python_smoke_test(instance):
            print('# python test')
            instance.runcmd(['python', '-c', 'print "Hello World"'],
                            stdout=self.output)

        # TODO: Come up with a better way of determining which tests to run
        tests.append(uname_test)
        tests.append(python_smoke_test)

        for test in tests:
            test(instance)

    def deploy_and_test_systems(self):
        """Run the deployments and tests"""

        # TODO: Don't assume root is the user we ssh to for tests
        host_machine = VMHost('root', self.settings['os-host'])

        deployment = Deployment(host_machine,
                                self.settings['net-id'],
                                self.settings['image-file'],
                                self.settings['flavour'])

        instance = deployment.deploy()
        try:
            self.run_tests(instance)
        finally:
            instance.delete()

    def process_args(self, args):
        """Process the command line args and kick off the builds/tests"""
        for setting in ('os-host', 'net-id', 'image-file', 'flavour'):
            self.settings.require(setting)

        if len(args) != 0:
            raise cliapp.AppException(
                'Usage: release-test-os --os-host <HOST> --net-id <ID> --image-file <PATH> --flavour <NAME>')
        self.deploy_and_test_systems()


if __name__ == '__main__':
    ReleaseApp().run()