-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate webhook test from bash to go
* rename postgres push test to operater e2e test
- Loading branch information
1 parent
60d4a69
commit a0f09f1
Showing
6 changed files
with
272 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package api_test | ||
|
||
import ( | ||
gocontext "context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
embeddedPG "github.com/fergusstrange/embedded-postgres" | ||
apiContext "github.com/flanksource/canary-checker/api/context" | ||
"github.com/flanksource/canary-checker/pkg/api" | ||
"github.com/flanksource/canary-checker/pkg/cache" | ||
"github.com/flanksource/canary-checker/pkg/db" | ||
"github.com/flanksource/commons/logger" | ||
"github.com/flanksource/duty" | ||
"github.com/flanksource/duty/context" | ||
"github.com/flanksource/duty/testutils" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/labstack/echo/v4" | ||
"github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var ( | ||
testEchoServer *echo.Echo | ||
testEchoServerPort = 9232 | ||
dbPort = 9999 | ||
ctx context.Context | ||
|
||
testDB *gorm.DB | ||
testPool *pgxpool.Pool | ||
|
||
postgresServer *embeddedPG.EmbeddedPostgres | ||
) | ||
|
||
func TestAPI(t *testing.T) { | ||
RegisterFailHandler(ginkgo.Fail) | ||
ginkgo.RunSpecs(t, "API Tests") | ||
} | ||
|
||
var _ = ginkgo.BeforeSuite(func() { | ||
var err error | ||
|
||
config, dbString := testutils.GetEmbeddedPGConfig("test_canary_job", dbPort) | ||
postgresServer = embeddedPG.NewDatabase(config) | ||
if err = postgresServer.Start(); err != nil { | ||
ginkgo.Fail(err.Error()) | ||
} | ||
logger.Infof("Started postgres on port: %d", dbPort) | ||
|
||
if testDB, testPool, err = duty.SetupDB(dbString, nil); err != nil { | ||
ginkgo.Fail(err.Error()) | ||
} | ||
cache.PostgresCache = cache.NewPostgresCache(testPool) | ||
|
||
// Set this because some functions directly use db.Gorm | ||
db.Gorm = testDB | ||
db.Pool = testPool | ||
|
||
ctx = context.NewContext(gocontext.Background()).WithDB(testDB, testPool) | ||
apiContext.DefaultContext = ctx | ||
|
||
testEchoServer = echo.New() | ||
testEchoServer.POST("/webhook/:id", api.WebhookHandler) | ||
listenAddr := fmt.Sprintf(":%d", testEchoServerPort) | ||
|
||
go func() { | ||
defer ginkgo.GinkgoRecover() // Required by ginkgo, if an assertion is made in a goroutine. | ||
if err := testEchoServer.Start(listenAddr); err != nil { | ||
if err == http.ErrServerClosed { | ||
logger.Infof("Server closed") | ||
} else { | ||
ginkgo.Fail(fmt.Sprintf("Failed to start test server: %v", err)) | ||
} | ||
} | ||
}() | ||
}) | ||
|
||
var _ = ginkgo.AfterSuite(func() { | ||
logger.Infof("Stopping test echo server") | ||
if err := testEchoServer.Shutdown(gocontext.Background()); err != nil { | ||
ginkgo.Fail(err.Error()) | ||
} | ||
|
||
logger.Infof("Stopping postgres") | ||
if err := postgresServer.Stop(); err != nil { | ||
ginkgo.Fail(err.Error()) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package api_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
netHTTP "net/http" | ||
|
||
v1 "github.com/flanksource/canary-checker/api/v1" | ||
"github.com/flanksource/canary-checker/checks" | ||
"github.com/flanksource/canary-checker/pkg/db" | ||
canaryJobs "github.com/flanksource/canary-checker/pkg/jobs/canary" | ||
"github.com/flanksource/commons/http" | ||
"github.com/flanksource/duty/job" | ||
"github.com/flanksource/duty/models" | ||
"github.com/flanksource/duty/types" | ||
"github.com/google/uuid" | ||
"github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = ginkgo.Describe("Test Sync Canary Job", ginkgo.Ordered, func() { | ||
canarySpec := v1.CanarySpec{ | ||
Schedule: "@every 1s", | ||
Webhook: &v1.WebhookCheck{ | ||
Description: v1.Description{ | ||
Name: "my-webhook", | ||
}, | ||
Templatable: v1.Templatable{ | ||
Transform: v1.Template{ | ||
Expression: ` | ||
results.json.alerts.map(r, | ||
{ | ||
'name': r.name + r.fingerprint, | ||
'labels': r.labels, | ||
'icon': 'alert', | ||
'message': r.annotations.summary, | ||
'description': r.annotations.description, | ||
'deletedAt': has(r.endsAt) ? r.endsAt : null, | ||
} | ||
).toJSON()`, | ||
}, | ||
}, | ||
Token: &types.EnvVar{ | ||
ValueStatic: "my-token", | ||
}, | ||
}, | ||
} | ||
|
||
var canaryM *models.Canary | ||
client := http.NewClient().BaseURL(fmt.Sprintf("http://localhost:%d", testEchoServerPort)).Header("Content-Type", "application/json") | ||
|
||
ginkgo.It("should save a canary spec", func() { | ||
b, err := json.Marshal(canarySpec) | ||
Expect(err).To(BeNil()) | ||
|
||
var spec types.JSON | ||
err = json.Unmarshal(b, &spec) | ||
Expect(err).To(BeNil()) | ||
|
||
canaryM = &models.Canary{ | ||
ID: uuid.New(), | ||
Spec: spec, | ||
Name: "alert-manager-canary", | ||
} | ||
err = testDB.Create(canaryM).Error | ||
Expect(err).To(BeNil()) | ||
|
||
response, err := db.GetAllCanariesForSync(ctx, "") | ||
Expect(err).To(BeNil()) | ||
Expect(len(response)).To(Equal(1)) | ||
}) | ||
|
||
ginkgo.It("schedule the canary job", func() { | ||
canaryJobs.CanaryScheduler.Start() | ||
jobCtx := job.JobRuntime{ | ||
Context: ctx, | ||
} | ||
|
||
err := canaryJobs.SyncCanaryJobs(jobCtx) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
ginkgo.It("Should have created the webhook check", func() { | ||
var checks []models.Check | ||
err := ctx.DB().Where("name = ?", canarySpec.Webhook.Name).Find(&checks).Error | ||
Expect(err).To(BeNil()) | ||
|
||
Expect(len(checks)).To(Equal(1)) | ||
}) | ||
|
||
ginkgo.It("Should forbid when webhook is called without the auth token", func() { | ||
resp, err := client.R(ctx).Post(fmt.Sprintf("/webhook/%s", canarySpec.Webhook.Name), nil) | ||
Expect(err).To(BeNil()) | ||
Expect(resp.StatusCode).To(Equal(netHTTP.StatusUnauthorized)) | ||
}) | ||
|
||
ginkgo.It("Should allow when webhook is called with the auth token", func() { | ||
body := `{ | ||
"version": "4", | ||
"status": "firing", | ||
"alerts": [ | ||
{ | ||
"status": "firing", | ||
"name": "first", | ||
"labels": { | ||
"severity": "critical", | ||
"alertName": "ServerDown", | ||
"location": "DataCenterA" | ||
}, | ||
"annotations": { | ||
"summary": "Server in DataCenterA is down", | ||
"description": "This alert indicates that a server in DataCenterA is currently down." | ||
}, | ||
"startsAt": "2023-10-30T08:00:00Z", | ||
"generatorURL": "http://example.com/generatorURL/serverdown", | ||
"fingerprint": "a1b2c3d4e5f6" | ||
}, | ||
{ | ||
"status": "resolved", | ||
"labels": { | ||
"severity": "warning", | ||
"alertName": "HighCPUUsage", | ||
"location": "DataCenterB" | ||
}, | ||
"annotations": { | ||
"summary": "High CPU Usage in DataCenterB", | ||
"description": "This alert indicates that there was high CPU usage in DataCenterB, but it is now resolved." | ||
}, | ||
"startsAt": "2023-10-30T09:00:00Z", | ||
"generatorURL": "http://example.com/generatorURL/highcpuusage", | ||
"name": "second", | ||
"fingerprint": "x1y2z3w4v5" | ||
} | ||
] | ||
}` | ||
resp, err := client.R(ctx).Post(fmt.Sprintf("/webhook/%s?token=%s", canarySpec.Webhook.Name, canarySpec.Webhook.Token.ValueStatic), body) | ||
Expect(err).To(BeNil()) | ||
Expect(resp.StatusCode).To(Equal(netHTTP.StatusOK)) | ||
}) | ||
|
||
ginkgo.It("Should have created 2 new checks from the webhook", func() { | ||
var result []models.Check | ||
err := testDB.Where("type = ?", checks.WebhookCheckType).Where("name != ?", canarySpec.Webhook.Name).Find(&result).Error | ||
Expect(err).To(BeNil()) | ||
Expect(len(result)).To(Equal(2)) | ||
}) | ||
|
||
ginkgo.It("Should have deleted one resolved alert from", func() { | ||
body := `{ | ||
"version": "4", | ||
"status": "firing", | ||
"alerts": [ | ||
{ | ||
"status": "firing", | ||
"name": "first", | ||
"labels": { | ||
"severity": "critical", | ||
"alertName": "ServerDown", | ||
"location": "DataCenterA" | ||
}, | ||
"annotations": { | ||
"summary": "Server in DataCenterA is down", | ||
"description": "This alert indicates that a server in DataCenterA is currently down." | ||
}, | ||
"startsAt": "2023-10-30T08:00:00Z", | ||
"generatorURL": "http://example.com/generatorURL/serverdown", | ||
"fingerprint": "a1b2c3d4e5f6", | ||
"endsAt": "2023-10-30T09:15:00Z" | ||
} | ||
] | ||
}` | ||
resp, err := client.R(ctx).Post(fmt.Sprintf("/webhook/%s?token=%s", canarySpec.Webhook.Name, canarySpec.Webhook.Token.ValueStatic), body) | ||
Expect(err).To(BeNil()) | ||
Expect(resp.StatusCode).To(Equal(netHTTP.StatusOK)) | ||
|
||
var result models.Check | ||
err = testDB.Where("name = 'firsta1b2c3d4e5f6'").Find(&result).Error | ||
Expect(err).To(BeNil()) | ||
Expect(result.DeletedAt).To(Not(BeNil())) | ||
}) | ||
}) |
File renamed without changes.
Oops, something went wrong.