def test_setup_and_cleanup(self, mock_osclients, mock_cleanup):

        mock_sahara = mock_osclients.Clients.return_value.sahara.return_value
        mock_sahara.data_sources.create.return_value = mock.MagicMock(id=42)

        sahara_ctx = sahara_input_data_sources.SaharaInputDataSources(
            self.context)
        sahara_ctx.generate_random_name = mock.Mock()

        input_ds_crete_calls = []

        for i in range(self.tenants_num):
            input_ds_crete_calls.append(
                mock.call(name=sahara_ctx.generate_random_name.return_value,
                          description="",
                          data_source_type="hdfs",
                          url="hdfs://test_host/"))

        sahara_ctx.setup()

        mock_sahara.data_sources.create.assert_has_calls(input_ds_crete_calls)

        sahara_ctx.cleanup()

        mock_cleanup.assert_called_once_with(names=["sahara.data_sources"],
                                             users=self.context["users"])
    def test_setup_and_cleanup(self, mock_osclients, mock_cleanup):

        mock_sahara = mock_osclients.Clients.return_value.sahara.return_value
        mock_sahara.data_sources.create.return_value = mock.MagicMock(id=42)

        sahara_ctx = sahara_input_data_sources.SaharaInputDataSources(
            self.context)
        sahara_ctx.generate_random_name = mock.Mock()

        input_ds_crete_calls = []

        for i in range(self.tenants_num):
            input_ds_crete_calls.append(mock.call(
                name=sahara_ctx.generate_random_name.return_value,
                description="",
                data_source_type="hdfs",
                url="hdfs://test_host/"))

        sahara_ctx.setup()

        mock_sahara.data_sources.create.assert_has_calls(
            input_ds_crete_calls)

        sahara_ctx.cleanup()

        mock_cleanup.assert_has_calls((
            mock.call(names=["swift.object", "swift.container"],
                      users=self.context["users"],
                      superclass=swift_utils.SwiftScenario,
                      task_id=self.context["owner_id"]),
            mock.call(
                names=["sahara.data_sources"],
                users=self.context["users"],
                superclass=sahara_input_data_sources.SaharaInputDataSources,
                task_id=self.context["owner_id"])))
    def test_setup_inputs_swift(self, mock_osclients, mock_get):
        mock_clients = mock_osclients.Clients(mock.MagicMock())
        mock_get.content = mock.MagicMock(content="OK")

        self.context.update({
            "config": {
                "users": {
                    "tenants": self.tenants_num,
                    "users_per_tenant": self.users_per_tenant,
                },
                "sahara_input_data_sources": {
                    "input_type":
                    "swift",
                    "input_url":
                    "swift://rally.sahara/input_url",
                    "swift_files": [{
                        "name": "first",
                        "download_url": "http://host"
                    }]
                },
            },
            "admin": {
                "credential": mock.MagicMock()
            },
            "task": mock.MagicMock(),
            "users": self.users_key,
            "tenants": self.tenants
        })
        sahara_ctx = sahara_input_data_sources.SaharaInputDataSources(
            self.context)
        sahara_ctx.generate_random_name = mock.Mock()

        input_ds_create_calls = []

        for i in range(self.tenants_num):
            input_ds_create_calls.append(
                mock.call(name=sahara_ctx.generate_random_name.return_value,
                          description="",
                          data_source_type="swift",
                          url="swift://rally.sahara/input_url",
                          credential_user="******",
                          credential_pass="******"))

        sahara_ctx.setup()

        self.assertEqual(input_ds_create_calls,
                         mock_clients.sahara().data_sources.create.mock_calls)

        sahara_ctx.cleanup()
    def test_setup_inputs_swift(self, mock_swift_utils, mock_resource_manager,
                                mock_osclients, mock_get):
        mock_swift_scenario = mock.Mock()
        mock_swift_scenario._create_container.side_effect = (
            lambda container_name: "container_%s" % container_name)
        mock_swift_scenario._upload_object.side_effect = iter(
            ["uploaded_%d" % i for i in range(10)])
        mock_swift_utils.SwiftScenario.return_value = mock_swift_scenario

        self.context.update({
            "config": {
                "users": {
                    "tenants": self.tenants_num,
                    "users_per_tenant": self.users_per_tenant,
                },
                "sahara_input_data_sources": {
                    "input_type":
                    "swift",
                    "input_url":
                    "swift://rally.sahara/input_url",
                    "swift_files": [{
                        "name": "first",
                        "download_url": "http://host"
                    }]
                },
            },
            "admin": {
                "credential": mock.MagicMock()
            },
            "task": mock.MagicMock(),
            "users": self.users,
            "tenants": self.tenants
        })
        sahara_ctx = sahara_input_data_sources.SaharaInputDataSources(
            self.context)
        sahara_ctx.generate_random_name = mock.Mock(
            side_effect=iter(["random_name_%d" % i for i in range(10)]))

        input_ds_create_calls = []

        for i in range(self.tenants_num):
            input_ds_create_calls.append(
                mock.call(name="random_name_%d" % i,
                          description="",
                          data_source_type="swift",
                          url="swift://rally.sahara/input_url",
                          credential_user="******" % i,
                          credential_pass="******"))

        sahara_ctx.setup()

        self.assertEqual(input_ds_create_calls,
                         (mock_osclients.Clients.return_value.sahara.
                          return_value.data_sources.create.mock_calls))

        self.assertEqual(
            {
                "container_name": "container_rally_rally",
                "swift_objects": ["uploaded_0", "uploaded_1"]
            }, self.context["sahara"])

        sahara_ctx.cleanup()

        mock_resource_manager.cleanup.assert_called_once_with(
            names=["sahara.data_sources"], users=self.context["users"])