def test_handle_heartbeat_missing_key(self):
     """
     If the given data is missing a required key, log a warning and
     return.
     """
     with patch.object(monitor_shove_instances, 'log') as log:
         monitor_shove_instances.handle_heartbeat_event({'incorrect': 'keys'})
         assert_true(log.warning.called)
         assert_equal(ShoveInstance.objects.count(), 0)
 def test_handle_heartbeat_project_names_not_string(self):
     """
     If the project name isn't a string, log a warning and return.
     """
     with patch.object(monitor_shove_instances, 'log') as log:
         monitor_shove_instances.handle_heartbeat_event({
             'routing_key': 'asdf',
             'hostname': 'paranoia.local',
             'project_names': ['foo', 'bar']
         })
         assert_true(log.warning.called)
         assert_equal(ShoveInstance.objects.count(), 0)
    def test_handle_heartbeat_remove_projects(self):
        """Remove any old projects not in the project_names list."""
        instance = ShoveInstanceFactory.create(routing_key='asdf')
        project1 = ProjectFactory.create(project_name='foo')
        project2 = ProjectFactory.create(project_name='bar')
        instance.projects.add(project1)

        monitor_shove_instances.handle_heartbeat_event({
            'routing_key': 'asdf',
            'hostname': 'paranoia.local',
            'project_names': 'bar,baz',
        })

        instance = ShoveInstance.objects.get(routing_key='asdf')
        assert_equal(list(instance.projects.all()), [project2])
    def test_handle_hearbeat_new_shove_instance(self):
        """
        If there is no existing shove instance for the given routing
        key, create a new one.
        """
        assert_equal(ShoveInstance.objects.count(), 0)
        monitor_shove_instances.handle_heartbeat_event({
            'routing_key': 'asdf',
            'hostname': 'paranoia.local',
            'project_names': '',
        })

        instance = ShoveInstance.objects.get(routing_key='asdf')
        assert_equal(instance.hostname, 'paranoia.local')
        assert_equal(instance.projects.count(), 0)
    def test_handle_heartbeat_existing_shove_instance(self):
        """
        If a shove instance for the given routing key already exists,
        update it with the latest hostname and projects.
        """
        instance = ShoveInstanceFactory.create(routing_key='asdf', hostname='old.local')
        project1 = ProjectFactory.create(project_name='foo')

        monitor_shove_instances.handle_heartbeat_event({
            'routing_key': 'asdf',
            'hostname': 'paranoia.local',
            'project_names': 'foo',
        })
        instance = ShoveInstance.objects.get(pk=instance.pk)
        assert_equal(instance.hostname, 'paranoia.local')
        assert_equal(list(instance.projects.all()), [project1])
    def test_handle_heartbeat_add_new_projects(self):
        """
        Add any projects specified by the project_names value to the
        list of projects supported by the ShoveInstance.
        """
        project1 = ProjectFactory.create(project_name='foo')
        project2 = ProjectFactory.create(project_name='bar')

        with patch.object(monitor_shove_instances, 'log') as log:
            monitor_shove_instances.handle_heartbeat_event({
                'routing_key': 'asdf',
                'hostname': 'paranoia.local',
                'project_names': 'foo,bar,baz',
            })
            # Warn about missing project but do not abort.
            assert_true(log.warning.called)

        instance = ShoveInstance.objects.get(routing_key='asdf')
        assert_equal(set(instance.projects.all()), set([project1, project2]))
    def test_handle_heartbeat_blank_keys(self):
        """
        If the routing key or hostname are blank, log a warning and
        return.
        """
        with patch.object(monitor_shove_instances, 'log') as log:
            monitor_shove_instances.handle_heartbeat_event({
                'routing_key': '',
                'hostname': 'paranoia.local',
                'project_names': 'foo,bar',
            })
            assert_true(log.warning.called)
            assert_equal(ShoveInstance.objects.count(), 0)

            log.warning.reset_mock()
            monitor_shove_instances.handle_heartbeat_event({
                'routing_key': 'asdf',
                'hostname': '',
                'project_names': 'foo,bar',
            })
            assert_true(log.warning.called)
            assert_equal(ShoveInstance.objects.count(), 0)