Skip to content

Commit

Permalink
Kanto CM CLI creates containers out of non json files #246
Browse files Browse the repository at this point in the history
  • Loading branch information
AnishVallolikalathilAchuthadas committed Jul 12, 2024
1 parent 26e797e commit 41d6e3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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")
}
return nil
}

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

0 comments on commit 41d6e3a

Please sign in to comment.