forked from opsgenie/kubernetes-event-exporter
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Evedel-migrate-yaml-library-to-expose-config-errors-eas…
…ier'
- Loading branch information
Showing
7 changed files
with
160 additions
and
17 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
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
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,33 @@ | ||
package setup | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/goccy/go-yaml" | ||
"github.com/resmoio/kubernetes-event-exporter/pkg/exporter" | ||
) | ||
|
||
func ParseConfigFromBites(configBytes []byte) (exporter.Config, error) { | ||
var config exporter.Config | ||
err := yaml.Unmarshal(configBytes, &config) | ||
if err != nil { | ||
errMsg := err.Error() | ||
errLines := strings.Split(errMsg, "\n") | ||
if len(errLines) > 0 { | ||
errMsg = errLines[0] | ||
} | ||
for _, line := range errLines { | ||
if strings.Contains(line, "> ") { | ||
errMsg += ": [ line " + line + "]" | ||
if strings.Contains(line, "{{") { | ||
errMsg += ": " + "Need to wrap values with special characters in quotes" | ||
} | ||
} | ||
} | ||
errMsg = "Cannot parse config to YAML: " + errMsg | ||
return exporter.Config{}, errors.New(errMsg) | ||
} | ||
|
||
return config, nil | ||
} |
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,87 @@ | ||
package setup | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ParseConfigFromBites_ExampleConfigIsCorrect(t *testing.T) { | ||
configBytes, err := os.ReadFile("../../config.example.yaml") | ||
if err != nil { | ||
assert.NoError(t, err, "cannot read config file: "+err.Error()) | ||
return | ||
} | ||
|
||
config, err := ParseConfigFromBites(configBytes) | ||
|
||
assert.NoError(t, err) | ||
assert.NotEmpty(t, config.LogLevel) | ||
assert.NotEmpty(t, config.LogFormat) | ||
assert.NotEmpty(t, config.Route) | ||
assert.NotEmpty(t, config.Route.Routes) | ||
assert.Equal(t, 3, len(config.Route.Routes)) | ||
assert.NotEmpty(t, config.Receivers) | ||
assert.Equal(t, 9, len(config.Receivers)) | ||
} | ||
|
||
func Test_ParseConfigFromBites_NoErrors(t *testing.T) { | ||
configBytes := []byte(` | ||
logLevel: info | ||
logFormat: json | ||
`) | ||
|
||
config, err := ParseConfigFromBites(configBytes) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "info", config.LogLevel) | ||
assert.Equal(t, "json", config.LogFormat) | ||
} | ||
|
||
func Test_ParseConfigFromBites_ErrorWhenCurlyBracesNotEscaped(t *testing.T) { | ||
configBytes := []byte(` | ||
logLevel: {{info}} | ||
logFormat: json | ||
`) | ||
|
||
config, err := ParseConfigFromBites(configBytes) | ||
|
||
expectedErrorLine := "> 2 | logLevel: {{info}}" | ||
expectedErrorSuggestion := "Need to wrap values with special characters in quotes" | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), expectedErrorLine) | ||
assert.Contains(t, err.Error(), expectedErrorSuggestion) | ||
assert.Equal(t, "", config.LogLevel) | ||
assert.Equal(t, "", config.LogFormat) | ||
} | ||
|
||
func Test_ParseConfigFromBites_OkWhenCurlyBracesEscaped(t *testing.T) { | ||
configBytes := []byte(` | ||
logLevel: "{{info}}" | ||
logFormat: json | ||
`) | ||
|
||
config, err := ParseConfigFromBites(configBytes) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, "{{info}}", config.LogLevel) | ||
assert.Equal(t, "json", config.LogFormat) | ||
} | ||
|
||
func Test_ParseConfigFromBites_ErrorErrorNotWithCurlyBraces(t *testing.T) { | ||
configBytes := []byte(` | ||
logLevelNotYAMLErrorError | ||
logFormat: json | ||
`) | ||
|
||
config, err := ParseConfigFromBites(configBytes) | ||
|
||
expectedErrorLine := "> 2 | logLevelNotYAMLErrorError" | ||
expectedErrorSuggestion := "Need to wrap values with special characters in quotes" | ||
assert.NotNil(t, err) | ||
assert.Contains(t, err.Error(), expectedErrorLine) | ||
assert.NotContains(t, err.Error(), expectedErrorSuggestion) | ||
assert.Equal(t, "", config.LogLevel) | ||
assert.Equal(t, "", config.LogFormat) | ||
} |