Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from django.test import TestCase 

2from django.urls import reverse 

3from django.conf import settings 

4from datetime import datetime 

5import json 

6 

7from .models import PDU, Machine, Event 

8 

9 

10class PDUTestCase(TestCase): 

11 def test_str(self): 

12 self.assertEqual(str(PDU(name="MyPDU")), "<PDU: MyPDU>") 

13 

14 

15class MachineTests(TestCase): 

16 def setUp(self): 

17 self.pdu = PDU.objects.create(name="MyPDU", pdu_model='PDU1') 

18 

19 self.machine = Machine.objects.create(base_name="gfx8", 

20 mac_address="de:ad:be:ef:ca:fe", 

21 ip_address="192.168.0.1", 

22 pdu=self.pdu, pdu_port_id="42") 

23 

24 def test_full_name__on_unsaved_object(self): 

25 with self.assertRaises(ValueError): 

26 Machine().full_name 

27 

28 def test_full_name(self): 

29 # Check the name of the fixture machine 

30 self.assertEqual(self.machine.full_name, f"{settings.FARM_NAME}-gfx8-1") 

31 

32 # Make sure that the index is not affected by other basenames, and 

33 # increases nicely 

34 for i in range(1, 9): 

35 m = Machine.objects.create(base_name="gfx9", 

36 mac_address=f"de:ad:be:ef:ca:0{i}", 

37 ip_address="192.168.0.1") 

38 self.assertEqual(m.full_name, f"{settings.FARM_NAME}-gfx9-{i}") 

39 

40 # Check that the FARM_NAME setting is honored 

41 with self.settings(FARM_NAME='tchar'): 

42 # Due to full_name being cached_property, create another machine to pick up the setting change. 

43 another_machine = Machine.objects.create(base_name="gfx10", 

44 mac_address="de:ad:be:ef:ca:ef", 

45 ip_address="192.168.0.2", 

46 pdu=self.pdu, pdu_port_id="43") 

47 # Check the name of the fixture machine 

48 self.assertEqual(another_machine.full_name, "tchar-gfx10-1") 

49 

50 def test_filter_ready_machine(self): 

51 for i in range(2, 10): 

52 m = Machine.objects.create(base_name="gfx9", 

53 mac_address=f"de:ad:be:ef:ca:0{i}", 

54 ip_address="192.168.0.1") 

55 if i in [2, 4, 6]: 

56 m.ready_for_service = True 

57 m.save() 

58 

59 url = reverse('machine-list') 

60 response = self.client.get(url + '?ready_for_service=True') 

61 self.assertEqual(len(response.json()), 3) 

62 

63 def test_str(self): 

64 self.assertEqual(str(self.machine), f"<Machine: {self.machine.full_name}>") 

65 

66 

67def _get_events(**filters): 

68 # These are sorted by reverse normally, so reverse it once again 

69 return Event.objects.filter(**filters).order_by('date') 

70 

71 

72class EventTests(TestCase): 

73 def setUp(self): 

74 self.machine = Machine.objects.create(base_name="gfx8", 

75 mac_address="de:ad:be:ef:ca:fe", 

76 ip_address="192.168.0.1") 

77 

78 def test_event_machine_created(self): 

79 events = _get_events(machine=self.machine) 

80 self.assertEqual(events.count(), 1) 

81 event = events[0] 

82 self.assertEqual(str(event), 

83 f"<Event id=1 machine=<Machine: {settings.FARM_NAME}-gfx8-1> category={Event.CATEGORY_MACHINE_CREATED}>") # noqa: E501 

84 self.assertEqual(json.loads(event.diff), 

85 {}) 

86 

87 def test_event_machine_updated(self): 

88 self.machine.save() 

89 events = _get_events(machine=self.machine) 

90 self.assertEqual(events.count(), 2) 

91 update_event = events[1] 

92 self.assertEqual(str(update_event), 

93 f"<Event id=2 machine=<Machine: {settings.FARM_NAME}-gfx8-1> category={Event.CATEGORY_MACHINE_UPDATED}>") # noqa: E501 

94 self.assertEqual(json.loads(update_event.diff), 

95 {}) 

96 

97 def test_event_machine_retired(self): 

98 self.machine.is_retired = True 

99 self.machine.save() 

100 events = _get_events(machine=self.machine) 

101 self.assertEqual(events.count(), 2) 

102 retire_event = events[1] 

103 self.assertEqual(str(retire_event), 

104 f"<Event id=2 machine=<Machine: {settings.FARM_NAME}-gfx8-1> category={Event.CATEGORY_MACHINE_RETIRED}>") # noqa: E501 

105 assert { 

106 'values_changed': { 

107 'root.is_retired': { 

108 'new_value': True, 

109 'old_value': False 

110 } 

111 } 

112 } == json.loads(retire_event.diff) 

113 

114 def test_event_date_filter(self): 

115 now = datetime.now() 

116 self.machine.save() 

117 self.machine.save() 

118 url = reverse('event-list') 

119 response = self.client.get(url + f'?since={now}') 

120 self.assertEqual(len(response.json()), 2) 

121 

122 def test_nullable_field_created(self): 

123 pdu = PDU(name='PDU1', pdu_model="dummy", config={"ports": ["label 1", "label 2"]}) 

124 pdu.save() 

125 self.machine.pdu = pdu 

126 self.machine.pdu_port_id = 'label 1' 

127 now = datetime.now() 

128 self.machine.save() 

129 url = reverse('event-list') 

130 response = self.client.get(url + f'?since={now}') 

131 events = response.json() 

132 assert len(events) == 1 

133 assert {"type_changes": { 

134 "root.pdu_id": { 

135 "old_type": "NoneType", 

136 "new_type": "int", 

137 "old_value": None, 

138 "new_value": 1 

139 }, 

140 "root.pdu_port_id": { 

141 "old_type": "NoneType", 

142 "new_type": "str", 

143 "old_value": None, 

144 "new_value": "label 1" 

145 } 

146 }} == json.loads(events[0]['diff'])