def test__check__protection_job__exists(self):
        module = FakeModule(
            cluster="cohesity.lab",
            username="******",
            password="******",
            validate_certs=True
        )

        source_list = """
        [{
          "id": 1,
          "name": "myendpoint"
        }]
        """

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.get__protection_jobs__by_environment')
        mock_check = self.patcher.start()
        mock_check.return_value = json.loads(source_list)

        data = dict(
            token='mytoken',
            environment='Physical',
            name='myendpoint'
        )

        return_id = cohesity_job.check__protection_job__exists(
            module, data)

        assert return_id == 1
    def test__stop_non_running_job__no_change(self):
        module = FakeModule(
            cluster="cohesity.lab",
            username="******",
            password="******",
            validate_certs=True
        )
        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.get__protection_run__all__by_id')
        mock_check = self.patcher.start()
        mock_check.return_value = dict()

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.stop_job')
        mock_check = self.patcher.start()
        mock_check.return_value = dict(id=24)

        data = dict(
            token='mytoken',
            id=24
        )

        return_id = cohesity_job.stop_job(module, data)

        assert return_id['id'] == 24
    def test__unregister_job__physical(self):
        module = FakeModule(
            cluster="cohesity.lab",
            username="******",
            password="******",
            validate_certs=True
        )

        source_list = """
        {
          "id": 241,
          "endpoint": "mylinux.host.lab"
        }
        """

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.unregister_job')
        mock_check = self.patcher.start()
        mock_check.return_value = json.loads(source_list)

        data = dict(
            token='mytoken',
            environment='Physical',
            endpoint='myendpoint'
        )

        return_id = cohesity_job.unregister_job(module, data)

        assert return_id['endpoint'] == "mylinux.host.lab"
        assert return_id['id'] == 241
    def test__protection_source_registration__status_generic_nas(self):
        module = FakeModule(cluster="cohesity.lab",
                            username="******",
                            password="******",
                            validate_certs=True)

        source_list = """
        {
          "nodes": [{
            "protectionSource": {
              "id": 1,
              "name": "myendpoint"
            }
          }]
        }
        """

        # =>
        self.patcher = patch(global_module_path +
                             '.cohesity_source.get__prot_source__all')
        mock_check = self.patcher.start()
        mock_check.return_value = json.loads(source_list)

        data = dict(token='mytoken',
                    environment='GenericNas',
                    endpoint='myendpoint')

        return_id = cohesity_source.get__protection_source_registration__status(
            module, data)

        assert return_id == 1
    def test__register_source__vmware(self):
        module = FakeModule(cluster="cohesity.lab",
                            username="******",
                            password="******",
                            validate_certs=True)

        source_list = """
        {
          "ProtectionSource": {
            "id": {
              "uuid": "ebd9bfce-b845-4aa3-842a-3f0dc381bbab"
            },
            "name": "vc-67.eco.eng.cohesity.com",
            "type": "kVCenter"
          }
        }
        """

        # =>
        self.patcher = patch(global_module_path +
                             '.cohesity_source.register_source')
        mock_check = self.patcher.start()
        mock_check.return_value = json.loads(source_list)

        data = dict(token='mytoken',
                    environment='Vmware',
                    endpoint='myendpoint')

        return_id = cohesity_source.register_source(module, data)

        assert return_id['ProtectionSource']['type'] == "kVCenter"
        assert return_id['ProtectionSource']['id'][
            'uuid'] == "ebd9bfce-b845-4aa3-842a-3f0dc381bbab"
    def test__register_source__physical(self):
        module = FakeModule(cluster="cohesity.lab",
                            username="******",
                            password="******",
                            validate_certs=True)

        source_list = """
        {
          "ProtectionSource": {
            "hostType": "kLinux",
            "id": {
              "clusterId": 8621173906188849,
              "clusterIncarnationId": 1538852526333,
              "id": 240
            },
            "name": "10.2.55.72",
            "type": "kHost"
          }
        }
        """

        # =>
        self.patcher = patch(global_module_path +
                             '.cohesity_source.register_source')
        mock_check = self.patcher.start()
        mock_check.return_value = json.loads(source_list)

        data = dict(token='mytoken',
                    environment='Physical',
                    endpoint='myendpoint')

        return_id = cohesity_source.register_source(module, data)

        assert return_id['ProtectionSource']['hostType'] == "kLinux"
        assert return_id['ProtectionSource']['id']['id'] == 240
Example #7
0
    def test__get__snapshot_information__for_file__find_snapshot(self):
        module = FakeModule(cluster="cohesity.lab",
                            username="******",
                            password="******",
                            validate_certs=True)

        source_list = """
        [{
          "name": "myendpoint",
          "uid": {
            "clusterId": 99,
            "clusterIncarnationId": 98,
            "id": 24
          },
          "sourceIds": [12]
        }]
        """

        # =>
        self.patcher = patch(
            global_module_path +
            '.cohesity_restore.get__protection_jobs__by_environment')
        mock_check = self.patcher.start()
        mock_check.return_value = json.loads(source_list)

        self.patch_url = patch(global_module_util_path +
                               '.cohesity_hints.open_url')
        self.open_url = self.patch_url.start()

        # =>
        stream = self.open_url.return_value
        stream.read.return_value = '[{"snapshot":{"jobRunId": "123","startedTimeUsecs": "1541603218"}}]'
        stream.getcode.return_value = 200
        self.open_url.return_value = stream
        mockData = json.loads(stream.read.return_value)

        data = dict(token='mytoken',
                    environment='Physical',
                    job_name='myendpoint',
                    file_names=["/C/data/file"])

        return_id = cohesity_restore.get__snapshot_information__for_file(
            module, data)

        self.assertEqual(1, self.open_url.call_count)
        assert return_id['jobRunId'] == "123"
        assert return_id['startedTimeUsecs'] == "1541603218"
        assert return_id['jobUid']['id'] == 24

        self.patch_url.stop()
    def test__get__cohesity_auth__token(self):
        module = FakeModule(cluster="cohesity.lab",
                            username="******",
                            password="******",
                            validate_certs=True)

        check_patcher = patch(global_module_util_path +
                              '.cohesity_auth.Authentication.get_token')
        mock_check = check_patcher.start()
        mock_check.return_value = 'mytoken'

        return_id = get__cohesity_auth__token(module)

        assert return_id == 'mytoken'
        check_patcher.stop()
    def test__stop_running_job__fail(self):
        module = FakeModule(
            cluster="cohesity.lab",
            username="******",
            password="******",
            validate_certs=True,
            cancel_active=False
        )

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.get__protection_run__all__by_id')
        mock_check = self.patcher.start()
        mock_check.return_value = [
            dict(
                id=24,
                backupRun=dict(
                    jobRunId='751'
                )
            )
        ]

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.open_url')
        mock_check = self.patcher.start()
        check_var = mock_check.return_value

        # =>
        check_var.read.return_value = ''
        check_var.getcode.return_value = 204
        mock_check.return_value = check_var

        data = dict(
            token='mytoken',
            id=24
        )
        with pytest.raises(Exception) as error:
            cohesity_job.stop_job(module, data)

        assert str(error.value) == 'FAIL'
        assert module.exit_kwargs == dict(
            changed=False,
            msg='The Protection Job for this host is active and cannot be stopped'
        )
    def test__stop_running_job__force__pass(self):
        module = FakeModule(
            cluster="cohesity.lab",
            username="******",
            password="******",
            validate_certs=True,
            cancel_active=True
        )

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.get__protection_run__all__by_id')
        mock_check = self.patcher.start()
        mock_check.return_value = [
            dict(
                id=24,
                backupRun=dict(
                    jobRunId='751'
                )
            )
        ]

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.open_url')
        mock_check = self.patcher.start()
        check_var = mock_check.return_value

        # =>
        check_var.read.return_value = ''
        check_var.getcode.return_value = 204
        mock_check.return_value = check_var

        data = dict(
            token='mytoken',
            id=24
        )
        self.patcher_transition = patch(
            global_module_path + '.cohesity_job.wait__for_job_state__transition')
        self.patcher_transition.start()
        return_id = cohesity_job.stop_job(module, data)

        self.patcher_transition.stop()
        assert return_id['id'] == 24
    def test__start_job__physical(self):
        module = FakeModule(
            cluster="cohesity.lab",
            username="******",
            password="******",
            validate_certs=True
        )

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.start_job')
        mock_check = self.patcher.start()
        mock_check.return_value = dict(id=24)

        data = dict(
            token='mytoken',
            id=24
        )

        return_id = cohesity_job.start_job(module, data)

        assert return_id['id'] == 24
    def test__register_job__physical(self):
        module = FakeModule(
            cluster="cohesity.lab",
            username="******",
            password="******",
            validate_certs=True
        )

        source_list = """
        {
          "environment": "Physical",
          "id": 24,
          "name": "myendpoint",
          "priority": "Low",
          "start_time": {
            "hour": "02",
            "minute": "00"
          }
        }
        """

        # =>
        self.patcher = patch(
            global_module_path + '.cohesity_job.register_job')
        mock_check = self.patcher.start()
        mock_check.return_value = json.loads(source_list)

        data = dict(
            token='mytoken',
            environment='Physical',
            endpoint='myendpoint'
        )

        return_id = cohesity_job.register_job(module, data)

        assert return_id['environment'] == "Physical"
        assert return_id['name'] == 'myendpoint'
        assert return_id['id'] == 24