Repository: NetApp/harvest
Pull request #286 (2021-07-02 02:02:53)

fix(config): harvest should not panic when influx is only exporter


Patch:
diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go
index 96de555b..6e737e6f 100644
--- a/pkg/conf/conf.go
+++ b/pkg/conf/conf.go
@@ -59,6 +59,12 @@ var Config = HarvestConfig{}
 var configRead = false
 var ValidatePortInUse = false
 
+// Used by testing code to reload a new config
+func loadHarvestConfig(configPath string) error {
+	configRead = false
+	return LoadHarvestConfig(configPath)
+}
+
 func LoadHarvestConfig(configPath string) error {
 	if configRead {
 		return nil
@@ -305,7 +311,7 @@ var promPortRangeMapping = make(map[string]PortMap)
 func loadPrometheusExporterPortRangeMapping() {
 	exporters := *Config.Exporters
 	for k, v := range exporters {
-		if *v.Type == "Prometheus" {
+		if v.Type != nil && *v.Type == "Prometheus" {
 			if v.PortRange != nil {
 				promPortRangeMapping[k] = PortMapFromRange(*v.Addr, v.PortRange)
 			}


Test patch:
diff --git a/cmd/tools/doctor/testdata/issue-284.yml b/cmd/tools/doctor/testdata/issue-284.yml
new file mode 100644
index 00000000..b0910bc7
--- /dev/null
+++ b/cmd/tools/doctor/testdata/issue-284.yml
@@ -0,0 +1,13 @@
+Exporters:
+  my_influxdb:
+    exporters: InfluxDB
+    url: https://localhost:8086/write?db=netapp&u=user&p=pass&precision=2
+    bucket: harvest
+    org: harvest
+    token: ZTTrt%24@#WNFM2VZTTNNT25wZWUdtUmhBZEdVUmd3dl@#
+
+Pollers:
+  my-poller:
+    collectors: [Simple]
+    exporters:
+      - my_influxdb
diff --git a/pkg/conf/conf_test.go b/pkg/conf/conf_test.go
index 59fb8a4a..5e06c5b4 100644
--- a/pkg/conf/conf_test.go
+++ b/pkg/conf/conf_test.go
@@ -7,14 +7,12 @@ import (
 	"testing"
 )
 
+var testYml = "../../cmd/tools/doctor/testdata/testConfig.yml"
+
 func TestGetPrometheusExporterPorts(t *testing.T) {
-	path := "../../cmd/tools/doctor/testdata/testConfig.yml"
-	err := LoadHarvestConfig(path)
+	loadTestData(testYml)
 	// Test without checking
 	ValidatePortInUse = true
-	if err != nil {
-		panic(err)
-	}
 	type args struct {
 		pollerName string
 	}
@@ -44,14 +42,26 @@ func TestGetPrometheusExporterPorts(t *testing.T) {
 	}
 }
 
-func TestPollerStructDefaults(t *testing.T) {
-	path := "../../cmd/tools/doctor/testdata/testConfig.yml"
-	err := LoadHarvestConfig(path)
+func TestGetPrometheusExporterPortsIssue284(t *testing.T) {
+	loadTestData("../../cmd/tools/doctor/testdata/issue-284.yml")
+	loadPrometheusExporterPortRangeMapping()
+	got, _ := GetPrometheusExporterPorts("issue-284")
+	if got != 0 {
+		t.Fatalf("expected port to be 0 but was %d", got)
+	}
+}
+
+func loadTestData(yml string) {
+	err := loadHarvestConfig(yml)
 	if err != nil {
 		panic(err)
 	}
+}
+
+func TestPollerStructDefaults(t *testing.T) {
+	loadTestData(testYml)
 	t.Run("poller exporters", func(t *testing.T) {
-		poller, err := GetPoller2(path, "zeros")
+		poller, err := GetPoller2(testYml, "zeros")
 		if err != nil {
 			panic(err)
 		}
@@ -69,7 +79,7 @@ func TestPollerStructDefaults(t *testing.T) {
 	})
 
 	t.Run("poller collector", func(t *testing.T) {
-		poller, err := GetPoller2(path, "cluster-01")
+		poller, err := GetPoller2(testYml, "cluster-01")
 		if err != nil {
 			panic(err)
 		}
@@ -87,7 +97,7 @@ func TestPollerStructDefaults(t *testing.T) {
 	})
 
 	t.Run("poller username", func(t *testing.T) {
-		poller, err := GetPoller2(path, "zeros")
+		poller, err := GetPoller2(testYml, "zeros")
 		if err != nil {
 			panic(err)
 		}
@@ -99,11 +109,7 @@ func TestPollerStructDefaults(t *testing.T) {
 }
 
 func TestPollerUnion(t *testing.T) {
-	path := "../../cmd/tools/doctor/testdata/testConfig.yml"
-	err := LoadHarvestConfig(path)
-	if err != nil {
-		panic(err)
-	}
+	loadTestData(testYml)
 	addr := "addr"
 	user := "user"
 	defaults := Poller{
@@ -157,13 +163,9 @@ func TestPollerUnion(t *testing.T) {
 }
 
 func TestFlowStyle(t *testing.T) {
-	path := "../../cmd/tools/doctor/testdata/testConfig.yml"
-	err := LoadHarvestConfig(path)
-	if err != nil {
-		panic(err)
-	}
+	loadTestData(testYml)
 	t.Run("poller with flow", func(t *testing.T) {
-		poller, err := GetPoller2(path, "flow")
+		poller, err := GetPoller2(testYml, "flow")
 		if err != nil {
 			panic(err)
 		}