Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
chore: do not exit with log.Fatal while reading JSON file (#1901) (#1903
Browse files Browse the repository at this point in the history
)

(cherry picked from commit f8ee29c)

Co-authored-by: Manuel de la Peña <[email protected]>
  • Loading branch information
mergify[bot] and mdelapenya authored Dec 15, 2021
1 parent a865d3c commit cc2f782
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
{
Expand All @@ -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)
Expand All @@ -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{} {
Expand Down

0 comments on commit cc2f782

Please sign in to comment.