Example #1
0
class TestClusterControllerWithStrategy(trove_testtools.TestCase):
    def setUp(self):
        super(TestClusterControllerWithStrategy, self).setUp()
        self.controller = ClusterController()
        self.cluster = {
            "cluster": {
                "name":
                "products",
                "datastore": {
                    "type": "redis",
                    "version": "3.0"
                },
                "instances": [
                    {
                        "flavorRef": "7",
                        "volume": {
                            "size": 1
                        },
                    },
                    {
                        "flavorRef": "7",
                        "volume": {
                            "size": 1
                        },
                    },
                    {
                        "flavorRef": "7",
                        "volume": {
                            "size": 1
                        },
                    },
                ]
            }
        }

    def tearDown(self):
        super(TestClusterControllerWithStrategy, self).tearDown()
        cfg.CONF.clear_override('cluster_support', group='redis')
        cfg.CONF.clear_override('api_strategy', group='redis')

    @patch.object(datastore_models, 'get_datastore_version')
    @patch.object(models.Cluster, 'create')
    def test_create_clusters_disabled(self, mock_cluster_create,
                                      mock_get_datastore_version):

        cfg.CONF.set_override('cluster_support', False, group='redis')

        body = self.cluster
        tenant_id = Mock()
        context = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = 'redis'
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        self.assertRaisesRegexp(exception.TroveError,
                                "Clusters not supported for",
                                self.controller.create, req, body, tenant_id)

    @patch.object(views.ClusterView, 'data', return_value={})
    @patch.object(datastore_models, 'get_datastore_version')
    @patch.object(models.Cluster, 'create')
    def test_create_clusters_enabled(self, mock_cluster_create,
                                     mock_get_datastore_version,
                                     mock_cluster_view_data):

        cfg.CONF.set_override('cluster_support', True, group='redis')

        body = self.cluster
        tenant_id = Mock()
        context = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = 'redis'
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        mock_cluster = Mock()
        mock_cluster.datastore_version.manager = 'redis'
        mock_cluster_create.return_value = mock_cluster
        self.controller.create(req, body, tenant_id)

    @patch.object(models.Cluster, 'load')
    def test_controller_action_no_strategy(self, mock_cluster_load):

        body = {'do_stuff2': {}}
        tenant_id = Mock()
        context = Mock()
        id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.datastore_version.manager = 'redis'
        mock_cluster_load.return_value = cluster

        self.assertRaisesRegexp(
            exception.TroveError, "No action 'do_stuff2' supplied " +
            "by strategy for manager 'redis'", self.controller.action, req,
            body, tenant_id, id)

    @patch.object(strategy, 'load_api_strategy')
    @patch.object(models.Cluster, 'load')
    def test_controller_action_found(self, mock_cluster_load,
                                     mock_cluster_api_strategy):

        body = {'do_stuff': {}}
        tenant_id = Mock()
        context = Mock()
        id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.datastore_version.manager = 'redis'
        mock_cluster_load.return_value = cluster

        strat = Mock()
        do_stuff_func = Mock()
        strat.cluster_controller_actions = \
            {'do_stuff': do_stuff_func}
        mock_cluster_api_strategy.return_value = strat

        self.controller.action(req, body, tenant_id, id)
        self.assertEqual(1, do_stuff_func.call_count)
Example #2
0
class TestClusterControllerWithStrategy(trove_testtools.TestCase):
    def setUp(self):
        super(TestClusterControllerWithStrategy, self).setUp()
        self.controller = ClusterController()
        self.cluster = {
            "cluster": {
                "name":
                "products",
                "datastore": {
                    "type": "mongodb",
                    "version": "2.4.10"
                },
                "instances": [{
                    "flavorRef": "7",
                    "volume": {
                        "size": 1
                    },
                }, {
                    "flavorRef": "7",
                    "volume": {
                        "size": 1
                    },
                }, {
                    "flavorRef": "7",
                    "volume": {
                        "size": 1
                    },
                }, {
                    "flavorRef": "7",
                    "volume": {
                        "size": 1
                    },
                }, {
                    "flavorRef": "7",
                    "volume": {
                        "size": 1
                    },
                }]
            }
        }

    def tearDown(self):
        super(TestClusterControllerWithStrategy, self).tearDown()
        cfg.CONF.clear_override('cluster_support', group='mongodb')
        cfg.CONF.clear_override('api_strategy', group='mongodb')

    @patch.object(datastore_models, 'get_datastore_version')
    @patch.object(models.Cluster, 'create')
    def test_create_clusters_disabled(self, mock_cluster_create,
                                      mock_get_datastore_version):

        cfg.CONF.set_override('cluster_support',
                              False,
                              group='mongodb',
                              enforce_type=True)

        body = self.cluster
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = 'mongodb'
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        self.assertRaises(exception.TroveError, self.controller.create, req,
                          body, tenant_id)

    @patch.object(views.ClusterView, 'data', return_value={})
    @patch.object(datastore_models, 'get_datastore_version')
    @patch.object(models.Cluster, 'create')
    def test_create_clusters_enabled(self, mock_cluster_create,
                                     mock_get_datastore_version,
                                     mock_cluster_view_data):

        cfg.CONF.set_override('cluster_support',
                              True,
                              group='mongodb',
                              enforce_type=True)

        body = self.cluster
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = 'mongodb'
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        mock_cluster = Mock()
        mock_cluster.datastore_version.manager = 'mongodb'
        mock_cluster_create.return_value = mock_cluster
        self.controller.create(req, body, tenant_id)

    @patch.object(models.Cluster, 'load')
    def test_controller_action_multi_action(self, mock_cluster_load):

        body = {'do_stuff': {}, 'do_stuff2': {}}
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)
        cluster_id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.instances_without_server = [Mock()]
        cluster.datastore_version.manager = 'test_dsv'
        mock_cluster_load.return_value = cluster

        self.assertRaisesRegexp(exception.TroveError,
                                'should have exactly one action specified',
                                self.controller.action, req, body, tenant_id,
                                cluster_id)

    @patch.object(models.Cluster, 'load')
    def test_controller_action_no_strategy(self, mock_cluster_load):

        body = {'do_stuff2': {}}
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)
        cluster_id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        db_info = DBCluster(ClusterTasks.NONE,
                            id=cluster_id,
                            tenant_id=tenant_id)
        cluster = Cluster(context,
                          db_info,
                          datastore='test_ds',
                          datastore_version='test_dsv')
        mock_cluster_load.return_value = cluster

        self.assertRaisesRegexp(exception.TroveError,
                                'Action do_stuff2 not supported',
                                self.controller.action, req, body, tenant_id,
                                cluster_id)

    @patch.object(strategy, 'load_api_strategy')
    @patch.object(models.Cluster, 'load')
    def test_controller_action_found(self, mock_cluster_load,
                                     mock_cluster_api_strategy):

        body = {'grow': {}}
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)
        cluster_id = 'test_uuid'

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.instances_without_server = [Mock()]
        cluster.datastore_version.manager = 'test_dsv'
        mock_cluster_load.return_value = cluster

        self.controller.action(req, body, tenant_id, cluster_id)
        self.assertEqual(1, cluster.action.call_count)
class TestClusterControllerWithStrategy(TestCase):
    def setUp(self):
        super(TestClusterControllerWithStrategy, self).setUp()
        self.controller = ClusterController()
        self.cluster = {
            "cluster": {
                "name": "products",
                "datastore": {"type": "mongodb", "version": "2.4.10"},
                "instances": [
                    {"flavorRef": "7", "volume": {"size": 1}},
                    {"flavorRef": "7", "volume": {"size": 1}},
                    {"flavorRef": "7", "volume": {"size": 1}},
                    {"flavorRef": "7", "volume": {"size": 1}},
                    {"flavorRef": "7", "volume": {"size": 1}},
                ],
            }
        }

    def tearDown(self):
        super(TestClusterControllerWithStrategy, self).tearDown()
        cfg.CONF.clear_override("cluster_support", group="mongodb")
        cfg.CONF.clear_override("api_strategy", group="mongodb")

    @patch.object(datastore_models, "get_datastore_version")
    @patch.object(models.Cluster, "create")
    def test_create_clusters_disabled(self, mock_cluster_create, mock_get_datastore_version):

        cfg.CONF.set_override("cluster_support", False, group="mongodb", enforce_type=True)

        body = self.cluster
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = "mongodb"
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        self.assertRaises(exception.TroveError, self.controller.create, req, body, tenant_id)

    @patch.object(views.ClusterView, "data", return_value={})
    @patch.object(datastore_models, "get_datastore_version")
    @patch.object(models.Cluster, "create")
    def test_create_clusters_enabled(self, mock_cluster_create, mock_get_datastore_version, mock_cluster_view_data):

        cfg.CONF.set_override("cluster_support", True, group="mongodb", enforce_type=True)

        body = self.cluster
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = "mongodb"
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        mock_cluster = Mock()
        mock_cluster.datastore_version.manager = "mongodb"
        mock_cluster_create.return_value = mock_cluster
        self.controller.create(req, body, tenant_id)

    @patch.object(models.Cluster, "load")
    def test_controller_action_multi_action(self, mock_cluster_load):

        body = {"do_stuff": {}, "do_stuff2": {}}
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)
        cluster_id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.instances_without_server = [Mock()]
        cluster.datastore_version.manager = "test_dsv"
        mock_cluster_load.return_value = cluster

        self.assertRaisesRegexp(
            exception.TroveError,
            "should have exactly one action specified",
            self.controller.action,
            req,
            body,
            tenant_id,
            cluster_id,
        )

    @patch.object(models.Cluster, "load")
    def test_controller_action_no_strategy(self, mock_cluster_load):

        body = {"do_stuff2": {}}
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)
        cluster_id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        db_info = DBCluster(ClusterTasks.NONE, id=cluster_id, tenant_id=tenant_id)
        cluster = Cluster(context, db_info, datastore="test_ds", datastore_version="test_dsv")
        mock_cluster_load.return_value = cluster

        self.assertRaisesRegexp(
            exception.TroveError,
            "Action do_stuff2 not supported",
            self.controller.action,
            req,
            body,
            tenant_id,
            cluster_id,
        )

    @patch.object(strategy, "load_api_strategy")
    @patch.object(models.Cluster, "load")
    def test_controller_action_found(self, mock_cluster_load, mock_cluster_api_strategy):

        body = {"grow": {}}
        tenant_id = Mock()
        context = trove_testtools.TroveTestContext(self)
        cluster_id = "test_uuid"

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.instances_without_server = [Mock()]
        cluster.datastore_version.manager = "test_dsv"
        mock_cluster_load.return_value = cluster

        self.controller.action(req, body, tenant_id, cluster_id)
        self.assertEqual(1, cluster.action.call_count)
Example #4
0
class TestClusterControllerWithStrategy(TestCase):
    def setUp(self):
        super(TestClusterControllerWithStrategy, self).setUp()
        self.controller = ClusterController()
        self.cluster = {
            "cluster": {
                "name": "products",
                "datastore": {
                    "type": "mongodb",
                    "version": "2.4.10"
                },
                "instances": [
                    {
                        "flavorRef": "7",
                        "volume": {
                            "size": 1
                        },
                    },
                    {
                        "flavorRef": "7",
                        "volume": {
                            "size": 1
                        },
                    },
                    {
                        "flavorRef": "7",
                        "volume": {
                            "size": 1
                        },
                    },
                    {
                        "flavorRef": "7",
                        "volume": {
                            "size": 1
                        },
                    },
                    {
                        "flavorRef": "7",
                        "volume": {
                            "size": 1
                        },
                    }
                ]
            }
        }

    def tearDown(self):
        super(TestClusterControllerWithStrategy, self).tearDown()
        cfg.CONF.clear_override('cluster_support', group='mongodb')
        cfg.CONF.clear_override('api_strategy', group='mongodb')

    @patch.object(datastore_models, 'get_datastore_version')
    @patch.object(models.Cluster, 'create')
    def test_create_clusters_disabled(self,
                                      mock_cluster_create,
                                      mock_get_datastore_version):

        cfg.CONF.set_override('cluster_support', False, group='mongodb')

        body = self.cluster
        tenant_id = Mock()
        context = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = 'mongodb'
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        self.assertRaises(exception.TroveError, self.controller.create, req,
                          body, tenant_id)

    @patch.object(views.ClusterView, 'data', return_value={})
    @patch.object(datastore_models, 'get_datastore_version')
    @patch.object(models.Cluster, 'create')
    def test_create_clusters_enabled(self,
                                     mock_cluster_create,
                                     mock_get_datastore_version,
                                     mock_cluster_view_data):

        cfg.CONF.set_override('cluster_support', True, group='mongodb')

        body = self.cluster
        tenant_id = Mock()
        context = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = 'mongodb'
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        mock_cluster = Mock()
        mock_cluster.datastore_version.manager = 'mongodb'
        mock_cluster_create.return_value = mock_cluster
        self.controller.create(req, body, tenant_id)

    @patch.object(models.Cluster, 'load')
    def test_controller_action_no_strategy(self,
                                           mock_cluster_load):

        body = {'do_stuff2': {}}
        tenant_id = Mock()
        context = Mock()
        id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.datastore_version.manager = 'mongodb'
        mock_cluster_load.return_value = cluster

        self.assertRaises(exception.TroveError, self.controller.action, req,
                          body, tenant_id, id)

    @patch.object(strategy, 'load_api_strategy')
    @patch.object(models.Cluster, 'load')
    def test_controller_action_found(self,
                                     mock_cluster_load,
                                     mock_cluster_api_strategy):

        body = {'do_stuff': {}}
        tenant_id = Mock()
        context = Mock()
        id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.datastore_version.manager = 'mongodb'
        mock_cluster_load.return_value = cluster

        strat = Mock()
        do_stuff_func = Mock()
        strat.cluster_controller_actions = \
            {'do_stuff': do_stuff_func}
        mock_cluster_api_strategy.return_value = strat

        self.controller.action(req, body, tenant_id, id)
        self.assertEqual(1, do_stuff_func.call_count)
class TestClusterControllerWithStrategy(trove_testtools.TestCase):
    def setUp(self):
        super(TestClusterControllerWithStrategy, self).setUp()
        self.controller = ClusterController()
        self.cluster = {
            "cluster": {
                "name": "products",
                "datastore": {"type": "pxc", "version": "5.5"},
                "instances": [
                    {"flavorRef": "7", "volume": {"size": 1}},
                    {"flavorRef": "7", "volume": {"size": 1}},
                    {"flavorRef": "7", "volume": {"size": 1}},
                ],
            }
        }

    def tearDown(self):
        super(TestClusterControllerWithStrategy, self).tearDown()
        cfg.CONF.clear_override("cluster_support", group="pxc")
        cfg.CONF.clear_override("api_strategy", group="pxc")

    @patch.object(datastore_models, "get_datastore_version")
    @patch.object(models.Cluster, "create")
    def test_create_clusters_disabled(self, mock_cluster_create, mock_get_datastore_version):

        cfg.CONF.set_override("cluster_support", False, group="pxc", enforce_type=True)

        body = self.cluster
        tenant_id = Mock()
        context = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = "pxc"
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        self.assertRaises(exception.TroveError, self.controller.create, req, body, tenant_id)

    @patch.object(views.ClusterView, "data", return_value={})
    @patch.object(datastore_models, "get_datastore_version")
    @patch.object(models.Cluster, "create")
    def test_create_clusters_enabled(self, mock_cluster_create, mock_get_datastore_version, mock_cluster_view_data):

        cfg.CONF.set_override("cluster_support", True, group="pxc", enforce_type=True)

        body = self.cluster
        tenant_id = Mock()
        context = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        datastore_version = Mock()
        datastore_version.manager = "pxc"
        mock_get_datastore_version.return_value = (Mock(), datastore_version)

        mock_cluster = Mock()
        mock_cluster.datastore_version.manager = "pxc"
        mock_cluster_create.return_value = mock_cluster
        self.controller.create(req, body, tenant_id)

    @patch.object(models.Cluster, "load")
    def test_controller_action_no_strategy(self, mock_cluster_load):

        body = {"do_stuff2": {}}
        tenant_id = Mock()
        context = Mock()
        id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.datastore_version.manager = "pxc"
        mock_cluster_load.return_value = cluster

        self.assertRaises(exception.TroveError, self.controller.action, req, body, tenant_id, id)

    @patch.object(strategy, "load_api_strategy")
    @patch.object(models.Cluster, "load")
    def test_controller_action_found(self, mock_cluster_load, mock_cluster_api_strategy):

        body = {"do_stuff": {}}
        tenant_id = Mock()
        context = Mock()
        id = Mock()

        req = Mock()
        req.environ = MagicMock()
        req.environ.get = Mock(return_value=context)

        cluster = Mock()
        cluster.datastore_version.manager = "pxc"
        mock_cluster_load.return_value = cluster

        strat = Mock()
        do_stuff_func = Mock()
        strat.cluster_controller_actions = {"do_stuff": do_stuff_func}
        mock_cluster_api_strategy.return_value = strat

        self.controller.action(req, body, tenant_id, id)
        self.assertEqual(1, do_stuff_func.call_count)