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() if err := apiClient.create("alert", alert); err != nil { t.Fatalf("Failed to create alert: %s", err.Error()) } alert2 := api.NewAlert() 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) } } }
Use: "alert", 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 := api.NewCrudClientFromConfig(&authenticationOpts) if client == nil { os.Exit(1) } alert := api.NewAlert() setFromFlag(cmd, "name", &alert.Name) setFromFlag(cmd, "description", &alert.Description) setFromFlag(cmd, "select", &alert.Select) setFromFlag(cmd, "action", &alert.Action) setFromFlag(cmd, "test", &alert.Test) if err := client.Create("alert", &alert); err != nil { logging.GetLogger().Errorf(err.Error()) os.Exit(1) } printJSON(&alert) }, } var AlertList = &cobra.Command{ Use: "list",