コード例 #1
0
    def test__get__snapshot_information__for_file__no_job(self):
        module = FakeModule(cluster="cohesity.lab",
                            username="******",
                            password="******",
                            validate_certs=True)

        source_list = """
        []
        """

        # =>
        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)

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

        with pytest.raises(Exception) as error:
            failure = cohesity_restore.get__snapshot_information__for_file(
                module, data)

        assert str(error.value) == "FAIL"
        fail_json = module.exit_kwargs
        assert fail_json['changed'] == "False"
        assert fail_json['job_name'] == data['job_name']
        assert fail_json[
            'msg'] == "Failed to find chosen Job name for the selected Environment Type."
コード例 #2
0
    def test__exception__check_token(self):
        ''' Test to see if a token refresh will handle an exception. '''

        server = "cohesity-api"
        uri = "https://" + server + "/irisservices/api/v1/public/nodes"

        # => In order to properly test this behavior, we will first need to Mock out
        # => the call to the method `check_token` and force it to return False. This
        # => should trigger the Code to return back to the `get_token` method.
        check_patcher = patch(global_module_util_path +
                              '.cohesity_auth.open_url')
        mock_check = check_patcher.start()
        mock_check.side_effect = urllib_error.HTTPError(
            uri, 500, 'Internal Server Error',
            {'Content-Type': 'application/json'},
            StringIO('Internal Server Error'))

        # => Create a new object of the Class::Authentication and
        # => assign the credentials including an expired Token.
        cohesity_auth = Authentication()
        cohesity_auth.username = "******"
        cohesity_auth.password = "******"
        cohesity_auth.token = "mytoken"

        # => Assert Test Cases are valid
        with pytest.raises(TokenException) as error:
            cohesity_auth.check_token(server)
        assert error.type == TokenException
        assert cohesity___reg_verify__helper(
            '.+(Internal Server Error)').__check__(str(error.value))
コード例 #3
0
    def test_convert__windows_file_name__no_drive_letter_exception(self):
        filename = "\\data\\file"

        with pytest.raises(Exception) as error:
            cohesity_restore.convert__windows_file_name(filename)
        assert str(
            error.value
        ) == "Windows Based files must be in /Drive/path/to/file or Drive:\\path\\to\\file format."
コード例 #4
0
    def test__get__auth_token_no_username(self):
        ''' Test to see if the token is expired and if so then trigger a refresh. '''
        server = "cohesity-api"

        # => Assert Test Cases are valid
        with pytest.raises(ParameterViolation) as error:
            Authentication().get_token(server)
        assert error.type == ParameterViolation
        error_list = str(error.value)

        assert error_list.index('Username is a required Parameter')
コード例 #5
0
    def test__get__snapshot_information__for_file__no_snapshot(self):
        module = FakeModule(token='mytoken',
                            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)

        # => Return an empty Array for the file snapshots
        self.patcher2 = patch(
            global_module_path +
            '.cohesity_restore.get__file_snapshot_information__by_filename')
        mock_check = self.patcher2.start()
        mock_check.return_value = json.loads("[]")

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

        with pytest.raises(Exception) as error:
            failure = cohesity_restore.get__snapshot_information__for_file(
                module, data)

        assert str(error.value) == "FAIL"
        fail_json = module.exit_kwargs
        assert fail_json['changed'] == "False"
        assert fail_json['job_name'] == data['job_name']
        assert fail_json['filename'] == data['file_names'][0]
        assert fail_json[
            'msg'] == "Failed to find a snapshot for the file in the chosen Job name."

        self.patcher2.stop()
コード例 #6
0
    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'
        )
コード例 #7
0
    def test_raise_exception_when_url_invalid(self):
        self.patcher = patch(global_module_util_path +
                             '.cohesity_auth.open_url')
        self.open_url = self.patcher.start()

        server = "bad-host-domain"
        uri = "https://" + server + "/irisservices/api/v1/public/accessTokens"
        self.open_url.side_effect = urllib_error.URLError(
            'Name or service not known')

        cohesity_auth = Authentication()
        cohesity_auth.username = "******"
        cohesity_auth.password = "******"

        with pytest.raises(TokenException) as error:
            cohesity_auth.get_token('bad-host-domain')
        assert error.type == TokenException
        assert cohesity___reg_verify__helper(
            '.+(Name or service not known).+').__check__(str(error.value))

        self.patcher.stop()