| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "net/http/httptest" |
| 5 | "net/url" |
| 6 | "reflect" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestDeploymentTargetsKeepObservationSeparate(t *testing.T) { |
| 12 | view := &AppView{ |
| 13 | Channels: []ChannelView{{Name: "edge", Version: "new"}, {Name: "stable", Err: "unavailable"}, {Name: "empty"}}, |
| 14 | Routes: []RouteView{ |
| 15 | {Account: "ours", Channel: "edge", Version: "alias", Platform: true}, |
| 16 | {Account: "ours", Channel: "edge", Version: "new", Kind: "static"}, |
| 17 | {Account: "ours", Channel: "edge", Version: "old", Kind: "service"}, |
| 18 | {Account: "ours", Channel: "edge", Version: "old"}, |
| 19 | {Account: "ours", Channel: "edge", Version: "new"}, |
| 20 | {Account: "ours", Channel: "edge", Version: "old"}, |
| 21 | {Account: "ours", Channel: "edge"}, |
| 22 | {Account: "ours", Channel: "canary/custom", Version: "custom"}, |
| 23 | {Account: "ours", Version: "running"}, |
| 24 | {Account: "theirs", Channel: "foreign", Version: "secret"}, |
| 25 | }, |
| 26 | } |
| 27 | want := []deploymentTarget{ |
| 28 | {Channel: "canary/custom", Total: 1, Versions: []deploymentObservedVersion{{Version: "custom", Count: 1}}}, |
| 29 | {Channel: "edge", Target: "new", Total: 6, Unknown: 3, Versions: []deploymentObservedVersion{{Version: "new", Count: 1}, {Version: "old", Count: 2}}}, |
| 30 | {Channel: "empty"}, |
| 31 | {Channel: "stable", TargetErr: "unavailable", Total: 1, Versions: []deploymentObservedVersion{{Version: "running", Count: 1}}}, |
| 32 | } |
| 33 | if got := assembleDeploymentTargets(view, "ours"); !reflect.DeepEqual(got, want) { |
| 34 | t.Fatalf("targets = %#v, want %#v", got, want) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestDeploymentRoutesAndHistory(t *testing.T) { |
| 39 | fx := newConsoleFixture(t) |
| 40 | c := signInPaul(t, fx) |
| 41 | status, _, body := get(t, c, fx.u("/apps/kass/deployments")) |
| 42 | if status != 200 { |
| 43 | t.Fatalf("deployments status = %d", status) |
| 44 | } |
| 45 | for _, want := range []string{`/apps/kass/releases#version-v1`, `/apps/kass/instances?channel=stable`, `/apps/kass/deployments" aria-current="page">`} { |
| 46 | if !strings.Contains(body, want) { |
| 47 | t.Errorf("deployments missing %q", want) |
| 48 | } |
| 49 | } |
| 50 | _, _, history := get(t, c, fx.u("/apps/kass/releases")) |
| 51 | if !strings.Contains(history, `id="version-v1"`) { |
| 52 | t.Error("history has no target version anchor") |
| 53 | } |
| 54 | assertNonMember404(t, fx, "/apps/kass/deployments") |
| 55 | } |
| 56 | |
| 57 | func TestReleaseBackTabOnlyAcceptsLocalDestinations(t *testing.T) { |
| 58 | for _, value := range []string{"releases", "deployments", "", "https://other.example/", "../settings", "//other.example"} { |
| 59 | r := httptest.NewRequest("POST", "/", strings.NewReader(url.Values{"back": {value}}.Encode())) |
| 60 | r.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 61 | want := "" |
| 62 | if value == "releases" || value == "deployments" { |
| 63 | want = value |
| 64 | } |
| 65 | if got := releaseBackTab(r); got != want { |
| 66 | t.Errorf("back %q = %q, want %q", value, got, want) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestDeploymentsDoNotDependOnReleaseListing(t *testing.T) { |
| 72 | fx := newConsoleFixture(t) |
| 73 | c := signInPaul(t, fx) |
| 74 | fx.srv.cfg.Releases = listFailStore{fx.rel} |
| 75 | status, _, body := get(t, c, fx.u("/apps/kass/deployments")) |
| 76 | if status != 200 || strings.Contains(body, "Could not list this app") { |
| 77 | t.Fatalf("deployments depended on release listing: status %d, body %s", status, body) |
| 78 | } |
| 79 | _, _, history := get(t, c, fx.u("/apps/kass/releases")) |
| 80 | if !strings.Contains(history, "Could not list this app") { |
| 81 | t.Fatal("fixture did not fail the release listing") |
| 82 | } |
| 83 | } |
| 84 | |