~jelmer/byoci/setuptools

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
# This file is part of Build Your Own CI
#
# Copyright 2018 Vincent Ladeuil
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

import io
import unittest

from byov import subprocesses
from byoci.host import config
from byoci.tests.host import (
    assertions,
    fixtures,
)


class TestMonitorConfig(unittest.TestCase):
    """Test master config."""

    def setUp(self):
        super().setUp()
        fixtures.isolate_from_disk_for_host(self)

    def test_defaults(self):
        conf = config.VmStack(None)
        self.assertEqual('selftest', conf.get('byoci'))
        self.assertEqual(self.monitor_name,
                         conf.expand_options('{{byoci}.monitor}'))
        self.assertEqual('/byoci',
                         conf.expand_options('{{byoci}.definition}'))


class TestMonitorSetup(unittest.TestCase):

    def setUp(self):
        super().setUp()
        fixtures.isolate_from_disk_for_host(self)
        fixtures.setup_byoci_conf(self)
        self.out = io.StringIO()
        self.err = io.StringIO()
        fixtures.override_logging(self)

    def test_setup_succeeds(self):
        fixtures.setup_master(self)
        fixtures.setup_monitor(self)
        master_conf = config.VmStack(self.master_name)
        monitor_conf = config.VmStack(self.monitor_name)
        # Ensure monitor knows where to talk to master
        self.assertEqual(master_conf.get('{byoci}.master.api'),
                         monitor_conf.get('{byoci}.master.api'))


class TestMonitorUpdateJobs(unittest.TestCase):

    def setUp(self):
        super().setUp()
        fixtures.isolate_from_disk_for_host(self)
        fixtures.setup_byoci_conf(self)
        self.out = io.StringIO()
        self.err = io.StringIO()
        fixtures.override_logging(self)

    def test_succeeds(self):
        fixtures.setup_master(self)
        fixtures.setup_monitor(self)
        test_cmd = 'cd /byoci && jenkins-jobs update jobs/'
        ret, out, err = subprocesses.run(
            ['byovm', 'shell', self.monitor_name, test_cmd])
        assertions.assertShellSuccess(self, test_cmd, ret, out, err)


class TestMonitorUpdatePlugins(unittest.TestCase):

    def setUp(self):
        super().setUp()
        fixtures.isolate_from_disk_for_host(self)
        fixtures.setup_byoci_conf(self)
        self.out = io.StringIO()
        self.err = io.StringIO()
        fixtures.override_logging(self)

    def test_succeeds(self):
        fixtures.setup_master(self)
        fixtures.setup_monitor(self)
        test_cmd = 'cd /byoci && ./byo-ci-monitor update-plugins'
        ret, out, err = subprocesses.run(
            ['byovm', 'shell', self.monitor_name, test_cmd])
        assertions.assertShellSuccess(self, test_cmd, ret, out, err)


class TestMonitorRunJob(unittest.TestCase):

    def setUp(self):
        super().setUp()
        fixtures.isolate_from_disk_for_host(self)
        fixtures.setup_byoci_conf(self)
        self.out = io.StringIO()
        self.err = io.StringIO()
        fixtures.override_logging(self)

    def test_succeeds(self):
        fixtures.setup_master(self)
        fixtures.setup_monitor(self)
        # Inject jobs first
        test_cmd = 'cd /byoci && jenkins-jobs update jobs/'
        ret, out, err = subprocesses.run(
            ['byovm', 'shell', self.monitor_name, test_cmd])
        assertions.assertShellSuccess(self, test_cmd, ret, out, err)
        # Now run a known one
        test_cmd = ('cd /byoci &&'
                    ' ./byo-ci-monitor run-job byoci/info-brz-slave-selftest')
        ret, out, err = subprocesses.run(
            ['byovm', 'shell', self.monitor_name, test_cmd])
        assertions.assertShellSuccess(self, test_cmd, ret, out, err)
        # The job is waiting for a slave (not provisioned here)
        # FIXME: If/when jenkins handling is rewritten as requests-based
        # module, more assertions could be made. -- vila 2018-11-23

    def test_fails(self):
        fixtures.setup_master(self)
        fixtures.setup_monitor(self)
        with self.assertRaises(subprocesses.errors.CommandError) as cm:
            test_cmd = ('cd /byoci &&'
                        ' ./byo-ci-monitor run-job I-dont-exist')
            subprocesses.run(['byovm', 'shell', self.monitor_name, test_cmd])
        self.assertTrue(
            cm.exception.err.endswith('No job matches I-dont-exist\n'))