Skip to content

Commit

Permalink
Provide integration test for the CLI sysinfo, logs and update commands (
Browse files Browse the repository at this point in the history
eclipse-kanto#125)

[eclipse-kanto#119] Provide integration test for the CLI sysinfo, logs and update commands

 - added integration tests for sysinfo, update and logs CLI commands
 - implemented custom result function for logs command
 - fixed stop_invalid_id test

Signed-off-by: Dimitar Dimitrov <dimitar.dimitrov3@bosch.io>
  • Loading branch information
dimitar-dimitrow authored Feb 6, 2023
1 parent d7afe61 commit 4108c97
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 2 deletions.
3 changes: 2 additions & 1 deletion integration/framework/cli/cmd_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
const TestData = "testdata"

var customResultDefaultFns = map[string]func(result icmd.Result, args ...string) assert.BoolOrComparison{
"REGEX": regex,
"REGEX": regex,
"LOGS_JSON": logs,
}

// TestCaseCMD represents a command and expected result
Expand Down
5 changes: 5 additions & 0 deletions integration/framework/cli/cmd_base_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ type Expected struct {
// customResult:
// name: REGEX
// args: ["([A-Za-z0-9]+(-[A-Za-z0-9]+)+)"]
// - LOGS_JSON - this function will validate that the stdout outputs consist of valid JSON entries and compare their count to args[0] if provided.
// example:
// customResult:
// name: LOGS_JSON
// args: ["5"]
type CustomResult struct {
Type string `yaml:"type"`
Args []string `yaml:"args"`
Expand Down
39 changes: 39 additions & 0 deletions integration/framework/cli/cmd_base_custom_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
package framework

import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strconv"
"strings"

"github.com/eclipse-kanto/container-management/containerm/log"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
)
Expand All @@ -41,3 +45,38 @@ func checkRegex(r *regexp.Regexp, s string) assert.BoolOrComparison {
}
return true
}

func logs(result icmd.Result, args ...string) assert.BoolOrComparison {
var (
expectedLogEntries int
err error
)
if len(args) > 0 {
expectedLogEntries, err = strconv.Atoi(args[0])
if err != nil {
return err
}
}

var logEntriesCount int
if result.Stdout() != "" {
lines := strings.Split(result.Stdout(), "\n")
for _, l := range lines {
if len(l) == 0 {
continue
}
var x map[string]interface{}
if err := json.Unmarshal([]byte(l), &x); err != nil {
return err
}
logEntriesCount++
}
}

if len(args) > 0 {
if logEntriesCount != expectedLogEntries {
return log.NewErrorf("unexpected number of log entries: %d", logEntriesCount)
}
}
return true
}
19 changes: 19 additions & 0 deletions integration/testdata/logs-help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Print the logs for a container.

Usage:
kanto-cm logs <container-id>

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

Flags:
-h, --help help for logs
-n, --name string Print the logs of a container with a specific name.
-t, --tail int32 Lines of recent log file to display. Setting it to -1 will return all logs. (default 100)

Global Flags:
--debug Switch commands log level to DEBUG mode
--host string Specify the address path to the Eclipse Kanto container management (default "/run/container-management/container-management.sock")
--timeout int Specify the connection timeout in seconds to the Eclipse Kanto container management (default 30)
84 changes: 84 additions & 0 deletions integration/testdata/logs-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: logs_help
command:
binary: kanto-cm
args: ["logs", "-h"]
expected:
exitCode: 0
goldenFile: "logs-help.golden"
---
name: logs_no_args
command:
binary: kanto-cm
args: ["logs"]
expected:
exitCode: 1
err: "Error: You must provide either an ID or a name for the container via --name (-n)"
---
name: logs_invalid_id
command:
binary: kanto-cm
args: ["logs", "invalid"]
expected:
exitCode: 1
err: "Error: The requested container with ID = invalid was not found."
---
name: logs_invalid_name
command:
binary: kanto-cm
args: ["logs", "-n", "invalid"]
expected:
exitCode: 1
err: "Error: The requested container with name = invalid was not found. Try using an ID instead."
---
name: logs_of_container_with_state_created
setupCmd:
- binary: kanto-cm
args: ["create", "--host", "$KANTO_HOST", "-n", "test-it", "docker.io/library/influxdb:1.8.4"]
command:
binary: kanto-cm
args: ["logs", "-n", "test-it"]
expected:
exitCode: 1
err: "Error: rpc error: code = Unknown desc = there are no logs for container with status \"Created\""
onExit:
- binary: "kanto-cm"
args: ["remove","--host", "$KANTO_HOST", "-n", "test-it", "-f"]
---
name: logs_of_container_with_state_running
setupCmd:
- binary: kanto-cm
args: ["create", "--host", "$KANTO_HOST", "-n", "test-it", "docker.io/library/influxdb:1.8.4"]
- binary: kanto-cm
args: ["start", "-n", "test-it"]
- binary: sleep
args: ["2"]
command:
binary: kanto-cm
args: ["logs", "-n", "test-it", "-t", "5"]
expected:
exitCode: 0
customResult:
type: LOGS_JSON
args: ["5"]
onExit:
- binary: "kanto-cm"
args: ["remove","--host", "$KANTO_HOST", "-n", "test-it", "-f"]
---
name: logs_of_container_with_state_stopped
setupCmd:
- binary: kanto-cm
args: ["create", "--host", "$KANTO_HOST", "-n", "test-it", "docker.io/library/influxdb:1.8.4"]
- binary: kanto-cm
args: ["start", "-n", "test-it"]
- binary: kanto-cm
args: ["stop", "-n", "test-it"]
command:
binary: kanto-cm
args: ["logs", "-n", "test-it"]
expected:
exitCode: 0
customResult:
type: LOGS_JSON
onExit:
- binary: "kanto-cm"
args: ["remove","--host", "$KANTO_HOST", "-n", "test-it", "-f"]
2 changes: 1 addition & 1 deletion integration/testdata/stop-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ expected:
name: stop_invalid_id
command:
binary: kanto-cm
args: ["start", "invalid"]
args: ["stop", "invalid"]
expected:
exitCode: 1
err: "Error: The requested container with ID = invalid was not found."
Expand Down
15 changes: 15 additions & 0 deletions integration/testdata/sysinfo-help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Show information about the container management runtime and its environment.

Usage:
kanto-cm sysinfo

Examples:
sysinfo

Flags:
-h, --help help for sysinfo

Global Flags:
--debug Switch commands log level to DEBUG mode
--host string Specify the address path to the Eclipse Kanto container management (default "/run/container-management/container-management.sock")
--timeout int Specify the connection timeout in seconds to the Eclipse Kanto container management (default 30)
17 changes: 17 additions & 0 deletions integration/testdata/sysinfo-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: sysinfo_help
command:
binary: kanto-cm
args: ["sysinfo", "-h"]
expected:
exitCode: 0
goldenFile: "sysinfo-help.golden"
---
name: sysinfo
command:
binary: kanto-cm
args: ["sysinfo"]
expected:
exitCode: 0
customResult:
type: REGEX
args: ["Engine v[^ ]*, API v[^ ]*, \\(build [^ ]* [^ ]*\\) [\\r?\\n]CLI v[^ ]*, API v[^ ]*, \\(build [^ ]* [^ ]*\\) [\\r?\\n]"]
33 changes: 33 additions & 0 deletions integration/testdata/update-help.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Update a container without recreating it. The provided configurations will be merged with the current one.

Usage:
kanto-cm update <container-id>

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


Flags:
-h, --help help for update
-m, --memory string Updates the max amount of memory the container can use in the form of 200m, 1.2g.
Use -1, to remove the memory usage limit.
--memory-reservation string Updates the soft memory limitation in the form of 200m, 1.2g.
Use -1, to remove the reservation memory limit.
--memory-swap string Updates the total amount of memory + swap that the container can use in the form of 200m, 1.2g.
Use -1, to remove the swap memory limit.
-n, --name string Updates the container with a specific name.
--rp string Updates the restart policy for the container. The policy will be applied when the container exits. Supported restart policies are - no, always, unless-stopped, on-failure.
no - no attempts to restart the container for any reason will be made
always - an attempt to restart the container will be made each time the container exits regardless of the exit code
unless-stopped - restart attempts will be made only if the container has not been stopped by the user
on-failure - restart attempts will be made if the container exits with an exit code != 0;
the additional flags (--rp-cnt and --rp-to) apply only for this policy; if max retry count is not provided - the system will retry until it succeeds endlessly

--rp-cnt int Updates the number of retries that will be made to restart the container on exit if the policy is on-failure (default -2147483648)
--rp-to int Updates the time out period in seconds for each retry that will be made to restart the container on exit if the policy is set to on-failure (default -9223372036854775808)

Global Flags:
--debug Switch commands log level to DEBUG mode
--host string Specify the address path to the Eclipse Kanto container management (default "/run/container-management/container-management.sock")
--timeout int Specify the connection timeout in seconds to the Eclipse Kanto container management (default 30)
76 changes: 76 additions & 0 deletions integration/testdata/update-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: update_help
command:
binary: kanto-cm
args: ["update", "-h"]
expected:
exitCode: 0
goldenFile: "update-help.golden"
---
name: update_no_args
command:
binary: kanto-cm
args: ["update"]
expected:
exitCode: 1
err: "Error: You must provide either an ID or a name for the container via --name (-n)"
---
name: update_invalid_id
command:
binary: kanto-cm
args: ["update", "invalid"]
expected:
exitCode: 1
err: "Error: The requested container with ID = invalid was not found."
---
name: update_invalid_name
command:
binary: kanto-cm
args: ["update", "-n", "invalid"]
expected:
exitCode: 1
err: "Error: The requested container with name = invalid was not found. Try using an ID instead."
---
name: update_container_with_state_running
setupCmd:
- binary: kanto-cm
args: ["create", "--host", "$KANTO_HOST", "-n", "test-it", "docker.io/library/influxdb:1.8.4"]
- binary: kanto-cm
args: ["start", "-n", "test-it"]
command:
binary: kanto-cm
args: ["update", "-n", "test-it", "--rp", "no"]
expected:
exitCode: 0
onExit:
- binary: "kanto-cm"
args: ["remove","--host", "$KANTO_HOST", "-n", "test-it", "-f"]
---
name: update_container_with_state_created
setupCmd:
- binary: kanto-cm
args: ["create", "--host", "$KANTO_HOST", "-n", "test-it", "docker.io/library/influxdb:1.8.4"]
command:
binary: kanto-cm
args: ["update", "-n", "test-it", "--rp", "no"]
expected:
exitCode: 0
onExit:
- binary: "kanto-cm"
args: ["remove","--host", "$KANTO_HOST", "-n", "test-it", "-f"]
---
name: update_container_with_state_stopped
setupCmd:
- binary: kanto-cm
args: ["create", "--host", "$KANTO_HOST", "-n", "test-it", "docker.io/library/influxdb:1.8.4"]
- binary: kanto-cm
args: [ "start", "-n", "test-it" ]
- binary: kanto-cm
args: [ "stop", "-n", "test-it" ]
command:
binary: kanto-cm
args: ["update", "-n", "test-it", "--rp", "no"]
expected:
exitCode: 0
onExit:
- binary: "kanto-cm"
args: ["remove","--host", "$KANTO_HOST", "-n", "test-it", "-f"]

0 comments on commit 4108c97

Please sign in to comment.