Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 7023d09

Browse files
authored
Merge pull request #357 from galasa-dev/ash-streams-delete
Added streams delete command
2 parents 5a0820f + f86ea66 commit 7023d09

File tree

9 files changed

+528
-1
lines changed

9 files changed

+528
-1
lines changed

docs/generated/errors-list.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ The `galasactl` tool can generate the following errors:
237237
- 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: '{}'
238238
- GAL1239E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are: '{}'
239239
- GAL1240E: Failed to get streams. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
240+
- GAL12412E: Failed to delete test stream with the given name from the Galasa service
241+
- GAL1242E: Failed to delete stream {}. Unexpected http status code {} received from the server.
242+
- GAL1243E: Failed to delete stream {}. Unexpected http status code {} received from the server. Error details from the server could not be read. Cause: {}
243+
- 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: '{}'
244+
- GAL1245E: Failed to delete stream {}. Unexpected http status code {} received from the server. Error details from the server are: '{}'
245+
- GAL1246E: Failed to delete stream {}. Unexpected http status code {} received from the server. Error details from the server are not in the json format.
240246
- 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 '{}'
241247
- GAL2501I: Downloaded {} artifacts to folder '{}'
242248

docs/generated/galasactl_streams.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ Parent command for managing test streams in a Galasa service
2525
### SEE ALSO
2626

2727
* [galasactl](galasactl.md) - CLI for Galasa
28+
* [galasactl streams delete](galasactl_streams_delete.md) - Deletes a test stream by name
2829
* [galasactl streams get](galasactl_streams_get.md) - Gets a list of test streams
2930

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## galasactl streams delete
2+
3+
Deletes a test stream by name
4+
5+
### Synopsis
6+
7+
Deletes a single test stream with the given name from the Galasa service
8+
9+
```
10+
galasactl streams delete [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Displays the options for the 'streams delete' command.
17+
--name string A mandatory field indicating the name of a test stream.
18+
```
19+
20+
### Options inherited from parent commands
21+
22+
```
23+
-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
24+
--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.
25+
-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.
26+
--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)
27+
--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)
28+
```
29+
30+
### SEE ALSO
31+
32+
* [galasactl streams](galasactl_streams.md) - Manages test streams in a Galasa service
33+

pkg/cmd/commandCollection.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const (
7474
COMMAND_NAME_ROLES_GET = "roles get"
7575
COMMAND_NAME_STREAMS = "streams"
7676
COMMAND_NAME_STREAMS_GET = "streams get"
77+
COMMAND_NAME_STREAMS_DELETE = "streams delete"
7778
)
7879

7980
// -----------------------------------------------------------------
@@ -519,14 +520,24 @@ func (commands *commandCollectionImpl) addStreamsCommands(factory spi.Factory, r
519520
var err error
520521
var streamsCommand spi.GalasaCommand
521522
var streamsGetCommand spi.GalasaCommand
523+
var streamsDeleteCommand spi.GalasaCommand
522524

523525
streamsCommand, err = NewStreamsCommand(rootCommand, commsFlagSet)
524526

525527
if err == nil {
528+
529+
commands.commandMap[streamsCommand.Name()] = streamsCommand
526530
streamsGetCommand, err = NewStreamsGetCommand(factory, streamsCommand, commsFlagSet)
531+
527532
if err == nil {
528-
commands.commandMap[streamsCommand.Name()] = streamsCommand
533+
529534
commands.commandMap[streamsGetCommand.Name()] = streamsGetCommand
535+
streamsDeleteCommand, err = NewStreamsDeleteCommand(factory, streamsCommand, commsFlagSet)
536+
537+
if err == nil {
538+
commands.commandMap[streamsDeleteCommand.Name()] = streamsDeleteCommand
539+
}
540+
530541
}
531542
}
532543

pkg/cmd/streamsDelete.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Copyright contributors to the Galasa project
3+
*
4+
* SPDX-License-Identifier: EPL-2.0
5+
*/
6+
7+
package cmd
8+
9+
import (
10+
"log"
11+
12+
"github.com/galasa-dev/cli/pkg/api"
13+
"github.com/galasa-dev/cli/pkg/galasaapi"
14+
"github.com/galasa-dev/cli/pkg/spi"
15+
"github.com/galasa-dev/cli/pkg/streams"
16+
"github.com/galasa-dev/cli/pkg/utils"
17+
"github.com/spf13/cobra"
18+
)
19+
20+
// Objective: Allow user to do this:
21+
//
22+
// streams delete
23+
type StreamsDeleteCommand struct {
24+
cobraCommand *cobra.Command
25+
}
26+
27+
// ------------------------------------------------------------------------------------------------
28+
// Constructors methods
29+
// ------------------------------------------------------------------------------------------------
30+
func NewStreamsDeleteCommand(
31+
factory spi.Factory,
32+
streamsDeleteCommand spi.GalasaCommand,
33+
commsFlagSet GalasaFlagSet,
34+
) (spi.GalasaCommand, error) {
35+
36+
cmd := new(StreamsDeleteCommand)
37+
err := cmd.init(factory, streamsDeleteCommand, commsFlagSet)
38+
return cmd, err
39+
40+
}
41+
42+
// ------------------------------------------------------------------------------------------------
43+
// Public methods
44+
// ------------------------------------------------------------------------------------------------
45+
func (cmd *StreamsDeleteCommand) Name() string {
46+
return COMMAND_NAME_STREAMS_DELETE
47+
}
48+
49+
func (cmd *StreamsDeleteCommand) CobraCommand() *cobra.Command {
50+
return cmd.cobraCommand
51+
}
52+
53+
func (cmd *StreamsDeleteCommand) Values() interface{} {
54+
return nil
55+
}
56+
57+
// ------------------------------------------------------------------------------------------------
58+
// Private methods
59+
// ------------------------------------------------------------------------------------------------
60+
61+
func (cmd *StreamsDeleteCommand) init(factory spi.Factory, streamsCommand spi.GalasaCommand, commsFlagSet GalasaFlagSet) error {
62+
63+
var err error
64+
65+
cmd.cobraCommand, err = cmd.createCobraCmd(factory, streamsCommand, commsFlagSet)
66+
67+
return err
68+
69+
}
70+
71+
func (cmd *StreamsDeleteCommand) createCobraCmd(
72+
factory spi.Factory,
73+
streamsCommand spi.GalasaCommand,
74+
commsFlagSet GalasaFlagSet,
75+
) (*cobra.Command, error) {
76+
77+
var err error
78+
79+
commsFlagSetValues := commsFlagSet.Values().(*CommsFlagSetValues)
80+
streamsCommandValues := streamsCommand.Values().(*StreamsCmdValues)
81+
82+
streamsDeleteCobraCmd := &cobra.Command{
83+
Use: "delete",
84+
Short: "Deletes a test stream by name",
85+
Long: "Deletes a single test stream with the given name from the Galasa service",
86+
Aliases: []string{COMMAND_NAME_STREAMS_DELETE},
87+
RunE: func(cobraCommand *cobra.Command, args []string) error {
88+
return cmd.executeStreamsDelete(
89+
factory, streamsCommand.Values().(*StreamsCmdValues), commsFlagSetValues,
90+
)
91+
},
92+
}
93+
94+
addStreamNameFlag(streamsDeleteCobraCmd, true, streamsCommandValues)
95+
streamsCommand.CobraCommand().AddCommand(streamsDeleteCobraCmd)
96+
97+
return streamsDeleteCobraCmd, err
98+
99+
}
100+
101+
func (cmd *StreamsDeleteCommand) executeStreamsDelete(
102+
factory spi.Factory,
103+
streamsCmdValues *StreamsCmdValues,
104+
commsFlagSetValues *CommsFlagSetValues,
105+
) error {
106+
107+
var err error
108+
109+
// Operations on the file system will all be relative to the current folder.
110+
fileSystem := factory.GetFileSystem()
111+
byteReader := factory.GetByteReader()
112+
113+
err = utils.CaptureLog(fileSystem, commsFlagSetValues.logFileName)
114+
115+
if err == nil {
116+
117+
commsFlagSetValues.isCapturingLogs = true
118+
119+
log.Println("Galasa CLI - Delete test stream from the Galasa service")
120+
121+
// Get the ability to query environment variables.
122+
env := factory.GetEnvironment()
123+
124+
var galasaHome spi.GalasaHome
125+
galasaHome, err = utils.NewGalasaHome(fileSystem, env, commsFlagSetValues.CmdParamGalasaHomePath)
126+
if err == nil {
127+
128+
var commsClient api.APICommsClient
129+
commsClient, err = api.NewAPICommsClient(
130+
commsFlagSetValues.bootstrap,
131+
commsFlagSetValues.maxRetries,
132+
commsFlagSetValues.retryBackoffSeconds,
133+
factory,
134+
galasaHome,
135+
)
136+
137+
if err == nil {
138+
deleteStreamFunc := func(apiClient *galasaapi.APIClient) error {
139+
// Call to process the command in a unit-testable way.
140+
return streams.DeleteStream(streamsCmdValues.name, apiClient, byteReader)
141+
}
142+
err = commsClient.RunAuthenticatedCommandWithRateLimitRetries(deleteStreamFunc)
143+
}
144+
}
145+
}
146+
147+
return err
148+
149+
}

pkg/cmd/streamsDelete_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright contributors to the Galasa project
3+
*
4+
* SPDX-License-Identifier: EPL-2.0
5+
*/
6+
7+
package cmd
8+
9+
import (
10+
"testing"
11+
12+
"github.com/galasa-dev/cli/pkg/utils"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestStreamsDeleteCommandInCommandCollectionHasName(t *testing.T) {
17+
18+
factory := utils.NewMockFactory()
19+
commands, _ := NewCommandCollection(factory)
20+
21+
StreamsDeleteCommand, err := commands.GetCommand(COMMAND_NAME_STREAMS_DELETE)
22+
assert.Nil(t, err)
23+
24+
assert.Equal(t, COMMAND_NAME_STREAMS_DELETE, StreamsDeleteCommand.Name())
25+
assert.NotNil(t, StreamsDeleteCommand.CobraCommand())
26+
27+
}
28+
29+
func TestStreamsDeleteHelpFlagSetCorrectly(t *testing.T) {
30+
// Given...
31+
factory := utils.NewMockFactory()
32+
33+
var args []string = []string{"streams", "delete", "--help"}
34+
35+
// When...
36+
err := Execute(factory, args)
37+
38+
// Then...
39+
// Check what the user saw is reasonable.
40+
checkOutput("Displays the options for the 'streams delete' command.", "", factory, t)
41+
42+
assert.Nil(t, err)
43+
}
44+
45+
func TestStreamsDeleteNamespaceNameFlagsReturnsOk(t *testing.T) {
46+
// Given...
47+
factory := utils.NewMockFactory()
48+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_STREAMS_DELETE, factory, t)
49+
50+
var args []string = []string{"streams", "delete", "--name", "mystream"}
51+
52+
// When...
53+
err := commandCollection.Execute(args)
54+
55+
// Then...
56+
assert.Nil(t, err)
57+
58+
// Check what the user saw was reasonable
59+
checkOutput("", "", factory, t)
60+
61+
assert.Nil(t, err)
62+
}
63+
64+
func TestStreamsDeleteWithoutNameFlagReturnsError(t *testing.T) {
65+
// Given...
66+
factory := utils.NewMockFactory()
67+
commandCollection, _ := setupTestCommandCollection(COMMAND_NAME_STREAMS_DELETE, factory, t)
68+
69+
var args []string = []string{"streams", "delete"}
70+
71+
// When...
72+
err := commandCollection.Execute(args)
73+
74+
// Then...
75+
assert.NotNil(t, err)
76+
77+
checkOutput("", "Error: required flag(s) \"name\" not set", factory, t)
78+
79+
assert.NotNil(t, err)
80+
}

pkg/errors/errorMessage.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,14 @@ var (
428428
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)
429429
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)
430430

431+
// Streams delete errors
432+
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)
433+
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)
434+
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)
435+
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)
436+
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)
437+
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)
438+
431439
// When getting multiple monitors...
432440
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)
433441
GALASA_ERROR_GET_MONITORS_NO_RESPONSE_CONTENT = NewMessageType("GAL1219E: Failed to get monitors. Unexpected http status code %v received from the server.", 1219, STACK_TRACE_NOT_WANTED)

0 commit comments

Comments
 (0)