def test_create_host_with_existing_host_different_ssh_key(self):
        """
        Verify create_host returns conflict existing hosts ssh keys do not match.
        """
        bus = mock.MagicMock()
        different = HOST.to_dict()
        different['ssh_priv_key'] = 'aaa'

        bus.request.return_value = create_response(ID, different)

        self.assertEquals(
            expected_error(ID, JSONRPC_ERRORS['CONFLICT']),
            hosts.create_host(SIMPLE_HOST_REQUEST, bus))
    def test_create_host(self):
        """
        Verify create_host saves new hosts.
        """
        bus = mock.MagicMock()
        bus.request.side_effect = (
            # Host doesn't exist yet
            _bus.RemoteProcedureCallError('test'),
            # Result from save
            create_response(ID, HOST.to_dict())
        )

        self.assertEquals(
            create_response(ID, HOST.to_dict()),
            hosts.create_host(SIMPLE_HOST_REQUEST, bus))
    def test_create_host_with_the_same_existing_host(self):
        """
        Verify create_host succeeds when a new host matches an existing one.
        """
        bus = mock.MagicMock()
        bus.request.side_effect = (
            # Existing host
            create_response(ID, HOST.to_dict(True)),
            # Result from save
            create_response(ID, HOST.to_dict())
        )

        self.assertEquals(
            create_response(ID, HOST.to_dict()),
            hosts.create_host(SIMPLE_HOST_REQUEST, bus))
    def test_create_host_with_invalid_cluster(self):
        """
        Verify create_host returns INVALID_PARAMETERS when the cluster does not exist.
        """
        bus = mock.MagicMock()

        bus.request.side_effect = (
            # Host doesn't exist yet
            _bus.RemoteProcedureCallError('test'),
            # Request the cluster which does not exist
            _bus.RemoteProcedureCallError('test')
        )

        self.assertEquals(
            expected_error(ID, JSONRPC_ERRORS['INVALID_PARAMETERS']),
            hosts.create_host(CLUSTER_HOST_REQUEST, bus))
    def test_create_host_without_an_address(self):
        """
        Verify create_host returns INVALID_PARAMETERS when no address is given.
        """
        bus = mock.MagicMock()
        bus.request.side_effect = (
            # Host doesn't exist yet
            _bus.RemoteProcedureCallError('test'),
            # Result from save
            create_response(ID, HOST.to_json())
        )

        addressless = copy.deepcopy(SIMPLE_HOST_REQUEST)
        addressless['params'] = {}

        self.assertEquals(
            expected_error(ID, JSONRPC_ERRORS['INVALID_PARAMETERS']),
            hosts.create_host(addressless, bus))
    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'] = ''

        bus.request.side_effect = (
            # Existing host
            create_response(ID, different),
            # Cluster
            create_response(ID, {'hostset': []}),
            create_response(ID, {'hostset': []})
        )

        self.assertEquals(
            expected_error(ID, JSONRPC_ERRORS['CONFLICT']),
            hosts.create_host(CLUSTER_HOST_REQUEST, bus))
    def test_create_host_with_cluster(self):
        """
        Verify create_host saves new hosts with it's cluster.
        """
        bus = mock.MagicMock()

        bus.request.side_effect = (
            # Host doesn't exist yet
            _bus.RemoteProcedureCallError('test'),
            # Request the cluster
            create_response(ID, {'hostset': []}),
            # Save of the cluster
            create_response(ID, {'hostset': []}),
            # Result from save (ignored)
            create_response(ID, HOST.to_dict())
        )

        self.assertEquals(
            create_response(ID, HOST.to_dict()),
            hosts.create_host(CLUSTER_HOST_REQUEST, bus))