53 lines
2.0 KiB
Go
53 lines
2.0 KiB
Go
package controlplanestore
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
func TestSchedulingPolicyWritesStayWithinTheirOrg(t *testing.T) {
|
|
tests := []struct {
|
|
name, org, value string
|
|
refused bool
|
|
}{
|
|
{"system org writes the full policy", storecontract.DefaultOrgID, `{"poll_interval":"1m","dispatch":{"trigger":"assignee"}}`, false},
|
|
{"system org omits the poll interval", storecontract.DefaultOrgID, `{"dispatch":{"trigger":"assignee"}}`, true},
|
|
{"another org sets its dispatch rule", "acme", `{"label":"go","dispatch":{"trigger":"label"}}`, false},
|
|
{"another org sets the instance poll interval", "acme", `{"poll_interval":"1s","dispatch":{"trigger":"label"},"label":"go"}`, true},
|
|
{"another org sets the instance retry cap", "acme", `{"max_retries":99}`, true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := (&Server{}).checkOrgWrite(t.Context(), tt.org, SchedulingPolicyKind, []byte(tt.value))
|
|
if refused := errors.Is(err, ErrValidation); refused != tt.refused {
|
|
t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyOrgSchedulingKeepsTheInstanceRuleForUnsetFields(t *testing.T) {
|
|
instance := config.Config{Label: "archie", Dispatch: config.Dispatch{Trigger: "assignee", AckReaction: "eyes"}}
|
|
tests := []struct {
|
|
name, stored string
|
|
want config.Config
|
|
}{
|
|
{"org overrides trigger and label", `{"label":"go","dispatch":{"trigger":"label"}}`, config.Config{Label: "go", Dispatch: config.Dispatch{Trigger: "label", AckReaction: "eyes"}}},
|
|
{"org leaves everything unset", `{}`, instance},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ApplyOrgScheduling(instance, []byte(tt.stored))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Label != tt.want.Label || got.Dispatch != tt.want.Dispatch {
|
|
t.Fatalf("got %+v/%q, want %+v/%q", got.Dispatch, got.Label, tt.want.Dispatch, tt.want.Label)
|
|
}
|
|
})
|
|
}
|
|
}
|