56 lines
2.1 KiB
Go
56 lines
2.1 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
agentpb "git.ipao.vip/rogee/go-sip/gen/agent"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func TestApplySIPOnlyAllowsActivatedSIPService(t *testing.T) {
|
|
now := time.Date(2026, 10, 3, 9, 0, 0, 0, time.UTC)
|
|
const id = "c046b893-8628-4589-ae50-619d049248a6"
|
|
attempts := 0
|
|
apply := func(_ context.Context, _ []byte) (map[string]int64, error) {
|
|
attempts++
|
|
return map[string]int64{"trunk-mock": 8}, nil
|
|
}
|
|
server := NewServer(ServerOptions{
|
|
Mode: "sip-only", Now: func() time.Time { return now },
|
|
Status: &agentpb.AgentStatus{AgentId: "agent-1", CellId: "cell-1", BootId: "boot-1"},
|
|
ApplySIP: apply,
|
|
})
|
|
_, err := server.ActivateAgent(context.Background(), &agentpb.ActivateAgentRequest{
|
|
Meta: testMeta("activate-sip", "", 0),
|
|
Binding: &agentpb.AgentBinding{AgentId: "agent-1", CellId: "cell-1", ExpectedBootId: "boot-1", DispatcherEpoch: "epoch-1", SessionGeneration: 1, DispatcherId: id},
|
|
ActivationOperationId: "activate-sip",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, err := os.ReadFile("../../contracts/local/examples/config-read-sip.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body = []byte(strings.ReplaceAll(string(body), `"transport":null`, `"transport":"udp"`))
|
|
body = []byte(strings.ReplaceAll(string(body), `"auth_mode":null`, `"auth_mode":"ip"`))
|
|
body = []byte(strings.ReplaceAll(string(body), `"registration_required":null`, `"registration_required":false`))
|
|
body = []byte(strings.ReplaceAll(string(body), `"server_host":"sip.example.invalid"`, `"server_host":"127.0.0.1"`))
|
|
req := &agentpb.ApplySIPRequest{Meta: testMeta("apply-sip", "apply-sip", 1), ApprovedSnapshotJson: body}
|
|
if _, err := server.ApplySIP(context.Background(), req); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if attempts != 1 {
|
|
t.Fatalf("apply attempts = %d", attempts)
|
|
}
|
|
server.mode = "mock"
|
|
if _, err := server.ApplySIP(context.Background(), req); status.Code(err) != codes.FailedPrecondition || attempts != 1 {
|
|
t.Fatalf("mock boundary allowed real SIP apply: attempts=%d err=%v", attempts, err)
|
|
}
|
|
}
|