-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw errors for invalid json config files
- Loading branch information
1 parent
a69a6c3
commit 5cb7d6e
Showing
3 changed files
with
131 additions
and
6 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
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) | ||
} |
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 |
---|---|---|
|
@@ -16,8 +16,9 @@ package cmd | |
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
|