73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/go-sip/internal/configread"
|
|
"git.ipao.vip/rogee/go-sip/internal/store"
|
|
)
|
|
|
|
func TestBootstrapAppliesChangedSIPContentUnderSameSourceRevision(t *testing.T) {
|
|
id := "c046b893-8628-4589-ae50-619d049248a6"
|
|
sipReads := 0
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
var response []byte
|
|
switch r.URL.Path {
|
|
case "/internal/v1/dispatcher/sip":
|
|
sipReads++
|
|
response = configExample(t, "config-read-sip")
|
|
if sipReads > 1 {
|
|
response = []byte(strings.Replace(string(response), `"dial_prefix":""`, `"dial_prefix":"9"`, 1))
|
|
}
|
|
case "/internal/v1/dispatcher/tasks":
|
|
if r.URL.Query().Get("after") == "" {
|
|
response = configExample(t, "task-discovery-page")
|
|
} else {
|
|
response = configExample(t, "task-discovery-end")
|
|
}
|
|
case "/internal/v1/dispatcher/ai-providers":
|
|
response = configExample(t, "config-read-providers")
|
|
case "/internal/v1/dispatcher/task/task-asr":
|
|
response = configExample(t, "config-read-task-asr")
|
|
case "/internal/v1/dispatcher/tenant/1001/quota":
|
|
response = configExample(t, "config-read-quota")
|
|
default:
|
|
t.Errorf("unexpected path %s", r.URL.Path)
|
|
}
|
|
_, _ = w.Write(response)
|
|
}))
|
|
defer server.Close()
|
|
client, err := configread.NewClient(server.URL, id, "test-secret", server.Client())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db, err := store.Open(filepath.Join(t.TempDir(), "state.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
bootstrap := Bootstrap{Client: client, Store: db, DispatcherID: id,
|
|
VerifySIP: func(context.Context, configread.SIP) error { return nil },
|
|
DrainControls: func(context.Context) error { return nil },
|
|
}
|
|
if err := bootstrap.Run(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := bootstrap.Run(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if admitted, err := db.CanAdmit(id, 1001, "task-asr"); err != nil || !admitted {
|
|
t.Fatalf("latest fetched SIP was not admitted: %v, %v", admitted, err)
|
|
}
|
|
bound, err := db.ReadSnapshot(id, 1001, "task-asr")
|
|
if err != nil || bound.SIP.Revision != 9 || !strings.Contains(string(bound.SIP.Trunks), `"dial_prefix":"9"`) {
|
|
t.Fatalf("latest content/local generation not preserved: %+v %v", bound.SIP, err)
|
|
}
|
|
}
|