def _create_mock_events():
     gen_list = mock_sync.simple_zone_generators(
         2, 4, snapshot_events=2, snap_vals={'sync_mode': 'init_snapshot'})
     gen_list += mock_sync.simple_host_generators(
         2, 4, 4, snap_vals={'sync_mode': 'init_snapshot'})
     gen_list += mock_sync.simple_instance_generators(
         4, 15, 15, snap_vals={'sync_mode': 'init_snapshot'})
     return mock_sync.generate_sequential_events_list(gen_list)
    def _create_event(self, spec_type=None, sync_mode=None,
                      event_type=None, properties=None):
        # generate event
        spec_list = mock_sync.simple_instance_generators(1, 1, 1)
        events_list = mock_sync.generate_random_events_list(
            spec_list)

        # update properties
        if sync_mode is not None:
            events_list[0]['sync_mode'] = sync_mode

        if event_type is not None:
            events_list[0]['event_type'] = event_type

        if properties is not None:
            for key, value in properties.iteritems():
                events_list[0][key] = value

        return events_list[0]
    def test_extract_key(self):
        LOG.debug('Test get key from nova instance transformer')

        # Test setup
        spec_list = mock_sync.simple_instance_generators(
            host_num=1,
            vm_num=1,
            snapshot_events=1,
            update_events=0
        )
        instance_events = mock_sync.generate_random_events_list(spec_list)

        instance_transformer = InstanceTransformer(self.transformers)
        for event in instance_events:
            # Test action
            observed_key = instance_transformer.extract_key(event)

            # Test assertions
            observed_key_fields = observed_key.split(
                TransformerBase.KEY_SEPARATOR)

            self.assertEqual(EntityTypes.RESOURCE, observed_key_fields[0])
            self.assertEqual(
                InstanceTransformer.INSTANCE_TYPE,
                observed_key_fields[1]
            )

            instance_id = tbase.extract_field_value(
                event,
                instance_transformer.INSTANCE_ID[event['sync_mode']]
            )

            self.assertEqual(instance_id, observed_key_fields[2])

            key_values = instance_transformer._key_values([instance_id])
            expected_key = tbase.build_key(key_values)

            self.assertEqual(expected_key, observed_key)
    def test_snapshot_event_transform(self):
        LOG.debug('Test tactual transform action for '
                  'snapshot and snapshot init events')

        # Test setup
        spec_list = mock_sync.simple_instance_generators(
            host_num=1,
            vm_num=1,
            snapshot_events=10,
            update_events=0
        )
        instance_events = mock_sync.generate_random_events_list(spec_list)

        for event in instance_events:
            # Test action
            instance_transformer = InstanceTransformer(self.transformers)
            wrapper = instance_transformer.transform(event)

            # Test assertions
            self._validate_vertex_props(wrapper.vertex, event)

            # Validate the neighbors: only one  valid host neighbor
            self.assertEqual(
                1,
                len(wrapper.neighbors),
                'Instance has only one host neighbor'
            )
            host_neighbor = wrapper.neighbors[0]
            self._validate_host_neighbor(host_neighbor, event)

            sync_mode = event['sync_mode']

            if sync_mode == SyncMode.INIT_SNAPSHOT:
                self.assertEqual(EventAction.CREATE, wrapper.action)
            elif sync_mode == SyncMode.SNAPSHOT:
                self.assertEqual(EventAction.UPDATE, wrapper.action)
    def test_update_event_transform(self):
        LOG.debug('Test tactual transform action for update events')

        # Test setup
        spec_list = mock_sync.simple_instance_generators(
            host_num=1,
            vm_num=1,
            snapshot_events=0,
            update_events=10
        )
        instance_events = mock_sync.generate_random_events_list(spec_list)

        for event in instance_events:
            # Test action
            instance_transformer = InstanceTransformer(self.transformers)
            wrapper = instance_transformer.transform(event)

            # Test assertions
            self._validate_vertex_props(wrapper.vertex, event)

            # Validate the neighbors: only one  valid host neighbor
            self.assertEqual(
                1,
                len(wrapper.neighbors),
                'Instance has only one host neighbor'
            )
            host_neighbor = wrapper.neighbors[0]
            self._validate_host_neighbor(host_neighbor, event)

            event_type = event['event_type']
            if event_type == 'compute.instance.delete.end':
                self.assertEqual(EventAction.DELETE, wrapper.action)
            elif event_type == 'compute.instance.create.start':
                self.assertEqual(EventAction.CREATE, wrapper.action)
            else:
                self.assertEqual(EventAction.UPDATE, wrapper.action)