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__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()
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__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__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))
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__valid__get_token(self): ''' Test to see if the token is valid and if so then do not trigger a refresh. ''' # => 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 True. This # => should trigger the Code to return back the current Token from the # => `get_token` method. check_patcher = patch(global_module_util_path + '.cohesity_auth.Authentication.check_token') mock_check = check_patcher.start() mock_check.return_value = True server = "cohesity-api" uri = "https://" + server + "/irisservices/api/v1/public/accessTokens" # => Create a new object of the Class::Authentication and # => assign the credentials including a pre-existing Token. cohesity_auth = Authentication() cohesity_auth.username = "******" cohesity_auth.password = "******" cohesity_auth.token = "mytoken" data = cohesity_auth.get_token(server) # => Assert Test Cases are valid self.assertEqual('mytoken', data) self.assertEqual(0, self.open_url.call_count)
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__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."
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__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
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 patch__methods(self, reg_return=False): source_list = self.set__default__return() patch_list = dict() token_patcher = patch(global_module_path + '.cohesity_restore.get__cohesity_auth__token') mock_check = token_patcher.start() patch_list.update(token_patcher=token_patcher) mock_check.return_value = 'mytoken' snapshot_list = """ { "jobRunId": "123", "jobUid": { "clusterId": 99, "clusterIncarnationId": 98, "id": 24 }, "protectionSourceId": 12, "startedTimeUsecs": "1541603218" } """ start_check_patcher = patch( global_module_path + '.cohesity_restore.check__protection_restore__exists') mock_check = start_check_patcher.start() patch_list.update(start_check_patcher=start_check_patcher) mock_check.return_value = False snapshot_info_patcher = patch( global_module_path + '.cohesity_restore.get__snapshot_information__for_file') mock_check = snapshot_info_patcher.start() patch_list.update(snapshot_info_patcher=snapshot_info_patcher) mock_check.return_value = json.loads(snapshot_list) start_job_patcher = patch(global_module_path + '.cohesity_restore.start_restore__files') mock_check = start_job_patcher.start() patch_list.update(start_job_patcher=start_job_patcher) mock_check.return_value = source_list return patch_list
def patch__methods(self, reg_return=False): source_list = """ { "ProtectionSource": { "id": { "uuid": "ebd9bfce-b845-4aa3-842a-3f0dc381bbab" }, "name": "vc-67.eco.eng.cohesity.com", "type": "kVCenter" } } """ patch_list = dict() token_patcher = patch(global_module_path + '.cohesity_source.get__cohesity_auth__token') mock_check = token_patcher.start() patch_list.update(token_patcher=token_patcher) mock_check.return_value = 'mytoken' registration_patcher = patch( global_module_path + '.cohesity_source.get__protection_source_registration__status') mock_check = registration_patcher.start() patch_list.update(registration_patcher=registration_patcher) mock_check.return_value = reg_return register_patcher = patch(global_module_path + '.cohesity_source.register_source') mock_check = register_patcher.start() patch_list.update(register_patcher=register_patcher) mock_check.return_value = json.loads(source_list) unregister_patcher = patch(global_module_path + '.cohesity_source.unregister_source') mock_check = unregister_patcher.start() patch_list.update(unregister_patcher=unregister_patcher) mock_check.return_value = json.loads(source_list) return patch_list
def test__start_restore__files__open_url(self): module = FakeModule(cluster="cohesity.lab", username="******", password="******", validate_certs=True) restore_job = """ { "fullViewName": "cohesity_int_15389", "id": 15389, "name": "Ansible Test Restore", "objects": [ { "jobRunId": 14294, "jobUid": { "clusterId": 8621173906188849, "clusterIncarnationId": 1538852526333, "id": 10539 }, "protectionSourceId": 531, "startedTimeUsecs": 1541435376134254 } ], "startTimeUsecs": 1541613680583185, "status": "kReadyToSchedule", "type": "kRestoreFiles", "viewBoxId": 5 } """ # => self.patcher = patch(global_module_path + '.cohesity_restore.open_url') self.open_url = self.patcher.start() # => stream = self.open_url.return_value stream.read.return_value = restore_job stream.getcode.return_value = 201 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.start_restore__files(module, data) assert return_id['id'] == 15389
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__check__protection_restore__exists(self): module = FakeModule(cluster="cohesity.lab", username="******", password="******", validate_certs=True) data = dict(token='mytoken', name="myhost") self.patcher = patch(global_module_path + '.cohesity_restore.get__restore_job__by_type') mock_check = self.patcher.start() mock_check.return_value = [] return_id = cohesity_restore.check__protection_restore__exists( module, data) assert return_id == "False"
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()
def test__refresh__get_token(self): ''' Test to see if the token is expired and if so then trigger a refresh. ''' # => 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.Authentication.check_token') mock_check = check_patcher.start() mock_check.return_value = False server = "cohesity-api" uri = "https://" + server + "/irisservices/api/v1/public/accessTokens" # => Mock the URL Return Data with a new Token. stream = self.open_url.return_value stream.read.return_value = '{"accessToken": "mynewtoken","tokenType": "Bearer"}' stream.getcode.return_value = 201 self.open_url.return_value = stream # => Create a new object of the Class::Authentication and # => assign the credentials including a pre-existing Token. cohesity_auth = Authentication() cohesity_auth.username = "******" cohesity_auth.password = "******" cohesity_auth.token = "mytoken" data = cohesity_auth.get_token(server) # => Assert Test Cases are valid self.assertEqual('mynewtoken', data) self.assertEqual(1, self.open_url.call_count) self.assertEqual(201, self.open_url.return_value.getcode.return_value) expected = call(url=uri, data=json.dumps({ "username": "******", "password": "******" }), headers={"Accept": "application/json"}, validate_certs=False) self.assertEqual(expected, self.open_url.call_args)
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
def test__main__restore__file__no_change(self): self.load_module_args() # => Configure Patchers patch_list = self.patch__methods() restore_job = """ [{ "id": 15389, "status": "kReadyToSchedule", "name": "myhost" }] """ start_check_patcher = patch_list['start_check_patcher'] start_check_patcher.stop() patch_list.pop('start_check_patcher') 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 = restore_job stream.getcode.return_value = 200 self.open_url.return_value = stream mockData = json.loads(stream.read.return_value) with self.assertRaises(AnsibleExitJson) as exc: cohesity_restore.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False, result) self.assertEqual(result['msg'], 'The Restore Job for is already registered', result) self.unload_patchers(patch_list)
def test__fail__check_token(self): ''' Test to see if the token is expired and if so then trigger a refresh. ''' # => 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() check_var = mock_check.return_value # => check_var.read.return_value = '[{"id": "1234","clusterPartitionName": "primary"}]' check_var.getcode.return_value = 200 mock_check.return_value = check_var server = "cohesity-api" uri = "https://" + server + "/irisservices/api/v1/public/nodes" # => Create a new object of the Class::Authentication and # => assign the credentials including a pre-existing Token. cohesity_auth = Authentication() cohesity_auth.username = "******" cohesity_auth.password = "******" cohesity_auth.token = "mytoken" data = cohesity_auth.check_token(server) # => Assert Test Cases are valid self.assertEqual('mytoken', data.token) self.assertEqual(1, mock_check.call_count) self.assertEqual(200, mock_check.return_value.getcode.return_value) headers = { "Accept": "application/json", "Authorization": "Bearer mytoken" } expected = call(url=uri, headers=headers, validate_certs=False) self.assertEqual(expected, mock_check.call_args)
def test__get__snapshot_information__for_file(self): module = FakeModule(cluster="cohesity.lab", username="******", password="******", validate_certs=True) source_list = """ { "jobRunId": "123", "jobUid": { "clusterId": 99, "clusterIncarnationId": 98, "id": 24 }, "protectionSourceId": 12, "startedTimeUsecs": "1541603218" } """ # => self.patcher = patch( global_module_path + '.cohesity_restore.get__snapshot_information__for_file') mock_check = self.patcher.start() mock_check.return_value = json.loads(source_list) data = dict(token='mytoken', environment='Physical', endpoint='myendpoint') return_id = cohesity_restore.get__snapshot_information__for_file( module, data) assert return_id['jobRunId'] == "123" assert return_id['startedTimeUsecs'] == '1541603218' assert return_id['jobUid']['id'] == 24
def patch__methods(self, reg_return=False): source_list = self.set__default__return() patch_list = dict() token_patcher = patch( global_module_path + '.cohesity_job.get__cohesity_auth__token') mock_check = token_patcher.start() patch_list.update(token_patcher=token_patcher) mock_check.return_value = 'mytoken' registration_patcher = patch( global_module_path + '.cohesity_job.check__protection_job__exists') mock_check = registration_patcher.start() patch_list.update(registration_patcher=registration_patcher) mock_check.return_value = reg_return mandatory_patcher = patch( global_module_path + '.cohesity_job.check__mandatory__params') mock_check = mandatory_patcher.start() patch_list.update(mandatory_patcher=mandatory_patcher) mock_check.return_value = False prot_source_patcher = patch( global_module_path + '.cohesity_job.get__prot_source_id__by_endpoint') mock_check = prot_source_patcher.start() patch_list.update(prot_source_patcher=prot_source_patcher) mock_check.return_value = 1 prot_root_patcher = patch( global_module_path + '.cohesity_job.get__prot_source_root_id__by_environment') mock_check = prot_root_patcher.start() patch_list.update(prot_root_patcher=prot_root_patcher) mock_check.return_value = 1 prot_policy_patcher = patch( global_module_path + '.cohesity_job.get__prot_policy_id__by_name') mock_check = prot_policy_patcher.start() patch_list.update(prot_policy_patcher=prot_policy_patcher) mock_check.return_value = 1 storage_domain_patcher = patch( global_module_path + '.cohesity_job.get__storage_domain_id__by_name') mock_check = storage_domain_patcher.start() patch_list.update(storage_domain_patcher=storage_domain_patcher) mock_check.return_value = 1 register_patcher = patch( global_module_path + '.cohesity_job.register_job') mock_check = register_patcher.start() patch_list.update(register_patcher=register_patcher) mock_check.return_value = source_list unregister_patcher = patch( global_module_path + '.cohesity_job.unregister_job') mock_check = unregister_patcher.start() patch_list.update(unregister_patcher=unregister_patcher) mock_check.return_value = source_list start_job_patcher = patch( global_module_path + '.cohesity_job.start_job') mock_check = start_job_patcher.start() patch_list.update(start_job_patcher=start_job_patcher) mock_check.return_value = source_list stop_job_patcher = patch( global_module_path + '.cohesity_job.stop_job') mock_check = stop_job_patcher.start() patch_list.update(stop_job_patcher=stop_job_patcher) mock_check.return_value = source_list return patch_list
def setUp(self): self.patcher = patch(global_module_util_path + '.cohesity_auth.open_url') self.open_url = self.patcher.start()