~coreygoldberg/uci-engine/subunit-results

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
# Ubuntu CI Engine
# Copyright 2014 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals


import os
import subprocess
import unittest


from uciconfig import options
from ucivms.tests import fixtures as vms_fixtures


from ci_utils import unit_config
from ci_utils.testing import (
    features,
)
from tstrun import (
    testbed,
    tests,
)


@features.requires(tests.nova_creds)
@features.requires(features.nova_compute)
class TestTestbed(unittest.TestCase):

    def setUp(self):
        super(TestTestbed, self).setUp()
        vms_fixtures.isolate_from_disk(self)
        self.tb_name = 'testing-testbed'
        # Prepare a suitable config, importing the nova credentials
        conf = testbed.vms_config_from_auth_config(
            self.tb_name, unit_config.get_auth_config())
        # Default to precise
        conf.set('vm.release', 'precise')
        # Avoid triggering the 'atexit' hook as the config files are long gone
        # at that point.
        self.addCleanup(conf.store.save_changes)
        self.conf = conf
        os.makedirs(self.conf.get('vm.vms_dir'))

    def get_image_id(self, release='precise'):
        # The following images are "known" to work when they are added while
        # testing manually. In general, they need to be updated because they
        # disappear to be replaced by newer ones. They also vary depending on
        # whether canonistack or hpcloud is used.
        if unit_config.is_hpcloud(self.conf.get('os.auth_url')):
            test_images = dict(
                precise=('Ubuntu 12.04.4 LTS (amd64 20140613)'
                         ' - CI Engineering'),
                saucy=('Ubuntu Server 13.10 (amd64 20140409.1)'
                       ' - Partner Image'),
            )
        else:
            test_images = dict(
                precise=('ubuntu-released/ubuntu-precise-12.04'
                         '-amd64-server-20140428-disk1.img'),
                saucy=('ubuntu-released/ubuntu-saucy-13.10'
                       '-amd64-server-20140212-disk1.img'),
            )
        return test_images[release]

    def test_create_no_image(self):
        tb = testbed.TestBed(self.conf)
        with self.assertRaises(options.errors.OptionMandatoryValueError) as cm:
            tb.setup()
        self.assertEqual('vm.image must be set.', unicode(cm.exception))

    def test_create_unknown(self):
        tb = testbed.TestBed(self.conf)
        image_name = "I don't exist and eat kittens"
        self.conf.set('vm.image', image_name)
        with self.assertRaises(testbed.TestBedException) as cm:
            tb.setup()
        self.assertEqual('Image "{}" cannot be found'.format(image_name),
                         unicode(cm.exception))

    def test_existing_home_ssh(self):
        # The first request for the worker requires creating ~/.ssh if it
        # doesn't exist, but it may happen that this directory already exists
        # (see http://pad.lv/1334146).
        tb = testbed.TestBed(self.conf)
        ssh_home = os.path.expanduser('~/sshkeys')
        os.mkdir(ssh_home)
        self.conf.set('vm.ssh_key_path', os.path.join(ssh_home, 'id_rsa'))
        tb.ensure_ssh_key_is_available()
        self.assertTrue(os.path.exists(ssh_home))
        self.assertTrue(os.path.exists(os.path.join(ssh_home, 'id_rsa')))
        self.assertTrue(os.path.exists(os.path.join(ssh_home, 'id_rsa.pub')))

    def test_create_new_ssh_key(self):
        tb = testbed.TestBed(self.conf)
        self.conf.set('vm.image', self.get_image_id())
        # We use a '~' path to cover proper uci-vms user expansion
        self.conf.set('vm.ssh_key_path', '~/sshkeys/id_rsa')
        tb.ensure_ssh_key_is_available()
        self.assertTrue(os.path.exists(os.path.expanduser('~/sshkeys/id_rsa')))
        self.assertTrue(
            os.path.exists(os.path.expanduser('~/sshkeys/id_rsa.pub')))

    def test_create_usable_testbed(self):
        tb = testbed.TestBed(self.conf)
        self.conf.set('vm.release', 'saucy')
        self.conf.set('vm.image', self.get_image_id('saucy'))
        self.addCleanup(tb.teardown)
        tb.setup()
        # We should be able to ssh with the right user
        proc, out, err = tb.ssh('whoami',
                                out=subprocess.PIPE, err=subprocess.PIPE)
        self.assertEqual(0, proc.returncode)
        self.assertEqual('ubuntu\n', out)

    def test_apt_get_update_retries(self):
        tb = testbed.TestBed(self.conf)
        self.conf.set('vm.image', self.get_image_id())
        self.conf.set('vm.apt_get.update.timeouts', '0.1, 0.1')
        self.nb_calls = 0

        class Proc(object):
            returncode = 0

        def failing_update():
            self.nb_calls += 1
            if self.nb_calls > 1:
                return Proc(), 'stdout success', 'stderr success'
            else:
                # Fake a failed apt-get update
                proc = Proc()
                proc.returncode = 1
                return proc, 'stdout error', 'stderr error'

        tb.apt_get_update = failing_update
        tb.safe_apt_get_update()
        self.assertEqual(2, self.nb_calls)

    def test_apt_get_update_fails(self):
        tb = testbed.TestBed(self.conf)
        self.conf.set('vm.image', self.get_image_id())
        self.conf.set('vm.apt_get.update.timeouts', '0.1, 0.1, 0.1')

        def failing_update():
            class Proc(object):
                pass

            proc = Proc()
            proc.returncode = 1
            return proc, 'stdout', 'stderr'

        tb.apt_get_update = failing_update
        with self.assertRaises(testbed.TestBedException) as cm:
            tb.safe_apt_get_update()
        self.assertEqual('apt-get update never succeeded',
                         unicode(cm.exception))