|
1 | 1 | package image |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "encoding/json" |
6 | 4 | "errors" |
7 | 5 | "io" |
8 | 6 | "net/http" |
9 | 7 | "strings" |
10 | 8 | "testing" |
11 | 9 |
|
12 | 10 | "github.com/docker/cli/internal/test" |
13 | | - "github.com/moby/moby/api/types/auxprogress" |
14 | 11 | "github.com/moby/moby/client" |
15 | | - ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
16 | 12 | "gotest.tools/v3/assert" |
17 | 13 | ) |
18 | 14 |
|
@@ -93,26 +89,50 @@ func TestNewPushCommandSuccess(t *testing.T) { |
93 | 89 | } |
94 | 90 |
|
95 | 91 | func TestRunPushRespectsNoColorForAuxNotes(t *testing.T) { |
96 | | - t.Setenv("NO_COLOR", "1") |
97 | | - cli := test.NewFakeCli(&fakeClient{ |
98 | | - imagePushFunc: func(ref string, options client.ImagePushOptions) (client.ImagePushResponse, error) { |
99 | | - aux, err := json.Marshal(auxprogress.ManifestPushedInsteadOfIndex{ |
100 | | - ManifestPushedInsteadOfIndex: true, |
101 | | - OriginalIndex: ocispec.Descriptor{Digest: "sha256:1111111111111111111111111111111111111111111111111111111111111111"}, |
102 | | - SelectedManifest: ocispec.Descriptor{Digest: "sha256:2222222222222222222222222222222222222222222222222222222222222222"}, |
103 | | - }) |
104 | | - assert.NilError(t, err) |
105 | | - line := append([]byte(`{"aux":`), aux...) |
106 | | - line = append(line, '}', '\n') |
107 | | - return fakeStreamResult{ReadCloser: io.NopCloser(bytes.NewReader(line))}, nil |
| 92 | + const ( |
| 93 | + originalDigest = "sha256:1111111111111111111111111111111111111111111111111111111111111111" |
| 94 | + selectedDigest = "sha256:2222222222222222222222222222222222222222222222222222222222222222" |
| 95 | + ) |
| 96 | + response := `{"aux":{"manifestPushedInsteadOfIndex":true,"originalIndex":{"digest":"` + originalDigest + `"},"selectedManifest":{"digest":"` + selectedDigest + `"}}}` + "\n" |
| 97 | + |
| 98 | + testCases := []struct { |
| 99 | + name string |
| 100 | + noColor bool |
| 101 | + hasANSI bool |
| 102 | + }{ |
| 103 | + { |
| 104 | + name: "terminal", |
| 105 | + hasANSI: true, |
108 | 106 | }, |
109 | | - }) |
110 | | - cli.Out().SetIsTerminal(true) |
| 107 | + { |
| 108 | + name: "no-color", |
| 109 | + noColor: true, |
| 110 | + hasANSI: false, |
| 111 | + }, |
| 112 | + } |
111 | 113 |
|
112 | | - err := runPush(t.Context(), cli, pushOptions{remote: "image:tag"}) |
113 | | - assert.NilError(t, err) |
| 114 | + for _, tc := range testCases { |
| 115 | + t.Run(tc.name, func(t *testing.T) { |
| 116 | + if tc.noColor { |
| 117 | + t.Setenv("NO_COLOR", "1") |
| 118 | + } else { |
| 119 | + t.Setenv("NO_COLOR", "") |
| 120 | + } |
114 | 121 |
|
115 | | - out := cli.OutBuffer().String() |
116 | | - assert.Assert(t, strings.Contains(out, "sha256:1111111111111111111111111111111111111111111111111111111111111111 -> sha256:2222222222222222222222222222222222222222222222222222222222222222")) |
117 | | - assert.Assert(t, !strings.Contains(out, "\x1b["), "output should not contain ANSI escape codes, output: %s", out) |
| 122 | + cli := test.NewFakeCli(&fakeClient{ |
| 123 | + imagePushFunc: func(ref string, options client.ImagePushOptions) (client.ImagePushResponse, error) { |
| 124 | + return fakeStreamResult{ReadCloser: io.NopCloser(strings.NewReader(response))}, nil |
| 125 | + }, |
| 126 | + }) |
| 127 | + cli.Out().SetIsTerminal(true) |
| 128 | + |
| 129 | + assert.NilError(t, runPush(t.Context(), cli, pushOptions{remote: "example.com/image:tag"})) |
| 130 | + |
| 131 | + out := cli.OutBuffer().String() |
| 132 | + assert.Check(t, strings.Contains(out, "Not all multiplatform-content is present")) |
| 133 | + assert.Check(t, strings.Contains(out, originalDigest)) |
| 134 | + assert.Check(t, strings.Contains(out, selectedDigest)) |
| 135 | + assert.Equal(t, tc.hasANSI, strings.Contains(out, "\x1b[")) |
| 136 | + }) |
| 137 | + } |
118 | 138 | } |
0 commit comments