Skip to content

Commit

Permalink
Add more runs delete unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Eamonn Mansour <[email protected]>
  • Loading branch information
eamansour committed Sep 3, 2024
1 parent 6cc5db4 commit cf0a375
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 76 deletions.
9 changes: 7 additions & 2 deletions docs/generated/errors-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ The `galasactl` tool can generate the following errors:
- GAL1154E: The provided token ID, '{}', does not match formatting requirements. The token ID can contain any character in the 'a'-'z', 'A'-'Z', '0'-'9', '-' (dash), or '_' (underscore) ranges only.
- GAL1155E: The id provided by the --id field cannot be an empty string.
- GAL1156E: '{}' is not supported as a valid value. Valid values are 'me'.
- GAL1157E: An attempt to delete a run failed. Cause is {}
- GAL1158E: An attempt to delete a run failed. The server responded with an error. Cause is {}
- GAL1157E: An attempt to delete a run named '{}' failed. Sending the delete request to the Galasa service failed. Cause is {}
- GAL1159E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server.
- GAL1160E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {}
- GAL1161E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}'
- GAL1162E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server. Error details from the server are: {}
- GAL1163E: The run named '{}' could not be deleted because it was not found by the Galasa service. Try listing runs using 'galasactl runs get' to identify the one you wish to delete
- GAL1164E: An attempt to delete a run named '{}' failed. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
- GAL1225E: Failed to open file '{}' cause: {}. Check that this file exists, and that you have read permissions.
- GAL1226E: Internal failure. Contents of gzip could be read, but not decoded. New gzip reader failed: file: {} error: {}
- GAL1227E: Internal failure. Contents of gzip could not be decoded. {} error: {}
Expand Down
13 changes: 10 additions & 3 deletions pkg/errors/errorMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,16 @@ var (
GALASA_ERROR_MISSING_USER_LOGIN_ID_FLAG = NewMessageType("GAL1155E: The id provided by the --id field cannot be an empty string.", 1155, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_LOGIN_ID_NOT_SUPPORTED = NewMessageType("GAL1156E: '%s' is not supported as a valid value. Valid values are 'me'.", 1156, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUN_FAILED = NewMessageType("GAL1157E: An attempt to delete a run named '%s' failed. Cause is %s", 1157, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_SERVER_DELETE_RUNS_FAILED = NewMessageType("GAL1158E: An attempt to delete a run named '%s' failed. The server responded with an error. Cause is %s", 1158, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_NO_RESPONSE_CONTENT = NewMessageType("GAL1159E: Failed to delete a run named '%s'. The server responded with an unexpected status code '%v' and did not contain any content. Cause is %s", 1159, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_BADLY_FORMATTED = NewMessageType("GAL1159E: Failed to delete a run named '%s'. The server responded with an unexpected status code '%v' and did not contain any content. Cause is %s", 1159, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_SERVER_DELETE_RUNS_FAILED = NewMessageType("GAL1157E: An attempt to delete a run named '%s' failed. Sending the delete request to the Galasa service failed. Cause is %v", 1157, STACK_TRACE_NOT_WANTED)

// 4 related but slightly different errors, when an HTTP response arrives from the Galasa server, and we can/can't parse the payload to get the message details out.
GALASA_ERROR_DELETE_RUNS_NO_RESPONSE_CONTENT = NewMessageType("GAL1159E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server.", 1159, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_RESPONSE_PAYLOAD_UNREADABLE = NewMessageType("GAL1160E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server could not be read. Cause: %s", 1160, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_UNPARSEABLE_CONTENT = NewMessageType("GAL1161E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1161, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_SERVER_REPORTED_ERROR = NewMessageType("GAL1162E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are: %s", 1162, STACK_TRACE_NOT_WANTED)

GALASA_ERROR_SERVER_DELETE_RUN_NOT_FOUND = NewMessageType("GAL1163E: The run named '%s' could not be deleted because it was not found by the Galasa service. Try listing runs using 'galasactl runs get' to identify the one you wish to delete", 1163, STACK_TRACE_NOT_WANTED)
GALASA_ERROR_DELETE_RUNS_EXPLANATION_NOT_JSON = NewMessageType("GAL1164E: An attempt to delete a run named '%s' failed. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1164, STACK_TRACE_NOT_WANTED)

// Warnings...
GALASA_WARNING_MAVEN_NO_GALASA_OBR_REPO = NewMessageType("GAL2000W: Warning: Maven configuration file settings.xml should contain a reference to a Galasa repository so that the galasa OBR can be resolved. The official release repository is '%s', and 'pre-release' repository is '%s'", 2000, STACK_TRACE_WANTED)
Expand Down
14 changes: 11 additions & 3 deletions pkg/errors/galasaAPIError.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,24 @@ type GalasaAPIError struct {
// the reason for the failure.
// NOTE: when this function is called ensure that the calling function has the `defer resp.Body.Close()`
// called in order to ensure that the response body is closed when the function completes
func GetApiErrorFromResponse(body []byte) (*GalasaAPIError, error){
func GetApiErrorFromResponse(body []byte) (*GalasaAPIError, error) {
return GetApiErrorFromResponseBytes(body, func(marshallingError error) error{
err := NewGalasaError(GALASA_ERROR_UNABLE_TO_READ_RESPONSE_BODY, marshallingError)
return err
},
)
}

func GetApiErrorFromResponseBytes(body []byte, marshallingErrorLambda func(marshallingError error) error) (*GalasaAPIError, error) {
var err error

apiError := new(GalasaAPIError)

err = json.Unmarshal(body, &apiError)

if err != nil {
log.Printf("GetApiErrorFromResponse FAIL - %v", err)
err = NewGalasaError(GALASA_ERROR_UNABLE_TO_READ_RESPONSE_BODY, err.Error())
log.Printf("GetApiErrorFromResponseBytes failed to unmarshal bytes into a galasa api error structure. %v", err.Error())
err = marshallingErrorLambda(err)
}
return apiError, err
}
69 changes: 43 additions & 26 deletions pkg/runs/runsDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ func RunsDelete(
runs, err = GetRunsFromRestApi(runName, requestorParameter, resultParameter, fromAgeHours, toAgeHours, shouldGetActive, timeService, apiClient)

if err == nil {
err = deleteRuns(runs, apiClient)
} else {

if len(runs) == 0 {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_SERVER_DELETE_RUN_NOT_FOUND, runName)
} else {
err = deleteRuns(runs, apiClient)
}
}

if err != nil {
console.WriteString(err.Error())
}
}
Expand All @@ -72,7 +79,7 @@ func deleteRuns(
if err == nil {
for _, run := range runs {
runId := run.GetRunId()
// runName := *run.GetTestStructure().RunName
runName := *run.GetTestStructure().RunName

apicall := apiClient.ResultArchiveStoreAPIApi.DeleteRasRunById(context, runId).ClientApiVersion(restApiVersion)
httpResponse, err = apicall.Execute()
Expand All @@ -83,13 +90,15 @@ func deleteRuns(
// We never got a response, error sending it or something ?
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_SERVER_DELETE_RUNS_FAILED, err.Error())
} else {
// err = convertToGalasaError(
// httpResponse,
// runName,
// GALASA_ERROR_DELETE_RUNS_NO_RESPONSE_CONTENT,
// GALASA_ERROR_DELETE_RUN_FAILED,

// )
err = httpResponseToGalasaError(
httpResponse,
runName,
galasaErrors.GALASA_ERROR_DELETE_RUNS_NO_RESPONSE_CONTENT,
galasaErrors.GALASA_ERROR_DELETE_RUNS_RESPONSE_PAYLOAD_UNREADABLE,
galasaErrors.GALASA_ERROR_DELETE_RUNS_UNPARSEABLE_CONTENT,
galasaErrors.GALASA_ERROR_DELETE_RUNS_SERVER_REPORTED_ERROR,
galasaErrors.GALASA_ERROR_DELETE_RUNS_EXPLANATION_NOT_JSON,
)
}
}

Expand All @@ -104,13 +113,14 @@ func deleteRuns(
return err
}

func convertToGalasaError(
func httpResponseToGalasaError(
response *http.Response,
identifier string,
errorMsgUnexpectedStatusCodeNoResponseBody *galasaErrors.MessageType,
errorMsgUnableToReadResponseBody *galasaErrors.MessageType,
errorMsgReceivedFromApiServer *galasaErrors.MessageType,
errorMsgResponsePayloadInWrongFormat *galasaErrors.MessageType,
errorMsgReceivedFromApiServer *galasaErrors.MessageType,
errorMsgResponseContentTypeNotJson *galasaErrors.MessageType,
) error {
defer response.Body.Close()
var err error
Expand All @@ -121,23 +131,30 @@ func convertToGalasaError(
log.Printf("Failed - HTTP response - status code: '%v'\n", statusCode)
err = galasaErrors.NewGalasaError(errorMsgUnexpectedStatusCodeNoResponseBody, identifier, statusCode)
} else {

responseBodyBytes, err = io.ReadAll(response.Body)
if err != nil {
err = galasaErrors.NewGalasaError(errorMsgUnableToReadResponseBody, identifier, statusCode, err.Error())
contentType := response.Header.Get("Content-Type")
if contentType != "application/json" {
err = galasaErrors.NewGalasaError(errorMsgResponseContentTypeNotJson, identifier, statusCode)
} else {

var errorFromServer *galasaErrors.GalasaAPIError
errorFromServer, err = galasaErrors.GetApiErrorFromResponse(responseBodyBytes)

responseBodyBytes, err = io.ReadAll(response.Body)
if err != nil {
//unable to parse response into api error. It should have been json.
log.Printf("Failed - HTTP response - status code: '%v' payload in response is not json: '%v' \n", statusCode, string(responseBodyBytes))
err = galasaErrors.NewGalasaError(errorMsgResponsePayloadInWrongFormat, identifier, statusCode)
err = galasaErrors.NewGalasaError(errorMsgUnableToReadResponseBody, identifier, statusCode, err.Error())
} else {
// server returned galasa api error structure we understand.
log.Printf("Failed - HTTP response - status code: '%v' server responded with error message: '%v' \n", statusCode, errorMsgReceivedFromApiServer)
err = galasaErrors.NewGalasaError(errorMsgReceivedFromApiServer, identifier, errorFromServer.Message)

var errorFromServer *galasaErrors.GalasaAPIError
errorFromServer, err = galasaErrors.GetApiErrorFromResponseBytes(
responseBodyBytes,
func (marshallingError error) error {
log.Printf("Failed - HTTP response - status code: '%v' payload in response is not json: '%v' \n", statusCode, string(responseBodyBytes))
return galasaErrors.NewGalasaError(errorMsgResponsePayloadInWrongFormat, identifier, statusCode, marshallingError)
},
)

if err == nil {
// server returned galasa api error structure we understand.
log.Printf("Failed - HTTP response - status code: '%v' server responded with error message: '%v' \n", statusCode, errorMsgReceivedFromApiServer)
err = galasaErrors.NewGalasaError(errorMsgReceivedFromApiServer, identifier, errorFromServer.Message)
}
}
}
}
Expand Down
Loading

0 comments on commit cf0a375

Please sign in to comment.