70 lines
2.6 KiB
Go
70 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
func TestDispatcherConfigurationClientReadsApprovedSIPWithExplicitIdentity(t *testing.T) {
|
|
const dispatcherID = "c046b893-8628-4589-ae50-619d049248a6"
|
|
const secret = "synthetic-placeholder-not-a-credential"
|
|
fixture, err := os.ReadFile(filepath.Join("..", "..", "contracts", "local", "examples", "config-read-sip.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var requests atomic.Int32
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
requests.Add(1)
|
|
if r.Method != http.MethodGet || r.URL.Path != "/internal/v1/dispatcher/sip" || r.Header.Get("X-DISPATCHER-id") != dispatcherID || r.Header.Get("X-DISPATCHER-SECRET-KEY") != secret {
|
|
t.Errorf("unexpected SaaS read method, path or Dispatcher headers: %s %s", r.Method, r.URL.Path)
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write(fixture)
|
|
}))
|
|
defer server.Close()
|
|
path := filepath.Join(t.TempDir(), "dispatcher.sqlite")
|
|
t.Setenv("DISPATCHER_ID", dispatcherID)
|
|
t.Setenv("DISPATCHER_SECRET_KEY", secret)
|
|
t.Setenv("SAAS_BASE_URL", server.URL)
|
|
t.Setenv("RABBITMQ_URL", "amqp://127.0.0.1:5672/%2Fmock")
|
|
t.Setenv("DISPATCHER_SQLITE_PATH", path)
|
|
|
|
settings, reader, err := dispatcherConfigurationClient("mock", server.Client())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if settings.DispatcherID != dispatcherID || settings.SQLitePath != path {
|
|
t.Fatal("dispatcher startup changed approved deployment identity or SQLite path")
|
|
}
|
|
sip, err := reader.ReadSIP(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sip.DispatcherID != dispatcherID || sip.Revision != 8 || requests.Load() != 1 {
|
|
t.Fatalf("wrong approved SIP snapshot or HTTP request count: revision=%d requests=%d", sip.Revision, requests.Load())
|
|
}
|
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
|
t.Fatalf("HTTP client assembly opened SQLite: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDispatcherConfigurationClientNeverUsesLegacyHTTPSetting(t *testing.T) {
|
|
t.Setenv("DISPATCHER_ID", "c046b893-8628-4589-ae50-619d049248a6")
|
|
t.Setenv("DISPATCHER_SECRET_KEY", "synthetic-placeholder-not-a-credential")
|
|
t.Setenv("SAAS_BASE_URL", "")
|
|
t.Setenv("DISPATCHER_CONFIG_READ_BASE_URL", "http://127.0.0.1:8080")
|
|
t.Setenv("RABBITMQ_URL", "amqp://127.0.0.1:5672/%2Fmock")
|
|
t.Setenv("DISPATCHER_SQLITE_PATH", filepath.Join(t.TempDir(), "dispatcher.sqlite"))
|
|
if _, _, err := dispatcherConfigurationClient("mock", nil); err == nil || !strings.Contains(err.Error(), "SAAS_BASE_URL") {
|
|
t.Fatalf("legacy HTTP source silently became a fallback: %v", err)
|
|
}
|
|
}
|