zitadel/internal/notification/handlers/telemetry_pusher_integration_test.go

88 lines
2.5 KiB
Go
Raw Normal View History

2023-07-03 11:45:18 +02:00
//go:build integration
package handlers_test
import (
2023-07-04 13:32:49 +02:00
"bytes"
"encoding/json"
"io"
"net"
"net/http"
"net/http/httptest"
2023-07-05 11:45:09 +02:00
"reflect"
2023-07-03 11:45:18 +02:00
"testing"
"time"
2023-07-04 13:32:49 +02:00
"github.com/zitadel/zitadel/pkg/grpc/management"
"github.com/zitadel/zitadel/pkg/grpc/system"
2023-07-03 11:45:18 +02:00
)
2023-07-04 16:09:43 +02:00
func TestServer_TelemetryPushMilestones(t *testing.T) {
primaryDomain, instanceID, systemUserCTX, iamOwnerCtx := Tester.UseIsolatedInstance(CTX)
2023-07-04 13:32:49 +02:00
bodies := make(chan []byte, 0)
2023-07-04 16:09:43 +02:00
t.Log("testing against instance with primary domain", primaryDomain)
2023-07-04 13:32:49 +02:00
mockServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
2023-07-05 11:45:09 +02:00
if r.Header.Get("single-value") != "single-value" {
t.Error("single-value header not set")
}
if reflect.DeepEqual(r.Header.Get("multi-value"), "multi-value-1,multi-value-2") {
t.Error("single-value header not set")
}
2023-07-04 13:32:49 +02:00
bodies <- body
w.WriteHeader(http.StatusOK)
}))
listener, err := net.Listen("tcp", "localhost:8081")
if err != nil {
t.Fatal(err)
}
mockServer.Listener = listener
mockServer.Start()
t.Cleanup(mockServer.Close)
2023-07-04 16:09:43 +02:00
awaitMilestone(t, bodies, primaryDomain, "InstanceCreated")
project, err := MgmtClient.AddProject(iamOwnerCtx, &management.AddProjectRequest{Name: "integration"})
2023-07-04 13:32:49 +02:00
if err != nil {
t.Fatal(err)
}
2023-07-04 16:09:43 +02:00
awaitMilestone(t, bodies, primaryDomain, "ProjectCreated")
if _, err = MgmtClient.AddOIDCApp(iamOwnerCtx, &management.AddOIDCAppRequest{
2023-07-04 13:32:49 +02:00
ProjectId: project.GetId(),
Name: "integration",
}); err != nil {
t.Fatal(err)
}
2023-07-04 16:09:43 +02:00
awaitMilestone(t, bodies, primaryDomain, "ApplicationCreated")
if _, err = SystemClient.RemoveInstance(systemUserCTX, &system.RemoveInstanceRequest{InstanceId: instanceID}); err != nil {
2023-07-04 13:32:49 +02:00
t.Fatal(err)
}
2023-07-04 16:09:43 +02:00
awaitMilestone(t, bodies, primaryDomain, "InstanceDeleted")
2023-07-03 11:45:18 +02:00
}
2023-07-04 16:09:43 +02:00
func awaitMilestone(t *testing.T, bodies chan []byte, primaryDomain, expectMilestoneType string) {
2023-07-04 13:32:49 +02:00
for {
select {
case body := <-bodies:
plain := new(bytes.Buffer)
if err := json.Indent(plain, body, "", " "); err != nil {
t.Fatal(err)
}
t.Log("received milestone", plain.String())
milestone := struct {
Type string
PrimaryDomain string
}{}
if err := json.Unmarshal(body, &milestone); err != nil {
t.Error(err)
}
2023-07-04 16:09:43 +02:00
if milestone.Type == expectMilestoneType && milestone.PrimaryDomain == primaryDomain {
2023-07-03 11:45:18 +02:00
return
}
2023-07-04 13:32:49 +02:00
case <-time.After(60 * time.Second):
2023-07-04 16:09:43 +02:00
t.Fatalf("timed out waiting for milestone %s in domain %s", expectMilestoneType, primaryDomain)
2023-07-04 13:32:49 +02:00
}
2023-07-03 11:45:18 +02:00
}
}