Пример #1
0
    def test_restore(self, subprocess_mock, gcs_service_mock, storage_mock,
                     os_mock):
        expected_backup_name = "mongodb-backup-mongo-operator-cluster-mongo-cluster-2018-02-28_140000.archive.gz"

        self.restore_helper.restore(self.cluster_object, expected_backup_name)

        self.assertEqual([
            call.getSecret("storage-serviceaccount", "mongo-operator-cluster")
        ], self.kubernetes_service.mock_calls)

        subprocess_mock.assert_called_once_with([
            "mongorestore", "--host", ",".join(self.expected_cluster_members),
            "--gzip", "--archive=/tmp/" + expected_backup_name
        ])

        expected_service_call = call.from_service_account_info(
            {"user": "******"})
        self.assertEqual([expected_service_call], gcs_service_mock.mock_calls)

        expected_storage_calls = [
            call(
                gcs_service_mock.from_service_account_info.return_value.
                project_id,
                gcs_service_mock.from_service_account_info.return_value),
            call().get_bucket("ultimaker-mongo-backups"),
            call().get_bucket().blob("test-backups/" + expected_backup_name),
            call().get_bucket().blob().download_to_filename(
                "/tmp/" + expected_backup_name),
        ]
        self.assertEqual(expected_storage_calls, storage_mock.mock_calls)

        expected_os_call = call.remove("/tmp/" + expected_backup_name)
        self.assertEqual([expected_os_call], os_mock.mock_calls)
Пример #2
0
    def test_restoreIfNeeded(self, restore_mock, gcs_service_mock,
                             storage_mock):
        get_bucket_mock = storage_mock.return_value.get_bucket
        get_bucket_mock.return_value.list_blobs.return_value = iter(
            [MockBlob()])

        self.restore_helper.restoreIfNeeded(self.cluster_object)

        self.assertEqual([
            call.getSecret("storage-serviceaccount", "mongo-operator-cluster")
        ], self.kubernetes_service.mock_calls)

        expected_service_call = call.from_service_account_info(
            {"user": "******"})
        self.assertEqual([expected_service_call], gcs_service_mock.mock_calls)

        get_bucket_mock.assert_called_once_with("ultimaker-mongo-backups")

        restore_mock.assert_called_once_with(self.cluster_object,
                                             "somebackupfile.gz")

        # Again, with no needed restore
        restore_mock.reset_mock()

        self.cluster_dict = getExampleClusterDefinition()
        self.cluster_object = V1MongoClusterConfiguration(**self.cluster_dict)

        self.restore_helper.restoreIfNeeded(self.cluster_object)
        self.assertFalse(restore_mock.called,
                         "restore_mock should not have been called")
Пример #3
0
    def test_backup(self, subprocess_mock, gcs_service_mock, storage_mock,
                    os_mock):
        current_date = datetime(2018, 2, 28, 14, 0, 0)
        expected_backup_name = "mongodb-backup-default-mongo-cluster-2018-02-28_140000.archive.gz"

        self.checker.backup(self.cluster_object, current_date)

        self.assertEqual([call.getSecret('storage-serviceaccount', 'default')],
                         self.kubernetes_service.mock_calls)

        subprocess_mock.assert_called_once_with([
            'mongodump', '--host',
            'mongo-cluster-2.mongo-cluster.default.svc.cluster.local',
            '--gzip', '--archive=/tmp/' + expected_backup_name
        ])

        expected_service_call = call.from_service_account_info(
            {'user': '******'})
        self.assertEqual([expected_service_call], gcs_service_mock.mock_calls)

        expected_storage_calls = [
            call(
                gcs_service_mock.from_service_account_info.return_value.
                project_id,
                gcs_service_mock.from_service_account_info.return_value),
            call().bucket('ultimaker-mongo-backups'),
            call().bucket().blob('test-backups/' + expected_backup_name),
            call().bucket().blob().upload_from_filename('/tmp/' +
                                                        expected_backup_name),
        ]
        self.assertEqual(expected_storage_calls, storage_mock.mock_calls)

        expected_os_call = call.remove('/tmp/' + expected_backup_name)
        self.assertEqual([expected_os_call], os_mock.mock_calls)
    def test_backup(self, subprocess_mock, gcs_service_mock, storage_mock,
                    os_mock):
        current_date = datetime(2018, 2, 28, 14, 0, 0)
        expected_backup_name = "mongodb-backup-mongo-operator-cluster-mongo-cluster-2018-02-28_140000.archive.gz"

        self.checker.backup(self.cluster_object, current_date)

        self.assertEqual([
            call.getSecret("storage-serviceaccount", "mongo-operator-cluster")
        ], self.kubernetes_service.mock_calls)

        subprocess_mock.assert_called_once_with([
            "mongodump", "--host",
            "mongo-cluster-2.mongo-cluster.mongo-operator-cluster.svc.cluster.local",
            "--gzip", "--archive=/tmp/" + expected_backup_name
        ])

        expected_service_call = call.from_service_account_info(
            {"user": "******"})
        self.assertEqual([expected_service_call], gcs_service_mock.mock_calls)

        expected_storage_calls = [
            call(
                gcs_service_mock.from_service_account_info.return_value.
                project_id,
                gcs_service_mock.from_service_account_info.return_value),
            call().bucket("ultimaker-mongo-backups"),
            call().bucket().blob("test-backups/" + expected_backup_name),
            call().bucket().blob().upload_from_filename("/tmp/" +
                                                        expected_backup_name),
        ]
        self.assertEqual(expected_storage_calls, storage_mock.mock_calls)

        expected_os_call = call.remove("/tmp/" + expected_backup_name)
        self.assertEqual([expected_os_call], os_mock.mock_calls)
Пример #5
0
    def test_createUsers_ok(self):
        self.kubernetes_service.execInPod.return_value = self._getFixture(
            "createUser-ok")

        self.service.createUsers(self.cluster_object)
        expected_calls = [
            call.getSecret('mongo-cluster-admin-credentials', 'default'),
            call.execInPod('mongodb', 'mongo-cluster-0', 'default', [
                'mongo', 'localhost:27017/admin', '--eval',
                self.expected_user_create
            ])
        ]
        self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
Пример #6
0
    def test_createUsers_TimeoutError(self):
        self.kubernetes_service.execInPod.return_value = self._getFixture(
            "createUser-notMaster")

        with self.assertRaises(TimeoutError) as context:
            self.service.createUsers(self.cluster_object)
        expected_calls = [
            call.getSecret('mongo-cluster-admin-credentials', 'default')
        ] + [
            call.execInPod('mongodb', 'mongo-cluster-' + str(pod), 'default', [
                'mongo', 'localhost:27017/admin', '--eval',
                self.expected_user_create
            ]) for _ in range(4) for pod in range(3)
        ]
        self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
        self.assertEquals(
            "Could not create users in any of the 3 pods of cluster mongo-cluster @ ns/default.",
            str(context.exception))
Пример #7
0
    def test_createUsers_not_master_then_already_exists(self):
        self.kubernetes_service.execInPod.side_effect = (
            self._getFixture("createUser-notMaster"),
            self._getFixture("createUser-exists"))

        self.service.createUsers(self.cluster_object)
        expected_calls = [
            call.getSecret('mongo-cluster-admin-credentials', 'default'),
            call.execInPod('mongodb', 'mongo-cluster-0', 'default', [
                'mongo', 'localhost:27017/admin', '--eval',
                self.expected_user_create
            ]),
            call.execInPod('mongodb', 'mongo-cluster-1', 'default', [
                'mongo', 'localhost:27017/admin', '--eval',
                self.expected_user_create
            ]),
        ]
        self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
Пример #8
0
    def test_createUsers_ValueError(self):
        self.kubernetes_service.execInPod.return_value = self._getFixture(
            "createUser-ok").replace('"user"', '"error"')

        with self.assertRaises(ValueError) as context:
            self.service.createUsers(self.cluster_object)
        expected_calls = [
            call.getSecret('mongo-cluster-admin-credentials', 'default'),
            call.execInPod('mongodb', 'mongo-cluster-0', 'default', [
                'mongo', 'localhost:27017/admin', '--eval',
                self.expected_user_create
            ])
        ]
        self.assertEquals(expected_calls, self.kubernetes_service.mock_calls)
        self.assertEquals(
            "Unexpected response creating users for pod mongo-cluster-0 @ ns/default:\n"
            "{'error': 'root', 'roles': [{'role': 'root', 'db': 'admin'}]}",
            str(context.exception))
Пример #9
0
 def test_checkCluster_new_version(self, check_mock, admin_mock,
                                   backup_mock, mongo_client_mock):
     admin_mock.return_value = "createUser", "foo", {}
     self.checker._cluster_versions[("mongo-cluster",
                                     "mongo-operator-cluster")] = "50"
     mongo_client_mock.return_value.admin.command.side_effect = (
         self._getMongoFixture("replica-status-ok"),
         self._getMongoFixture("createUser-ok"))
     self.checker._checkCluster(self.cluster_object)
     self.assertEqual({("mongo-cluster", "mongo-operator-cluster"): "100"},
                      self.checker._cluster_versions)
     expected = [
         call.getSecret("mongo-cluster-admin-credentials",
                        "mongo-operator-cluster")
     ]
     self.assertEqual(expected, self.kubernetes_service.mock_calls)
     self.assertEqual([call(self.cluster_object)] * 3,
                      check_mock.mock_calls)
     backup_mock.assert_called_once_with(self.cluster_object)
     self.assertEqual([call(self.kubernetes_service.getSecret())],
                      admin_mock.mock_calls)
Пример #10
0
 def test_checkCluster_new_version(self, check_mock, admin_mock,
                                   backup_mock):
     self.checker.cluster_versions[("mongo-cluster", "default")] = "50"
     self.kubernetes_service.execInPod.side_effect = (
         self._getMongoFixture("replica-status-ok"),
         self._getMongoFixture("createUser-exists"))
     self.checker.checkCluster(self.cluster_object)
     self.assertEqual({("mongo-cluster", "default"): "100"},
                      self.checker.cluster_versions)
     expected = [
         call.execInPod(
             'mongodb', 'mongo-cluster-0', 'default',
             ['mongo', 'localhost:27017/admin', '--eval', 'rs.status()']),
         call.getSecret('mongo-cluster-admin-credentials', 'default'),
         call.execInPod('mongodb', 'mongo-cluster-0', 'default', [
             'mongo', 'localhost:27017/admin', '--eval',
             admin_mock.return_value
         ])
     ]
     self.assertEqual(expected, self.kubernetes_service.mock_calls)
     self.assertEqual([call(self.cluster_object)] * 3,
                      check_mock.mock_calls)
     backup_mock.assert_called_once_with(self.cluster_object)