Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#246] Kanto CM CLI creates containers out of non json files #247

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions containerm/cli/cli_command_ctrs_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"time"

"github.com/eclipse-kanto/container-management/containerm/containers/types"
Expand Down Expand Up @@ -114,6 +115,10 @@ func (cc *createCmd) containerFromFile() (*types.Container, error) {
if err != nil {
return nil, err
}
if !strings.HasSuffix(strings.ToLower(cc.config.containerFile), ".json") {
fileName := cc.config.containerFile
return nil, log.NewError(fmt.Sprintf("the file %s is not a json file", fileName))
}
byteValue, err := os.ReadFile(cc.config.containerFile)
if err != nil {
return nil, err
Expand Down
18 changes: 16 additions & 2 deletions containerm/cli/cli_command_ctrs_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func (createTc *createCommandTest) generateRunExecutionConfigs() map[string]test
},
"test_create_container_file_invalid_path": {
flags: map[string]string{
createCmdFlagContainerFile: "/test/test",
createCmdFlagContainerFile: "/test/test.json",
},
mockExecution: createTc.mockExecCreateContainerFileInvalidPath,
},
Expand All @@ -501,6 +501,12 @@ func (createTc *createCommandTest) generateRunExecutionConfigs() map[string]test
},
mockExecution: createTc.mockExecCreateContainerFileWithArgs,
},
"test_create_container_file_with_non_json": {
flags: map[string]string{
createCmdFlagContainerFile: "../pkg/testutil/config/container/non_json_files/file1.txt",
},
mockExecution: createTc.mockExecCreateContainerFileWithNonJSON,
},
// Test terminal
"test_create_terminal": {
args: createCmdArgs,
Expand Down Expand Up @@ -1020,7 +1026,7 @@ func (createTc *createCommandTest) mockExecCreateContainerFile(_ []string) error

func (createTc *createCommandTest) mockExecCreateContainerFileInvalidPath(_ []string) error {
createTc.mockClient.EXPECT().Create(gomock.AssignableToTypeOf(context.Background()), gomock.Any()).Times(0)
_, err := os.ReadFile("/test/test")
_, err := os.ReadFile("/test/test.json")
return err
}

Expand All @@ -1036,6 +1042,14 @@ func (createTc *createCommandTest) mockExecCreateContainerFileWithArgs(_ []strin
return log.NewError("no arguments are expected when creating a container from file")
}

func (createTc *createCommandTest) mockExecCreateContainerFileWithNonJSON(_ []string) error {
createTc.mockClient.EXPECT().Create(gomock.AssignableToTypeOf(context.Background()), gomock.Any()).Times(0)
if !strings.HasSuffix("../pkg/testutil/config/container/non_json_files/file1.txt", ".json") {
return log.NewError("../pkg/testutil/config/container/non_json_files/file1.txt is not a json file")
}
Comment on lines +1047 to +1049
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !strings.HasSuffix("../pkg/testutil/config/container/non_json_files/file1.txt", ".json") {
return log.NewError("../pkg/testutil/config/container/non_json_files/file1.txt is not a json file")
}
return log.NewError("../pkg/testutil/config/container/non_json_files/file1.txt is not a json file")

return nil
}

func (createTc *createCommandTest) mockExecCreateWithTerminal(args []string) error {
container := initExpectedCtr(&types.Container{
Image: types.Image{
Expand Down
Loading