Skip to content

Commit

Permalink
[eclipse-kanto#213] Fixed according to comments and caught a bug with…
Browse files Browse the repository at this point in the history
… the stop timeout flag.
  • Loading branch information
daniel-milchev committed Dec 19, 2023
1 parent e744e05 commit 6d01bf4
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 31 deletions.
23 changes: 12 additions & 11 deletions containerm/cli/cli_command_ctrs_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"fmt"
"os"
"strconv"

"strings"
"text/tabwriter"

Expand Down Expand Up @@ -47,7 +46,7 @@ func (cc *listCmd) init(cli *cli) {
RunE: func(cmd *cobra.Command, args []string) error {
return cc.run(args)
},
Example: " list\n list --name <container-name>\n list -n <container-name>\n list -q\n list --filter <status/image/exitcode>",
Example: " list\n list --name <container-name>\n list --quiet\n list --filter status=created",
}
cc.setupFlags()
}
Expand All @@ -61,18 +60,21 @@ func (cc *listCmd) run(args []string) error {
if err != nil {
return err
}
if len(cc.config.filter) > 0 && len(ctrs) > 0 {
if len(cc.config.filter) > 0 {
filtered, err := filterBy(cc.config.filter, ctrs)
if err != nil {
return err
}
ctrs = filtered
}
if cc.config.quiet != false && len(ctrs) > 0 {
for _, ctr := range ctrs {
fmt.Printf("%s ", ctr.ID)
if cc.config.quiet {
for i, ctr := range ctrs {
if i != len(ctrs)-1 {
fmt.Printf("%s ", ctr.ID)
} else {
fmt.Println(ctr.ID)
}
}
fmt.Println()
return nil
}
if len(ctrs) == 0 {
Expand All @@ -88,7 +90,7 @@ func (cc *listCmd) setupFlags() {
// init name flags
flagSet.StringVarP(&cc.config.name, "name", "n", "", "List all containers with a specific name.")
flagSet.BoolVarP(&cc.config.quiet, "quiet", "q", false, "List only container IDs.")
flagSet.StringSliceVar(&cc.config.filter, "filter", nil, "Lists only containers with the filter.")
flagSet.StringSliceVar(&cc.config.filter, "filter", nil, "Lists only containers with a specified filter. The containers can be filtered by their status, image and exitcode.")
}

func filterBy(input []string, ctrs []*types.Container) ([]*types.Container, error) {
Expand All @@ -105,13 +107,12 @@ func filterBy(input []string, ctrs []*types.Container) ([]*types.Container, erro
} else if strings.HasPrefix(inp, "image=") {
holderImage = strings.TrimPrefix(inp, "image=")
} else if strings.HasPrefix(inp, "exitcode=") {
exitcodeHolder := strings.TrimPrefix(inp, "exitcode=")
convertedExitCode, err = strconv.Atoi(exitcodeHolder)
convertedExitCode, err = strconv.Atoi(strings.TrimPrefix(inp, "exitcode="))
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("no such filter")
return nil, fmt.Errorf("no filter: %s", strings.Split(inp, "=")[0])
}
}
for _, ctr := range ctrs {
Expand Down
20 changes: 14 additions & 6 deletions containerm/cli/cli_command_ctrs_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package main

import (
"context"
"fmt"
"testing"

"github.com/eclipse-kanto/container-management/containerm/client"
Expand Down Expand Up @@ -212,11 +213,18 @@ func (listTc *listCommandTest) mockExecListByNameNoCtrs(args []string) error {

func (listTc *listCommandTest) mockExecListQuiet(args []string) error {
// setup expected calls
ctrs := []*types.Container{{
ID: listContainerID,
Name: listFlagName,
State: &types.State{},
}}
ctrs := []*types.Container{
{
ID: fmt.Sprintf("%s-%d", listContainerID, 1),
Name: listFlagName,
State: &types.State{},
},
{
ID: fmt.Sprintf("%s-%d", listContainerID, 2),
Name: listFlagName,
State: &types.State{},
},
}
listTc.mockClient.EXPECT().List(context.Background()).Times(1).Return(ctrs, nil)
// no error expected
return nil
Expand All @@ -235,7 +243,7 @@ func (listTc *listCommandTest) mockExecListWithFilter(args []string) error {
}

func (listTc *listCommandTest) mockExecListWithFilterError(args []string) error {
err := log.NewError("no such filter")
err := log.NewError("no filter: test")
ctrs := []*types.Container{{
ID: listContainerID,
Name: listFlagName,
Expand Down
4 changes: 2 additions & 2 deletions containerm/cli/cli_command_ctrs_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (cc *stopCmd) init(cli *cli) {
RunE: func(cmd *cobra.Command, args []string) error {
return cc.run(args)
},
Example: "stop <container-id>\n stop --name <container-name>\n stop -n <container-name>",
Example: " stop <container-id>\n stop --name <container-name>\n stop -n <container-name>",
}
cc.setupFlags()
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (cc *stopCmd) run(args []string) error {
func (cc *stopCmd) setupFlags() {
flagSet := cc.cmd.Flags()
// init timeout flag
flagSet.Int64VarP(&cc.config.timeout, "timeout", "t", math.MinInt64, "Sets the timeout period in seconds to gracefully stop the container. When timeout expires the container process would be forcibly killed.")
flagSet.Int64VarP(&cc.config.timeout, "time", "t", math.MinInt64, "Sets the timeout period in seconds to gracefully stop the container. When timeout expires the container process would be forcibly killed.")
// init name flag
flagSet.StringVarP(&cc.config.name, "name", "n", "", "Stop a container with a specific name.")
// init force flag
Expand Down
2 changes: 1 addition & 1 deletion containerm/cli/cli_command_ctrs_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

const (
// command flags
stopCmdFlagTimeout = "timeout"
stopCmdFlagTimeout = "time"
stopCmdFlagName = "name"
stopCmdFlagForce = "force"
stopCmdFlagSignal = "signal"
Expand Down
4 changes: 0 additions & 4 deletions integration/framework/cli/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ func RunCmdTestCases(t *testing.T, cmdList []TestCaseCMD) {
runMultipleCommands(t, *cmd.setupCmd)
}
checkArguments(t, &cmd.icmd)
fmt.Printf("Running command: %s %s\n", cmd.icmd.Command[0], cmd.icmd.Command[1:])
result := icmd.RunCommand(cmd.icmd.Command[0], cmd.icmd.Command[1:]...)
// result := icmd.RunCmd(cmd.icmd)
if cmd.goldenFile != "" {
assert.Assert(t, golden.String(result.Stdout(), cmd.goldenFile))
}
Expand Down Expand Up @@ -167,7 +165,6 @@ func fromAPITestCommand(cmd TestCommand) TestCaseCMD {
return TestCaseCMD{
name: cmd.Name,
icmd: icmd.Command(cmd.Command.Binary, cmd.Command.Args...),
// icmd: *checkArguments(cmd.Command.Binary, cmd.Command.Args...),
expected: icmd.Expected{
ExitCode: cmd.Expected.ExitCode,
Timeout: cmd.Expected.Timeout,
Expand All @@ -190,7 +187,6 @@ func buildCmdArrFromCommand(cmd *[]Command) *[]icmd.Cmd {
cmds := make([]icmd.Cmd, 0)
for _, cmd := range *cmd {
cmds = append(cmds, icmd.Command(cmd.Binary, cmd.Args...))
// cmds = append(cmds, *checkArguments(cmd.Binary, cmd.Args...))
}
return &cmds
}
7 changes: 3 additions & 4 deletions integration/testdata/list-help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ Usage:
Examples:
list
list --name <container-name>
list -n <container-name>
list -q
list --filter <status/image/exitcode>
list --quiet
list --filter status=created

Flags:
--filter strings Lists only containers with the filter.
--filter strings Lists only containers with a specified filter. The containers can be filtered by their status, image and exitcode.
-h, --help help for list
-n, --name string List all containers with a specific name.
-q, --quiet List only container IDs.
Expand Down
25 changes: 24 additions & 1 deletion integration/testdata/list-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ setupCmd:
args: ["create", "--host", "$KANTO_HOST", "-n", "list_containers_with_quiet_two", "docker.io/library/influxdb:1.8.4"]
command:
binary: kanto-cm
args: ["list", "--host", "$KANTO_HOST", "-q"]
args: ["list", "--host", "$KANTO_HOST", "--quiet"]
expected:
exitCode: 0
customResult:
Expand All @@ -70,6 +70,29 @@ onExit:
- binary: "kanto-cm"
args: ["remove", "--host", "$KANTO_HOST", "-n", "list_containers_with_quiet_two", "-f"]
---
name: list_containers_with_filter
setupCmd:
- binary: kanto-cm
args: ["create", "--host", "$KANTO_HOST", "-n", "list_containers_with_filter_one", "docker.io/library/influxdb:1.8.4"]
- binary: kanto-cm
args: ["create", "--host", "$KANTO_HOST", "-n", "list_containers_with_filter_two", "docker.io/library/influxdb:1.8.4"]
command:
binary: kanto-cm
args: ["list", "--host", "$KANTO_HOST", "--filter", "status=created"]
expected:
exitCode: 0
customResult:
type: REGEX
args: ["ID |Name |Image |Status |Finished At |Exit Code |
------------------------------------- |------------------------------------- |------------------------------------------------------------ |---------- |------------------------------ |---------- |
[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12} |list_containers_with_filter_one |docker.io/library/influxdb:1.8.4 |Created | |0 |
[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12} |list_containers_with_filter_two |docker.io/library/influxdb:1.8.4 |Created | |0 |"]
onExit:
- binary: "kanto-cm"
args: ["remove", "--host", "$KANTO_HOST", "-n", "list_containers_with_filter_one", "-f"]
- binary: "kanto-cm"
args: ["remove", "--host", "$KANTO_HOST", "-n", "list_containers_with_filter_two", "-f"]
---
name: list_existing_container
setupCmd:
- binary: kanto-cm
Expand Down
3 changes: 2 additions & 1 deletion integration/testdata/stop-help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Usage:
kanto-cm stop <container-id>

Examples:
stop <container-id>
stop <container-id>
stop --name <container-name>
stop -n <container-name>

Expand All @@ -13,6 +13,7 @@ Flags:
-h, --help help for stop
-n, --name string Stop a container with a specific name.
-s, --signal string Stop a container using a specific signal. Signals could be specified by using their names or numbers, e.g. SIGINT or 2. (default "SIGTERM")
-t, --time int Sets the timeout period in seconds to gracefully stop the container. When timeout expires the container process would be forcibly killed. (default -9223372036854775808)

Global Flags:
--debug Switch commands log level to DEBUG mode
Expand Down
2 changes: 1 addition & 1 deletion integration/testdata/stop-test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: remove_help
name: stop_help
command:
binary: kanto-cm
args: ["stop", "-h"]
Expand Down

0 comments on commit 6d01bf4

Please sign in to comment.