Пример #1
0
func TestAlertApi(t *testing.T) {
	apiServer, err := createAPIServer(t, "noauth")
	if err != nil {
		t.Fatal(err)
	}

	defer apiServer.Stop()
	apiClient, err := apiServer.GetClient()
	if err != nil {
		t.Fatal(err)
	}

	alert := api.NewAlert()
	alert.Select = "MTU"
	alert.Test = "MTU>1500"
	alert.Action = "LOG"
	if err := apiClient.create("alert", alert); err != nil {
		t.Fatalf("Failed to create alert: %s", err.Error())
	}

	alert2 := api.NewAlert()
	alert2.Select = "MTU"
	alert2.Test = "MTU>1500"
	alert2.Action = "LOG"
	if err := apiClient.Get("alert", alert.UUID, &alert2); err != nil {
		t.Error(err)
	}

	if *alert != *alert2 {
		t.Errorf("Alert corrupted: %+v != %+v", alert, alert2)
	}

	var alerts map[string]api.Alert
	if err := apiClient.List("alert", &alerts); err != nil {
		t.Error(err)
	} else {
		if len(alerts) != 1 {
			t.Errorf("Wrong number of alerts: got %d, expected 1", len(alerts))
		}
	}

	if alerts[alert.UUID] != *alert {
		t.Errorf("Alert corrupted: %+v != %+v", alerts[alert.UUID], alert)
	}

	if err := apiClient.Delete("alert", alert.UUID); err != nil {
		t.Errorf("Failed to delete alert: %s", err.Error())
	}

	var alerts2 map[string]api.Alert
	if err := apiClient.List("alert", &alerts2); err != nil {
		t.Errorf("Failed to list alerts: %s", err.Error())
	} else {
		if len(alerts2) != 0 {
			t.Errorf("Wrong number of alerts: got %d, expected 0 (%+v)", len(alerts2), alerts2)
		}
	}
}
Пример #2
0
	Short:        "Manage alerts",
	Long:         "Manage alerts",
	SilenceUsage: false,
}

var AlertCreate = &cobra.Command{
	Use:   "create",
	Short: "Create alert",
	Long:  "Create alert",
	Run: func(cmd *cobra.Command, args []string) {
		client, err := api.NewCrudClientFromConfig(&authenticationOpts)
		if err != nil {
			logging.GetLogger().Criticalf(err.Error())
		}

		alert := api.NewAlert()
		alert.Name = alertName
		alert.Description = alertDescription
		alert.Select = alertSelect
		alert.Action = alertAction
		alert.Test = alertTest
		if errs := validator.Validate(alert); errs != nil {
			fmt.Println("Error: ", errs)
			cmd.Usage()
			os.Exit(1)
		}
		if err := client.Create("alert", &alert); err != nil {
			logging.GetLogger().Errorf(err.Error())
			os.Exit(1)
		}
		printJSON(&alert)