Skip to content

Commit

Permalink
Enhance tests to ensure the expected error message is thrown by inval…
Browse files Browse the repository at this point in the history
…id agent config user input. (#64)
  • Loading branch information
qingling128 authored Apr 22, 2021
1 parent 7035210 commit d3cce4a
Show file tree
Hide file tree
Showing 45 changed files with 57 additions and 31 deletions.
65 changes: 34 additions & 31 deletions confgenerator/confgenerator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ var (
goldenParserPath = validTestdataDir + "/%s/%s/golden_fluent_bit_parser.conf"
goldenCollectdPath = validTestdataDir + "/%s/%s/golden_collectd.conf"
goldenOtelPath = validTestdataDir + "/%s/%s/golden_otel.conf"
goldenErrorPath = invalidTestdataDir + "/%s/%s/golden_error"
invalidInputPath = invalidTestdataDir + "/%s/%s/input.yaml"
)

var platform string
Expand Down Expand Up @@ -93,8 +95,8 @@ func TestGenerateConfsWithValidInput(t *testing.T) {
}

// Retrieve the expected golden conf files.
expectedMainConfig := expectedConfig(testName, goldenMainPath, t)
expectedParserConfig := expectedConfig(testName, goldenParserPath, t)
expectedMainConfig := readFileContent(testName, goldenMainPath, t, true)
expectedParserConfig := readFileContent(testName, goldenParserPath, t, true)
// Generate the actual conf files.
mainConf, parserConf, err := uc.GenerateFluentBitConfigs(logsDir, stateDir)
if err != nil {
Expand All @@ -105,15 +107,15 @@ func TestGenerateConfsWithValidInput(t *testing.T) {
updateOrCompareGolden(t, testName, expectedParserConfig, parserConf, goldenParserPath)

if platform == "windows" {
expectedOtelConfig := expectedConfig(testName, goldenOtelPath, t)
expectedOtelConfig := readFileContent(testName, goldenOtelPath, t, true)
otelConf, err := uc.GenerateOtelConfig()
if err != nil {
t.Fatalf("GenerateOtelConfig got %v", err)
}
// Compare the expected and actual and error out in case of diff.
updateOrCompareGolden(t, testName, expectedOtelConfig, otelConf, goldenOtelPath)
} else {
expectedCollectdConfig := expectedConfig(testName, goldenCollectdPath, t)
expectedCollectdConfig := readFileContent(testName, goldenCollectdPath, t, true)
collectdConf, err := uc.GenerateCollectdConfig(defaultLogsDir)
if err != nil {
t.Fatalf("GenerateCollectdConfig got %v", err)
Expand All @@ -125,59 +127,60 @@ func TestGenerateConfsWithValidInput(t *testing.T) {
}
}

func expectedConfig(testName string, validFilePathFormat string, t *testing.T) string {
goldenPath := fmt.Sprintf(validFilePathFormat, platform, testName)
rawExpectedConfig, err := ioutil.ReadFile(goldenPath)
func readFileContent(testName string, filePathFormat string, t *testing.T, respectGolden bool) []byte {
filePath := fmt.Sprintf(filePathFormat, platform, testName)
rawExpectedConfig, err := ioutil.ReadFile(filePath)
if err != nil {
if *updateGolden {
if *updateGolden && respectGolden {
// Tolerate the file not found error because we will overwrite it later anyway.
return ""
return []byte("")
} else {
t.Fatalf("test %q: error reading the golden conf from %s : %s", testName, goldenPath, err)
t.Fatalf("test %q: error reading the file from %s : %s", testName, filePath, err)
}
}
return string(rawExpectedConfig)
return rawExpectedConfig
}

func updateOrCompareGolden(t *testing.T, testName string, expected string, actual string, path string) {
func updateOrCompareGolden(t *testing.T, testName string, expectedBytes []byte, actual string, path string) {
t.Helper()
expected = strings.ReplaceAll(expected, "\r\n", "\n")
actual = strings.ReplaceAll(actual, "\r\n", "\n")
expected := strings.TrimSuffix(strings.ReplaceAll(string(expectedBytes), "\r\n", "\n"), "\n")

This comment has been minimized.

Copy link
@sophieyfang

sophieyfang Apr 28, 2021

Contributor

I think this here somehow removed the last empty lines in the updated conf files..

actual = strings.TrimSuffix(strings.ReplaceAll(actual, "\r\n", "\n"), "\n")
goldenPath := fmt.Sprintf(path, platform, testName)
if diff := cmp.Diff(actual, expected); diff != "" {
if *updateGolden {
// Update the expected to match the actual.
goldenPath := fmt.Sprintf(path, platform, testName)
t.Logf("Detected -update_golden flag. Rewriting the %q golden file to apply the following diff\n%s.", goldenPath, diff)
if err := ioutil.WriteFile(goldenPath, []byte(actual), 0644); err != nil {
t.Fatalf("error updating golden file at %q : %s", goldenPath, err)
}
} else {
t.Fatalf("conf mismatch (-got +want):\n%s", diff)
t.Fatalf("test %q: golden file at %s mismatch (-got +want):\n%s", testName, goldenPath, diff)
}
}
}

func TestGenerateConfigsWithInvalidInput(t *testing.T) {
filePath := invalidTestdataDir + "/" + platform
files, err := ioutil.ReadDir(filePath)
dirPath := invalidTestdataDir + "/" + platform
dirs, err := ioutil.ReadDir(dirPath)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
testName := f.Name()
for _, d := range dirs {
testName := d.Name()
t.Run(testName, func(t *testing.T) {
unifiedConfigFilePath := fmt.Sprintf(filePath+"/%s", testName)
data, err := ioutil.ReadFile(unifiedConfigFilePath)
if err != nil {
t.Fatalf("ReadFile(%q) got %v", unifiedConfigFilePath, err)
invalidInput := readFileContent(testName, invalidInputPath, t, false)
expectedError := readFileContent(testName, goldenErrorPath, t, true)
// The expected error could be triggered by:
// 1. Parsing phase of the agent config when the config is not YAML.
// 2. Config generation phase when the config is invalid.
uc, actualError := ParseUnifiedConfig(invalidInput)
if actualError == nil {
actualError = generateConfigs(uc, defaultLogsDir, defaultStateDir)
}
uc, err := ParseUnifiedConfig(data)
if err != nil {
// Unparsable config is a success for this test
return
}
if err := generateConfigs(uc, defaultLogsDir, defaultStateDir); err == nil {
t.Errorf("test %q: generateConfigs succeeded, want error. input yaml:\n%s", testName, data)
if actualError == nil {
t.Errorf("test %q: generateConfigs succeeded, want error:\n%s\ninvalid input:\n%s", testName, expectedError, invalidInput)
} else {
updateOrCompareGolden(t, testName, expectedError, actualError.Error(), goldenErrorPath)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yaml: line 1: did not find expected key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pipeline "default_pipeline" cannot have an exporter "google" which is not "google_cloud_logging" type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pipeline "test_pipeline" cannot have an exporter "test_google_cloud_logging" which is not "google_cloud_logging" type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pipeline id prefix 'lib:' is reserved for pre-defined pipelines. Pipeline ID "lib:default_pipeline" is not allowed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process id prefix 'lib:' is reserved for pre-defined processors. Processor ID "lib:parse_json_1" is not allowed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
receiver id prefix 'lib:' is reserved for pre-defined receivers. Receiver ID "lib:syslog" is not allowed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pipeline "test_pipeline" cannot have an exporter "test_google_cloud_logging" which is not "google_cloud_logging" type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pipeline "test_pipeline" cannot have an exporter "test_google_cloud_logging" which is not "google_cloud_logging" type
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
syslog type receiver "test_syslog_receiver_invalid_transport_protocol" should have the mode as one of the "tcp", "udp"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
collection interval 2s for metrics receiver hostmetrics_receiver is below the minimum threshold of 10s.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
receiver hostmetrics_receiver has invalid collection interval "24": time: missing unit in duration "24"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export id prefix 'lib:' is reserved for pre-defined exporters. Exporter ID "lib:google" is not allowed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pipeline id prefix 'lib:' is reserved for pre-defined pipelines. Pipeline ID "lib:default_pipeline" is not allowed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
receiver id prefix 'lib:' is reserved for pre-defined receivers. Receiver ID "lib:hostmetrics" is not allowed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yaml: unmarshal errors:
line 1: cannot unmarshal !!str `sdfd` into confgenerator.UnifiedConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
collection interval 2s for metrics receiver hostmetrics is below the minimum threshold of 10s.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
receiver hostmetrics has invalid collection interval "40": time: missing unit in duration "40"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exporter "google" of pipeline "default_pipeline" is not defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pipeline id prefix 'lib:' is reserved for pre-defined pipelines. Pipeline ID "lib:default_pipeline" is not allowed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
receiver "hostmetrics" of pipeline "default_pipeline" is not defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exporter "google" of pipeline "default_pipeline" is not defined
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
receiver "mssql" of pipeline "default_pipeline" is not defined

0 comments on commit d3cce4a

Please sign in to comment.