func TestSensorObserve(t *testing.T) { cmd1 := utils.StrToCmd("./testdata/test.sh doStuff --debug") sensor := &Sensor{checkCmd: cmd1} if val, err := sensor.observe(); err != nil { t.Fatalf("Unexpected error from sensor check: %s", err) } else if val != "Running doStuff with args: --debug\n" { t.Fatalf("Unexpected output from sensor check: %s", val) } // Ensure we can run it more than once if _, err := sensor.observe(); err != nil { t.Fatalf("Unexpected error from sensor check (x2): %s", err) } // Ensure bad commands return error cmd2 := utils.StrToCmd("./testdata/doesNotExist.sh") sensor = &Sensor{checkCmd: cmd2} if val, err := sensor.observe(); err == nil { t.Fatalf("Expected error from sensor check but got %s", val) } else if err.Error() != "fork/exec ./testdata/doesNotExist.sh: no such file or directory" { t.Fatalf("Unexpected error from invalid sensor check: %s", err) } }
func TestOnChangeCmd(t *testing.T) { cmd1 := utils.StrToCmd("./testdata/test.sh doStuff --debug") backend := &Backend{ onChangeCmd: cmd1, } if _, err := backend.OnChange(); err != nil { t.Errorf("Unexpected error OnChange: %s", err) } // Ensure we can run it more than once if _, err := backend.OnChange(); err != nil { t.Errorf("Unexpected error OnChange (x2): %s", err) } }
func TestHealthCheck(t *testing.T) { cmd1 := utils.StrToCmd("./testdata/test.sh doStuff --debug") service := &Service{ healthCheckCmd: cmd1, } if _, err := service.CheckHealth(); err != nil { t.Errorf("Unexpected error CheckHealth: %s", err) } // Ensure we can run it more than once if _, err := service.CheckHealth(); err != nil { t.Errorf("Unexpected error CheckHealth (x2): %s", err) } }
func TestSensorPollAction(t *testing.T) { testServer := httptest.NewServer(prometheus.UninstrumentedHandler()) defer testServer.Close() sensor := &Sensor{ Type: "counter", checkCmd: utils.StrToCmd("./testdata/test.sh measureStuff"), collector: prometheus.NewCounter(prometheus.CounterOpts{ Namespace: "telemetry", Subsystem: "sensors", Name: "TestSensorPollAction", Help: "help", })} prometheus.MustRegister(sensor.collector) sensor.PollAction() resp := getFromTestServer(t, testServer) if strings.Count(resp, "telemetry_sensors_TestSensorPollAction 42") != 1 { t.Fatalf("Failed to get match for sensor in response: %s", resp) } }
func TestSensorBadRecord(t *testing.T) { sensor := &Sensor{ checkCmd: utils.StrToCmd("./testdata/test.sh doStuff --debug"), } sensor.PollAction() // logs but no crash }
func TestSensorBadPollAction(t *testing.T) { sensor := &Sensor{ checkCmd: utils.StrToCmd("./testdata/doesNotExist.sh"), } sensor.PollAction() // logs but no crash }