128 lines
5.3 KiB
Go
128 lines
5.3 KiB
Go
package app_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"proxyctl/internal/app"
|
|
"proxyctl/internal/profile"
|
|
"proxyctl/internal/target"
|
|
)
|
|
|
|
type controlledDetectionTarget struct {
|
|
id string
|
|
description target.Description
|
|
release <-chan struct{}
|
|
detection target.Detection
|
|
}
|
|
|
|
func (t *controlledDetectionTarget) ID() string { return t.id }
|
|
func (t *controlledDetectionTarget) Description() target.Description { return t.description }
|
|
func (t *controlledDetectionTarget) Availability(context.Context) target.Availability {
|
|
panic("progressive detection must use the structured Detector seam")
|
|
}
|
|
func (t *controlledDetectionTarget) Inspect(context.Context, *profile.Profile) target.Inspection {
|
|
panic("progressive detection must use the structured Detector seam")
|
|
}
|
|
func (t *controlledDetectionTarget) Confirmation(bool) string { return "" }
|
|
func (t *controlledDetectionTarget) SetDesired(context.Context, bool, *profile.Profile) target.OperationResult {
|
|
return target.OperationResult{}
|
|
}
|
|
func (t *controlledDetectionTarget) Detect(context.Context, *profile.Profile) target.Detection {
|
|
<-t.release
|
|
return t.detection
|
|
}
|
|
|
|
func TestApplicationStreamsIndependentTargetDetectionInProductOrder(t *testing.T) {
|
|
firstRelease := make(chan struct{})
|
|
secondRelease := make(chan struct{})
|
|
failedRelease := make(chan struct{})
|
|
adapters := []target.Target{
|
|
&controlledDetectionTarget{id: "first", description: target.Description{Name: "First", Summary: "first description"}, release: firstRelease, detection: target.Detection{Availability: target.Availability{Available: true, Evidence: "first available"}, Inspection: target.Inspection{State: target.Enabled, Evidence: "first enabled"}}},
|
|
&controlledDetectionTarget{id: "second", description: target.Description{Name: "Second", Summary: "second description"}, release: secondRelease, detection: target.Detection{Availability: target.Availability{Available: true, Evidence: "second available"}, Inspection: target.Inspection{State: target.External, Evidence: "second external"}}},
|
|
&controlledDetectionTarget{id: "failed", description: target.Description{Name: "Failed", Summary: "failed description"}, release: failedRelease, detection: target.Detection{Err: errors.New("inspection command failed")}},
|
|
}
|
|
application, err := app.NewWithTargets(profile.NewFileStore(filepath.Join(t.TempDir(), "profiles.json")), adapters)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
before := application.Snapshot()
|
|
assertTargetOrder(t, before, []string{"first", "second", "failed"})
|
|
for _, item := range before.Targets {
|
|
if item.DetectionPhase != app.TargetChecking || item.Description.Name == "" || item.Description.Summary == "" {
|
|
t.Fatalf("Target identity was not useful before detection: %#v", item)
|
|
}
|
|
}
|
|
|
|
checking, updates := application.BeginTargetDetection()
|
|
for _, item := range checking.Targets {
|
|
if item.DetectionPhase != app.TargetChecking {
|
|
t.Fatalf("Target did not begin in Checking: %#v", item)
|
|
}
|
|
}
|
|
|
|
close(secondRelease)
|
|
second := <-updates
|
|
if second.Result.TargetID != "second" || second.Result.Phase != app.TargetReady || second.Result.Inspection.State != target.External {
|
|
t.Fatalf("second completion = %#v", second)
|
|
}
|
|
assertOnlyDetectionChanged(t, checking, second.Snapshot, "second")
|
|
assertTargetOrder(t, second.Snapshot, []string{"first", "second", "failed"})
|
|
|
|
close(failedRelease)
|
|
failed := <-updates
|
|
if failed.Result.TargetID != "failed" || failed.Result.Phase != app.TargetDetectionFailed || failed.Result.Err == nil {
|
|
t.Fatalf("failed completion = %#v", failed)
|
|
}
|
|
assertOnlyDetectionChanged(t, second.Snapshot, failed.Snapshot, "failed")
|
|
|
|
close(firstRelease)
|
|
first := <-updates
|
|
if first.Result.TargetID != "first" || first.Result.Phase != app.TargetReady || first.Result.Inspection.State != target.Enabled {
|
|
t.Fatalf("first completion = %#v", first)
|
|
}
|
|
assertOnlyDetectionChanged(t, failed.Snapshot, first.Snapshot, "first")
|
|
|
|
final := <-updates
|
|
if !final.Complete {
|
|
t.Fatalf("final update = %#v", final)
|
|
}
|
|
assertTargetOrder(t, final.Snapshot, []string{"first", "second", "failed"})
|
|
if final.Snapshot.Targets[0].State != target.Enabled || final.Snapshot.Targets[1].State != target.External || final.Snapshot.Targets[2].DetectionPhase != app.TargetDetectionFailed {
|
|
t.Fatalf("final snapshot = %#v", final.Snapshot.Targets)
|
|
}
|
|
}
|
|
|
|
func assertTargetOrder(t *testing.T, snapshot app.Snapshot, want []string) {
|
|
t.Helper()
|
|
got := make([]string, len(snapshot.Targets))
|
|
for i := range snapshot.Targets {
|
|
got[i] = snapshot.Targets[i].ID
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("Target order = %#v, want %#v", got, want)
|
|
}
|
|
}
|
|
|
|
func assertOnlyDetectionChanged(t *testing.T, before, after app.Snapshot, changedID string) {
|
|
t.Helper()
|
|
if len(before.Targets) != len(after.Targets) {
|
|
t.Fatalf("Target count changed from %d to %d", len(before.Targets), len(after.Targets))
|
|
}
|
|
for i := range before.Targets {
|
|
if before.Targets[i].ID == changedID {
|
|
if reflect.DeepEqual(before.Targets[i], after.Targets[i]) {
|
|
t.Fatalf("completed Target %q did not change", changedID)
|
|
}
|
|
continue
|
|
}
|
|
if !reflect.DeepEqual(before.Targets[i], after.Targets[i]) {
|
|
t.Fatalf("completion for %q changed unrelated Target %q: before=%#v after=%#v", changedID, before.Targets[i].ID, before.Targets[i], after.Targets[i])
|
|
}
|
|
}
|
|
}
|