# Copyright 2019 AT&T Intellectual Property. All other rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from copy import deepcopy import os import unittest from unittest import mock import pytest from spyglass.data_extractor import models from spyglass_plugin_xls.excel import ExcelPlugin from spyglass_plugin_xls.excel_parser import ExcelParser from spyglass_plugin_xls.exceptions import ExcelFileNotSpecified from spyglass_plugin_xls.exceptions import ExcelSpecNotSpecified FIXTURE_DIR = os.path.join( os.path.dirname(os.path.dirname(__file__)), 'shared') EXCEL_SPEC_PATH = os.path.join(FIXTURE_DIR, 'excel_spec.yaml') INVALID_EXCEL_SPEC_PATH = os.path.join(FIXTURE_DIR, 'invalid_excel_spec.yaml') EXCEL_FILE_PATH = os.path.join(FIXTURE_DIR, 'SiteDesignSpec_v0.1.xlsx') SITE_CONFIG_PATH = os.path.join(FIXTURE_DIR, 'site_config.yaml') @pytest.mark.usefixtures('site_data') class TestExcelPlugin(unittest.TestCase): """Tests for ExcelPlugin""" def test___init__(self): region = 'test_region' result = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) self.assertEqual(region, result.region) self.assertEqual('excel', result.source_type) self.assertEqual('spyglass-plugin-xls', result.source_name) self.assertEqual(EXCEL_FILE_PATH, result.excel_path) self.assertEqual(EXCEL_SPEC_PATH, result.excel_spec) self.assertIsInstance(result.excel_obj, ExcelParser) self.assertTrue(result.parsed_xl_data) def test___init___no_excel_file(self): region = 'test_region' with self.assertRaises(ExcelFileNotSpecified): ExcelPlugin(region, excel_spec=EXCEL_SPEC_PATH) def test___init___no_excel_spec(self): region = 'test_region' with self.assertRaises(ExcelSpecNotSpecified): ExcelPlugin(region, excel_file=EXCEL_FILE_PATH) def test_get_racks(self): region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_racks(region) self.assertEqual(2, len(result)) for rack in result: self.assertIsInstance(rack, models.Rack) self.assertEqual(6, len(rack.hosts)) for host in rack.hosts: self.assertIn(host.name, self.site_data['ipmi_data'][0]) self.assertEqual( self.site_data['ipmi_data'][0][host.name]['host_profile'], host.host_profile) def test_get_hosts(self): region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_hosts(region) self.assertEqual(12, len(result)) for host in result: self.assertIn(host.name, self.site_data['ipmi_data'][0]) self.assertEqual( self.site_data['ipmi_data'][0][host.name]['host_profile'], host.host_profile) def test_get_hosts_using_rack(self): region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_hosts(region, 'rack73') self.assertEqual(6, len(result)) for host in result: self.assertIn(host.name, self.site_data['ipmi_data'][0]) self.assertEqual('rack73', host.rack_name) self.assertNotIn('r72', host.name) self.assertEqual( self.site_data['ipmi_data'][0][host.name]['host_profile'], host.host_profile) def test_get_networks(self): expected_network_types = { 'oob': { 'type': 'public', 'name': 'oob' }, 'oam': { 'type': 'public', 'name': 'oam' }, 'calico': { 'type': 'private', 'name': 'Calico BGP peering addresses' }, 'ingress': { 'type': 'public', 'name': 'ingress' }, 'overlay': { 'type': 'private', 'name': 'Overlay' }, 'pxe': { 'type': 'private', 'name': 'PXE' }, 'storage': { 'type': 'private', 'name': 'iSCSI/Storage' } } network_data = deepcopy(self.site_data['network_data']) region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_networks(region) self.assertEqual(7, len(result)) for vlan_data in result: self.assertIn(vlan_data.name, expected_network_types) data = expected_network_types[vlan_data.name] if vlan_data.name != 'ingress': self.assertEqual( network_data[data['type']][data['name']]['subnet'], vlan_data.subnet) else: self.assertEqual( network_data[data['type']][data['name']], vlan_data.subnet[0]) def test_get_ips(self): region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) host_name = 'cab2r72c15' result = obj.get_ips(region, host_name) self.assertIsInstance(result, models.IPList) self.assertEqual( self.site_data['ipmi_data'][0][host_name]['ipmi_address'], result.oob) def test_get_ldap_information(self): expected_ldap_data = deepcopy(self.site_data['site_info']['ldap']) expected_ldap_data['domain'] = 'example' expected_ldap_data['url'] = expected_ldap_data['url'].split(' ')[1] region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_ldap_information(region) self.assertDictEqual(expected_ldap_data, result) def test_get_ntp_servers(self): expected_ntp_servers = deepcopy(self.site_data['site_info']['ntp'][:1]) region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_ntp_servers(region) self.assertIsInstance(result, models.ServerList) self.assertEqual(expected_ntp_servers, result.servers) def test_get_dns_servers(self): expected_dns_servers = [ self.site_data['site_info']['dns'][0], self.site_data['site_info']['dns'][2] ] region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_dns_servers(region) self.assertIsInstance(result, models.ServerList) self.assertEqual(expected_dns_servers, result.servers) def test_get_domain_name(self): region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_domain_name(region) self.assertEqual(self.site_data['site_info']['domain'], result) def test_get_location_information(self): expected_location_data = deepcopy( self.site_data['site_info']['location']) expected_location_data['corridor'] = 'c1' expected_location_data[ 'physical_location_id'] = expected_location_data.pop( 'physical_location') region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj.get_location_information(region) self.assertDictEqual(expected_location_data, result) def test_get_site_info(self): expected_ntp_servers = deepcopy(self.site_data['site_info']['ntp'][:1]) expected_dns_servers = [ self.site_data['site_info']['dns'][0], self.site_data['site_info']['dns'][2] ] expected_location_data = deepcopy( self.site_data['site_info']['location']) expected_location_data['corridor'] = 'c1' expected_location_data[ 'physical_location_id'] = expected_location_data.pop( 'physical_location') expected_ldap_data = deepcopy(self.site_data['site_info']['ldap']) expected_ldap_data['domain'] = 'example' expected_ldap_data['url'] = expected_ldap_data['url'].split(' ')[1] region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = self.site_data result = obj.get_site_info(region) self.assertIsInstance(result, models.SiteInfo) self.assertEqual(region, result.region_name) self.assertEqual(expected_dns_servers, result.dns.servers) self.assertEqual(expected_ntp_servers, result.ntp.servers) self.assertDictEqual(expected_ldap_data, result.ldap) self.assertEqual(expected_location_data['corridor'], result.corridor) self.assertEqual(expected_location_data['state'], result.state) self.assertEqual(expected_location_data['country'], result.country) self.assertEqual( expected_location_data['physical_location_id'], result.physical_location_id) self.assertEqual(expected_location_data['name'], result.name) @mock.patch('spyglass_plugin_xls.excel_parser.ExcelParser') def test__get_excel_obj(self, excel_parser): region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.excel_spec = EXCEL_SPEC_PATH obj.excel_path = EXCEL_FILE_PATH obj._get_excel_obj() self.assertIsInstance(obj.excel_obj, ExcelParser) def test__extract_raw_data_from_excel(self): region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.excel_obj = mock.MagicMock(spec=ExcelParser) obj.excel_obj.get_data.return_value = 'success' obj._extract_raw_data_from_excel() obj.excel_obj.get_data.assert_called_once() self.assertEqual('success', obj.parsed_xl_data) def test__get_network_name_from_vlan_name(self): result = ExcelPlugin._get_network_name_from_vlan_name('ksn') self.assertEqual('calico', result) result = ExcelPlugin._get_network_name_from_vlan_name('calico') self.assertEqual('calico', result) result = ExcelPlugin._get_network_name_from_vlan_name('storage') self.assertEqual('storage', result) result = ExcelPlugin._get_network_name_from_vlan_name('oam') self.assertEqual('oam', result) result = ExcelPlugin._get_network_name_from_vlan_name('server') self.assertEqual('oam', result) result = ExcelPlugin._get_network_name_from_vlan_name('ovs') self.assertEqual('overlay', result) result = ExcelPlugin._get_network_name_from_vlan_name('overlay') self.assertEqual('overlay', result) result = ExcelPlugin._get_network_name_from_vlan_name('oob') self.assertEqual('oob', result) result = ExcelPlugin._get_network_name_from_vlan_name('pxe') self.assertEqual('pxe', result) def test__get_network_name_from_vlan_name_dne(self): result = ExcelPlugin._get_network_name_from_vlan_name('dne') self.assertEqual('', result) def test__get_formatted_server_list(self): test_list = [ '124.1.23.54', '(example.com)', '192.168.1.0', '(anotherexample.com)' ] expected_list = ['124.1.23.54', '192.168.1.0'] result = ExcelPlugin._get_formatted_server_list(test_list) self.assertIsInstance(result, models.ServerList) self.assertEqual(expected_list, result.servers) def test__get_rack(self): region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) result = obj._get_rack('cab2r72c15') self.assertEqual('r72', result) def test__get_rackwise_hosts(self): expected_data = { 'rack72': [ 'cab2r72c12', 'cab2r72c13', 'cab2r72c14', 'cab2r72c15', 'cab2r72c16', 'cab2r72c17' ], 'rack73': [ 'cab2r73c12', 'cab2r73c13', 'cab2r73c14', 'cab2r73c15', 'cab2r73c16', 'cab2r73c17' ] } region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj._get_rackwise_hosts() self.assertDictEqual(expected_data, result) def test__get_rack_data(self): expected_data = {'r72': 'rack72', 'r73': 'rack73'} region = 'test_region' obj = ExcelPlugin( region, excel_file=EXCEL_FILE_PATH, excel_spec=EXCEL_SPEC_PATH) obj.parsed_xl_data = deepcopy(self.site_data) result = obj._get_rack_data() self.assertDictEqual(expected_data, result)