| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/gob" |
| 6 | "io" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/registry" |
| 11 | "github.com/carlosframework/platform/internal/release" |
| 12 | ) |
| 13 | |
| 14 | func TestPlan14NoCensusAdoptionTrace(t *testing.T) { |
| 15 | for _, flag := range []string{"false", "true"} { |
| 16 | for _, shape := range []string{"empty", "non-adoptable", "ordinary"} { |
| 17 | t.Run(flag+"/"+shape, func(t *testing.T) { |
| 18 | t.Setenv("CARLOS_HOST_CLAIM", flag) |
| 19 | var rows []registry.Route |
| 20 | if shape != "empty" { |
| 21 | rows = []registry.Route{{Host: "shop.example", Account: "bab", App: "shop", Channel: "stable", Kind: "instance", Socket: "/run/shop.sock"}} |
| 22 | } |
| 23 | if shape == "non-adoptable" { |
| 24 | rows[0].Kind = "static" |
| 25 | rows[0].Channel = "" |
| 26 | } |
| 27 | run := func(protected bool) []string { |
| 28 | input, send := io.Pipe() |
| 29 | recv, output := io.Pipe() |
| 30 | var trace []string |
| 31 | done := make(chan struct{}) |
| 32 | go func() { |
| 33 | defer close(done) |
| 34 | defer output.Close() |
| 35 | defer input.Close() |
| 36 | enc, dec := gob.NewEncoder(output), gob.NewDecoder(input) |
| 37 | type response struct { |
| 38 | Failed bool |
| 39 | Routes []registry.Route |
| 40 | Route *registry.Route |
| 41 | Times map[string]int64 |
| 42 | } |
| 43 | enc.Encode(response{}) |
| 44 | for { |
| 45 | var q struct{ Method, Host string } |
| 46 | if dec.Decode(&q) != nil { |
| 47 | return |
| 48 | } |
| 49 | trace = append(trace, q.Method+":"+q.Host) |
| 50 | if enc.Encode(response{Routes: rows, Times: map[string]int64{}}) != nil { |
| 51 | return |
| 52 | } |
| 53 | } |
| 54 | }() |
| 55 | reg, err := registry.OpenRemote("trace", send, recv, nil) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | var opts []release.PassOption |
| 60 | if protected { |
| 61 | opts, err = rootAdoptionOptions(opts, rootRestartChecks{legacy: func() (bool, error) { return true, nil }}, nil, func() release.PassOption { |
| 62 | return release.WithRecovery(func(_ context.Context, rt registry.Route) (bool, error) { |
| 63 | _, err := reg.Get(rt.Host) |
| 64 | return false, err |
| 65 | }) |
| 66 | }) |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | } |
| 71 | err = release.Pass(t.Context(), reg, release.NewDirStore(t.TempDir()), nil, t.TempDir(), func(string) error { return nil }, opts...) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | reg.Close() |
| 76 | <-done |
| 77 | return trace |
| 78 | } |
| 79 | want, got := run(false), run(true) |
| 80 | if !reflect.DeepEqual(got, want) { |
| 81 | t.Fatalf("no-census trace %v, main %v", got, want) |
| 82 | } |
| 83 | if len(got) == 0 { |
| 84 | t.Fatal("pass skipped registry") |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |