Skip to content

Commit

Permalink
Throw errors for invalid json config files
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneGuerlais committed Nov 16, 2022
1 parent a69a6c3 commit 5cb7d6e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 6 deletions.
11 changes: 6 additions & 5 deletions cli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package cmd

import (
"encoding/json"
"github.com/pkg/errors"
"io/ioutil"
"log"
"os"
"path"
"strings"

"github.com/pkg/errors"

"github.com/ricardo-ch/go-kafka-connect/v3/lib/connectors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -88,19 +89,20 @@ func getCreateCmdConfig(cmd *cobra.Command) ([]connectors.CreateConnectorRequest
}

func getConfigFromFolder(folderPath string) ([]connectors.CreateConnectorRequest, error) {
configs := []connectors.CreateConnectorRequest{}
var configs []connectors.CreateConnectorRequest
configFiles, err := ioutil.ReadDir(folderPath)
if err != nil {
return configs, err
}
for _, fileInfo := range configFiles {
// Do not evaluate folders
if fileInfo.IsDir() {
log.Printf("found unexpected subfolder in folder: %s. This command will not search through it.", filePath)
continue
}
config, err := getConfigFromFile(path.Join(folderPath, fileInfo.Name()))
if err != nil {
log.Printf("found unexpected not config file in folder: %s", filePath)
return []connectors.CreateConnectorRequest{}, errors.Wrapf(err, "could not read configuration file: %s", fileInfo.Name())
} else {
configs = append(configs, config)
}
Expand All @@ -109,7 +111,7 @@ func getConfigFromFolder(folderPath string) ([]connectors.CreateConnectorRequest
}

func getConfigFromFile(filePath string) (connectors.CreateConnectorRequest, error) {
config := connectors.CreateConnectorRequest{}
var config connectors.CreateConnectorRequest
fileReader, err := os.Open(filePath)
if err != nil {
return config, err
Expand All @@ -126,5 +128,4 @@ func init() {
createCmd.MarkFlagFilename("path")
createCmd.PersistentFlags().StringVarP(&configString, "string", "s", "", "JSON configuration string")
createCmd.PersistentFlags().BoolVarP(&sync, "sync", "y", false, "execute synchronously")

}
123 changes: 123 additions & 0 deletions cli/cmd/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package cmd

import (
"encoding/json"
"io/ioutil"
"os"
"testing"

"github.com/ricardo-ch/go-kafka-connect/v3/lib/connectors"
"github.com/stretchr/testify/assert"
)

func Test_getCreateCmdConfig_Invalid_Path(t *testing.T) {
configs, err := getConfigFromFolder("missingFolder")

assert.NotNil(t, err)
assert.Zero(t, configs)
}

func Test_getConfigFromFolder_OK(t *testing.T) {
expected := []connectors.CreateConnectorRequest{
{
ConnectorRequest: connectors.ConnectorRequest{
Name: "test-connector",
},
Config: map[string]interface{}{
"config-entry": "some-value",
},
},
{
ConnectorRequest: connectors.ConnectorRequest{
Name: "test-connector-2",
},
Config: map[string]interface{}{
"config-entry-2": "some-value-2",
},
},
}

_ = os.Mkdir("test", os.ModePerm)
defer os.RemoveAll("test")

j1, _ := json.MarshalIndent(expected[0], "", "")
j2, _ := json.MarshalIndent(expected[1], "", "")

_ = ioutil.WriteFile("test/test1.json", j1, os.ModePerm)
_ = ioutil.WriteFile("test/test2.json", j2, os.ModePerm)

actual, err := getConfigFromFolder("test")

assert.Nil(t, err)
assert.Equal(t, expected, actual)
}

func Test_getConfigFromFolder_Invalid_JSON(t *testing.T) {
unsupported := `{"some-key": "some-value"}`

_ = os.Mkdir("test", os.ModePerm)
defer os.RemoveAll("test")

j1, _ := json.MarshalIndent(unsupported, "", "")

_ = ioutil.WriteFile("test/test1.json", j1, os.ModePerm)

actual, err := getConfigFromFolder("test")

assert.NotNil(t, err)
assert.Equal(t, []connectors.CreateConnectorRequest{}, actual)
}

func Test_getConfigFromFolder_Missing_Folder(t *testing.T) {
configs, err := getConfigFromFolder("test")

assert.NotNil(t, err)
assert.Zero(t, configs)
}

func Test_getConfigFromFolder_Not_A_Folder(t *testing.T) {
// Create a file instead of a folder
_, _ = os.Create("test")
defer os.Remove("test")

configs, err := getConfigFromFolder("test")

assert.NotNil(t, err)
assert.Zero(t, configs)
}

func Test_getConfigFromFile_OK(t *testing.T) {
expected := connectors.CreateConnectorRequest{
ConnectorRequest: connectors.ConnectorRequest{
Name: "test-connector",
},
Config: map[string]interface{}{
"config-entry": "some-value",
},
}

j, _ := json.MarshalIndent(expected, "", "")

_ = ioutil.WriteFile("test.json", j, 0644)
defer os.Remove("test.json")

actual, err := getConfigFromFile("test.json")

assert.Nil(t, err)
assert.Equal(t, expected, actual)

}

func Test_getConfigFromFile_Invalid_JSON(t *testing.T) {
unsupported := `{"some-key": "some-value"}`

j, _ := json.MarshalIndent(unsupported, "", "")

_ = ioutil.WriteFile("test.json", j, os.ModePerm)
defer os.Remove("test.json")

actual, err := getConfigFromFile("test.json")

assert.NotNil(t, err)
assert.Zero(t, actual)
}
3 changes: 2 additions & 1 deletion cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"

"github.com/spf13/cobra"
)

var (
Expand Down

0 comments on commit 5cb7d6e

Please sign in to comment.