def test_GIVEN_redundant_progress_WHEN_upload_image_to_repo_THEN_filter( self, add_annotations_to_manifest): # GIVEN repository_uri = self.ecr_client.create_repository( repositoryName=self.REPO_NAME)["repository"]["repositoryUri"] status_dict = { "status": "pushing", "progressDetail": { "total": 100, "current": 10 } } statuses = (str.encode(json.dumps(status_dict)), str.encode(json.dumps(status_dict))) docker_client = docker.from_env() docker_client.api.push.return_value = statuses add_annotations_to_manifest = MagicMock() # WHEN output_gen = UploadToRepoHandler.upload_image_to_repo( self.REPO_NAME, repository_uri, "", "") output_statuses = [status for status in output_gen] # THEN # verify only 2 are returned (instead of three) # there's an extra 100 progress status for marking completion expected = [ ImageUploadStatus(progress=10, error_msg=None, error_trace=None), ImageUploadStatus(progress=100, error_msg=None, error_trace=None) ] self.assertEquals(expected, output_statuses)
def test_GIVEN_no_variables_WHEN_get_manifest_annotations_THEN_ONLY_NAME_AND_DESC( self): # GIVEN variables = [] # WHEN observed = UploadToRepoHandler.get_manifest_annotations( variables, self.container_name, self.container_description) # THEN self.assertCountEqual(self.expected_annotations, observed)
def test_GIVEN_16_progress_WHEN_cap_progress_THEN_return_status_with_16_progress( self): # GIVEN uncapped = ImageUploadStatus(progress=16, error_msg=None, error_trace=None) # WHEN capped = UploadToRepoHandler.cap_progress(uncapped) # THEN self.assertEquals(uncapped, capped)
def test_GIVEN_no_notebook_path_WHEN_get_invalid_params_resp_THEN_still_return_error( self): # GIVEN del self.input_params["notebook_path"] # WHEN observed_resp = yield UploadToRepoHandler.get_invalid_params_resp( self.input_params) # THEN error_msg = "The following fields were not specified: notebook_path." expected_msg = ContainerizationStatusLogEntry.of_image_creation( None, error_msg=error_msg, progress=0) self.assertEqual(expected_msg, observed_resp)
def test_GIVEN_current_and_total_WHEN_parse_status_THEN_return_status_with_progress( self): status = json.dumps({ "status": "pushing", "progressDetail": { "total": 100, "current": 10 } }) expected = ImageUploadStatus(progress=10, error_msg=None, error_trace=None) self.assertEquals(expected, UploadToRepoHandler.parse_upload_status(status))
def test_GIVEN_layer_already_upload_WHEN_parse_status_THEN_return_status_with_error( self): status = json.dumps({ "status": "pushing", "progressDetail": UploadToRepoHandler.LAYER_EXISTS_MSG }) expected = ImageUploadStatus( progress=100, error_trace=None, error_msg="This image has already been uploaded to this repository." ) self.assertEquals(expected, UploadToRepoHandler.parse_upload_status(status))
def test_GIVEN_diff_from_previous_progress_WHEN_status_should_be_reported_THEN( self): # GIVEN previous_progress = 2 recent_progress = 2 status = ImageUploadStatus(progress=recent_progress, error_msg=None, error_trace=None) # WHEN observed = UploadToRepoHandler.status_should_be_reported( status, previous_progress) # THEN self.assertFalse(observed)
def test_GIVEN_invalid_var_name_WHEN_get_invalid_params_resp_THEN_fill_error_msg( self): # GIVEN self.input_params["variables"][0]["name"] = "@hi" # WHEN observed_msg = yield UploadToRepoHandler.get_invalid_params_resp( self.input_params) # THEN error_msg = "The following names are not valid python identifiers: @hi." expected_msg = ContainerizationStatusLogEntry.of_image_creation( self.input_params["notebook_path"], error_msg=error_msg, progress=0) self.assertEqual(expected_msg, observed_msg)
def test_GIVEN_empty_desc_WHEN_get_manifest_annotations_THEN_null_desc( self): # GIVEN variables = [{"type": "string", "description": "", "name": "name"}] # WHEN observed = UploadToRepoHandler.get_manifest_annotations( variables, self.container_name, self.container_description) # THEN expected_annotations = { **self.expected_annotations, "name": json.dumps({ "type": "string", "description": None }) } self.assertCountEqual(expected_annotations, observed)
def test_GIVEN_redundant_progress_with_error_WHEN_upload_image_to_repo_THEN_dont_filter( self, add_annotations_to_manifest): # GIVEN repository_uri = self.ecr_client.create_repository( repositoryName=self.REPO_NAME)["repository"]["repositoryUri"] image = "image" status_dict1 = { "status": "pushing", "progressDetail": { "total": 100, "current": 99 } } status_dict2 = { "status": "pushing", "progressDetail": UploadToRepoHandler.LAYER_EXISTS_MSG } statuses = (str.encode(json.dumps(status_dict1)), str.encode(json.dumps(status_dict2))) docker_client = docker.from_env() docker_client.api.push.return_value = statuses add_annotations_to_manifest = MagicMock() # WHEN output_gen = UploadToRepoHandler.upload_image_to_repo( self.REPO_NAME, repository_uri, image, "") output_statuses = [status for status in output_gen] # THEN error_msg = "This image has already been uploaded to this repository." expected = [ ImageUploadStatus(progress=99, error_msg=None, error_trace=None), ImageUploadStatus(progress=99, error_msg=error_msg, error_trace=None) ] self.assertEquals(expected, output_statuses)
def test_GIVEN_valid_var_params_WHEN_get_invalid_params_resp_THEN_none( self): observed_resp = yield UploadToRepoHandler.get_invalid_params_resp( self.input_params) self.assertFalse(observed_resp)
def test_GIVEN_no_progress_detail_WHEN_parse_status_THEN_return_None(self): status = json.dumps({"status": {"key": "value"}}) self.assertEquals(None, UploadToRepoHandler.parse_upload_status(status))
def test_GIVEN_this_push_refers_to_WHEN_parse_status_THEN_return_None( self): status = json.dumps( {"status": UploadToRepoHandler.REFERS_TO_REPO_PREFIX}) self.assertEquals(None, UploadToRepoHandler.parse_upload_status(status))
def test_GIVEN_no_status_WHEN_parse_status_THEN_return_None(self): status = json.dumps({"other": ""}) self.assertEquals(None, UploadToRepoHandler.parse_upload_status(status))
def test_GIVEN_not_json_WHEN_parse_status_THEN_return_None(self): self.assertEquals(None, UploadToRepoHandler.parse_upload_status("test"))