From cc2f782512f40d76c848f66e4c4fd6f9ac9246e6 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 15 Dec 2021 09:57:56 +0100 Subject: [PATCH] chore: do not exit with log.Fatal while reading JSON file (#1901) (#1903) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit f8ee29cdfa7e85321a811071458b819a8940be90) Co-authored-by: Manuel de la Peña --- e2e/_suites/fleet/fleet.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/e2e/_suites/fleet/fleet.go b/e2e/_suites/fleet/fleet.go index 3971a151ec..0976e0fec9 100644 --- a/e2e/_suites/fleet/fleet.go +++ b/e2e/_suites/fleet/fleet.go @@ -183,7 +183,11 @@ func (fts *FleetTestSuite) beforeScenario() { } systemMetricsFile := filepath.Join(testResourcesDir, "/default_system_metrics.json") - jsonData := readJSONFile(systemMetricsFile) + jsonData, err := readJSONFile(systemMetricsFile) + if err != nil { + return err + } + for _, item := range jsonData.Children() { if item.Path("type").Data().(string) == "system/metrics" { packageDataStream.Inputs = append(packageDataStream.Inputs, kibana.Input{ @@ -1510,7 +1514,12 @@ func (fts *FleetTestSuite) getAgentDefaultAPIKey() (string, error) { func metricsInputs(integration string, set string, file string, metrics string) []kibana.Input { metricsFile := filepath.Join(testResourcesDir, file) - jsonData := readJSONFile(metricsFile) + jsonData, err := readJSONFile(metricsFile) + if err != nil { + log.Warnf("An error happened while reading metrics file, returning an empty array of inputs: %v", err) + return []kibana.Input{} + } + data := parseJSONMetrics(jsonData, integration, set, metrics) return []kibana.Input{ { @@ -1523,7 +1532,7 @@ func metricsInputs(integration string, set string, file string, metrics string) return []kibana.Input{} } -func readJSONFile(file string) *gabs.Container { +func readJSONFile(file string) (*gabs.Container, error) { jsonFile, err := os.Open(file) if err != nil { fmt.Println(err) @@ -1535,13 +1544,15 @@ func readJSONFile(file string) *gabs.Container { defer jsonFile.Close() data, err := ioutil.ReadAll(jsonFile) if err != nil { - log.Fatalf("error: %v", err) + return nil, err } + jsonParsed, err := gabs.ParseJSON(data) if err != nil { - log.Fatal("Unable to parse json") + return nil, err } - return jsonParsed.S("inputs") + + return jsonParsed.S("inputs"), nil } func parseJSONMetrics(data *gabs.Container, integration string, set string, metrics string) []interface{} {