aboutsummaryrefslogtreecommitdiff
path: root/lava_dispatcher/test/test_vland.py
blob: c30e9da1e92fb83b46778eaa109b3f591ec29b6d (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
330
331
332
333
334
335
336
337
338
339
340
# Copyright (C) 2015 Linaro Limited
#
# Author: Neil Williams <neil.williams@linaro.org>
#
# This file is part of LAVA Dispatcher.
#
# LAVA Dispatcher 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; either version 2 of the License, or
# (at your option) any later version.
#
# LAVA Dispatcher 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, see <http://www.gnu.org/licenses>.


import os
import yaml
import socket
from lava_dispatcher.device import NewDevice
from lava_dispatcher.parser import JobParser
from lava_dispatcher.action import JobError
from lava_dispatcher.connection import Protocol
from lava_dispatcher.protocols.vland import VlandProtocol
from lava_dispatcher.protocols.multinode import MultinodeProtocol
from lava_dispatcher.test.test_basic import StdoutTestCase, Factory
from lava_dispatcher.test.utils import DummyLogger

# pylint: disable=superfluous-parens


class TestVland(StdoutTestCase):  # pylint: disable=too-many-public-methods

    def setUp(self):
        super().setUp()
        self.filename = os.path.join(os.path.dirname(__file__), 'sample_jobs/bbb-group-vland-alpha.yaml')
        self.beta_filename = os.path.join(os.path.dirname(__file__), 'sample_jobs/bbb-group-vland-beta.yaml')
        self.factory = Factory()
        (rendered, _) = self.factory.create_device('bbb-01.jinja2')
        self.device = NewDevice(yaml.load(rendered))
        self.job_id = "100"

    def test_file_structure(self):
        with open(self.filename) as yaml_data:
            alpha_data = yaml.load(yaml_data)
        self.assertIn('protocols', alpha_data)
        self.assertTrue(VlandProtocol.accepts(alpha_data))
        level_tuple = Protocol.select_all(alpha_data)
        self.assertEqual(len(level_tuple), 2)
        self.assertEqual(
            VlandProtocol,
            [
                item[0] for item in sorted(level_tuple, key=lambda data: data[1])
            ][1]
        )
        vprotocol = VlandProtocol(alpha_data, self.job_id)
        self.assertIn(
            'arbit',
            vprotocol.base_group,
        )
        self.assertNotIn(
            'group',
            vprotocol.base_group,
        )
        vprotocol.set_up()
        self.assertIn('port', vprotocol.settings)
        self.assertIn('poll_delay', vprotocol.settings)
        self.assertIn('vland_hostname', vprotocol.settings)
        self.assertEqual(
            vprotocol.base_message,
            {
                "port": vprotocol.settings['port'],
                "poll_delay": vprotocol.settings["poll_delay"],
                "host": vprotocol.settings['vland_hostname'],
                "client_name": socket.gethostname(),
            }
        )
        for name in vprotocol.names:
            vlan = vprotocol.params[name]
            self.assertIn('tags', vlan)

    def test_device(self):
        self.assertIsNotNone(self.device)
        self.assertIn('eth0', self.device['parameters']['interfaces'])
        self.assertIn('eth1', self.device['parameters']['interfaces'])
        self.assertIn('sysfs', self.device['parameters']['interfaces']['eth0'])
        self.assertIn('mac', self.device['parameters']['interfaces']['eth0'])
        self.assertIn('switch', self.device['parameters']['interfaces']['eth0'])
        self.assertIn('port', self.device['parameters']['interfaces']['eth0'])
        self.assertIn('tags', self.device['parameters']['interfaces']['eth0'])
        self.assertIn('sysfs', self.device['parameters']['interfaces']['eth1'])
        self.assertIn('mac', self.device['parameters']['interfaces']['eth1'])
        self.assertIn('switch', self.device['parameters']['interfaces']['eth1'])
        self.assertIn('port', self.device['parameters']['interfaces']['eth1'])
        self.assertIn('tags', self.device['parameters']['interfaces']['eth1'])
        self.assertIsInstance(self.device['parameters']['interfaces']['eth1']['tags'], list)
        self.assertIsNone(self.device['parameters']['interfaces']['eth0']['tags'])
        csv_list = []
        for interface in self.device['parameters']['interfaces']:
            csv_list.extend(
                [
                    self.device['parameters']['interfaces'][interface]['sysfs'],
                    self.device['parameters']['interfaces'][interface]['mac'],
                    interface
                ]
            )
        self.assertEqual(
            set(csv_list),
            {
                '/sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/eth1', '00:24:d7:9b:c0:8c', 'eth1',
                '/sys/devices/pci0000:00/0000:00:19.0/net/eth0', 'f0:de:f1:46:8c:21', 'eth0'
            }
        )
        tag_list = []
        for interface in self.device['parameters']['interfaces']:
            if interface == 'eth0':
                continue
            for tag in self.device['parameters']['interfaces'][interface]['tags']:
                tag_list.extend([interface, tag])
        self.assertEqual(set(tag_list), {'RJ45', '100M', 'eth1', '10M'})

    def test_configure(self):
        with open(self.filename) as yaml_data:
            alpha_data = yaml.load(yaml_data)
        self.assertIn('protocols', alpha_data)
        self.assertTrue(VlandProtocol.accepts(alpha_data))
        vprotocol = VlandProtocol(alpha_data, self.job_id)
        vprotocol.set_up()
        with open(self.filename) as sample_job_data:
            parser = JobParser()
            job = parser.parse(sample_job_data, self.device, 4212, None, "")
        ret = vprotocol.configure(self.device, job)
        if not ret:
            print(vprotocol.errors)
        self.assertTrue(ret)
        nodes = {}
        for name in vprotocol.names:
            vlan = vprotocol.params[name]
            # self.assertNotIn('tags', vlan)
            uid = ' '.join([vlan['switch'], str(vlan['port'])])
            nodes[uid] = name
        self.assertEqual(len(nodes.keys()), len(vprotocol.names))
        self.assertIn('vlan_one', vprotocol.names)
        self.assertNotIn('vlan_two', vprotocol.names)
        self.assertIn('switch', vprotocol.params['vlan_one'])
        self.assertIn('port', vprotocol.params['vlan_one'])
        self.assertIsNotNone(vprotocol.multinode_protocol)

        (rendered, _) = self.factory.create_device('bbb-01.jinja2')
        bbb2 = NewDevice(yaml.load(rendered))
        bbb2['parameters']['interfaces']['eth0']['switch'] = '192.168.0.2'
        bbb2['parameters']['interfaces']['eth0']['port'] = '6'
        bbb2['parameters']['interfaces']['eth1']['switch'] = '192.168.0.2'
        bbb2['parameters']['interfaces']['eth1']['port'] = '4'
        self.assertEqual(
            vprotocol.params, {
                'vlan_one': {'switch': '192.168.0.2', 'iface': 'eth1', 'port': 7, 'tags': ['100M', 'RJ45', '10M']}
            }
        )
        # already configured the vland protocol in the same job
        self.assertTrue(vprotocol.configure(bbb2, job))
        self.assertEqual(
            vprotocol.params, {
                'vlan_one': {
                    'switch': '192.168.0.2', 'iface': 'eth1', 'port': 7, 'tags': ['100M', 'RJ45', '10M']}
            }
        )
        self.assertTrue(vprotocol.valid)
        self.assertEqual(vprotocol.names, {'vlan_one': '4212vlanone'})

    def test_job(self):
        with open(self.filename) as yaml_data:
            alpha_data = yaml.load(yaml_data)
        self.assertIn('protocols', alpha_data)
        self.assertIn(VlandProtocol.name, alpha_data['protocols'])
        with open(self.filename) as sample_job_data:
            parser = JobParser()
            job = parser.parse(sample_job_data, self.device, 4212, None, "")
        job.logger = DummyLogger()
        description_ref = self.pipeline_reference('bbb-group-vland-alpha.yaml', job=job)
        self.assertEqual(description_ref, job.pipeline.describe(False))
        job.validate()
        self.assertNotEqual([], [protocol.name for protocol in job.protocols if protocol.name == MultinodeProtocol.name])
        ret = {"message": {"kvm01": {"vlan_name": "name", "vlan_tag": 6}}, "response": "ack"}
        self.assertEqual(('name', 6), (ret['message']['kvm01']['vlan_name'], ret['message']['kvm01']['vlan_tag'],))
        self.assertIn('protocols', job.parameters)
        self.assertIn(VlandProtocol.name, job.parameters['protocols'])
        self.assertIn(MultinodeProtocol.name, job.parameters['protocols'])
        vprotocol = [vprotocol for vprotocol in job.protocols if vprotocol.name == VlandProtocol.name][0]
        self.assertTrue(vprotocol.valid)
        self.assertEqual(vprotocol.names, {'vlan_one': '4212vlanone'})
        self.assertFalse(vprotocol.check_timeout(120, {'request': 'no call'}))
        self.assertRaises(JobError, vprotocol.check_timeout, 60, 'deploy_vlans')
        self.assertRaises(JobError, vprotocol.check_timeout, 60, {'request': 'deploy_vlans'})
        self.assertTrue(vprotocol.check_timeout(120, {'request': 'deploy_vlans'}))
        for vlan_name in job.parameters['protocols'][VlandProtocol.name]:
            if vlan_name == 'yaml_line':
                continue
            self.assertIn(vlan_name, vprotocol.params)
            self.assertIn('switch', vprotocol.params[vlan_name])
            self.assertIn('port', vprotocol.params[vlan_name])
            self.assertIn('iface', vprotocol.params[vlan_name])
        params = job.parameters['protocols'][vprotocol.name]
        names = []
        for key, _ in params.items():
            if key == 'yaml_line':
                continue
            names.append(",".join([key, vprotocol.params[key]['iface']]))
        # this device only has one interface with interface tags
        self.assertEqual(names, ['vlan_one,eth1'])

    def test_vland_overlay(self):
        with open(self.filename) as yaml_data:
            alpha_data = yaml.load(yaml_data)
        for vlan_key, _ in alpha_data['protocols'][VlandProtocol.name].items():
            alpha_data['protocols'][VlandProtocol.name][vlan_key] = {'tags': []}
        # removed tags from original job to simulate job where any interface tags will be acceptable
        self.assertEqual(
            alpha_data['protocols'][VlandProtocol.name],
            {'vlan_one': {'tags': []}}
        )
        parser = JobParser()
        job = parser.parse(yaml.dump(alpha_data), self.device, 4212, None, "")
        job.logger = DummyLogger()
        job.validate()
        tftp_deploy = [action for action in job.pipeline.actions if action.name == 'tftp-deploy'][0]
        prepare = [action for action in tftp_deploy.internal_pipeline.actions if action.name == 'prepare-tftp-overlay'][0]
        overlay = [action for action in prepare.internal_pipeline.actions if action.name == 'lava-overlay'][0]
        vland = [action for action in overlay.internal_pipeline.actions if action.name == 'lava-vland-overlay'][0]
        self.assertTrue(os.path.exists(vland.lava_vland_test_dir))
        vland_files = os.listdir(vland.lava_vland_test_dir)
        self.assertIn('lava-vland-names', vland_files)
        self.assertIn('lava-vland-tags', vland_files)
        self.assertIn('lava-vland-self', vland_files)

    def test_job_no_tags(self):
        with open(self.filename) as yaml_data:
            alpha_data = yaml.load(yaml_data)
        for vlan_key, _ in alpha_data['protocols'][VlandProtocol.name].items():
            alpha_data['protocols'][VlandProtocol.name][vlan_key] = {'tags': []}
        # removed tags from original job to simulate job where any interface tags will be acceptable
        self.assertEqual(
            alpha_data['protocols'][VlandProtocol.name],
            {'vlan_one': {'tags': []}}
        )
        parser = JobParser()
        job = parser.parse(yaml.dump(alpha_data), self.device, 4212, None, "")
        job.logger = DummyLogger()
        job.validate()
        vprotocol = [vprotocol for vprotocol in job.protocols if vprotocol.name == VlandProtocol.name][0]
        self.assertTrue(vprotocol.valid)
        self.assertEqual(vprotocol.names, {'vlan_one': '4212vlanone'})
        self.assertFalse(vprotocol.check_timeout(120, {'request': 'no call'}))
        self.assertRaises(JobError, vprotocol.check_timeout, 60, 'deploy_vlans')
        self.assertRaises(JobError, vprotocol.check_timeout, 60, {'request': 'deploy_vlans'})
        self.assertTrue(vprotocol.check_timeout(120, {'request': 'deploy_vlans'}))
        for vlan_name in job.parameters['protocols'][VlandProtocol.name]:
            if vlan_name == 'yaml_line':
                continue
            self.assertIn(vlan_name, vprotocol.params)
            self.assertIn('switch', vprotocol.params[vlan_name])
            self.assertIn('port', vprotocol.params[vlan_name])

    def test_job_bad_tags(self):
        with open(self.filename) as yaml_data:
            alpha_data = yaml.load(yaml_data)
        for vlan_key, _ in alpha_data['protocols'][VlandProtocol.name].items():
            alpha_data['protocols'][VlandProtocol.name][vlan_key] = {'tags': ['spurious']}
        # replaced tags from original job to simulate job where an unsupported tag is specified
        self.assertEqual(
            alpha_data['protocols'][VlandProtocol.name],
            {'vlan_one': {'tags': ['spurious']}}
        )
        parser = JobParser()
        job = parser.parse(yaml.dump(alpha_data), self.device, 4212, None, "")
        job.logger = DummyLogger()
        self.assertRaises(JobError, job.validate)

    def test_primary_interface(self):
        with open(self.filename) as yaml_data:
            alpha_data = yaml.load(yaml_data)
        for interface in self.device['parameters']['interfaces']:
            # jinja2 processing of tags: [] results in tags:
            if self.device['parameters']['interfaces'][interface]['tags'] == []:
                self.device['parameters']['interfaces'][interface]['tags'] = None
        parser = JobParser()
        job = parser.parse(yaml.dump(alpha_data), self.device, 4212, None, "")
        deploy = [action for action in job.pipeline.actions if action.name == 'tftp-deploy'][0]
        prepare = [action for action in deploy.internal_pipeline.actions if action.name == 'prepare-tftp-overlay'][0]
        overlay = [action for action in prepare.internal_pipeline.actions if action.name == 'lava-overlay'][0]
        vland_overlay = [action for action in overlay.internal_pipeline.actions if action.name == 'lava-vland-overlay'][0]
        vland_overlay.validate()
        job.logger = DummyLogger()
        job.validate()

    # pylint: disable=protected-access
    def demo(self):
        with open(self.filename) as yaml_data:
            alpha_data = yaml.load(yaml_data)
        vprotocol = VlandProtocol(alpha_data, 422)
        vprotocol.settings = vprotocol.read_settings()
        self.assertIn('port', vprotocol.settings)
        self.assertIn('poll_delay', vprotocol.settings)
        self.assertIn('vland_hostname', vprotocol.settings)
        vprotocol.base_message = {
            "port": vprotocol.settings['port'],
            "poll_delay": vprotocol.settings["poll_delay"],
            "host": vprotocol.settings['vland_hostname'],
            "client_name": socket.gethostname(),
        }
        count = 0
        print("\nTesting vland live using connections.")
        for friendly_name in vprotocol.parameters['protocols'][vprotocol.name]:
            print("Processing VLAN: %s" % friendly_name)
            vprotocol.names[friendly_name] = vprotocol.base_group + '%02d' % count
            count += 1
            vprotocol.vlans[friendly_name], tag = vprotocol._create_vlan(friendly_name)
            print("[%s] Created vlan with id %s" % (friendly_name, vprotocol.vlans[friendly_name]))
            print("[%s] tag: %s" % (friendly_name, tag))
            for hostname in vprotocol.parameters['protocols'][vprotocol.name][friendly_name]:
                params = vprotocol.parameters['protocols'][vprotocol.name][friendly_name][hostname]
                print("[%s] to use switch %s and port %s" % (friendly_name, params['switch'], params['port']))
                self.assertIn('switch', params)
                self.assertIn('port', params)
                self.assertIsNotNone(params['switch'])
                self.assertIsNotNone(params['port'])
                switch_id = vprotocol._lookup_switch_id(params['switch'])
                self.assertIsNotNone(switch_id)
                print("[%s] Using switch ID %s" % (friendly_name, switch_id))
                port_id = vprotocol._lookup_port_id(switch_id, params['port'])
                print("%s Looked up port ID %s for %s" % (friendly_name, port_id, params['port']))
                vprotocol._set_port_onto_vlan(vprotocol.vlans[friendly_name], port_id)
                vprotocol.ports.append(port_id)
        print("Finalising - tearing down vlans")
        vprotocol.finalise_protocol()