def test_delete_happens_one_vm_matches(app, trigger_event, mock_http, caplog): """Deletes the A record matching the IP address of the VM.""" mock_http.append(({'status': '200'}, readfile('instance.json'))) mock_http.append(({'status': '200'}, readfile('rrsets.json'))) mock_http.append(({'status': '200'}, readfile('dns-change.json'))) mock_http.append(({'status': '200'}, readfile('rrsets-empty.json'))) num_deleted = app.handle_event(trigger_event) assert 1 == num_deleted assert 'my-vpc-host/managedZones/my-nonprod-private-zone/changes' \ in mock_http.saved_requests[4]['uri']
def mock_http(): """Mocks an http instance suitable for use with discovery based API's.""" http = HttpMockSequenceRecorder([ # Get IP address path ({ 'status': '200' }, readfile('compute-v1.json')), ({ 'status': '200' }, readfile('dns-v1.json')), ]) return http
def __init__(self, fpath, lang, regex, any_ext=False): """ :param fpath: :param lang: :param regex: :param any_ext: """ # Check lang input if lang not in LANGUAGE_EXTENSIONS: raise ValueError('Unrecognized input language "{:s}"'.format(lang)) # Check file extension if not any_ext: ext = getext(fpath) if ext not in LANGUAGE_EXTENSIONS[lang]: raise FileExistsError('File extension is not supported') # Check file content self.content = readfile(fpath) if self.content.strip() == '': raise FileExistsError('File is empty') # Define self properties self.lang = lang.lower() self.regex = regex # Parse file self.nodes = OrderedDict() self.founds = find_matches(self.content, self.regex['main']) if self.founds: self.parse()
def main(): data = readfile('inputs/day16_input.txt') split_data = data.split('\n\n') rules = split_data[0].split('\n') my_ticket = split_data[1].split('\n')[1].split(',') nearby_tickets = split_data[2].split('\n')[1:] rules = _prepare_rules(rules) my_ticket = [int(x) for x in my_ticket] nearby_tickets = [[int(x) for x in line.split(',')] for line in nearby_tickets] ''' Test data: nearby_tickets = [ [7,37,47], [40,4,50], [55,2,20], [38,6,12] ] rules = { 'class': [[1,3], [5,7]], 'row': [[6,11], [33,44]], 'seat': [[13,40], [45,50]] } ''' invalid_values = [] valid_tickets = [] for ticket in nearby_tickets: valid_ticket = False for value in ticket: not_valid_for_any = False for rule in rules.values(): if value in rule[0] or value in rule[1]: not_valid_for_any = False break else: not_valid_for_any = True if not_valid_for_any: invalid_values.append(value) valid_ticket = False break else: valid_ticket = True if valid_ticket: valid_tickets.append(ticket) print(sum(invalid_values)) part2(valid_tickets)
def main(): """Day 6 solution""" data = readfile('inputs/day6_input.txt') part1_solution = part1(data) part2_solution = part2(data) print(f'Part 1 solution: {part1_solution}') print(f'Part 2 solution: {part2_solution}')
def test_managed_zone_not_found(app, trigger_event, mock_http, caplog): """Handles a 404 not found and suggests checking configuration""" mock_http.append(({'status': '200'}, readfile('instance.json'))) # These are the API calls to get the managed zones. mock_http.append(({'status': '404'}, '')) mock_http.append(({'status': '404'}, '')) num_deleted = app.handle_event(trigger_event) expected = ('Check managed zones specified in DNS_VM_GC_DNS_ZONES ' 'exist in DNS_VM_GC_DNS_PROJECT') assert 0 == num_deleted assert expected in caplog.text
def main(): """Day 4 solution""" data = readfile('inputs/day4_input.txt') passports = parse_passports(data) part1_solution = part1(passports) part2_solution = part2(passports) print(f'Part 1 solution is {part1_solution}') print(f'Part 2 solution is {part2_solution}')
def main(): ''' Test deck: p1_deck = [9,2,6,3,1] p2_deck = [5,8,4,7,10] ''' data = readfile('inputs/day22_input.txt') p1, p2 = data.split('\n\n') p1_deck = [int(x) for x in p1.split('\n')[1:]] p2_deck = [int(x) for x in p2.split('\n')[1:]] round_num = 1 while len(p1_deck) > 0 and len(p2_deck) > 0: print(f'-- Round {round_num} --') print(f'Player 1 deck: {(", ").join([str(x) for x in p1_deck])}') print(f'Player 2 deck: {(", ").join([str(x) for x in p2_deck])}') turn_p1 = p1_deck.pop(0) turn_p2 = p2_deck.pop(0) print(f'Player 1 plays: {str(turn_p1)}') print(f'Player 2 plays: {str(turn_p2)}') if turn_p1 > turn_p2: p1_deck.extend([turn_p1, turn_p2]) print(f'Player 1 wins the round!') else: p2_deck.extend([turn_p2, turn_p1]) print(f'Player 2 wins the round!') round_num += 1 print() print('== Post-game results ==') print(f'Player 1 deck: {(", ").join([str(x) for x in p1_deck])}') print(f'Player 2 deck: {(", ").join([str(x) for x in p2_deck])}') winning_deck = p1_deck if len(p1_deck) > 0 else p2_deck winner_score = 0 for i, item in enumerate(winning_deck[::-1]): winner_score += item * (i + 1) print() print(f'The winners score is: {str(winner_score)}')
def main(): data = readfile('inputs/day20_test_input.txt') tiles = data.split('\n\n') tiles = [x.split('\n') for x in tiles] #tiles = {i[0].replace('Tile ','').replace(':', ''): [[c for c in x] for x in i[1:]] for i in tiles} _rotate() return tiles = {i[0].replace('Tile ','').replace(':', ''): i[1:] for i in tiles} all_edges = {} for tile in tiles: t = tiles[tile] edges_with_reversed = [] edges = [ t[0], t[-1], ('').join([x[0] for x in t]), ('').join([x[-1] for x in t]) ] for edge in edges: edges_with_reversed.append(edge) edges_with_reversed.append(('').join(list(reversed(edge)))) for edge in edges_with_reversed: if not all_edges.get(edge): all_edges[edge] = [tile] else: all_edges[edge].append(tile) x = [(k,v) for k,v in all_edges.items() if '1171' in v] print(x)
def mock_instance_resource_no_network(): """Mocks a compute.instances.get Resource with no network interface.""" return json.loads(readfile('instance-no-network.json'))
def mock_instance_resource(): """Mocks a compute.instances.get Resource.""" return json.loads(readfile('instance.json'))
def trigger_event_done(): """Input data from a Pub/Sub GCE_OPERATION_DONE trigger event""" data = base64.b64encode(readfile('trigger-event-done.json').encode()) return {'data': data.decode()}
def trigger_event(): """Input data from a Pub/Sub GCE_API_CALL trigger event""" data = base64.b64encode(readfile('trigger-event.json').encode()) return {'data': data.decode()}