def test_all_ok(self): d = Datacenter('Berlin', {'BER-100': { 'networks': {}, 'security_level': 1 }}) self.assertIsInstance(d, Datacenter) d.remove_invalid_clusters() self.assertIs(len(d.clusters), 1)
def test_invalid_digits(self): with self.assertLogs() as cm: d = Datacenter('Berlin', {'BER-1000': { 'networks': {}, 'security_level': 1 }}) self.assertIsInstance(d, Datacenter) d.remove_invalid_clusters() self.assertEqual(len(d.clusters), 0) self.assertEqual(len(cm.output), 1) self.assertRegex(cm.output[0], r'removing .* from')
def test_dc_short_name(): clusters = { 'BA-007': { "security_level": 1, "networks": {} }, 'LON-007': { "security_level": 1, "networks": {} } } dc_out = Datacenter('Ba', clusters) dc_out.remove_invalid_clusters() name_len = len(dc_out.clusters[0].name.split('-')[0]) assert name_len == 3, "Should be 3"
def test_rem_one_1(): clusters = { 'LOND-007': { "security_level": 1, "networks": {} }, 'LON-007': { "security_level": 1, "networks": {} } } dc_in = Datacenter('London', clusters) dc_out = Datacenter('London', clusters) dc_out.remove_invalid_clusters() assert dc_out.clusters[0].name == 'LON-007', "Should be LON-007"
def main(): """ Main entry to our program. """ data = get_data(URL) # data = generate_bigger_data_set() if not data: raise ValueError('No data to process') start_time = time.time() logger.info("Organizing the fetched data into structures ...") datacenters = [] for datacenter_name, datacenter_dict in data.items(): try: datacenters.append(Datacenter(datacenter_name, datacenter_dict)) except Exception as e: logger.error(str(e)) data_processing_time = time.time() - start_time logger.info("Dumping datacenter data to file ...") dump_datacenter_to_file(datacenters) logger.info(f"Datacenter processing time: {data_processing_time} seconds")
def get_data(): with open('response.json') as json_file: data = json.load(json_file) return [ Datacenter(key, value) for key, value in data.items() ]
def test_rem_one(): clusters = { 'LOND-007': { "security_level": 1, "networks": {} }, 'LON-007': { "security_level": 1, "networks": {} } } dc_in = Datacenter('London', clusters) dc_out = Datacenter('London', clusters) dc_out.remove_invalid_clusters() assert len(dc_in.clusters) == len( dc_out.clusters) + 1, "Should remove one cluster"
def json_example_data_centers(response_json_example): """ Gives a dict with the data centers from the json example indexed by name """ yield { name: Datacenter(name, cluster_data) for name, cluster_data in response_json_example.items() }
def main(): """ Main entry to our program. """ data = get_data(URL) if not data: raise ValueError('No data to process') datacenters = [Datacenter(key, value) for key, value in data.items()]
def main(): """ Main entry to our program. """ data = get_data(URL) if not data: raise ValueError('No data to process') datacenters = (Datacenter(key, value) for key, value in data.items()) for dc in datacenters: dc.remove_invalid_clusters()
def test_records_order_2(): """ Validate that network entries are correctly ordered """ logger.info("Starting test case test_records_order_2") datacenter_dict = deepcopy(DATA) datacenter_dict["Berlin"]["BER-1"]["networks"]["192.168.200.0/24"].append({ "address": "192.168.200.8", "available": False, "last_used": "30/01/20 16:00:00" }) datacenter = Datacenter("Berlin", datacenter_dict["Berlin"]) assert datacenter.name == "Berlin", \ f"Expected datacenter name to be Berlin, got {datacenter.name}" assert len(datacenter.clusters) == 1, \ f"Expected only one cluster, got {len(datacenter.clusters)} " \ f"clusters" assert datacenter.clusters[0].name == "BER-1", \ f"Expected cluster name BER-1, got " \ f"{datacenter.clusters[0].name}" assert datacenter.clusters[0].security_level == 3, \ f"Expcetd cluster security level 3, got " \ f"{datacenter.clusters[0].security_level}" assert len(datacenter.clusters[0].networks) == 1, \ f"Expected only one cluster network, got " \ f"{len(datacenter.clusters[0].networks)}" assert datacenter.clusters[0].networks[ 0].ipv4_network == IPv4Network("192.168.200.0/24"), \ f"Expected cluster IPV4 network to be " \ f"IPv4Network('192.168.200.0/24'), got " \ f"{datacenter.clusters[0].networks[0].ipv4_network}" assert len(datacenter.clusters[0].networks[0].entries) == 2, \ f"Expected two network entries, got " \ f"{len(datacenter.clusters[0].networks[0].entries)}" assert datacenter.clusters[0].networks[0].entries[ 0].address == "192.168.200.8", \ f"Expected network entry address '192.168.200.8', got " \ f"{datacenter.clusters[0].networks[0].entries[0].address}" assert datacenter.clusters[0].networks[0].entries[ 1].address == "192.168.200.8", \ f"Expected network entry address '192.168.200.8', got " \ f"{datacenter.clusters[0].networks[0].entries[1].address}" logger.info("Test case test_records_order_2 PASSED")
def main(): """ Main entry to our program. """ logging.basicConfig(filename='main.log', level=logging.DEBUG) data = get_data(URL) if not data: raise ValueError('No data to process') datacenters = [Datacenter(key, value) for key, value in data.items()] print(datacenters) for d in datacenters: d.remove_invalid_clusters() for c in d.clusters: for n in c.networks: n.remove_invalid_records() n.sort_records() print(datacenters)
def two_cluster_data_center(): yield Datacenter( "Berlin", { "BER-1": { "security_level": 1, "networks": { "192.168.0.0/24": [ { "address": "255.255.255.0", "available": True, "last_used": "30/01/20 17:00:00" }, { "address": "192.168.0.1", "available": False, "last_used": "20/12/19 17:10:01" } ], "10.0.8.0/22": [ { "address": "10.0.11.254", "available": True, "last_used": "30/01/20 17:00:00" }, ] } }, "BER-2030": { "security_level": 3, "networks": { "192.168.10.0/24": [ { "address": "192.168.10.8", "available": True, "last_used": "30/01/20 17:00:00" } ] } } } )
def common_check_invalid_entry(datacenter_dict=None, datacenter_obj=None): """ Common function used for checking the datacenter object attributes. """ datacenter = Datacenter( "Berlin", datacenter_dict["Berlin"] ) if datacenter_dict is not None else datacenter_obj assert datacenter.name == "Berlin", \ f"Expected datacenter name to be Berlin, got {datacenter.name}" assert len(datacenter.clusters) == 1, \ f"Expected only one cluster, got {len(datacenter.clusters)} " \ f"clusters" assert datacenter.clusters[0].name == "BER-1", \ f"Expected cluster name BER-1, got " \ f"{datacenter.clusters[0].name}" assert datacenter.clusters[0].security_level == 3, \ f"Expcetd cluster security level 3, got " \ f"{datacenter.clusters[0].security_level}" assert len(datacenter.clusters[0].networks) == 1, \ f"Expected only one cluster network, got " \ f"{len(datacenter.clusters[0].networks)}" assert datacenter.clusters[0].networks[ 0].ipv4_network == IPv4Network("192.168.200.0/24"), \ f"Expected cluster IPV4 network to be " \ f"IPv4Network('192.168.200.0/24'), got " \ f"{datacenter.clusters[0].networks[0].ipv4_network}" assert len(datacenter.clusters[0].networks[0].entries) == 1, \ f"Expected only one network entry, got " \ f"{len(datacenter.clusters[0].networks[0].entries)}" assert datacenter.clusters[0].networks[0].entries[ 0].address == "192.168.200.8", \ f"Expected network entry address '192.168.200.8', got " \ f"{datacenter.clusters[0].networks[0].entries[0].address}" assert datacenter.clusters[0].networks[0].entries[ 0].available is True, \ f"Expected network entry available field True, got " \ f"{datacenter.clusters[0].networks[0].entries[0].available}" assert datacenter.clusters[0].networks[0].entries[ 0].last_used == "30/01/20 17:00:00", \ f"Expected network entry last_used field 30/01/20 17:00:00, " \ f"got {datacenter.clusters[0].networks[0].entries[0].last_used}"
def test_invalid_datacenter_clusters_type(): """ Validate that invalid values for the 'clusters' attribute of a Cluster object are ignored """ logger.info("Starting test case test_invalid_datacenter_clusters_type") datacenter_dict = { "Berlin": { "BER-1": { "security_level": 3, "networks": { "192.168.200.0/24": [{ "address": "192.168.200.8", "available": True, "last_used": "30/01/20 17:00:00" }] } } }, "Paris": [{ "security_level": 3, "networks": { "192.168.200.0/24": [{ "address": "192.168.200.8", "available": True, "last_used": "30/01/20 17:00:00" }] } }] } datacenters = [] for datacenter_name, datacenter_dict in datacenter_dict.items(): try: datacenters.append(Datacenter(datacenter_name, datacenter_dict)) except Exception as e: logger.debug(str(e)) assert len(datacenters) == 1, \ f"Expcted only one datacenter, got {len(datacenters)}" datacenter_obj = datacenters[0] common_check_invalid_entry(datacenter_obj=datacenter_obj) logger.info("Test case test_invalid_datacenter_clusters_type PASSED")
def main(): """ Main entry to our program. """ data = get_data(URL) if not data: raise ValueError('No data to process') datacenters = [Datacenter(key, value) for key, value in data.items()] for i in datacenters: i.remove_invalid_clusters() print(i.name) for j in i.clusters: print('\t'+j.name) # print (str(j.networks) + ' {}'.format(type(j.networks))) for k in j.networks: k.remove_invalid_records() print('\tNetwork {}'.format(k.ipv4_network)) for e in k.entries: print('\t\t'+e.address) k.sort_records() print('\tSorted record entries:') for e in k.entries: print('\t\t'+e.address) with open('mocky_response.json', 'w') as f: json.dump(data, f, indent=3)
def __data_centers_from_dict(data): return [ Datacenter(name, cluster_data) for name, cluster_data in data.items() ]
def setUp(self): with open('response.json') as fin: data = json.load(fin) self.dc = Datacenter('Berlin', data['Berlin'])
def test_datacenter_inits_name(): # WHEN a Datacenter is created datacenter = Datacenter("Berlin", {}) # THEN name is initialized assert datacenter.name == "Berlin"