Exemplo n.º 1
0
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)
	}

}
Exemplo n.º 2
0
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)
	}
}
Exemplo n.º 3
0
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)
	}
}
Exemplo n.º 4
0
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)
	}
}
Exemplo n.º 5
0
func TestSensorBadRecord(t *testing.T) {
	sensor := &Sensor{
		checkCmd: utils.StrToCmd("./testdata/test.sh doStuff --debug"),
	}
	sensor.PollAction() // logs but no crash
}
Exemplo n.º 6
0
func TestSensorBadPollAction(t *testing.T) {
	sensor := &Sensor{
		checkCmd: utils.StrToCmd("./testdata/doesNotExist.sh"),
	}
	sensor.PollAction() // logs but no crash
}