Skip to content

Commit 6cf14f9

Browse files
popsiclelmlmthaJeztah
authored andcommitted
image: TestRunPushRespectsNoColorForAuxNotes: use table-test
Signed-off-by: popsiclelmlm <no_7seven@163.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 90f2f30 commit 6cf14f9

1 file changed

Lines changed: 43 additions & 23 deletions

File tree

cli/command/image/push_test.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package image
22

33
import (
4-
"bytes"
5-
"encoding/json"
64
"errors"
75
"io"
86
"net/http"
97
"strings"
108
"testing"
119

1210
"github.com/docker/cli/internal/test"
13-
"github.com/moby/moby/api/types/auxprogress"
1411
"github.com/moby/moby/client"
15-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1612
"gotest.tools/v3/assert"
1713
)
1814

@@ -93,26 +89,50 @@ func TestNewPushCommandSuccess(t *testing.T) {
9389
}
9490

9591
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,
108106
},
109-
})
110-
cli.Out().SetIsTerminal(true)
107+
{
108+
name: "no-color",
109+
noColor: true,
110+
hasANSI: false,
111+
},
112+
}
111113

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+
}
114121

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+
}
118138
}

0 commit comments

Comments
 (0)