Layering reads forge-connections and takes the root connection for the instance's own forge, keeping the file's webhook intake and secret, which are instance infrastructure rather than an account. A document with no root connection disables the forge instead of falling back to a credential the operator may have withdrawn.
76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
)
|
|
|
|
// stubResources answers one kind and reports every other as absent, which is
|
|
// the shape a boot sees for a kind nothing has been stored for.
|
|
type stubResources map[string][]byte
|
|
|
|
func (s stubResources) Query(_ context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
|
value, ok := s[kind]
|
|
if !ok {
|
|
return 0, false, nil
|
|
}
|
|
return 1, true, decode(value)
|
|
}
|
|
|
|
// TestForgeLayeringReadsTheConnection pins where the instance's forge comes
|
|
// from and what a missing connection means. The file's [forge] is only the
|
|
// seed: once the resource holds a document, that document is the forge, and a
|
|
// document without a root connection disables it rather than falling back to
|
|
// the file's credential -- which the operator may have withdrawn.
|
|
func TestForgeLayeringReadsTheConnection(t *testing.T) {
|
|
base := config.Config{
|
|
Forge: config.Forge{
|
|
Type: "github", Host: "file.example.com",
|
|
Token: config.SecretRef{Engine: "env", Key: "FILE_TOKEN"},
|
|
Intake: config.ForgeIntakeBoth,
|
|
WebhookSecret: config.SecretRef{Engine: "env", Key: "HOOK"},
|
|
},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
connections string
|
|
wantType string
|
|
wantHost string
|
|
wantKey string
|
|
wantWebhookKept bool
|
|
}{
|
|
{
|
|
name: "the root connection replaces the file's forge",
|
|
connections: `[{"id":"default","name":"Instance","type":"gitea","host":"gitea.example.com","token":{"engine":"vault","key":"forge/root"}}]`,
|
|
wantType: "gitea",
|
|
wantHost: "gitea.example.com",
|
|
wantKey: "forge/root",
|
|
wantWebhookKept: true,
|
|
},
|
|
{
|
|
name: "a document with no root connection disables the forge",
|
|
connections: `[{"id":"acme","name":"Acme","type":"github","host":"github.com"}]`,
|
|
wantType: "",
|
|
wantHost: "",
|
|
wantKey: "",
|
|
wantWebhookKept: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
cfg, _, err := runtimeConfigFrom(t.Context(), stubResources{ForgeConnectionsKind: []byte(tt.connections)}, base)
|
|
if err != nil {
|
|
t.Fatalf("layer: %v", err)
|
|
}
|
|
if cfg.Forge.Type != tt.wantType || cfg.Forge.Host != tt.wantHost || cfg.Forge.Token.Key != tt.wantKey {
|
|
t.Fatalf("forge = %+v, want type %q host %q key %q", cfg.Forge, tt.wantType, tt.wantHost, tt.wantKey)
|
|
}
|
|
if kept := cfg.Forge.WebhookSecret.Key == "HOOK" && cfg.Forge.Intake == config.ForgeIntakeBoth; kept != tt.wantWebhookKept {
|
|
t.Fatalf("webhook intake/secret = %q/%q, want them kept from the file", cfg.Forge.Intake, cfg.Forge.WebhookSecret.Key)
|
|
}
|
|
})
|
|
}
|
|
}
|