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_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_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_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.request.side_effect = Exception

        self.assertEquals(
            expected_error(ID, JSONRPC_ERRORS['INVALID_REQUEST']),
            clusters.create_cluster(
                {
                    'jsonrpc': '2.0',
                    'id': ID,
                    'params': {
                        'name': bad_cluster.name
                    }
                }, bus))