Skip to content

Commit

Permalink
Merge pull request #811 from wafuwafu13/config-with-invalid-toml
Browse files Browse the repository at this point in the history
config_test: Add the case of LoadConfigWithInvalidToml
  • Loading branch information
lufia committed Sep 7, 2022
2 parents 857b47d + 5072d8f commit 04bd6d5
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestLoadConfig(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

config, err := LoadConfig(tmpFile.Name())
if err != nil {
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestLoadConfigWithHostStatus(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

config, err := LoadConfig(tmpFile.Name())
if err != nil {
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestLoadConfigWithMountPoint(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

config, err := LoadConfig(tmpFile.Name())
if err != nil {
Expand All @@ -177,14 +177,34 @@ func TestLoadConfigWithInvalidIgnoreRegexp(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

_, err = LoadConfig(tmpFile.Name())
if err == nil {
t.Errorf("should raise error: invalid ignore case.")
}
}

var sampleConfigWithInvalidToml = `
apikey = "abcde"
[plugin.metrics.mysql
command = "ruby /path/to/your/plugin/mysql.rb"
`

func TestLoadConfigWithInvalidToml(t *testing.T) {
tmpFile, err := newTempFileWithContent(sampleConfigWithInvalidToml)
if err != nil {
t.Errorf("should not raise error: %v", err)
}
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

_, err = LoadConfig(tmpFile.Name())
if err == nil {
t.Errorf("should raise error: invalid toml case.")
}
}

var sampleConfigWithInvalidMetricsCommand = `
apikey = "abcde"
Expand All @@ -198,11 +218,11 @@ func TestLoadConfigWithInvalidMetricsCommand(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

_, err = LoadConfig(tmpFile.Name())
if err == nil {
t.Errorf("should raise error: %v", err)
t.Errorf("should raise error: invalid metrics command case.")
}
if !strings.Contains(err.Error(), "should be string or string slice, but int64") {
t.Errorf("should raise error containing type information: %v", err)
Expand All @@ -223,11 +243,11 @@ func TestLoadConfigWithInvalidCheckCommand(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

_, err = LoadConfig(tmpFile.Name())
if err == nil {
t.Errorf("should raise error: %v", err)
t.Errorf("should raise error: invalid check command case.")
}
if !strings.Contains(err.Error(), "should be string or string slice, but <nil>") {
t.Errorf("should raise error containing type information: %v", err)
Expand Down Expand Up @@ -258,7 +278,7 @@ func TestLoadConfigWithTooLargeCheckMemo(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

config, err := LoadConfig(tmpFile.Name())

Expand Down Expand Up @@ -294,11 +314,11 @@ func TestLoadConfigWithInvalidMetadataCommand(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

_, err = LoadConfig(tmpFile.Name())
if err == nil {
t.Errorf("should raise error: %v", err)
t.Errorf("should raise error: invalid metadata command case.")
}
if !strings.Contains(err.Error(), "should be string or string slice, but []interface {}") {
t.Errorf("should raise error containing type information: %v", err)
Expand Down Expand Up @@ -332,7 +352,7 @@ func TestLoadConfigWithCloudPlatform(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

config, err := LoadConfig(tmpFile.Name())
if err != nil {
Expand All @@ -355,11 +375,11 @@ func TestLoadConfigWithInvalidCloudPlatform(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

_, err = LoadConfig(tmpFile.Name())
if err == nil {
t.Errorf("should raise error: %v", err)
t.Errorf("should raise error: invalid cloud platform case.")
}
}

Expand All @@ -368,7 +388,7 @@ func TestLoadConfigFile(t *testing.T) {
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
t.Cleanup(func() { os.Remove(tmpFile.Name()) })

config, err := loadConfigFile(tmpFile.Name())
if err != nil {
Expand Down Expand Up @@ -605,7 +625,7 @@ command = "this will be overwritten"

configFile, err := newTempFileWithContent(configContent)
assertNoError(t, err)
defer os.Remove(configFile.Name())
t.Cleanup(func() { os.Remove(configFile.Name()) })

includedContent := `
roles = [ "Service:role" ]
Expand Down Expand Up @@ -652,7 +672,7 @@ include = "%s/*.conf"

configFile, err := newTempFileWithContent(configContent)
assertNoError(t, err)
defer os.Remove(configFile.Name())
t.Cleanup(func() { os.Remove(configFile.Name()) })

includedContent := `
apikey = "new-api-key"
Expand Down Expand Up @@ -717,7 +737,7 @@ silent = true
if err != nil {
t.Fatalf("should not raise error: %s", err)
}
defer os.Remove(conff.Name())
t.Cleanup(func() { os.Remove(conff.Name()) })

config, err := loadConfigFile(conff.Name())
assertNoError(t, err)
Expand All @@ -736,7 +756,7 @@ command = ["perl", "-E", "say 'Hello'"]
if err != nil {
t.Fatalf("should not raise error: %s", err)
}
defer os.Remove(conff.Name())
t.Cleanup(func() { os.Remove(conff.Name()) })

config, err := loadConfigFile(conff.Name())
assertNoError(t, err)
Expand Down Expand Up @@ -811,7 +831,7 @@ func main() {
os.Remove(tmpf.Name())
t.Fatalf("should not raise error: %s", err)
}
defer os.Remove(gof)
t.Cleanup(func() { os.Remove(gof) })

conf := fmt.Sprintf(`
apikey = "abcde"
Expand Down Expand Up @@ -839,7 +859,7 @@ env = { "SAMPLE_KEY1" = " foo bar ", "SAMPLE_KEY2" = " baz qux " }
if err != nil {
t.Fatalf("should not raise error: %s", err)
}
defer os.Remove(conff.Name())
t.Cleanup(func() { os.Remove(conff.Name()) })

config, err := loadConfigFile(conff.Name())
assertNoError(t, err)
Expand Down

0 comments on commit 04bd6d5

Please sign in to comment.