From 24956f893fde75c2e18dd027faefa63d2c037e83 Mon Sep 17 00:00:00 2001 From: Aashir Siddiqui Date: Wed, 9 Apr 2025 14:03:38 +0100 Subject: [PATCH 1/3] Added streams delete command Signed-off-by: Aashir Siddiqui --- docs/generated/errors-list.md | 2 + docs/generated/galasactl_streams.md | 1 + docs/generated/galasactl_streams_delete.md | 33 +++++ pkg/cmd/commandCollection.go | 13 +- pkg/cmd/streamsDelete.go | 149 +++++++++++++++++++++ pkg/cmd/streamsDelete_test.go | 62 +++++++++ pkg/errors/errorMessage.go | 2 + pkg/streams/streamsDelete.go | 86 ++++++++++++ pkg/streams/streamsDelete_test.go | 148 ++++++++++++++++++++ 9 files changed, 495 insertions(+), 1 deletion(-) create mode 100644 docs/generated/galasactl_streams_delete.md create mode 100644 pkg/cmd/streamsDelete.go create mode 100644 pkg/cmd/streamsDelete_test.go create mode 100644 pkg/streams/streamsDelete.go create mode 100644 pkg/streams/streamsDelete_test.go diff --git a/docs/generated/errors-list.md b/docs/generated/errors-list.md index cf53ad81..83d11ce0 100644 --- a/docs/generated/errors-list.md +++ b/docs/generated/errors-list.md @@ -237,6 +237,8 @@ The `galasactl` tool can generate the following errors: - GAL1238E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}' - GAL1239E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are: '{}' - GAL1240E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in the json format. +- GAL1241E: The test stream could not be deleted by name because it was not found by the Galasa service. Try listing streams using 'galasactl streams get' to identify the one you wish to delete +- GAL1242E: Failed to test stream from database by stream name. - 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 '{}', and 'pre-release' repository is '{}' - GAL2501I: Downloaded {} artifacts to folder '{}' diff --git a/docs/generated/galasactl_streams.md b/docs/generated/galasactl_streams.md index d6b0002d..cd4d3b23 100644 --- a/docs/generated/galasactl_streams.md +++ b/docs/generated/galasactl_streams.md @@ -25,5 +25,6 @@ Parent command for managing test streams in a Galasa service ### SEE ALSO * [galasactl](galasactl.md) - CLI for Galasa +* [galasactl streams delete](galasactl_streams_delete.md) - Deletes a test stream by name * [galasactl streams get](galasactl_streams_get.md) - Gets a list of test streams diff --git a/docs/generated/galasactl_streams_delete.md b/docs/generated/galasactl_streams_delete.md new file mode 100644 index 00000000..f1bbfa6d --- /dev/null +++ b/docs/generated/galasactl_streams_delete.md @@ -0,0 +1,33 @@ +## galasactl streams delete + +Deletes a test stream by name + +### Synopsis + +Deletes a single test stream by the given name from the API Server + +``` +galasactl streams delete [flags] +``` + +### Options + +``` + -h, --help Displays the options for the 'streams delete' command. + --name string An optional field indicating the name of a test stream +``` + +### Options inherited from parent commands + +``` + -b, --bootstrap string Bootstrap URL. Should start with 'http://' or 'file://'. If it starts with neither, it is assumed to be a fully-qualified path. If missing, it defaults to use the 'bootstrap.properties' file in your GALASA_HOME. Example: http://example.com/bootstrap, file:///user/myuserid/.galasa/bootstrap.properties , file://C:/Users/myuserid/.galasa/bootstrap.properties + --galasahome string Path to a folder where Galasa will read and write files and configuration settings. The default is '${HOME}/.galasa'. This overrides the GALASA_HOME environment variable which may be set instead. + -l, --log string File to which log information will be sent. Any folder referred to must exist. An existing file will be overwritten. Specify "-" to log to stderr. Defaults to not logging. + --rate-limit-retries int The maximum number of retries that should be made when requests to the Galasa Service fail due to rate limits being exceeded. Must be a whole number. Defaults to 3 retries (default 3) + --rate-limit-retry-backoff-secs float The amount of time in seconds to wait before retrying a command if it failed due to rate limits being exceeded. Defaults to 1 second. (default 1) +``` + +### SEE ALSO + +* [galasactl streams](galasactl_streams.md) - Manages test streams in a Galasa service + diff --git a/pkg/cmd/commandCollection.go b/pkg/cmd/commandCollection.go index 0522ce55..c574ed99 100644 --- a/pkg/cmd/commandCollection.go +++ b/pkg/cmd/commandCollection.go @@ -74,6 +74,7 @@ const ( COMMAND_NAME_ROLES_GET = "roles get" COMMAND_NAME_STREAMS = "streams" COMMAND_NAME_STREAMS_GET = "streams get" + COMMAND_NAME_STREAMS_DELETE = "streams delete" ) // ----------------------------------------------------------------- @@ -519,14 +520,24 @@ func (commands *commandCollectionImpl) addStreamsCommands(factory spi.Factory, r var err error var streamsCommand spi.GalasaCommand var streamsGetCommand spi.GalasaCommand + var streamsDeleteCommand spi.GalasaCommand streamsCommand, err = NewStreamsCommand(rootCommand, commsFlagSet) if err == nil { + + commands.commandMap[streamsCommand.Name()] = streamsCommand streamsGetCommand, err = NewStreamsGetCommand(factory, streamsCommand, commsFlagSet) + if err == nil { - commands.commandMap[streamsCommand.Name()] = streamsCommand + commands.commandMap[streamsGetCommand.Name()] = streamsGetCommand + streamsDeleteCommand, err = NewStreamsDeleteCommand(factory, streamsCommand, commsFlagSet) + + if err == nil { + commands.commandMap[streamsDeleteCommand.Name()] = streamsDeleteCommand + } + } } diff --git a/pkg/cmd/streamsDelete.go b/pkg/cmd/streamsDelete.go new file mode 100644 index 00000000..1a17c497 --- /dev/null +++ b/pkg/cmd/streamsDelete.go @@ -0,0 +1,149 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ + +package cmd + +import ( + "log" + + "github.com/galasa-dev/cli/pkg/api" + "github.com/galasa-dev/cli/pkg/galasaapi" + "github.com/galasa-dev/cli/pkg/spi" + "github.com/galasa-dev/cli/pkg/streams" + "github.com/galasa-dev/cli/pkg/utils" + "github.com/spf13/cobra" +) + +// Objective: Allow user to do this: +// +// streams delete +type StreamsDeleteCommand struct { + cobraCommand *cobra.Command +} + +// ------------------------------------------------------------------------------------------------ +// Constructors methods +// ------------------------------------------------------------------------------------------------ +func NewStreamsDeleteCommand( + factory spi.Factory, + streamsDeleteCommand spi.GalasaCommand, + commsFlagSet GalasaFlagSet, +) (spi.GalasaCommand, error) { + + cmd := new(StreamsDeleteCommand) + err := cmd.init(factory, streamsDeleteCommand, commsFlagSet) + return cmd, err + +} + +// ------------------------------------------------------------------------------------------------ +// Public methods +// ------------------------------------------------------------------------------------------------ +func (cmd *StreamsDeleteCommand) Name() string { + return COMMAND_NAME_STREAMS_DELETE +} + +func (cmd *StreamsDeleteCommand) CobraCommand() *cobra.Command { + return cmd.cobraCommand +} + +func (cmd *StreamsDeleteCommand) Values() interface{} { + return nil +} + +// ------------------------------------------------------------------------------------------------ +// Private methods +// ------------------------------------------------------------------------------------------------ + +func (cmd *StreamsDeleteCommand) init(factory spi.Factory, streamsCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error { + + var err error + + cmd.cobraCommand, err = cmd.createCobraCmd(factory, streamsCommand, commsFlagSet) + + return err + +} + +func (cmd *StreamsDeleteCommand) createCobraCmd( + factory spi.Factory, + streamsCommand spi.GalasaCommand, + commsFlagSet GalasaFlagSet, +) (*cobra.Command, error) { + + var err error + + commsFlagSetValues := commsFlagSet.Values().(*CommsFlagSetValues) + streamsCommandValues := streamsCommand.Values().(*StreamsCmdValues) + + streamsDeleteCobraCmd := &cobra.Command{ + Use: "delete", + Short: "Deletes a test stream by name", + Long: "Deletes a single test stream by the given name from the API Server", + Aliases: []string{COMMAND_NAME_STREAMS_DELETE}, + RunE: func(cobraCommand *cobra.Command, args []string) error { + return cmd.executeStreamsDelete( + factory, streamsCommand.Values().(*StreamsCmdValues), commsFlagSetValues, + ) + }, + } + + addStreamNameFlag(streamsDeleteCobraCmd, false, streamsCommandValues) + streamsCommand.CobraCommand().AddCommand(streamsDeleteCobraCmd) + + return streamsDeleteCobraCmd, err + +} + +func (cmd *StreamsDeleteCommand) executeStreamsDelete( + factory spi.Factory, + streamsCmdValues *StreamsCmdValues, + commsFlagSetValues *CommsFlagSetValues, +) error { + + var err error + + // Operations on the file system will all be relative to the current folder. + fileSystem := factory.GetFileSystem() + byteReader := factory.GetByteReader() + + err = utils.CaptureLog(fileSystem, commsFlagSetValues.logFileName) + + if err == nil { + + commsFlagSetValues.isCapturingLogs = true + + log.Println("Galasa CLI - Delete test stream from the Galasa service") + + // Get the ability to query environment variables. + env := factory.GetEnvironment() + + var galasaHome spi.GalasaHome + galasaHome, err = utils.NewGalasaHome(fileSystem, env, commsFlagSetValues.CmdParamGalasaHomePath) + if err == nil { + + var commsClient api.APICommsClient + commsClient, err = api.NewAPICommsClient( + commsFlagSetValues.bootstrap, + commsFlagSetValues.maxRetries, + commsFlagSetValues.retryBackoffSeconds, + factory, + galasaHome, + ) + + if err == nil { + deleteStreamFunc := func(apiClient *galasaapi.APIClient) error { + // Call to process the command in a unit-testable way. + return streams.DeleteStream(streamsCmdValues.name, apiClient, byteReader) + } + err = commsClient.RunAuthenticatedCommandWithRateLimitRetries(deleteStreamFunc) + } + } + } + + return err + +} diff --git a/pkg/cmd/streamsDelete_test.go b/pkg/cmd/streamsDelete_test.go new file mode 100644 index 00000000..32b0664b --- /dev/null +++ b/pkg/cmd/streamsDelete_test.go @@ -0,0 +1,62 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ + +package cmd + +import ( + "testing" + + "github.com/galasa-dev/cli/pkg/utils" + "github.com/stretchr/testify/assert" +) + +func TestStreamsDeleteCommandInCommandCollectionHasName(t *testing.T) { + + factory := utils.NewMockFactory() + commands, _ := NewCommandCollection(factory) + + StreamsDeleteCommand, err := commands.GetCommand(COMMAND_NAME_STREAMS_DELETE) + assert.Nil(t, err) + + assert.Equal(t, COMMAND_NAME_STREAMS_DELETE, StreamsDeleteCommand.Name()) + assert.NotNil(t, StreamsDeleteCommand.CobraCommand()) + +} + +func TestStreamsDeleteHelpFlagSetCorrectly(t *testing.T) { + // Given... + factory := utils.NewMockFactory() + + var args []string = []string{"streams", "delete", "--help"} + + // When... + err := Execute(factory, args) + + // Then... + // Check what the user saw is reasonable. + checkOutput("Displays the options for the 'streams delete' command.", "", factory, t) + + assert.Nil(t, err) +} + +func TestStreamsDeleteNamespaceNameFlagsReturnsOk(t *testing.T) { + // Given... + factory := utils.NewMockFactory() + commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_STREAMS_DELETE, factory, t) + + var args []string = []string{"streams", "delete", "--name", "mystream"} + + // When... + err := commandCollection.Execute(args) + + // Then... + assert.Nil(t, err) + + // Check what the user saw was reasonable + checkOutput("", "", factory, t) + + assert.Nil(t, err) +} diff --git a/pkg/errors/errorMessage.go b/pkg/errors/errorMessage.go index 1321382a..a4912962 100644 --- a/pkg/errors/errorMessage.go +++ b/pkg/errors/errorMessage.go @@ -427,6 +427,8 @@ var ( GALASA_ERROR_GET_STREAMS_UNPARSEABLE_CONTENT = NewMessageType("GAL1238E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1238, STACK_TRACE_NOT_WANTED) GALASA_ERROR_GET_STREAMS_SERVER_REPORTED_ERROR = NewMessageType("GAL1239E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1239, STACK_TRACE_NOT_WANTED) GALASA_ERROR_GET_STREAMS_EXPLANATION_NOT_JSON = NewMessageType("GAL1240E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1240, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_NOT_FOUND = NewMessageType("GAL1241E: The test stream could not be deleted by name because it was not found by the Galasa service. Try listing streams using 'galasactl streams get' to identify the one you wish to delete", 1241, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_FAILED_TO_DELETE_STREAM = NewMessageType("GAL1242E: Failed to test stream from database by stream name.", 1242, STACK_TRACE_NOT_WANTED) // When getting multiple monitors... GALASA_ERROR_GET_MONITORS_REQUEST_FAILED = NewMessageType("GAL1218E: Failed to get monitors. Sending the get request to the Galasa service failed. Cause is %v", 1218, STACK_TRACE_NOT_WANTED) diff --git a/pkg/streams/streamsDelete.go b/pkg/streams/streamsDelete.go new file mode 100644 index 00000000..ef21e056 --- /dev/null +++ b/pkg/streams/streamsDelete.go @@ -0,0 +1,86 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ + +package streams + +import ( + "context" + "log" + "net/http" + + "github.com/galasa-dev/cli/pkg/embedded" + galasaErrors "github.com/galasa-dev/cli/pkg/errors" + "github.com/galasa-dev/cli/pkg/galasaapi" + "github.com/galasa-dev/cli/pkg/spi" +) + +func DeleteStream(streamName string, apiClient *galasaapi.APIClient, byteReader spi.ByteReader) error { + + streams, err := getStreamsFromRestApi(streamName, apiClient, byteReader) + + if err == nil { + + if len(streams) != 0 { + err = deleteStreamFromRestApi(streams[0], apiClient, byteReader) + } else { + err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_DELETE_STREAMS_NOT_FOUND) + } + + } + + return err + +} + +func deleteStreamFromRestApi( + stream galasaapi.Stream, + apiClient *galasaapi.APIClient, + byteReader spi.ByteReader, +) error { + + var context context.Context = nil + var resp *http.Response + + restApiVersion, err := embedded.GetGalasactlRestApiVersion() + + if err == nil { + + streamName := stream.Metadata.GetName() + apiCall := apiClient.StreamsAPIApi.DeleteStreamByName(context, streamName).ClientApiVersion(restApiVersion) + resp, err = apiCall.Execute() + + if resp != nil { + defer resp.Body.Close() + } + + if err != nil { + + if resp == nil { + + err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_FAILED_TO_DELETE_STREAM, err.Error()) + + } else { + + err = galasaErrors.HttpResponseToGalasaError( + resp, + streamName, + byteReader, + galasaErrors.GALASA_ERROR_GET_STREAMS_NO_RESPONSE_CONTENT, + galasaErrors.GALASA_ERROR_GET_STREAMS_RESPONSE_BODY_UNREADABLE, + galasaErrors.GALASA_ERROR_GET_STREAMS_UNPARSEABLE_CONTENT, + galasaErrors.GALASA_ERROR_GET_STREAMS_SERVER_REPORTED_ERROR, + galasaErrors.GALASA_ERROR_GET_STREAMS_EXPLANATION_NOT_JSON, + ) + + } + + log.Printf("Test stream with name '%s', was deleted OK.\n", streamName) + } + } + + return err + +} diff --git a/pkg/streams/streamsDelete_test.go b/pkg/streams/streamsDelete_test.go new file mode 100644 index 00000000..fffd33a5 --- /dev/null +++ b/pkg/streams/streamsDelete_test.go @@ -0,0 +1,148 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ + +package streams + +import ( + "encoding/json" + "fmt" + "net/http" + "strconv" + "strings" + "testing" + + "github.com/galasa-dev/cli/pkg/api" + "github.com/galasa-dev/cli/pkg/galasaapi" + "github.com/galasa-dev/cli/pkg/utils" + "github.com/stretchr/testify/assert" +) + +func createMockStream(name string, description string) galasaapi.Stream { + + stream := *galasaapi.NewStream() + streamMetadata := *galasaapi.NewStreamMetadata() + + streamMetadata.SetName(name) + streamMetadata.SetDescription(description) + + stream.SetMetadata(streamMetadata) + + return stream + +} + +func WriteMockStreamResponse( + t *testing.T, + writer http.ResponseWriter, + req *http.Request, + name string, + streamResultStrings []string) { + + writer.Header().Set("Content-Type", "application/json") + values := req.URL.Path + path := strings.Split(values, "/") + streamPathVar := path[2] + assert.Equal(t, streamPathVar, name) + + writer.Write([]byte(fmt.Sprintf(` + { + "metadata":{ + "name": "%s", + "description": "This is a dummy stream" + } + }`, name))) + +} + +func TestStreamUserDeleteAUser(t *testing.T) { + + //Given... + name := "mystream" + description := "This is a dummy stream" + + streamToDelete := createMockStream(name, description) + streamToDeleteBytes, _ := json.Marshal(streamToDelete) + streamToDeleteJson := string(streamToDeleteBytes) + + getStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodGet) + getStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { + WriteMockStreamResponse(t, writer, req, name, []string{streamToDeleteJson}) + } + + deleteStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodDelete) + deleteStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { + writer.WriteHeader(http.StatusNoContent) + } + + interactions := []utils.HttpInteraction{ + getStreamInteraction, + deleteStreamInteraction, + } + + server := utils.NewMockHttpServer(t, interactions) + defer server.Server.Close() + + console := utils.NewMockConsole() + apiServerUrl := server.Server.URL + apiClient := api.InitialiseAPI(apiServerUrl) + mockByteReader := utils.NewMockByteReader() + + // When... + err := DeleteStream( + name, + apiClient, + mockByteReader) + + // Then... + assert.Nil(t, err, "DeleteStream returned an unexpected error") + assert.Empty(t, console.ReadText(), "The console was written to on a successful deletion, it should be empty") +} + +func TestStreamDeleteThrowsAnUnexpectedError(t *testing.T) { + + //Given... + name := "mystream" + description := "This is a dummy stream" + + streamToDelete := createMockStream(name, description) + streamToDeleteBytes, _ := json.Marshal(streamToDelete) + streamToDeleteJson := string(streamToDeleteBytes) + + getStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodGet) + getStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { + WriteMockStreamResponse(t, writer, req, name, []string{streamToDeleteJson}) + } + + deleteStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodDelete) + deleteStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { + writer.Header().Set("Content-Type", "application/json") + writer.WriteHeader(http.StatusInternalServerError) + writer.Write([]byte(`{}`)) + } + + interactions := []utils.HttpInteraction{ + getStreamInteraction, + deleteStreamInteraction, + } + + server := utils.NewMockHttpServer(t, interactions) + defer server.Server.Close() + + apiServerUrl := server.Server.URL + apiClient := api.InitialiseAPI(apiServerUrl) + mockByteReader := utils.NewMockByteReader() + + // When... + err := DeleteStream( + name, + apiClient, + mockByteReader) + + // Then... + assert.NotNil(t, err, "DeleteStream returned an unexpected error") + assert.Contains(t, err.Error(), strconv.Itoa(http.StatusInternalServerError)) + assert.Contains(t, err.Error(), "GAL1239E") +} From 5b7cdfc7d9be5b8852dbfefccb1eacee8074ad04 Mon Sep 17 00:00:00 2001 From: Aashir Siddiqui Date: Wed, 9 Apr 2025 16:44:41 +0100 Subject: [PATCH 2/3] Removed unwanted get interaction Signed-off-by: Aashir Siddiqui --- docs/generated/errors-list.md | 8 ++- docs/generated/galasactl_streams_delete.md | 4 +- pkg/cmd/streamsDelete.go | 4 +- pkg/cmd/streamsDelete_test.go | 18 +++++++ pkg/errors/errorMessage.go | 10 +++- pkg/streams/streamsDelete.go | 25 ++++------ pkg/streams/streamsDelete_test.go | 58 +++++++++++++--------- 7 files changed, 80 insertions(+), 47 deletions(-) diff --git a/docs/generated/errors-list.md b/docs/generated/errors-list.md index 83d11ce0..1079376c 100644 --- a/docs/generated/errors-list.md +++ b/docs/generated/errors-list.md @@ -237,8 +237,12 @@ The `galasactl` tool can generate the following errors: - GAL1238E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}' - GAL1239E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are: '{}' - GAL1240E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in the json format. -- GAL1241E: The test stream could not be deleted by name because it was not found by the Galasa service. Try listing streams using 'galasactl streams get' to identify the one you wish to delete -- GAL1242E: Failed to test stream from database by stream name. +- GAL12412E: Failed to delete test stream with the given name from the Galasa service +- GAL1242E: Failed to delete streams. Unexpected http status code {} received from the server. +- GAL1243E: Failed to delete streams. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {} +- GAL1244E: Failed to delete streams. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}' +- GAL1245E: Failed to delete streams. Unexpected http status code {} received from the server. Error details from the server are: '{}' +- GAL1246E: Failed to delete streams. Unexpected http status code {} received from the server. Error details from the server are not in the json format. - 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 '{}', and 'pre-release' repository is '{}' - GAL2501I: Downloaded {} artifacts to folder '{}' diff --git a/docs/generated/galasactl_streams_delete.md b/docs/generated/galasactl_streams_delete.md index f1bbfa6d..1831065d 100644 --- a/docs/generated/galasactl_streams_delete.md +++ b/docs/generated/galasactl_streams_delete.md @@ -4,7 +4,7 @@ Deletes a test stream by name ### Synopsis -Deletes a single test stream by the given name from the API Server +Deletes a single test stream with the given name from the Galasa service ``` galasactl streams delete [flags] @@ -14,7 +14,7 @@ galasactl streams delete [flags] ``` -h, --help Displays the options for the 'streams delete' command. - --name string An optional field indicating the name of a test stream + --name string A mandatory field indicating the name of a test stream. ``` ### Options inherited from parent commands diff --git a/pkg/cmd/streamsDelete.go b/pkg/cmd/streamsDelete.go index 1a17c497..90f78082 100644 --- a/pkg/cmd/streamsDelete.go +++ b/pkg/cmd/streamsDelete.go @@ -82,7 +82,7 @@ func (cmd *StreamsDeleteCommand) createCobraCmd( streamsDeleteCobraCmd := &cobra.Command{ Use: "delete", Short: "Deletes a test stream by name", - Long: "Deletes a single test stream by the given name from the API Server", + Long: "Deletes a single test stream with the given name from the Galasa service", Aliases: []string{COMMAND_NAME_STREAMS_DELETE}, RunE: func(cobraCommand *cobra.Command, args []string) error { return cmd.executeStreamsDelete( @@ -91,7 +91,7 @@ func (cmd *StreamsDeleteCommand) createCobraCmd( }, } - addStreamNameFlag(streamsDeleteCobraCmd, false, streamsCommandValues) + addStreamNameFlag(streamsDeleteCobraCmd, true, streamsCommandValues) streamsCommand.CobraCommand().AddCommand(streamsDeleteCobraCmd) return streamsDeleteCobraCmd, err diff --git a/pkg/cmd/streamsDelete_test.go b/pkg/cmd/streamsDelete_test.go index 32b0664b..cd8073cf 100644 --- a/pkg/cmd/streamsDelete_test.go +++ b/pkg/cmd/streamsDelete_test.go @@ -60,3 +60,21 @@ func TestStreamsDeleteNamespaceNameFlagsReturnsOk(t *testing.T) { assert.Nil(t, err) } + +func TestStreamsDeleteNamespaceWithoutNameFlagReturnsError(t *testing.T) { + // Given... + factory := utils.NewMockFactory() + commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_STREAMS_DELETE, factory, t) + + var args []string = []string{"streams", "delete"} + + // When... + err := commandCollection.Execute(args) + + // Then... + assert.NotNil(t, err) + + checkOutput("", "Error: required flag(s) \"name\" not set", factory, t) + + assert.NotNil(t, err) +} diff --git a/pkg/errors/errorMessage.go b/pkg/errors/errorMessage.go index a4912962..2a9e48d6 100644 --- a/pkg/errors/errorMessage.go +++ b/pkg/errors/errorMessage.go @@ -427,8 +427,14 @@ var ( GALASA_ERROR_GET_STREAMS_UNPARSEABLE_CONTENT = NewMessageType("GAL1238E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1238, STACK_TRACE_NOT_WANTED) GALASA_ERROR_GET_STREAMS_SERVER_REPORTED_ERROR = NewMessageType("GAL1239E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1239, STACK_TRACE_NOT_WANTED) GALASA_ERROR_GET_STREAMS_EXPLANATION_NOT_JSON = NewMessageType("GAL1240E: Failed to get streams. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1240, STACK_TRACE_NOT_WANTED) - GALASA_ERROR_DELETE_STREAMS_NOT_FOUND = NewMessageType("GAL1241E: The test stream could not be deleted by name because it was not found by the Galasa service. Try listing streams using 'galasactl streams get' to identify the one you wish to delete", 1241, STACK_TRACE_NOT_WANTED) - GALASA_ERROR_FAILED_TO_DELETE_STREAM = NewMessageType("GAL1242E: Failed to test stream from database by stream name.", 1242, STACK_TRACE_NOT_WANTED) + + // Streams delete errors + GALASA_ERROR_FAILED_TO_DELETE_STREAM = NewMessageType("GAL12412E: Failed to delete test stream with the given name from the Galasa service", 1241, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_NO_RESPONSE_CONTENT = NewMessageType("GAL1242E: Failed to delete streams. Unexpected http status code %v received from the server.", 1242, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_RESPONSE_BODY_UNREADABLE = NewMessageType("GAL1243E: Failed to delete streams. Unexpected http status code %v received from the server. Error details from the server could not be read. Cause: %s", 1243, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_UNPARSEABLE_CONTENT = NewMessageType("GAL1244E: Failed to delete streams. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1244, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_SERVER_REPORTED_ERROR = NewMessageType("GAL1245E: Failed to delete streams. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1245, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_EXPLANATION_NOT_JSON = NewMessageType("GAL1246E: Failed to delete streams. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1246, STACK_TRACE_NOT_WANTED) // When getting multiple monitors... GALASA_ERROR_GET_MONITORS_REQUEST_FAILED = NewMessageType("GAL1218E: Failed to get monitors. Sending the get request to the Galasa service failed. Cause is %v", 1218, STACK_TRACE_NOT_WANTED) diff --git a/pkg/streams/streamsDelete.go b/pkg/streams/streamsDelete.go index ef21e056..0af709b3 100644 --- a/pkg/streams/streamsDelete.go +++ b/pkg/streams/streamsDelete.go @@ -19,16 +19,12 @@ import ( func DeleteStream(streamName string, apiClient *galasaapi.APIClient, byteReader spi.ByteReader) error { - streams, err := getStreamsFromRestApi(streamName, apiClient, byteReader) + var err error - if err == nil { - - if len(streams) != 0 { - err = deleteStreamFromRestApi(streams[0], apiClient, byteReader) - } else { - err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_DELETE_STREAMS_NOT_FOUND) - } + streamName, err = validateStreamName(streamName) + if err == nil { + err = deleteStreamFromRestApi(streamName, apiClient, byteReader) } return err @@ -36,7 +32,7 @@ func DeleteStream(streamName string, apiClient *galasaapi.APIClient, byteReader } func deleteStreamFromRestApi( - stream galasaapi.Stream, + streamName string, apiClient *galasaapi.APIClient, byteReader spi.ByteReader, ) error { @@ -48,7 +44,6 @@ func deleteStreamFromRestApi( if err == nil { - streamName := stream.Metadata.GetName() apiCall := apiClient.StreamsAPIApi.DeleteStreamByName(context, streamName).ClientApiVersion(restApiVersion) resp, err = apiCall.Execute() @@ -68,11 +63,11 @@ func deleteStreamFromRestApi( resp, streamName, byteReader, - galasaErrors.GALASA_ERROR_GET_STREAMS_NO_RESPONSE_CONTENT, - galasaErrors.GALASA_ERROR_GET_STREAMS_RESPONSE_BODY_UNREADABLE, - galasaErrors.GALASA_ERROR_GET_STREAMS_UNPARSEABLE_CONTENT, - galasaErrors.GALASA_ERROR_GET_STREAMS_SERVER_REPORTED_ERROR, - galasaErrors.GALASA_ERROR_GET_STREAMS_EXPLANATION_NOT_JSON, + galasaErrors.GALASA_ERROR_DELETE_STREAMS_NO_RESPONSE_CONTENT, + galasaErrors.GALASA_ERROR_DELETE_STREAMS_RESPONSE_BODY_UNREADABLE, + galasaErrors.GALASA_ERROR_DELETE_STREAMS_UNPARSEABLE_CONTENT, + galasaErrors.GALASA_ERROR_DELETE_STREAMS_SERVER_REPORTED_ERROR, + galasaErrors.GALASA_ERROR_DELETE_STREAMS_EXPLANATION_NOT_JSON, ) } diff --git a/pkg/streams/streamsDelete_test.go b/pkg/streams/streamsDelete_test.go index fffd33a5..73051d29 100644 --- a/pkg/streams/streamsDelete_test.go +++ b/pkg/streams/streamsDelete_test.go @@ -7,7 +7,6 @@ package streams import ( - "encoding/json" "fmt" "net/http" "strconv" @@ -57,20 +56,10 @@ func WriteMockStreamResponse( } -func TestStreamUserDeleteAUser(t *testing.T) { +func TestStreamDeleteAStream(t *testing.T) { //Given... name := "mystream" - description := "This is a dummy stream" - - streamToDelete := createMockStream(name, description) - streamToDeleteBytes, _ := json.Marshal(streamToDelete) - streamToDeleteJson := string(streamToDeleteBytes) - - getStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodGet) - getStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { - WriteMockStreamResponse(t, writer, req, name, []string{streamToDeleteJson}) - } deleteStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodDelete) deleteStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { @@ -78,7 +67,6 @@ func TestStreamUserDeleteAUser(t *testing.T) { } interactions := []utils.HttpInteraction{ - getStreamInteraction, deleteStreamInteraction, } @@ -101,21 +89,44 @@ func TestStreamUserDeleteAUser(t *testing.T) { assert.Empty(t, console.ReadText(), "The console was written to on a successful deletion, it should be empty") } -func TestStreamDeleteThrowsAnUnexpectedError(t *testing.T) { +func TestStreamDeleteAnInvalidStreamNameReturnsError(t *testing.T) { //Given... - name := "mystream" - description := "This is a dummy stream" + name := "my.stream" - streamToDelete := createMockStream(name, description) - streamToDeleteBytes, _ := json.Marshal(streamToDelete) - streamToDeleteJson := string(streamToDeleteBytes) + deleteStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodDelete) + deleteStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { + writer.WriteHeader(http.StatusBadRequest) + } - getStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodGet) - getStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { - WriteMockStreamResponse(t, writer, req, name, []string{streamToDeleteJson}) + interactions := []utils.HttpInteraction{ + deleteStreamInteraction, } + server := utils.NewMockHttpServer(t, interactions) + defer server.Server.Close() + + apiServerUrl := server.Server.URL + apiClient := api.InitialiseAPI(apiServerUrl) + mockByteReader := utils.NewMockByteReader() + + // When... + err := DeleteStream( + name, + apiClient, + mockByteReader) + + // Then... + assert.NotNil(t, err, "DeleteStream returned an unexpected error") + assert.Contains(t, err.Error(), "GAL1235E") + assert.Contains(t, err.Error(), "he name provided with the --name flag cannot be empty and must only contain characters in the following ranges:") +} + +func TestStreamDeleteThrowsAnUnexpectedError(t *testing.T) { + + //Given... + name := "mystream" + deleteStreamInteraction := utils.NewHttpInteraction("/streams/"+name, http.MethodDelete) deleteStreamInteraction.WriteHttpResponseFunc = func(writer http.ResponseWriter, req *http.Request) { writer.Header().Set("Content-Type", "application/json") @@ -124,7 +135,6 @@ func TestStreamDeleteThrowsAnUnexpectedError(t *testing.T) { } interactions := []utils.HttpInteraction{ - getStreamInteraction, deleteStreamInteraction, } @@ -144,5 +154,5 @@ func TestStreamDeleteThrowsAnUnexpectedError(t *testing.T) { // Then... assert.NotNil(t, err, "DeleteStream returned an unexpected error") assert.Contains(t, err.Error(), strconv.Itoa(http.StatusInternalServerError)) - assert.Contains(t, err.Error(), "GAL1239E") + assert.Contains(t, err.Error(), "GAL1245E") } From f86ea66fb876160e9ee601ab2434656476609d7a Mon Sep 17 00:00:00 2001 From: Aashir Siddiqui Date: Thu, 10 Apr 2025 09:39:23 +0100 Subject: [PATCH 3/3] Enhanced error messages Signed-off-by: Aashir Siddiqui --- docs/generated/errors-list.md | 10 +++++----- pkg/cmd/streamsDelete_test.go | 2 +- pkg/errors/errorMessage.go | 10 +++++----- pkg/streams/streamsDelete_test.go | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/generated/errors-list.md b/docs/generated/errors-list.md index 1079376c..59305578 100644 --- a/docs/generated/errors-list.md +++ b/docs/generated/errors-list.md @@ -238,11 +238,11 @@ The `galasactl` tool can generate the following errors: - GAL1239E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are: '{}' - GAL1240E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in the json format. - GAL12412E: Failed to delete test stream with the given name from the Galasa service -- GAL1242E: Failed to delete streams. Unexpected http status code {} received from the server. -- GAL1243E: Failed to delete streams. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {} -- GAL1244E: Failed to delete streams. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}' -- GAL1245E: Failed to delete streams. Unexpected http status code {} received from the server. Error details from the server are: '{}' -- GAL1246E: Failed to delete streams. Unexpected http status code {} received from the server. Error details from the server are not in the json format. +- GAL1242E: Failed to delete stream {}. Unexpected http status code {} received from the server. +- GAL1243E: Failed to delete stream {}. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {} +- GAL1244E: Failed to delete stream {}. Unexpected http status code {} received from the server. Error details from the server are not in a valid json format. Cause: '{}' +- GAL1245E: Failed to delete stream {}. Unexpected http status code {} received from the server. Error details from the server are: '{}' +- GAL1246E: Failed to delete stream {}. Unexpected http status code {} received from the server. Error details from the server are not in the json format. - 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 '{}', and 'pre-release' repository is '{}' - GAL2501I: Downloaded {} artifacts to folder '{}' diff --git a/pkg/cmd/streamsDelete_test.go b/pkg/cmd/streamsDelete_test.go index cd8073cf..8a89680c 100644 --- a/pkg/cmd/streamsDelete_test.go +++ b/pkg/cmd/streamsDelete_test.go @@ -61,7 +61,7 @@ func TestStreamsDeleteNamespaceNameFlagsReturnsOk(t *testing.T) { assert.Nil(t, err) } -func TestStreamsDeleteNamespaceWithoutNameFlagReturnsError(t *testing.T) { +func TestStreamsDeleteWithoutNameFlagReturnsError(t *testing.T) { // Given... factory := utils.NewMockFactory() commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_STREAMS_DELETE, factory, t) diff --git a/pkg/errors/errorMessage.go b/pkg/errors/errorMessage.go index 2a9e48d6..617809b1 100644 --- a/pkg/errors/errorMessage.go +++ b/pkg/errors/errorMessage.go @@ -430,11 +430,11 @@ var ( // Streams delete errors GALASA_ERROR_FAILED_TO_DELETE_STREAM = NewMessageType("GAL12412E: Failed to delete test stream with the given name from the Galasa service", 1241, STACK_TRACE_NOT_WANTED) - GALASA_ERROR_DELETE_STREAMS_NO_RESPONSE_CONTENT = NewMessageType("GAL1242E: Failed to delete streams. Unexpected http status code %v received from the server.", 1242, STACK_TRACE_NOT_WANTED) - GALASA_ERROR_DELETE_STREAMS_RESPONSE_BODY_UNREADABLE = NewMessageType("GAL1243E: Failed to delete streams. Unexpected http status code %v received from the server. Error details from the server could not be read. Cause: %s", 1243, STACK_TRACE_NOT_WANTED) - GALASA_ERROR_DELETE_STREAMS_UNPARSEABLE_CONTENT = NewMessageType("GAL1244E: Failed to delete streams. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1244, STACK_TRACE_NOT_WANTED) - GALASA_ERROR_DELETE_STREAMS_SERVER_REPORTED_ERROR = NewMessageType("GAL1245E: Failed to delete streams. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1245, STACK_TRACE_NOT_WANTED) - GALASA_ERROR_DELETE_STREAMS_EXPLANATION_NOT_JSON = NewMessageType("GAL1246E: Failed to delete streams. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1246, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_NO_RESPONSE_CONTENT = NewMessageType("GAL1242E: Failed to delete stream %s. Unexpected http status code %v received from the server.", 1242, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_RESPONSE_BODY_UNREADABLE = NewMessageType("GAL1243E: Failed to delete stream %s. Unexpected http status code %v received from the server. Error details from the server could not be read. Cause: %s", 1243, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_UNPARSEABLE_CONTENT = NewMessageType("GAL1244E: Failed to delete stream %s. Unexpected http status code %v received from the server. Error details from the server are not in a valid json format. Cause: '%s'", 1244, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_SERVER_REPORTED_ERROR = NewMessageType("GAL1245E: Failed to delete stream %s. Unexpected http status code %v received from the server. Error details from the server are: '%s'", 1245, STACK_TRACE_NOT_WANTED) + GALASA_ERROR_DELETE_STREAMS_EXPLANATION_NOT_JSON = NewMessageType("GAL1246E: Failed to delete stream %s. Unexpected http status code %v received from the server. Error details from the server are not in the json format.", 1246, STACK_TRACE_NOT_WANTED) // When getting multiple monitors... GALASA_ERROR_GET_MONITORS_REQUEST_FAILED = NewMessageType("GAL1218E: Failed to get monitors. Sending the get request to the Galasa service failed. Cause is %v", 1218, STACK_TRACE_NOT_WANTED) diff --git a/pkg/streams/streamsDelete_test.go b/pkg/streams/streamsDelete_test.go index 73051d29..e8ed1cc0 100644 --- a/pkg/streams/streamsDelete_test.go +++ b/pkg/streams/streamsDelete_test.go @@ -119,7 +119,7 @@ func TestStreamDeleteAnInvalidStreamNameReturnsError(t *testing.T) { // Then... assert.NotNil(t, err, "DeleteStream returned an unexpected error") assert.Contains(t, err.Error(), "GAL1235E") - assert.Contains(t, err.Error(), "he name provided with the --name flag cannot be empty and must only contain characters in the following ranges:") + assert.Contains(t, err.Error(), "The name provided with the --name flag cannot be empty and must only contain characters in the following ranges:") } func TestStreamDeleteThrowsAnUnexpectedError(t *testing.T) {