def test_check_python3_xrange(self): func = checks.check_python3_xrange self.assertThat(list(func('for i in xrange(10)')), matchers.HasLength(1)) self.assertThat(list(func('for i in range(10)')), IsEmpty()) self.assertThat(list(func('for i in six.moves.range(10)')), IsEmpty()) self.assertThat(list(func('testxrange(10)')), IsEmpty())
def test_dict_iterkeys(self): self.assertThat( list(checks.check_python3_no_iterkeys("obj.iterkeys()")), matchers.HasLength(1)) self.assertThat( list(checks.check_python3_no_iterkeys("six.iterkeys(obj)")), IsEmpty()) self.assertThat(list(checks.check_python3_no_iterkeys("obj.keys()")), IsEmpty())
def test_dict_constructor_with_list_copy(self): self.assertThat( list( checks.dict_constructor_with_list_copy( " dict([(i, connect_info[i])")), matchers.HasLength(1)) self.assertThat( list( checks.dict_constructor_with_list_copy( " attrs = dict([(k, _from_json(v))")), matchers.HasLength(1)) self.assertThat( list( checks.dict_constructor_with_list_copy( " type_names = dict((value, key) for key, value in") ), matchers.HasLength(1)) self.assertThat( list( checks.dict_constructor_with_list_copy( " dict((value, key) for key, value in")), matchers.HasLength(1)) self.assertThat( list( checks.dict_constructor_with_list_copy( "foo(param=dict((k, v) for k, v in bar.items()))")), matchers.HasLength(1)) self.assertThat( list( checks.dict_constructor_with_list_copy( " dict([[i,i] for i in range(3)])")), matchers.HasLength(1)) self.assertThat( list( checks.dict_constructor_with_list_copy( " dd = dict([i,i] for i in range(3))")), matchers.HasLength(1)) self.assertThat( list( checks.dict_constructor_with_list_copy( " create_kwargs = dict(snapshot=snapshot,")), IsEmpty()) self.assertThat( list( checks.dict_constructor_with_list_copy( " self._render_dict(xml, data_el, data.__dict__)")), IsEmpty())
def test_graph(self): g = NXGraph('test_graph') self.assertEqual('test_graph', g.name, 'graph name') self.assertThat(g, IsEmpty(), 'graph __len__') g.add_vertex(v_node) g.add_vertex(v_host) g.add_edge(e_node_to_host) self.assertThat(g, matchers.HasLength(2), 'graph __len__ after add vertices') graph_copy = g.copy() self.assertEqual('test_graph', graph_copy.name, 'graph copy name') self.assertThat(graph_copy, matchers.HasLength(2), 'graph copy __len__') g.remove_vertex(v_node) self.assertThat(g, matchers.HasLength(1), 'graph __len__ after remove vertex') self.assertThat(graph_copy, matchers.HasLength(2), 'graph copy __len__') updated_vertex = g.get_vertex(v_host.vertex_id) updated_vertex[VProps.VITRAGE_CATEGORY] = ALARM g.update_vertex(updated_vertex) v_from_g = g.get_vertex(v_host.vertex_id) v_from_graph_copy = graph_copy.get_vertex(v_host.vertex_id) self.assertEqual(ALARM, v_from_g[VProps.VITRAGE_CATEGORY], 'graph vertex changed after update') self.assertEqual(NOVA_HOST_DATASOURCE, v_from_graph_copy[VProps.VITRAGE_TYPE], 'graph copy vertex unchanged after update')
def test_delete_alarm(self): # Test setup alarm_data1 = self._extract_alarm_data(value='1') alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2') alarm_data3 = self._extract_alarm_data(z_resource_name='compute-2', triggerid='2') # Step 1 - delete inactive alarm # Step setup zabbix_driver = MockZabbixDriver(self.conf) zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2]) # Step action alarms = zabbix_driver._get_all_alarms() # Step assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(1)) self._assert_contains(alarm_data1, alarms) # Step 2 - delete active alarm # Step setup zabbix_driver.set_alarm_datas([alarm_data2]) # Step action alarms = zabbix_driver._get_all_alarms() # Step assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(1)) self._assert_contains(alarm_data1, alarms) self.assertEqual(GraphAction.DELETE_ENTITY, alarms[0][DSProps.EVENT_TYPE]) # Step 3 - get changes after get all should not return deleted alarm # Step action alarms = zabbix_driver._get_changed_alarms() # Step assertions self.assertIsNotNone(alarms, 'alarms is None') self.assertThat(alarms, IsEmpty()) # Step 4 - # Step setup zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2]) zabbix_driver._get_all_alarms() zabbix_driver.set_alarm_datas([alarm_data2]) # Step action alarms = zabbix_driver._get_changed_alarms() # Step assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(1)) self._assert_contains(alarm_data1, alarms) self.assertEqual(GraphAction.DELETE_ENTITY, alarms[0][DSProps.EVENT_TYPE])
def test_assert_equal_type(self): self.assertThat( list( checks.assert_equal_type( "self.assertEqual(type(als['QuicAssist']), list)")), matchers.HasLength(1)) self.assertThat(list(checks.assert_equal_type("self.assertTrue()")), IsEmpty())
def test_get_undo_recipe(self): # Test Action action_steps = ExecuteMistral.get_undo_recipe(self.action_spec) # Test Assertions # expecting for zero steps (no undo for this action) self.assertThat(action_steps, IsEmpty())
def test_assert_true_instance(self): self.assertThat( list( checks.assert_true_instance( "self.assertTrue(isinstance(e, " "exception.BuildAbortException))")), matchers.HasLength(1)) self.assertThat( list(checks.assert_true_instance("self.assertTrue(" ")")), IsEmpty())
def test_no_direct_use_of_unicode_function(self): self.assertThat( list( checks.no_direct_use_of_unicode_function( "unicode('the party don't start til the unicode walks in')" )), matchers.HasLength(1)) self.assertThat( list( checks.no_direct_use_of_unicode_function( """unicode('something ' 'something else""")), matchers.HasLength(1)) self.assertThat( list( checks.no_direct_use_of_unicode_function( "six.text_type('party over')")), IsEmpty()) self.assertThat( list( checks.no_direct_use_of_unicode_function( "not_actually_unicode('something completely different')")), IsEmpty())
def test_no_mutable_default_args(self): self.assertThat( list( checks.no_mutable_default_args( " def fake_suds_context(calls={}):")), matchers.HasLength(1)) self.assertThat( list( checks.no_mutable_default_args( "def get_info_from_bdm(virt_type, bdm, mapping=[])")), matchers.HasLength(1)) self.assertThat(list(checks.no_mutable_default_args("defined = []")), IsEmpty()) self.assertThat( list( checks.no_mutable_default_args("defined, undefined = [], {}")), IsEmpty())
def test_no_contextlib_nested(self): self.assertThat( list(checks.check_no_contextlib_nested("with contextlib.nested(")), matchers.HasLength(1)) self.assertThat( list(checks.check_no_contextlib_nested("with nested(")), matchers.HasLength(1)) self.assertThat( list(checks.check_no_contextlib_nested("with foo as bar")), IsEmpty())
def test_resource_list_with_not_admin_project_and_no_existing_type(self): # Setup graph = self._create_graph() apis = ResourceApis(graph, None, self.api_lock) ctx = {'tenant': 'project_2', 'is_admin': False} # Action resources = apis.get_resources(ctx, resource_type=NOVA_HOST_DATASOURCE, all_tenants=False) resources = decompress_obj(resources)['resources'] # Test assertions self.assertThat(resources, IsEmpty())
def test_basic_regex_with_no_match(self): event_properties = { "time": 121354, "vitrage_type": "zabbix", "vitrage_category": "ALARM", "rawtext": "No Match", "host": "some_host_kukoo" } event_vertex = Vertex(vertex_id="test_vertex", properties=event_properties) relevant_scenarios = \ self.scenario_repository.get_scenarios_by_vertex( event_vertex) self.assertThat(relevant_scenarios, IsEmpty())
def test_get_changes(self): # Setup entities = self.static_physical_driver.get_all(DatasourceAction.UPDATE) self.assertThat(entities, matchers.HasLength(5)) self.conf = cfg.ConfigOpts() self.conf.register_opts(self.CHANGES_OPTS, group=STATIC_PHYSICAL_DATASOURCE) self.static_physical_driver.cfg = self.conf # Action changes = self.static_physical_driver.get_changes( GraphAction.UPDATE_ENTITY) # Test Assertions status = any(change[StaticFields.TYPE] == SWITCH and change[StaticFields.ID] == '12345' for change in changes) self.assertFalse(status) status = any( change[StaticFields.TYPE] == SWITCH and change[StaticFields.ID] == '23456' and change[DSProps.EVENT_TYPE] == GraphAction.DELETE_ENTITY for change in changes) self.assertTrue(status) status = any(change[StaticFields.TYPE] == SWITCH and change[StaticFields.ID] == '34567' for change in changes) self.assertTrue(status) status = any(change[StaticFields.TYPE] == SWITCH and change[StaticFields.ID] == '45678' for change in changes) self.assertTrue(status) status = any(change[StaticFields.TYPE] == SWITCH and change[StaticFields.ID] == '56789' for change in changes) self.assertTrue(status) self.assertThat(changes, matchers.HasLength(4)) # Action changes = self.static_physical_driver.get_changes( GraphAction.UPDATE_ENTITY) # Test Assertions self.assertThat(changes, IsEmpty())
def test_get_changes(self): # Setup entities = self.static_driver.get_all(DatasourceAction.UPDATE) self.assertThat(entities, matchers.HasLength(8)) self.conf = cfg.ConfigOpts() self.conf.register_opts(self.CHANGES_OPTS, group=STATIC_DATASOURCE) self.static_driver.cfg = self.conf # Action changes = self.static_driver.get_changes(GraphAction.UPDATE_ENTITY) # Test Assertions self.assertThat(changes, IsEmpty()) for entity in changes: self._validate_static_entity(entity)
def _check_portions(self, all_items, chunks_count): chunks = [] for i in range(chunks_count): chunks.append(set(utils.get_portion(all_items, chunks_count, i))) union = (a for a in itertools.chain(*chunks)) self._assert_set_equal(union, set(all_items), 'chunks union differs') combinations = itertools.combinations(range(len(chunks)), 2) for i, j in combinations: self.assertThat(chunks[i].intersection(chunks[j]), IsEmpty(), "Each two chunks should not have " "intersecting items") max_size = len(max(chunks, key=lambda x: len(x))) min_size = len(min(chunks, key=lambda x: len(x))) expected_max_difference = 1 if len(all_items) % len(chunks) else 0 self.assertEqual(expected_max_difference, max_size - min_size, 'chunks sizes should not differ by more than 1')
def test_asserttruefalse(self): true_fail_code1 = """ test_bool = True self.assertEqual(True, test_bool) """ true_fail_code2 = """ test_bool = True self.assertEqual(test_bool, True) """ true_pass_code = """ test_bool = True self.assertTrue(test_bool) """ false_fail_code1 = """ test_bool = False self.assertEqual(False, test_bool) """ false_fail_code2 = """ test_bool = False self.assertEqual(test_bool, False) """ false_pass_code = """ test_bool = False self.assertFalse(test_bool) """ self.assertThat(list(checks.check_assert_true_false(true_fail_code1)), matchers.HasLength(1)) self.assertThat(list(checks.check_assert_true_false(true_fail_code2)), matchers.HasLength(1)) self.assertThat(list(checks.check_assert_true_false(true_pass_code)), IsEmpty()) self.assertThat(list(checks.check_assert_true_false(false_fail_code1)), matchers.HasLength(1)) self.assertThat(list(checks.check_assert_true_false(false_fail_code2)), matchers.HasLength(1)) self.assertFalse(list(checks.check_assert_true_false(false_pass_code)))
def test_get_all_functionality(self): # Step 1 - Services with status OK should not be returned # Test setup scenario zabbix_driver = MockZabbixDriver(self.conf) alarm_data1 = self._extract_alarm_data() alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2') alarm_data3 = self._extract_alarm_data(z_resource_name='compute-2', triggerid='2') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) # Test action alarms = zabbix_driver._get_all_alarms() # Test assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, IsEmpty()) # Step 2 - one raised alarm # Test setup alarm_data1 = self._extract_alarm_data(value='1') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) # Test action alarms = zabbix_driver._get_all_alarms() # Test assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(1)) self._assert_contains(alarm_data1, alarms) # Step 3 - two raised alarms # Test setup alarm_data1 = self._extract_alarm_data(value='1', priority='4') alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2', value='1') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) expected_alarm1 = alarm_data1 expected_alarm2 = copy.copy(alarm_data2) expected_alarm2[ZProps.RESOURCE_NAME] = 'host2' # Test action alarms = zabbix_driver._get_all_alarms() # Test assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(2)) self._assert_contains(expected_alarm1, alarms) self._assert_contains(expected_alarm2, alarms) # Step 4 - Check inactive alarms. Get all function should return # inactive alarm (alarm that its status has changed to OK) # Test setup alarm_data1 = self._extract_alarm_data() alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) expected_alarm1 = alarm_data1 expected_alarm2 = copy.copy(alarm_data2) expected_alarm2[ZProps.RESOURCE_NAME] = 'host2' # Test action alarms = zabbix_driver._get_all_alarms() # Test assertions # The alarms of alarm_data1/2 should be returned although their # status is OK, because they were not OK earlier self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(2)) self._assert_contains(expected_alarm1, alarms) self._assert_contains(expected_alarm2, alarms) # Step 4 - get all when all alarms are inactivated and their status # was not changed # Test action alarms = zabbix_driver._get_all_alarms() # Test assertions self.assertIsNotNone(alarms, 'alarms is None') self.assertThat(alarms, IsEmpty())
def test_edge_crud(self): g = NXGraph('test_edge_crud') g.add_vertex(v_node) g.add_vertex(v_host) g.add_edge(e_node_to_host) self.assertEqual(1, g.num_edges(), 'graph __len__ after add edge') label = e_node_to_host[EProps.RELATIONSHIP_TYPE] e = g.get_edge(v_node.vertex_id, v_host.vertex_id, label) self.assertEqual(e_node_to_host[EProps.RELATIONSHIP_TYPE], e[EProps.RELATIONSHIP_TYPE], 'edge properties are saved') self.assertEqual(e_node_to_host.source_id, e.source_id, 'edge vertex_id is saved') self.assertEqual(e_node_to_host.target_id, e.target_id, 'edge vertex_id is saved') # Edge is correct v_node_neig = g.neighbors(v_node.vertex_id, direction=Direction.OUT) self.assertThat(v_node_neig, matchers.HasLength(1), 'v_node OUT neighbor count') self.assertEqual(v_host.vertex_id, v_node_neig.pop().vertex_id, 'v_node OUT neighbor is v_host') v_node_neig = g.neighbors(v_node.vertex_id, direction=Direction.IN) self.assertThat(v_node_neig, IsEmpty(), 'v_node IN neighbor count') v_host_neig = g.neighbors(v_host.vertex_id, direction=Direction.OUT) self.assertThat(v_host_neig, IsEmpty(), 'v_host OUT neighbor count') v_host_neig = g.neighbors(v_host.vertex_id, direction=Direction.IN) self.assertThat(v_host_neig, matchers.HasLength(1), 'v_host IN neighbor count') self.assertEqual(v_node.vertex_id, v_host_neig.pop().vertex_id, 'v_host IN neighbor is v_node') # Changing the referenced item updated_e = e updated_e[EProps.VITRAGE_IS_DELETED] = 'KUKU' updated_e[EProps.UPDATE_TIMESTAMP] = 'CHANGED' # Get it again e = g.get_edge(v_node.vertex_id, v_host.vertex_id, label) self.assertFalse(e.get(EProps.VITRAGE_IS_DELETED, None), 'Change should not affect graph item') self.assertEqual(e_node_to_host[EProps.UPDATE_TIMESTAMP], e[EProps.UPDATE_TIMESTAMP], 'Change should not affect graph item') # Update the graph item and see changes take place g.update_edge(updated_e) # Get it again e = g.get_edge(v_node.vertex_id, v_host.vertex_id, label) self.assertEqual(updated_e[EProps.VITRAGE_IS_DELETED], e[EProps.VITRAGE_IS_DELETED], 'Graph item should change after update') self.assertEqual(updated_e[EProps.UPDATE_TIMESTAMP], e[EProps.UPDATE_TIMESTAMP], 'Graph item should change after update') # Update the graph item and see changes take place updated_e[EProps.VITRAGE_IS_DELETED] = None g.update_edge(updated_e) # Get it again e = g.get_edge(v_node.vertex_id, v_host.vertex_id, label) self.assertNotIn( EProps.VITRAGE_IS_DELETED, e.properties, 'Update value to None should entirely remove the key') # check metadata another_label = 'ANOTHER_LABEL' another_edge = utils.create_edge(source_id=v_node.vertex_id, target_id=v_host.vertex_id, relationship_type=another_label, metadata={'some_meta': 'DATA'}) g.add_edge(another_edge) self.assertEqual(2, g.num_edges(), 'graph __len__ after add edge') e = g.get_edge(v_node.vertex_id, v_host.vertex_id, another_label) self.assertEqual(another_edge[EProps.RELATIONSHIP_TYPE], e[EProps.RELATIONSHIP_TYPE], 'edge properties are saved') self.assertEqual('DATA', e['some_meta'], 'edge properties are saved') # Remove the item g.remove_edge(another_edge) self.assertEqual(1, g.num_edges(), 'graph __len__ after remove edge') e = g.get_edge(v_node.vertex_id, v_host.vertex_id, another_label) self.assertIsNone(e, 'removed edge not in graph') # Check get_edge returns None when item is missing edge = g.get_edge(v_host.vertex_id, 'ddd', '333') self.assertIsNone(edge) edge = g.get_edge('eee', v_node.vertex_id, '333') self.assertIsNone(edge) edge = g.get_edge(v_host.vertex_id, v_node.vertex_id, None) self.assertIsNone(edge) edge = g.get_edge(None, v_node.vertex_id, '333') self.assertIsNone(edge)
def test_get_changes_and_get_all(self): # Step 1 - get changes # Step setup zabbix_driver = MockZabbixDriver(self.conf) alarm_data1 = self._extract_alarm_data(priority='2', value='1') alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2', priority='2') alarm_data3 = self._extract_alarm_data(z_resource_name='compute-2', triggerid='2') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) # Step action alarms = zabbix_driver._get_changed_alarms() # Step assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(1)) self._assert_contains(alarm_data1, alarms) # Step 2 - get changes when no change occurred (returns nothing) # Step action alarms = zabbix_driver._get_changed_alarms() # Step assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, IsEmpty()) # Step 3 - get all # Step action alarms = zabbix_driver._get_all_alarms() # Step assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(1)) self._assert_contains(alarm_data1, alarms) # Step 4 - get all for second time # (when no change has occurred it returns the same) # Step action alarms = zabbix_driver._get_all_alarms() # Step assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(1)) self._assert_contains(alarm_data1, alarms) # Step 5 - calling get changes right after get all (returns nothing) # Step setup alarm_data1 = self._extract_alarm_data(priority='4', value='1') alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2', priority='1', value='1') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) expected_alarm1 = alarm_data1 expected_alarm2 = copy.copy(alarm_data2) expected_alarm2[ZProps.RESOURCE_NAME] = 'host2' # Step action get_all_alarms = zabbix_driver._get_all_alarms() changed_alarms = zabbix_driver._get_changed_alarms() # Step assertions self.assertIsNotNone(get_all_alarms, 'No alarms returned') self.assertThat(get_all_alarms, matchers.HasLength(2)) self._assert_contains(expected_alarm1, get_all_alarms) self._assert_contains(expected_alarm2, get_all_alarms) self.assertIsNotNone(changed_alarms, 'No alarms returned') self.assertThat(changed_alarms, IsEmpty()) # Step 6 - get changes # Step setup alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2', priority='4', value='1') alarm_data3 = self._extract_alarm_data(z_resource_name='compute-2', triggerid='2', priority='4', value='1') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) expected_alarm1 = copy.copy(alarm_data2) expected_alarm1[ZProps.RESOURCE_NAME] = 'host2' expected_alarm2 = copy.copy(expected_alarm1) expected_alarm2[ZProps.TRIGGER_ID] = '2' # Step action alarms = zabbix_driver._get_changed_alarms() # Step assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(2)) self._assert_contains(expected_alarm1, alarms) self._assert_contains(expected_alarm2, alarms)
def test_get_changes_functionality(self): # Step 1 - get changes when all alarms are OK # Test setup zabbix_driver = MockZabbixDriver(self.conf) alarm_data1 = self._extract_alarm_data(priority='2') alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2', priority='2') alarm_data3 = self._extract_alarm_data(z_resource_name='compute-2', description='Uptime', priority='3') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) # Test action alarms = zabbix_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, IsEmpty()) # Step 2 - get changes when alarm is raised # Test setup alarm_data1 = self._extract_alarm_data(priority='2', value='1') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) # Test action alarms = zabbix_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(1)) self._assert_contains(alarm_data1, alarms) # Step 3 - get changes when the priority of inactive alarm is changed # Test setup alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2', priority='3') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) # Test action alarms = zabbix_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, IsEmpty()) # Step 4 - get changes when: # 1. alarm1 - priority of active alarm is changed (should be returned) # 2. alarm2 - raised alarm (should be returned) # 3. alarm3 - priority of inactive alarm is changed (should not # be returned) # Test setup alarm_data1 = self._extract_alarm_data(priority='4', value='1') alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2', priority='1', value='1') alarm_data3 = self._extract_alarm_data(z_resource_name='compute-2', triggerid='22222', priority='1') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) expected_alarm1 = alarm_data1 expected_alarm2 = copy.copy(alarm_data2) expected_alarm2[ZProps.RESOURCE_NAME] = 'host2' # Test action alarms = zabbix_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(2)) self._assert_contains(expected_alarm1, alarms) self._assert_contains(expected_alarm2, alarms) # Step 5 - get changes when all active alarms are changed to inactive # Test setup alarm_data1 = self._extract_alarm_data(priority='4') alarm_data2 = self._extract_alarm_data(z_resource_name='compute-2', priority='1') zabbix_driver.set_alarm_datas([alarm_data1, alarm_data2, alarm_data3]) expected_alarm1 = alarm_data1 expected_alarm2 = copy.copy(alarm_data2) expected_alarm2[ZProps.RESOURCE_NAME] = 'host2' # Test action alarms = zabbix_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(alarms, 'No alarms returned') self.assertThat(alarms, matchers.HasLength(2)) self._assert_contains(expected_alarm1, alarms) self._assert_contains(expected_alarm2, alarms) # Step 6 - get changes when no change occurred # Action alarms = zabbix_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(alarms, 'alarms is None') self.assertThat(alarms, IsEmpty())
def test_get_changes(self): """Check get_changes functionality. Check the logic of which tests are returned: tests that their status was changed since the last call """ # Setup nagios_driver = MockNagiosDriver() # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_changed_alarms() # Test assertions # Services with status OK should not be returned self.assertIsNotNone(services, 'No services returned') self.assertThat(services, IsEmpty()) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(2)) self._assert_contains(service_data1, services) self._assert_contains(service_data2, services) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data2, services) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(2)) self._assert_contains(service_data1, services) self._assert_contains(service_data2, services) # Action services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'services is None') self.assertThat(services, IsEmpty())
def test_get_changes_and_get_all(self): """Check get_changes and get_all functionalities """ # Setup nagios_driver = MockNagiosDriver() # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) # Action services = nagios_driver._get_changed_alarms() # Test assertions # Calling get_changes for the second time should return nothing self.assertIsNotNone(services, 'No services returned') self.assertThat(services, IsEmpty()) # Action services = nagios_driver._get_all_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) # Action services = nagios_driver._get_all_alarms() # Test assertions # Calling get_all for the second time should return the same results self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_all_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(2)) self._assert_contains(service_data1, services) self._assert_contains(service_data2, services) # Action services = nagios_driver._get_changed_alarms() # Test assertions # Calling get_changes after get_all should return nothing self.assertIsNotNone(services, 'No services returned') self.assertThat(services, IsEmpty()) # Action services = nagios_driver._get_all_alarms() # Test assertions # Calling get_all for the second time should return the same results self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(2)) self._assert_contains(service_data1, services) self._assert_contains(service_data2, services) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(2)) self._assert_contains(service_data2, services) self._assert_contains(service_data3, services) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) # Action services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'services is None') self.assertThat(services, IsEmpty()) # Action services = nagios_driver._get_all_alarms() # Test assertions # Calling get_all for the second time should return the same results self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(3)) self._assert_contains(service_data1, services) self._assert_contains(service_data2, services) self._assert_contains(service_data3, services)
def test_get_all(self): """Check get_all functionality. Check the logic of which tests are returned: tests that are not OK, or tests that were changed from not-OK to OK """ # Setup nagios_driver = MockNagiosDriver() # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_all_alarms() # Test assertions # Services with status OK should not be returned self.assertIsNotNone(services, 'No services returned') self.assertThat(services, IsEmpty()) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_all_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.CRITICAL } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_all_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(2)) self._assert_contains(service_data1, services) self._assert_contains(service_data2, services) # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_all_alarms() # Test assertions # The services of service_data1/2 should be returned although their # status is OK, because they were not OK earlier self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(2)) self._assert_contains(service_data1, services) self._assert_contains(service_data2, services) # Action services = nagios_driver._get_all_alarms() # Test assertions # Calling get_services again should not return anything, since all # services are still OK self.assertIsNotNone(services, 'services is None') self.assertThat(services, IsEmpty())
def test_no_log_warn(self): self.assertThat(list(checks.no_log_warn('LOG.warning("bl")')), IsEmpty()) self.assertThat(list(checks.no_log_warn('LOG.warn("foo")')), matchers.HasLength(1))
def test_delete_service(self): """Check get_all and get_changes with a deleted service""" # Setup nagios_driver = MockNagiosDriver() # Action service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } service_data3 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'Uptime', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas( [service_data1, service_data2, service_data3]) services = nagios_driver._get_all_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) # Action - delete a service that was OK service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas([service_data1, service_data2]) services = nagios_driver._get_all_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) # Action - delete a service that was not OK service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas([service_data2]) services = nagios_driver._get_all_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) self.assertEqual(GraphAction.DELETE_ENTITY, services[0][DSProps.EVENT_TYPE]) # Action - get changes, should not return the deleted alarm again services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'services is None') self.assertThat(services, IsEmpty()) # Action - "undelete" the service that was OK service_data1 = { NagiosProps.RESOURCE_NAME: 'compute-0', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.WARNING } service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas([service_data1, service_data2]) services = nagios_driver._get_all_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) self.assertNotIn(DSProps.EVENT_TYPE, services[0]) # Action - delete a service that was not OK and call get_changes service_data2 = { NagiosProps.RESOURCE_NAME: 'compute-1', NagiosProps.SERVICE: 'CPU utilization', NagiosProps.STATUS: NagiosTestStatus.OK } nagios_driver.set_service_datas([service_data2]) services = nagios_driver._get_changed_alarms() # Test assertions self.assertIsNotNone(services, 'No services returned') self.assertThat(services, matchers.HasLength(1)) self._assert_contains(service_data1, services) self.assertEqual(GraphAction.DELETE_ENTITY, services[0][DSProps.EVENT_TYPE])