package host import ( "context" "encoding/gob" "io" "reflect" "testing" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/release" ) func TestPlan14NoCensusAdoptionTrace(t *testing.T) { for _, flag := range []string{"false", "true"} { for _, shape := range []string{"empty", "non-adoptable", "ordinary"} { t.Run(flag+"/"+shape, func(t *testing.T) { t.Setenv("CARLOS_HOST_CLAIM", flag) var rows []registry.Route if shape != "empty" { rows = []registry.Route{{Host: "shop.example", Account: "bab", App: "shop", Channel: "stable", Kind: "instance", Socket: "/run/shop.sock"}} } if shape == "non-adoptable" { rows[0].Kind = "static" rows[0].Channel = "" } run := func(protected bool) []string { input, send := io.Pipe() recv, output := io.Pipe() var trace []string done := make(chan struct{}) go func() { defer close(done) defer output.Close() defer input.Close() enc, dec := gob.NewEncoder(output), gob.NewDecoder(input) type response struct { Failed bool Routes []registry.Route Route *registry.Route Times map[string]int64 } enc.Encode(response{}) for { var q struct{ Method, Host string } if dec.Decode(&q) != nil { return } trace = append(trace, q.Method+":"+q.Host) if enc.Encode(response{Routes: rows, Times: map[string]int64{}}) != nil { return } } }() reg, err := registry.OpenRemote("trace", send, recv, nil) if err != nil { t.Fatal(err) } var opts []release.PassOption if protected { opts, err = rootAdoptionOptions(opts, rootRestartChecks{legacy: func() (bool, error) { return true, nil }}, nil, func() release.PassOption { return release.WithRecovery(func(_ context.Context, rt registry.Route) (bool, error) { _, err := reg.Get(rt.Host) return false, err }) }) if err != nil { t.Fatal(err) } } err = release.Pass(t.Context(), reg, release.NewDirStore(t.TempDir()), nil, t.TempDir(), func(string) error { return nil }, opts...) if err != nil { t.Fatal(err) } reg.Close() <-done return trace } want, got := run(false), run(true) if !reflect.DeepEqual(got, want) { t.Fatalf("no-census trace %v, main %v", got, want) } if len(got) == 0 { t.Fatal("pass skipped registry") } }) } } }