def test_update_cluster_members_with_failed_old_host(self):
        """
        Verify that update_cluster_members keeps members regardless of status
        """
        bus = mock.MagicMock()
        old_hostset = ['192.168.1.1']
        new_hostset = old_hostset + ['192.168.1.2']
        cluster = Cluster.new(name='test', hostset=old_hostset)
        hosts = [Host.new(address=new_hostset[1], status=C.HOST_STATUS_ACTIVE)]

        bus.storage.get_cluster.return_value = cluster
        bus.storage.get_many.return_value = hosts

        message = {
            'jsonrpc': '2.0',
            'id': ID,
            'params': {
                'name': 'test',
                'old': old_hostset,
                'new': new_hostset
            }
        }

        with mock.patch('commissaire_http.handlers.clusters.'
                        'update_new_cluster_member_status') as uncms:
            result = clusters.update_cluster_members.handler(message, bus)

        # Check the 1st positional argument.
        args, kwargs = bus.storage.get_many.call_args
        list_of_host_models = args[0]
        self.assertEquals(
            [x.address for x in list_of_host_models],
            ['192.168.1.2'])
    def test_update_new_cluster_member_status(self):
        """
        Verify update_new_cluster_member_status works as expected
        """
        bus = mock.MagicMock()
        hosts = (
            Host.new(address='192.168.1.1'),
            Host.new(address='192.168.1.2')
        )
        cluster = Cluster.new(name='test')

        cluster.container_manager = None
        clusters.update_new_cluster_member_status(bus, cluster, *hosts)
        bus.request.assert_not_called()
        for n in range(len(hosts)):
            self.assertEquals(hosts[n].status, C.HOST_STATUS_DISASSOCIATED)
        bus.storage.save_many.assert_called_once_with(hosts)

        bus.reset_mock()

        cluster.container_manager = C.CONTAINER_MANAGER_OPENSHIFT
        clusters.update_new_cluster_member_status(bus, cluster, *hosts)
        for n in range(len(hosts)):
            bus.request.assert_any_call(
                'container.register_node',
                params=[C.CONTAINER_MANAGER_OPENSHIFT, hosts[n].address])
            self.assertEquals(hosts[n].status, C.HOST_STATUS_ACTIVE)
        bus.storage.save_many.assert_called_once_with(hosts)
    def test_update_cluster_members_with_valid_input(self):
        """
        Verify that update_cluster_members handles valid input.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(
            name='test', hostset=['127.0.0.1'])

        bus.storage.get_cluster.return_value = cluster
        bus.storage.save.return_value = cluster

        message = {
            'jsonrpc': '2.0',
            'id': ID,
            'params': {
                'name': 'test',
                'old': ['127.0.0.1'],
                'new': []
            }
        }

        with mock.patch('commissaire_http.handlers.clusters.'
                        'update_new_cluster_member_status') as uncms:
            result = clusters.update_cluster_members.handler(message, bus)

        self.assertEquals([], result['result']['hostset'])
Пример #4
0
    def test_delete_host_thats_in_a_container_manager(self):
        """
        Verify delete_host deletes existing host and removes it from its cluster
        and container manager.
        """
        bus = mock.MagicMock()
        # The delete shouldn't return anything
        bus.storage.delete.return_value = None
        # The cluster response on save (which is ignored)
        bus.storage.save.return_value = None
        # The clusters list
        bus.storage.list.return_value = Clusters.new(clusters=[
            Cluster.new(name='mycluster',
                        hostset=[HOST.address],
                        container_manager='test')
        ])
        self.assertEquals({
            'jsonrpc': '2.0',
            'result': [],
            'id': '123',
        }, hosts.delete_host.handler(SIMPLE_HOST_REQUEST, bus))

        # Verify we had a host delete_host
        bus.storage.delete.assert_called_with(mock.ANY)
        # Verify we had a list of clusters
        bus.storage.list.assert_called_with(Clusters)
        # Verify we had a 'container.remove_node'
        bus.request.assert_called_with('container.remove_node',
                                       params=mock.ANY)
        # Verify we had a cluster save
        bus.storage.save.assert_called_with(mock.ANY)
    def test_update_cluster_members_with_failed_new_host(self):
        """
        Verify that update_cluster_members rejects a failed host
        """
        bus = mock.MagicMock()
        old_hostset = ['192.168.1.1']
        new_hostset = old_hostset + ['192.168.1.2']
        cluster = Cluster.new(name='test', hostset=old_hostset)
        hosts = [Host.new(address=new_hostset[0], status=C.HOST_STATUS_ACTIVE),
                 Host.new(address=new_hostset[1], status=C.HOST_STATUS_FAILED)]

        bus.storage.get_cluster.return_value = cluster
        bus.storage.get_many.return_value = hosts

        message = {
            'jsonrpc': '2.0',
            'id': ID,
            'params': {
                'name': 'test',
                'old': old_hostset,
                'new': new_hostset
            }
        }

        with mock.patch('commissaire_http.handlers.clusters.'
                        'update_new_cluster_member_status') as uncms:
            result = clusters.update_cluster_members.handler(message, bus)

        self.assertEquals(
            result, expected_error(ID, JSONRPC_ERRORS['METHOD_NOT_ALLOWED']))
Пример #6
0
 def test_get_uniform_model_type_with_multiple_types(self):
     """
     Verify get_uniform_model_type raises when types are not the same.
     """
     self.assertRaises(
         TypeError,
         storage.get_uniform_model_type,
         [Host.new(address='127.0.0.1'),
          Cluster.new(name='test')])
 def test_list_cluster_members(self):
     """
     Verify that list_cluster_members returns proper information.
     """
     bus = mock.MagicMock()
     bus.storage.get_cluster.return_value = Cluster.new(
         name='test', hostset=['127.0.0.1'])
     self.assertEquals(
         create_jsonrpc_response(ID, ['127.0.0.1']),
         clusters.list_cluster_members.handler(SIMPLE_CLUSTER_REQUEST, bus))
    def test_create_cluster(self):
        """
        Verify create_cluster saves new clusters.
        """
        bus = mock.MagicMock()
        cluster_json = Cluster.new(name='test').to_json()
        bus.request.return_value = create_response(ID, cluster_json)

        self.assertEquals(create_response(ID, cluster_json),
                          clusters.create_cluster(SIMPLE_CLUSTER_REQUEST, bus))
    def test_delete_cluster_member_with_valid_member(self):
        """
        Verify that delete_cluster_member actually removes a member.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(name='test', hostset=['127.0.0.1'])

        bus.request.return_value = create_response(ID, cluster.to_dict(True))
        self.assertEquals(
            create_response(ID, []),
            clusters.delete_cluster_member(CHECK_CLUSTER_REQUEST, bus))
    def test_add_cluster_member_with_valid_member(self):
        """
        Verify that add_cluster_member actually adds a new member..
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(name='test', hostset=[])

        bus.request.return_value = create_response(ID, cluster.to_dict(True))
        expected_response = create_response(ID, ['127.0.0.1'])
        self.assertEquals(
            expected_response,
            clusters.add_cluster_member(CHECK_CLUSTER_REQUEST, bus))
    def test_check_cluster_member_with_valid_member(self):
        """
        Verify that check_cluster_member returns proper data when a valid member is requested.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(name='test', hostset=['127.0.0.1'])

        bus.request.return_value = create_response(ID, cluster.to_dict(True))

        self.assertEquals(
            create_response(ID, ['127.0.0.1']),
            clusters.check_cluster_member(CHECK_CLUSTER_REQUEST, bus))
Пример #12
0
    def _get_cluster_and_network_models(self, cluster_data):
        """
        Creates cluster and network models from the given cluster data.

        :param cluster_data: Data for a cluster
        :type cluster_data: dict
        :returns: a Cluster and Network model
        :rtype: tuple
        """
        try:
            cluster = Cluster.new(**cluster_data)
            network = Network.new(name=cluster.network)
            params = {
                'model_type_name': network.__class__.__name__,
                'model_json_data': network.to_json()
            }
            response = self.request('storage.get', params=params)
            network = Network.new(**response['result'])
        except TypeError:
            cluster = Cluster.new(type=C.CLUSTER_TYPE_HOST)
            network = Network.new(**C.DEFAULT_CLUSTER_NETWORK_JSON)

        return cluster, network
Пример #13
0
 def test_delete_host(self):
     """
     Verify delete_host deletes existing hosts.
     """
     bus = mock.MagicMock()
     # The delete shouldn't return anything
     bus.storage.delete.return_value = None
     bus.storage.list.return_value = Clusters.new(
         clusters=[Cluster.new(name='test')])
     self.assertEquals({
         'jsonrpc': '2.0',
         'result': [],
         'id': '123',
     }, hosts.delete_host.handler(SIMPLE_HOST_REQUEST, bus))
Пример #14
0
    def test_create_host_with_existing_host_different_cluster_memebership(
            self):
        """
        Verify create_host returns conflict existing cluster memebership does not match.
        """
        bus = mock.MagicMock()
        different = HOST.to_dict()
        different['ssh_priv_key'] = ''

        # Existing host
        bus.storage.get_host.return_value = Host.new(**different)
        # Cluster
        bus.storage.get_cluster.return_value = Cluster.new(name='test')

        self.assertEquals(expected_error(ID, JSONRPC_ERRORS['CONFLICT']),
                          hosts.create_host.handler(CLUSTER_HOST_REQUEST, bus))
    def test_create_cluster_with_valid_network(self):
        """
        Verify create_cluster uses valid networks as expected.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(name='test', network='test')
        # The cluster doesn't exist yet
        bus.storage.get_cluster.side_effect = Exception
        # Network response
        bus.storage.get_network.return_value = Network.new(name='test')
        # Creation of the cluster
        bus.storage.save.return_value = cluster

        # Call the handler...
        clusters.create_cluster.handler(copy.deepcopy(NETWORK_CLUSTER_REQUEST), bus)

        bus.storage.save.assert_called_with(mock.ANY)
 def test_list_cluster_members(self):
     """
     Verify that list_cluster_members returns proper information.
     """
     bus = mock.MagicMock()
     bus.request.return_value = {
         'jsonrpc':
         '2.0',
         'result':
         Cluster.new(name='test',
                     hostset=['127.0.0.1']).to_dict(secure=True),
         'id':
         ID
     }
     self.assertEquals(
         create_response(ID, ['127.0.0.1']),
         clusters.list_cluster_members(SIMPLE_CLUSTER_REQUEST, bus))
Пример #17
0
    def _get_cluster_and_network_models(self, cluster_data):
        """
        Creates cluster and network models from the given cluster data.

        :param cluster_data: Data for a cluster
        :type cluster_data: dict
        :returns: a Cluster and Network model
        :rtype: tuple
        """
        try:
            cluster = Cluster.new(**cluster_data)
            network = self.storage.get_network(cluster.network)
        except TypeError:
            cluster = None
            network = Network.new(**C.DEFAULT_CLUSTER_NETWORK_JSON)

        return cluster, network
    def test_create_cluster_with_invalid_data(self):
        """
        Verify create_cluster saves new clusters.
        """
        bus = mock.MagicMock()
        # names must be a str, not an int
        bad_cluster = Cluster.new(name=123)

        bus.storage.get_cluster.side_effect = Exception
        bus.storage.save.side_effect = ValidationError

        self.assertEquals(
            expected_error(ID, JSONRPC_ERRORS['INVALID_REQUEST']),
            clusters.create_cluster.handler({
                'jsonrpc': '2.0',
                'id': ID,
                'params': {'name': bad_cluster.name}
                }, bus))
    def test_delete_cluster_member_with_container_manager(self):
        """
        Verify that delete_cluster_member handles a container manager
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(
            name='test', hostset=['127.0.0.1'],
            container_manager=C.CONTAINER_MANAGER_OPENSHIFT)

        bus.storage.get_cluster.return_value = cluster
        bus.storage.save.return_value = None

        self.assertEquals(
            create_jsonrpc_response(ID, []),
            clusters.delete_cluster_member.handler(CHECK_CLUSTER_REQUEST, bus))

        # Verify we had a 'container.remove_node'
        bus.request.assert_called_with('container.remove_node', params=mock.ANY)
    def test_delete_cluster_member_with_valid_member(self):
        """
        Verify that delete_cluster_member actually removes a member.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(
            name='test', hostset=['127.0.0.1'])

        bus.storage.get_cluster.return_value = cluster
        bus.storage.save.return_value = None

        self.assertEquals(
            create_jsonrpc_response(ID, []),
            clusters.delete_cluster_member.handler(CHECK_CLUSTER_REQUEST, bus))

        # Verify we did NOT have a 'container.remove_node'
        # XXX Fragile; will break if another bus.request call is added.
        bus.request.assert_not_called()
    def test_check_cluster_member_with_invalid_member(self):
        """
        Verify that check_cluster_member returns proper data when an invalid member is requested.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(
            name='test', hostset=['127.0.0.1'])

        bus.storage.get_cluster.return_value = cluster

        result = clusters.check_cluster_member.handler({
            'jsonrpc': '2.0',
            'id': ID,
            'params': {'name': 'test', 'host': '127.0.0.2'}
        }, bus)

        self.assertEquals(
            expected_error(ID, JSONRPC_ERRORS['NOT_FOUND']),
            result)
    def test_create_cluster_with_invalid_network(self):
        """
        Verify create_cluster reacts to invalid networks as expected.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(name='test', network='test')
        # The cluster doesn't exist yet
        bus.storage.get_cluster.side_effect = Exception
        # The network doesn't exist
        bus.storage.get_network.side_effect = Exception
        # The cluster creation
        bus.storage.save.return_value = cluster

        # Call the handler...
        clusters.create_cluster.handler(copy.deepcopy(NETWORK_CLUSTER_REQUEST), bus)
        # Update clusters network to be 'default' as we expect 'test' to be
        # rejected by the handler
        cluster.network = 'default'
        bus.storage.save.assert_called_with(mock.ANY)
Пример #23
0
    def test_create_host_with_cluster(self):
        """
        Verify create_host saves new hosts with it's cluster.
        """
        bus = mock.MagicMock()

        # Host doesn't exist yet
        bus.storage.get_host.side_effect = _bus.RemoteProcedureCallError(
            'test')
        # Request the cluster
        bus.storage.get_cluster.return_value = Cluster.new(name='mycluster')
        bus.storage.save.side_effect = (
            # Cluster save (ignored)
            None,
            # Result from save
            HOST)

        self.assertEquals(create_jsonrpc_response(ID, HOST.to_dict_safe()),
                          hosts.create_host.handler(CLUSTER_HOST_REQUEST, bus))
    def test_update_cluster_members_with_conflicting_input(self):
        """
        Verify that update_cluster_members handles conflicting input.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(
            name='test', hostset=['127.0.0.1'])

        bus.storage.get_cluster.return_value = cluster

        result = clusters.update_cluster_members.handler({
            'jsonrpc': '2.0',
            'id': '123',
            'params': {'name': 'test', 'old': [], 'new': []}
        }, bus)

        self.assertEquals(
            expected_error(ID, JSONRPC_ERRORS['CONFLICT']),
            result)
    def test_create_cluster_with_valid_network(self):
        """
        Verify create_cluster uses valid networks as expected.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(name='test', network='test')
        bus.request.side_effect = (
            # The cluster doesn't exist yet
            Exception,
            # Network response
            Network.new(name='test'),
            # Creation of the cluster
            create_response(ID, cluster.to_json()),
        )

        # Call the handler...
        clusters.create_cluster(copy.deepcopy(NETWORK_CLUSTER_REQUEST), bus)

        bus.request.assert_called_with('storage.save',
                                       params=['Cluster',
                                               cluster.to_dict()])
    def test_add_cluster_member_with_valid_member(self):
        """
        Verify that add_cluster_member actually adds a new member..
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(
            name='test', hostset=[])

        bus.storage.get_host.return_value = Host.new(
            address='127.0.0.1', status=C.HOST_STATUS_DISASSOCIATED)
        bus.storage.get_cluster.return_value = cluster
        bus.storage.save.return_value = None

        expected_response = create_jsonrpc_response(ID, ['127.0.0.1'])

        with mock.patch('commissaire_http.handlers.clusters.'
                        'update_new_cluster_member_status') as uncms:
            actual_response = clusters.add_cluster_member.handler(
                CHECK_CLUSTER_REQUEST, bus)

        self.assertEquals(actual_response, expected_response)
    def test_update_cluster_members_with_valid_input(self):
        """
        Verify that update_cluster_members handles valid input.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(name='test', hostset=['127.0.0.1'])

        bus.request.return_value = create_response(ID, cluster.to_dict(True))

        result = clusters.update_cluster_members(
            {
                'jsonrpc': '2.0',
                'id': '123',
                'params': {
                    'name': 'test',
                    'old': ['127.0.0.1'],
                    'new': []
                }
            }, bus)

        self.assertEquals([], result['result']['hostset'])
    def test_create_cluster_with_invalid_network(self):
        """
        Verify create_cluster reacts to invalid networks as expected.
        """
        bus = mock.MagicMock()
        cluster = Cluster.new(name='test', network='test')
        bus.request.side_effect = (
            # The cluster doesn't exist yet
            Exception,
            # The network doesn't exist
            Exception,
            # The cluster creation
            create_response(ID, cluster.to_json()),
        )

        # Call the handler...
        clusters.create_cluster(copy.deepcopy(NETWORK_CLUSTER_REQUEST), bus)
        # Update clusters network to be 'default' as we expect 'test' to be
        # rejected by the handler
        cluster.network = 'default'
        bus.request.assert_called_with('storage.save',
                                       params=['Cluster',
                                               cluster.to_dict()])
    def test_add_cluster_member_with_various_status(self):
        """
        Verify that add_cluster_member rejects hosts with bad status
        """
        bus = mock.MagicMock()

        host = Host.new(address='127.0.0.1')
        cluster = Cluster.new(name='test')

        bus.storage.get_host.return_value = host
        bus.storage.get_cluster.return_value = cluster
        bus.storage.save.return_value = None

        host_statuses = [
            (C.HOST_STATUS_INVESTIGATING, False),
            (C.HOST_STATUS_BOOTSTRAPPING, False),
            (C.HOST_STATUS_ACTIVE, True),
            (C.HOST_STATUS_DISASSOCIATED, True),
            (C.HOST_STATUS_FAILED, False)
        ]

        for status, expect_to_add in host_statuses:
            host.status = status
            cluster.hostset = []

            with mock.patch('commissaire_http.handlers.clusters.'
                            'update_new_cluster_member_status') as uncms:
                actual_result = clusters.add_cluster_member.handler(
                    CHECK_CLUSTER_REQUEST, bus)

            if expect_to_add:
                expected_result = create_jsonrpc_response(ID, ['127.0.0.1'])
            else:
                expected_result = expected_error(
                    ID, JSONRPC_ERRORS['METHOD_NOT_ALLOWED'])

            self.assertEquals(actual_result, expected_result)
Пример #30
0
    def test_get_host_status_with_container_manager(self):
        """
        Verify get_host status includes container manager status
        """
        bus = mock.MagicMock()
        bus.storage.get_host.return_value = HOST

        cluster = Cluster.new(name='test',
                              hostset=['127.0.0.1'],
                              container_manager='trivial')
        bus.storage.list.return_value = Clusters.new(clusters=[cluster])

        # XXX Fragile; will break if another bus.request call is added.
        bus.request.return_value = {'status': 'ok'}

        host_status = HostStatus.new(host={
            'last_check': '',
            'status': ''
        },
                                     type='host_only',
                                     container_manager={'status': 'ok'})
        self.assertEquals(
            create_jsonrpc_response(ID, host_status.to_dict()),
            hosts.get_host_status.handler(SIMPLE_HOST_REQUEST, bus))