def test_existing_file_returns_contents(self): self.assertEqual( cost_reader.read_cost('test_cost_file_valid_data.json'), {'us-east': { 'large': 0.12, 'xlarge': 0.23, '2xlarge': 0.45 }})
def setUp(self): self.region_dict = cost_reader.read_cost( './test_cost_file_valid_data.json') self.instance_dict = { "large": 1, "xlarge": 2, "2xlarge": 4, "4xlarge": 8, "8xlarge": 16, "10xlarge": 32 }
def get_costs(n_cpu=None, n_hours=1, max_price=None): region_dict = read_cost('cost_file.json') if n_cpu is not None: if max_price is not None: print("CPU + Max prize combo") result = instance_allocator.instances_for_given_cpu_and_price( region_dict, n_hours, n_cpu, max_price) else: print("CPU + H hours combo") result = instance_allocator.instances_for_given_cpu_count( region_dict, n_hours, n_cpu) elif max_price is not None: print("Max price + H hours combo") result = instance_allocator.instances_for_given_price( region_dict, n_hours, max_price) else: print("Invalid input combo") result = [{'ERROR': 'Invalid input combination'}] pretty_result(result)
def test_non_existing_file_returns_error(self): self.assertEqual(cost_reader.read_cost('invalid_file.json'), {"ERROR": "File not found"})
def test_existing_file_invalid_data_returns_invalid_data_error(self): self.assertEqual( cost_reader.read_cost('test_cost_file_invalid_data.json'), {"ERROR": "Invalid input data"})
# The instance is included only if it satisfies the CPU as well as cost if current_instance_cpu <= number_of_cpu_per_region and current_instance_cost <= max_price_for_one_hour: number_of_cpu_per_region = number_of_cpu_per_region - current_instance_cpu max_price_for_one_hour = max_price_for_one_hour - current_instance_cost price_for_one_hour = price_for_one_hour + current_instance_cost server_count_dict[current_instance] = server_count_dict[current_instance] + 1 elif instance_index == 0: break # Going through instances index in round robin instance_index = instance_index - 1 if instance_index >= 1 else len(instances_list) - 1 per_region_result["region"] = region per_region_result["total_cost"] = '$' + str(round(price_for_one_hour * hours, 2)) for server, count in server_count_dict.items(): if count > 0: per_region_result["servers"].append((server, count)) result.append(per_region_result) result = sorted(result, key=lambda x: x['total_cost']) return result if __name__ == '__main__': from cost_file_reader import read_cost region_dict_input = read_cost('./cost_file.json') print(instances_for_given_price(region_dict_input, 3, 30)) print(instances_for_given_cpu_count(region_dict_input, 3, 115)) print(instances_for_given_cpu_and_price(region_dict_input, 4, 100, 40))