-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exit status management and query functions + unit tests (#12)
Signed-off-by: Tullio Sebastiani <[email protected]>
- Loading branch information
1 parent
e29d6aa
commit 856affa
Showing
30 changed files
with
1,054 additions
and
496 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/krkn-chaos/krknctl/internal/config" | ||
provider_models "github.com/krkn-chaos/krknctl/pkg/provider/models" | ||
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator" | ||
"github.com/krkn-chaos/krknctl/pkg/scenario_orchestrator/models" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
func resolveContainerIdOrName(orchestrator scenario_orchestrator.ScenarioOrchestrator, arg string, conn context.Context, conf config.Config) error { | ||
var scenarioContainer *models.ScenarioContainer | ||
var containerId *string | ||
|
||
containerId, err := orchestrator.ResolveContainerName(arg, conn) | ||
if err != nil { | ||
return err | ||
} | ||
if containerId == nil { | ||
containerId = &arg | ||
} | ||
|
||
scenarioContainer, err = orchestrator.InspectScenario(models.Container{Id: *containerId}, conn) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if scenarioContainer == nil { | ||
return fmt.Errorf("scenarioContainer with id or name %s not found", arg) | ||
} | ||
|
||
containerJson, err := json.Marshal(scenarioContainer.Container) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(containerJson)) | ||
if scenarioContainer.Container.ExitStatus != 0 { | ||
return fmt.Errorf("%s %d", conf.ContainerExitStatusPrefix, scenarioContainer.Container.ExitStatus) | ||
} | ||
return nil | ||
} | ||
|
||
func resolveGraphFile(orchestrator scenario_orchestrator.ScenarioOrchestrator, filename string, conn context.Context, conf config.Config) error { | ||
var scenarioFile = make(map[string]provider_models.ScenarioDetail) | ||
var containers = make([]models.Container, 0) | ||
|
||
fileData, err := os.ReadFile(filename) | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal(fileData, &scenarioFile) | ||
if err != nil { | ||
return err | ||
} | ||
for key, _ := range scenarioFile { | ||
scenario, err := orchestrator.ResolveContainerName(key, conn) | ||
if err != nil { | ||
return err | ||
} | ||
if scenario != nil { | ||
containerScenario, err := orchestrator.InspectScenario(models.Container{Id: *scenario}, conn) | ||
if err != nil { | ||
return err | ||
} | ||
if containerScenario != nil { | ||
if (*containerScenario).Container != nil { | ||
containers = append(containers, *(*containerScenario).Container) | ||
} | ||
} | ||
} | ||
} | ||
containersJson, err := json.Marshal(containers) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(containersJson)) | ||
return nil | ||
} | ||
|
||
func NewQueryStatusCommand(scenarioOrchestrator *scenario_orchestrator.ScenarioOrchestrator, config config.Config) *cobra.Command { | ||
var command = &cobra.Command{ | ||
Use: "query-status", | ||
Short: "checks the status of a container or a list of containers", | ||
Long: `checks the status of a container or a list of containers by container name or container Id`, | ||
Args: cobra.MaximumNArgs(1), | ||
SilenceUsage: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
socket, err := (*scenarioOrchestrator).GetContainerRuntimeSocket(nil) | ||
if err != nil { | ||
return err | ||
} | ||
conn, err := (*scenarioOrchestrator).Connect(*socket) | ||
if err != nil { | ||
return err | ||
} | ||
if len(args) > 0 { | ||
err = resolveContainerIdOrName(*scenarioOrchestrator, args[0], conn, config) | ||
return err | ||
} | ||
|
||
graphPath, err := cmd.Flags().GetString("graph") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if graphPath == "" { | ||
return fmt.Errorf("neither container Id or name nor graph plan file specified") | ||
} | ||
|
||
if CheckFileExists(graphPath) == false { | ||
return fmt.Errorf("graph file %s not found", graphPath) | ||
} | ||
|
||
err = resolveGraphFile(*scenarioOrchestrator, graphPath, conn, config) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
return command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package factory | ||
|
||
import ( | ||
"github.com/krkn-chaos/krknctl/internal/config" | ||
"github.com/krkn-chaos/krknctl/pkg/provider" | ||
"github.com/krkn-chaos/krknctl/pkg/provider/offline" | ||
"github.com/krkn-chaos/krknctl/pkg/provider/quay" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestProviderFactory_NewInstance(t *testing.T) { | ||
typeScenarioQuay := &quay.ScenarioProvider{} | ||
typeScenarioOffline := &offline.ScenarioProvider{} | ||
conf, err := config.LoadConfig() | ||
assert.Nil(t, err) | ||
assert.NotNil(t, conf) | ||
|
||
factory := NewProviderFactory(&conf) | ||
assert.NotNil(t, factory) | ||
|
||
factoryQuay := factory.NewInstance(provider.Online) | ||
assert.NotNil(t, factoryQuay) | ||
assert.IsType(t, factoryQuay, typeScenarioQuay) | ||
|
||
factoryOffline := factory.NewInstance(provider.Offline) | ||
assert.NotNil(t, factoryOffline) | ||
assert.IsType(t, factoryOffline, typeScenarioOffline) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/krkn-chaos/krknctl/pkg/typing" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func getScenarioDetail(t *testing.T) ScenarioDetail { | ||
data := ` | ||
{ | ||
"title":"title", | ||
"description":"description", | ||
"fields":[ | ||
{ | ||
"name":"testfield_1", | ||
"type":"string", | ||
"description":"test field 1", | ||
"variable":"TESTFIELD_1" | ||
}, | ||
{ | ||
"name":"testfield_2", | ||
"type":"number", | ||
"description":"test field 2", | ||
"variable":"TESTFIELD_2" | ||
}, | ||
{ | ||
"name":"testfield_3", | ||
"description":"test field 3", | ||
"type":"boolean", | ||
"variable":"TESTFIELD_3" | ||
}, | ||
{ | ||
"name":"testfield_4", | ||
"description":"test field 4", | ||
"type":"file", | ||
"variable":"TESTFIELD_4", | ||
"mount_path":"/mnt/test" | ||
} | ||
] | ||
} | ||
` | ||
scenarioDetail := ScenarioDetail{} | ||
err := json.Unmarshal([]byte(data), &scenarioDetail) | ||
assert.Nil(t, err) | ||
return scenarioDetail | ||
} | ||
|
||
func TestScenarioDetail_GetFieldByEnvVar(t *testing.T) { | ||
scenarioDetail := getScenarioDetail(t) | ||
field1 := scenarioDetail.GetFieldByEnvVar("TESTFIELD_1") | ||
assert.NotNil(t, field1) | ||
assert.Equal(t, (*field1).Type, typing.String) | ||
assert.Equal(t, *((*field1).Description), "test field 1") | ||
assert.Equal(t, *((*field1).Variable), "TESTFIELD_1") | ||
field2 := scenarioDetail.GetFieldByEnvVar("TESTFIELD_2") | ||
assert.NotNil(t, field2) | ||
assert.Equal(t, (*field2).Type, typing.Number) | ||
assert.Equal(t, *((*field2).Description), "test field 2") | ||
assert.Equal(t, *((*field2).Variable), "TESTFIELD_2") | ||
field3 := scenarioDetail.GetFieldByEnvVar("TESTFIELD_3") | ||
assert.NotNil(t, field3) | ||
assert.Equal(t, (*field3).Type, typing.Boolean) | ||
assert.Equal(t, *((*field3).Description), "test field 3") | ||
assert.Equal(t, *((*field3).Variable), "TESTFIELD_3") | ||
|
||
nofield := scenarioDetail.GetFieldByName("nofield") | ||
assert.Nil(t, nofield) | ||
|
||
} | ||
|
||
func TestScenarioDetail_GetFieldByName(t *testing.T) { | ||
scenarioDetail := getScenarioDetail(t) | ||
field1 := scenarioDetail.GetFieldByName("testfield_1") | ||
assert.NotNil(t, field1) | ||
assert.Equal(t, (*field1).Type, typing.String) | ||
assert.Equal(t, *((*field1).Description), "test field 1") | ||
assert.Equal(t, *((*field1).Variable), "TESTFIELD_1") | ||
field2 := scenarioDetail.GetFieldByName("testfield_2") | ||
assert.NotNil(t, field2) | ||
assert.Equal(t, (*field2).Type, typing.Number) | ||
assert.Equal(t, *((*field2).Description), "test field 2") | ||
assert.Equal(t, *((*field2).Variable), "TESTFIELD_2") | ||
field3 := scenarioDetail.GetFieldByName("testfield_3") | ||
assert.NotNil(t, field3) | ||
assert.Equal(t, (*field3).Type, typing.Boolean) | ||
assert.Equal(t, *((*field3).Description), "test field 3") | ||
assert.Equal(t, *((*field3).Variable), "TESTFIELD_3") | ||
|
||
nofield := scenarioDetail.GetFieldByName("nofield") | ||
assert.Nil(t, nofield) | ||
|
||
} | ||
|
||
func TestScenarioDetail_GetFileFieldByMountPath(t *testing.T) { | ||
scenarioDetail := getScenarioDetail(t) | ||
field4 := scenarioDetail.GetFileFieldByMountPath("/mnt/test") | ||
assert.NotNil(t, field4) | ||
assert.Equal(t, (*field4).Type, typing.File) | ||
assert.Equal(t, *((*field4).Description), "test field 4") | ||
assert.Equal(t, *((*field4).Variable), "TESTFIELD_4") | ||
|
||
nofield := scenarioDetail.GetFieldByName("/mnt/notfound") | ||
assert.Nil(t, nofield) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.