Skip to content

Commit

Permalink
chore: migrate webhook test from bash to go
Browse files Browse the repository at this point in the history
* rename postgres push test to operater e2e test
  • Loading branch information
adityathebe committed Nov 1, 2023
1 parent 60d4a69 commit 840a971
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:
- "**.yaml"
- "**.yml"
- "test/**"
name: Postgres-and-Push-Test
name: Operator E2E Test
permissions:
contents: read
jobs:
Expand All @@ -41,4 +41,4 @@ jobs:
cache-
- run: make bin
- name: Test
run: ./test/e2e-postgres-push.sh
run: ./test/e2e-operator.sh
43 changes: 0 additions & 43 deletions .github/workflows/webhook-check-test.yml

This file was deleted.

90 changes: 90 additions & 0 deletions pkg/api/suite_test.go
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())
}
})
194 changes: 194 additions & 0 deletions pkg/api/webhook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package api_test

import (
"encoding/json"
"fmt"
netHTTP "net/http"
"time"

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 count = 30
for {
time.Sleep(time.Second) // Wait for SyncCanaryJob to create the check
count--

var checks []models.Check
err := ctx.DB().Where("name = ?", canarySpec.Webhook.Name).Find(&checks).Error
Expect(err).To(BeNil())

if len(checks) == 1 {
break
}

if len(checks) != 1 && count <= 0 {
ginkgo.Fail("expected check to be created")
}
}
})

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.
Loading

0 comments on commit 840a971

Please sign in to comment.