Skip to content

Commit

Permalink
unit tests (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani authored Dec 9, 2024
1 parent cd398af commit 64c689b
Show file tree
Hide file tree
Showing 22 changed files with 788 additions and 76 deletions.
3 changes: 3 additions & 0 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func NewGraphRunCommand(factory *providerfactory.ProviderFactory, scenarioOrches
if err != nil {
return err
}
if kubeconfigPath == nil {
return fmt.Errorf("kubeconfig not found: %s", kubeconfig)
}
volumes[*kubeconfigPath] = config.KubeconfigPath

if metricsProfile != "" {
Expand Down
23 changes: 15 additions & 8 deletions cmd/query_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/krkn-chaos/krknctl/internal/config"
provider_models "github.com/krkn-chaos/krknctl/pkg/provider/models"
Expand Down Expand Up @@ -47,7 +48,7 @@ func resolveContainerIdOrName(orchestrator scenario_orchestrator.ScenarioOrchest

fmt.Println(buf.String())
if scenarioContainer.Container.ExitStatus != 0 {
return utils.StatusCodeToError(scenarioContainer.Container.ExitStatus, conf)
return &utils.ExitError{ExitStatus: int(scenarioContainer.Container.ExitStatus)}
}
return nil
}
Expand Down Expand Up @@ -78,7 +79,7 @@ func resolveGraphFile(orchestrator scenario_orchestrator.ScenarioOrchestrator, f
if (*containerScenario).Container != nil {
containers = append(containers, *(*containerScenario).Container)
if (*containerScenario).Container.ExitStatus != 0 {
statusCodeError = utils.StatusCodeToError((*containerScenario).Container.ExitStatus, orchestrator.GetConfig())
return &utils.ExitError{ExitStatus: int((*containerScenario).Container.ExitStatus)}
}
}
}
Expand Down Expand Up @@ -115,8 +116,11 @@ func NewQueryStatusCommand(scenarioOrchestrator *scenario_orchestrator.ScenarioO
}
if len(args) > 0 {
err = resolveContainerIdOrName(*scenarioOrchestrator, args[0], conn, config)
if exit := utils.ErrorToStatusCode(err, config); exit != nil {
os.Exit(int(*exit))
var staterr *utils.ExitError
if errors.As(err, &staterr) {
if staterr.ExitStatus != 0 {
os.Exit(staterr.ExitStatus)
}
}
return err
}
Expand All @@ -135,10 +139,13 @@ func NewQueryStatusCommand(scenarioOrchestrator *scenario_orchestrator.ScenarioO
}

err = resolveGraphFile(*scenarioOrchestrator, graphPath, conn, config)
if exit := utils.ErrorToStatusCode(err, config); exit != nil {
// since multiple errors of different values might be set
// on graph it exits with a generic 1
os.Exit(1)
// since multiple errors of different values might be set
// on graph it exits with a generic 1
var staterr *utils.ExitError
if errors.As(err, &staterr) {
if staterr.ExitStatus != 0 {
os.Exit(1)
}
}

if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"github.com/fatih/color"
"github.com/krkn-chaos/krknctl/internal/config"
Expand Down Expand Up @@ -143,6 +144,9 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena
if err != nil {
return err
}
if kubeconfigPath == nil {
return fmt.Errorf("kubeconfig not found: %s", *foundKubeconfig)
}
volumes[*kubeconfigPath] = config.KubeconfigPath
if metricsProfile != nil {
volumes[*metricsProfile] = config.MetricsProfilePath
Expand Down Expand Up @@ -218,11 +222,11 @@ func NewRunCommand(factory *factory.ProviderFactory, scenarioOrchestrator *scena

_, err = (*scenarioOrchestrator).RunAttached(quayImageUri+":"+scenarioDetail.Name, containerName, environment, false, volumes, os.Stdout, os.Stderr, &commChan, conn, debug)
if err != nil {
if exit := utils.ErrorToStatusCode(err, config); exit != nil {
os.Exit(int(*exit))
} else {
return err
var staterr *utils.ExitError
if errors.As(err, &staterr) {
os.Exit(staterr.ExitStatus)
}
return err
}
scenarioDuration := time.Since(startTime)
fmt.Println(fmt.Sprintf("%s ran for %s", scenarioDetail.Name, scenarioDuration.String()))
Expand Down
1 change: 0 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type Config struct {
KubeconfigPrefix string `json:"kubeconfig_prefix"`
PodmanDarwinSocketTemplate string `json:"podman_darwin_socket_template"`
PodmanLinuxSocketTemplate string `json:"podman_linux_socket_template"`
ContainerExitStatusPrefix string `json:"container_exit_status_prefix"`
PodmanSocketRoot string `json:"podman_socket_root_linux"`
PodmanRunningState string `json:"podman_running_state"`
DockerSocketRoot string `json:"docker_socket_root"`
Expand Down
1 change: 0 additions & 1 deletion internal/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"krknctl_logs": "krknct-log",
"podman_darwin_socket_template": "unix://%s/.local/share/containers/podman/machine/podman.sock",
"podman_linux_socket_template": "unix://run/user/%d/podman/podman.sock",
"container_exit_status_prefix": "!#KRKN_EXIT_STATUS",
"podman_socket_root_linux": "unix://run/podman/podman.sock",
"podman_running_state": "running",
"docker_socket_root": "unix:///var/run/docker.sock",
Expand Down
5 changes: 4 additions & 1 deletion pkg/scenario_orchestrator/common_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func CommonRunAttached(image string, containerName string, env map[string]string
}
// if there is an error exit status it is propagated via error to the cmd
if containerStatus.Container.ExitStatus > 0 {
return containerId, utils.StatusCodeToError(containerStatus.Container.ExitStatus, c.GetConfig())
return containerId, &utils.ExitError{ExitStatus: int(containerStatus.Container.ExitStatus)}
}

return containerId, nil
Expand Down Expand Up @@ -133,6 +133,9 @@ func CommonListRunningScenarios(c ScenarioOrchestrator, ctx context.Context) (*[
if err != nil {
return nil, err
}
if scenario == nil {
continue
}
runningScenarios = append(runningScenarios, *scenario)

}
Expand Down
8 changes: 6 additions & 2 deletions pkg/scenario_orchestrator/docker/scenario_orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ func (c *ScenarioOrchestrator) ListRunningContainers(ctx context.Context) (*map[
if err != nil {
return nil, err
}
containerName := strings.Replace(container.Names[0], "/", "", 1)
scenarios[index] = orchestratormodels.Container{
Name: container.Names[0],
Name: containerName,
Id: container.ID,
Image: container.Image,
Started: index,
Expand All @@ -335,9 +336,12 @@ func (c *ScenarioOrchestrator) InspectScenario(container orchestratormodels.Cont
}
inspectData, err := cli.ContainerInspect(ctx, container.Id)
if err != nil {
if strings.Contains(err.Error(), "No such container") {
return nil, nil
}
return nil, err
}
container.Name = inspectData.Name
container.Name = strings.Replace(inspectData.Name, "/", "", 1)
container.Status = inspectData.State.Status
container.ExitStatus = int32(inspectData.State.ExitCode)

Expand Down
37 changes: 30 additions & 7 deletions pkg/scenario_orchestrator/docker/scenario_orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func TestScenarioOrchestrator_Docker_AttachWait(t *testing.T) {
}

func TestScenarioOrchestrator_Docker_Kill(t *testing.T) {

config := test.CommonGetConfig(t)
sodocker := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Docker}
test.CommonTestScenarioOrchestratorKillContainers(t, &sodocker, config)
}

func TestScenarioOrchestrator_Docker_ListRunningContainers(t *testing.T) {
Expand All @@ -104,22 +106,43 @@ func TestScenarioOrchestrator_Docker_ListRunningContainers(t *testing.T) {
}

func TestScenarioOrchestrator_Docker_ListRunningScenarios(t *testing.T) {

config := test.CommonGetConfig(t)
sodocker := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Docker}
test.CommonTestScenarioOrchestratorListRunningScenarios(t, &sodocker, config)
}
func TestScenarioOrchestrator_Docker_InspectRunningScenario(t *testing.T) {
config := test.CommonGetConfig(t)
sodocker := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Docker}
test.CommonTestScenarioOrchestratorInspectRunningScenario(t, &sodocker, config)

}

func TestScenarioOrchestrator_Docker_GetContainerRuntimeSocket(t *testing.T) {
config := test.CommonGetConfig(t)
sodocker := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Docker}
var uid *int = nil
envuid := os.Getenv("USERID")
if envuid != "" {
_uid, err := strconv.Atoi(envuid)
assert.Nil(t, err)
uid = &_uid
fmt.Println("USERID -> ", *uid)
} else {
_uid := 1337
uid = &_uid
}
assert.NotNil(t, uid)

}

func TestScenarioOrchestrator_Docker_GetContainerRuntime(t *testing.T) {
socket, err := sodocker.GetContainerRuntimeSocket(uid)
assert.Nil(t, err)
assert.Equal(t, config.DockerSocketRoot, *socket)

}

func TestScenarioOrchestrator_Docker_PrintContainerRuntime(t *testing.T) {

func TestScenarioOrchestrator_Docker_GetContainerRuntime(t *testing.T) {
config := test.CommonGetTestConfig(t)
sopodman := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Docker}
assert.Equal(t, sopodman.GetContainerRuntime(), models.Docker)
}

func TestScenarioOrchestrator_Docker_ResolveContainerId(t *testing.T) {
Expand Down
28 changes: 28 additions & 0 deletions pkg/scenario_orchestrator/factory/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package factory

import (
"github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/docker"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/podman"
"github.com/stretchr/testify/assert"
"testing"
)

func TestScenarioOrchestratorFactory_NewInstance(t *testing.T) {
typeProviderPodman := &podman.ScenarioOrchestrator{}
typeProviderDocker := &docker.ScenarioOrchestrator{}
conf, err := config.LoadConfig()
assert.Nil(t, err)
assert.NotNil(t, conf)
factory := NewScenarioOrchestratorFactory(conf)
assert.NotNil(t, factory)
scenarioDocker := factory.NewInstance(models.Docker)
assert.NotNil(t, scenarioDocker)
assert.IsType(t, scenarioDocker, typeProviderDocker)

scenarioPodman := factory.NewInstance(models.Podman)
assert.NotNil(t, scenarioPodman)
assert.IsType(t, scenarioPodman, typeProviderPodman)

}
13 changes: 9 additions & 4 deletions pkg/scenario_orchestrator/podman/scenario_orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package podman
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/containers/podman/v5/pkg/bindings"
"github.com/containers/podman/v5/pkg/bindings/containers"
"github.com/containers/podman/v5/pkg/bindings/images"
"github.com/containers/podman/v5/pkg/errorhandling"
"github.com/containers/podman/v5/pkg/specgen"
"github.com/docker/docker/api/types/mount"
"github.com/krkn-chaos/krknctl/internal/config"
Expand Down Expand Up @@ -227,12 +229,15 @@ func (c *ScenarioOrchestrator) InspectScenario(container orchestratormodels.Cont
scenario.Volumes = make(map[string]string)
scenario.Env = make(map[string]string)
runningScenario.Container = &container

inspectData, err := containers.Inspect(ctx, container.Id, nil)
if err != nil {
return nil, err
}
if inspectData == nil {
return nil, fmt.Errorf("container %s not found", container.Id)
var customErr *errorhandling.ErrorModel
if errors.As(err, &customErr) {
if customErr.ResponseCode == 404 {
return nil, nil
}
}
}

container.Name = inspectData.Name
Expand Down
61 changes: 48 additions & 13 deletions pkg/scenario_orchestrator/podman/scenario_orchestrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models"
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/test"
"github.com/stretchr/testify/assert"
"os"
operating_system "os"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestScenarioOrchestrator_Podman_RunGraph(t *testing.T) {

func findContainers(t *testing.T, config config.Config) (int, context.Context) {
_true := true
envuid := os.Getenv("USERID")
envuid := operating_system.Getenv("USERID")
var uid *int = nil
if envuid != "" {
_uid, err := strconv.Atoi(envuid)
Expand Down Expand Up @@ -93,35 +94,69 @@ func TestScenarioOrchestrator_Podman_AttachWait(t *testing.T) {
}

func TestScenarioOrchestrator_Podman_Kill(t *testing.T) {
config := test.CommonGetConfig(t)
sopodman := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Podman}
test.CommonTestScenarioOrchestratorKillContainers(t, &sopodman, config)

}

func TestScenarioOrchestrator_Podman_ListRunningContainers(t *testing.T) {
config := test.CommonGetConfig(t)
sodocker := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Podman}
test.CommonTestScenarioOrchestratorListRunningContainers(t, &sodocker, config)
sopodman := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Podman}
test.CommonTestScenarioOrchestratorListRunningContainers(t, &sopodman, config)
}

func TestScenarioOrchestrator_Podman_ListRunningScenarios(t *testing.T) {

config := test.CommonGetConfig(t)
sopodman := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Podman}
test.CommonTestScenarioOrchestratorListRunningScenarios(t, &sopodman, config)
}
func TestScenarioOrchestrator_Podman_InspectRunningScenario(t *testing.T) {

config := test.CommonGetConfig(t)
sodocker := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Docker}
test.CommonTestScenarioOrchestratorInspectRunningScenario(t, &sodocker, config)
}

func TestScenarioOrchestrator_Podman_GetContainerRuntimeSocket(t *testing.T) {
config := test.CommonGetConfig(t)
sodocker := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Docker}
var uid *int = nil
envuid := operating_system.Getenv("USERID")
if envuid != "" {
_uid, err := strconv.Atoi(envuid)
assert.Nil(t, err)
uid = &_uid
fmt.Println("USERID -> ", *uid)
} else {
_uid := 1337
uid = &_uid
}
assert.NotNil(t, uid)

}

func TestScenarioOrchestrator_Podman_GetContainerRuntime(t *testing.T) {
socket, err := sodocker.GetContainerRuntimeSocket(uid)
assert.Nil(t, err)
switch os := runtime.GOOS; os {
case "darwin":
home, err := operating_system.UserHomeDir()
assert.Nil(t, err)
assert.NotNil(t, home)
assert.Equal(t, fmt.Sprintf(config.PodmanDarwinSocketTemplate, home), *socket)
case "linux":
assert.Equal(t, fmt.Sprintf(config.PodmanLinuxSocketTemplate, *uid), *socket)
default:
panic("😱")
}

}

func TestScenarioOrchestrator_Podman_PrintContainerRuntime(t *testing.T) {

func TestScenarioOrchestrator_Podman_GetContainerRuntime(t *testing.T) {
config := test.CommonGetTestConfig(t)
sopodman := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Podman}
assert.Equal(t, sopodman.GetContainerRuntime(), models.Podman)
}

func TestScenarioOrchestrator_Podman_ResolveContainerId(t *testing.T) {
config := test.CommonGetTestConfig(t)
sodocker := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Podman}
test.CommonTestScenarioOrchestratorResolveContainerName(t, &sodocker, config, 3)
sopodman := ScenarioOrchestrator{Config: config, ContainerRuntime: models.Podman}
test.CommonTestScenarioOrchestratorResolveContainerName(t, &sopodman, config, 3)
}
Loading

0 comments on commit 64c689b

Please sign in to comment.