Skip to content

Commit 4afbd61

Browse files
committed
implement some ad-hoc mocks for responses
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 056e314 commit 4afbd61

File tree

7 files changed

+48
-21
lines changed

7 files changed

+48
-21
lines changed

cli/command/image/client_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package image
33
import (
44
"context"
55
"io"
6+
"net/http"
67
"strings"
78
"time"
89

@@ -28,6 +29,14 @@ type fakeClient struct {
2829
imageBuildFunc func(context.Context, io.Reader, client.ImageBuildOptions) (client.ImageBuildResult, error)
2930
}
3031

32+
type fakeStreamResult struct {
33+
io.ReadCloser
34+
client.ImagePushResponse // same interface as [client.ImagePullResponse]
35+
}
36+
37+
func (e fakeStreamResult) Read(p []byte) (int, error) { return e.ReadCloser.Read(p) }
38+
func (e fakeStreamResult) Close() error { return e.ReadCloser.Close() }
39+
3140
func (cli *fakeClient) ImageTag(_ context.Context, options client.ImageTagOptions) (client.ImageTagResult, error) {
3241
if cli.imageTagFunc != nil {
3342
return cli.imageTagFunc(options)
@@ -54,7 +63,7 @@ func (cli *fakeClient) ImagePush(_ context.Context, ref string, options client.I
5463
return cli.imagePushFunc(ref, options)
5564
}
5665
// FIXME(thaJeztah): how to mock this?
57-
return nil, nil
66+
return fakeStreamResult{ReadCloser: http.NoBody}, nil
5867
}
5968

6069
func (cli *fakeClient) Info(_ context.Context) (system.Info, error) {
@@ -69,7 +78,7 @@ func (cli *fakeClient) ImagePull(_ context.Context, ref string, options client.I
6978
return cli.imagePullFunc(ref, options)
7079
}
7180
// FIXME(thaJeztah): how to mock this?
72-
return nil, nil
81+
return fakeStreamResult{ReadCloser: http.NoBody}, nil
7382
}
7483

7584
func (cli *fakeClient) ImagesPrune(_ context.Context, opts client.ImagePruneOptions) (client.ImagePruneResult, error) {

cli/command/image/load_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"reflect"
8+
"strings"
79
"testing"
10+
"unsafe"
811

912
"github.com/docker/cli/internal/test"
1013
"github.com/moby/moby/client"
@@ -71,8 +74,18 @@ func TestNewLoadCommandInvalidInput(t *testing.T) {
7174
assert.ErrorContains(t, err, expectedError)
7275
}
7376

77+
func mockImageLoadResult(content string, json bool) client.ImageLoadResult {
78+
out := client.ImageLoadResult{JSON: json}
79+
80+
// Set unexported field "body"
81+
v := reflect.ValueOf(&out).Elem()
82+
f := v.FieldByName("body")
83+
r := io.NopCloser(strings.NewReader(content))
84+
reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Elem().Set(reflect.ValueOf(r))
85+
return out
86+
}
87+
7488
func TestNewLoadCommandSuccess(t *testing.T) {
75-
t.Skip("FIXME(thaJeztah): how to mock this?")
7689
testCases := []struct {
7790
name string
7891
args []string
@@ -84,20 +97,19 @@ func TestNewLoadCommandSuccess(t *testing.T) {
8497
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (client.ImageLoadResult, error) {
8598
// FIXME(thaJeztah): how to mock this?
8699
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
87-
return client.ImageLoadResult{}, nil
100+
return mockImageLoadResult(`Success`, false), nil
88101
},
89102
},
90103
{
91104
name: "json",
92105
args: []string{},
93106
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (client.ImageLoadResult, error) {
94107
// FIXME(thaJeztah): how to mock this?
95-
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
96108
// return client.ImageLoadResult{
97109
// Body: io.NopCloser(strings.NewReader(`{"ID": "1"}`)),
98110
// JSON: true,
99111
// }, nil
100-
return client.ImageLoadResult{JSON: true}, nil
112+
return mockImageLoadResult(`{"ID":"1"}`, true), nil
101113
},
102114
},
103115
{
@@ -106,7 +118,7 @@ func TestNewLoadCommandSuccess(t *testing.T) {
106118
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (client.ImageLoadResult, error) {
107119
// FIXME(thaJeztah): how to mock this?
108120
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
109-
return client.ImageLoadResult{}, nil
121+
return mockImageLoadResult(`Success`, false), nil
110122
},
111123
},
112124
{
@@ -118,7 +130,7 @@ func TestNewLoadCommandSuccess(t *testing.T) {
118130
// assert.Check(t, is.Contains(options, client.ImageHistoryWithPlatform(ocispec.Platform{OS: "linux", Architecture: "amd64"})))
119131
// FIXME(thaJeztah): how to mock this?
120132
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
121-
return client.ImageLoadResult{}, nil
133+
return mockImageLoadResult(`Success`, false), nil
122134
},
123135
},
124136
{
@@ -128,7 +140,7 @@ func TestNewLoadCommandSuccess(t *testing.T) {
128140
assert.Check(t, len(options) > 0) // can be 1 or two depending on whether a terminal is attached :/
129141
// FIXME(thaJeztah): how to mock this?
130142
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
131-
return client.ImageLoadResult{}, nil
143+
return mockImageLoadResult(`Success`, false), nil
132144
},
133145
},
134146
{
@@ -138,7 +150,7 @@ func TestNewLoadCommandSuccess(t *testing.T) {
138150
assert.Check(t, len(options) > 0) // can be 1 or two depending on whether a terminal is attached :/
139151
// FIXME(thaJeztah): how to mock this?
140152
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
141-
return client.ImageLoadResult{}, nil
153+
return mockImageLoadResult(`Success`, false), nil
142154
},
143155
},
144156
}

cli/command/image/pull_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package image
22

33
import (
4-
"errors"
54
"fmt"
65
"io"
6+
"net/http"
77
"testing"
88

99
"github.com/docker/cli/internal/test"
@@ -49,7 +49,6 @@ func TestNewPullCommandErrors(t *testing.T) {
4949
}
5050

5151
func TestNewPullCommandSuccess(t *testing.T) {
52-
t.Skip("FIXME(thaJeztah): how to mock this?")
5352
testCases := []struct {
5453
name string
5554
args []string
@@ -77,7 +76,7 @@ func TestNewPullCommandSuccess(t *testing.T) {
7776
imagePullFunc: func(ref string, options client.ImagePullOptions) (client.ImagePullResponse, error) {
7877
assert.Check(t, is.Equal(tc.expectedTag, ref), tc.name)
7978
// FIXME(thaJeztah): how to mock this?
80-
return nil, nil
79+
return fakeStreamResult{ReadCloser: http.NoBody}, nil
8180
},
8281
})
8382
cmd := newPullCommand(cli)
@@ -123,7 +122,7 @@ func TestNewPullCommandWithContentTrustErrors(t *testing.T) {
123122
cli := test.NewFakeCli(&fakeClient{
124123
imagePullFunc: func(ref string, options client.ImagePullOptions) (client.ImagePullResponse, error) {
125124
// FIXME(thaJeztah): how to mock this?
126-
return nil, errors.New("shouldn't try to pull image")
125+
return fakeStreamResult{ReadCloser: http.NoBody}, nil
127126
},
128127
})
129128
cli.SetNotaryClient(tc.notaryFunc)

cli/command/image/push_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package image
33
import (
44
"errors"
55
"io"
6+
"net/http"
67
"testing"
78

89
"github.com/docker/cli/internal/test"
@@ -49,7 +50,6 @@ func TestNewPushCommandErrors(t *testing.T) {
4950
}
5051

5152
func TestNewPushCommandSuccess(t *testing.T) {
52-
t.Skip("FIXME(thaJeztah): how to mock this?")
5353
testCases := []struct {
5454
name string
5555
args []string
@@ -72,7 +72,7 @@ func TestNewPushCommandSuccess(t *testing.T) {
7272
cli := test.NewFakeCli(&fakeClient{
7373
imagePushFunc: func(ref string, options client.ImagePushOptions) (client.ImagePushResponse, error) {
7474
// FIXME(thaJeztah): how to mock this?
75-
return nil, nil
75+
return fakeStreamResult{ReadCloser: http.NoBody}, nil
7676
},
7777
})
7878
cmd := newPushCommand(cli)

cli/command/plugin/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package plugin
33
import (
44
"context"
55
"io"
6-
"strings"
6+
"net/http"
77

88
"github.com/moby/moby/api/types/system"
99
"github.com/moby/moby/client"
@@ -79,5 +79,5 @@ func (c *fakeClient) PluginUpgrade(_ context.Context, name string, options clien
7979
return c.pluginUpgradeFunc(name, options)
8080
}
8181
// FIXME(thaJeztah): how to mock this?
82-
return io.NopCloser(strings.NewReader("")), nil
82+
return http.NoBody, nil
8383
}

cli/command/trust/inspect_pretty_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"bytes"
55
"context"
66
"encoding/hex"
7-
"errors"
87
"io"
8+
"net/http"
99
"testing"
1010

1111
"github.com/docker/cli/cli/trust"
@@ -27,6 +27,14 @@ type fakeClient struct {
2727
client.Client
2828
}
2929

30+
type fakeStreamResult struct {
31+
io.ReadCloser
32+
client.ImagePushResponse // same interface as [client.ImagePullResponse]
33+
}
34+
35+
func (e fakeStreamResult) Read(p []byte) (int, error) { return e.ReadCloser.Read(p) }
36+
func (e fakeStreamResult) Close() error { return e.ReadCloser.Close() }
37+
3038
func (*fakeClient) Info(context.Context) (system.Info, error) {
3139
return system.Info{}, nil
3240
}
@@ -37,7 +45,7 @@ func (*fakeClient) ImageInspect(context.Context, string, ...client.ImageInspectO
3745

3846
func (*fakeClient) ImagePush(context.Context, string, client.ImagePushOptions) (client.ImagePushResponse, error) {
3947
// FIXME(thaJeztah): how to mock this?
40-
return nil, errors.New("don't handle response")
48+
return fakeStreamResult{ReadCloser: http.NoBody}, nil
4149
}
4250

4351
func TestTrustInspectPrettyCommandErrors(t *testing.T) {

cli/command/trust/sign_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ func TestSignCommandChangeListIsCleanedOnError(t *testing.T) {
272272
}
273273

274274
func TestSignCommandLocalFlag(t *testing.T) {
275-
t.Skip("FIXME(thaJeztah): how to mock this?")
276275
cli := test.NewFakeCli(&fakeClient{})
277276
cli.SetNotaryClient(notaryfake.GetEmptyTargetsNotaryRepository)
278277
cmd := newSignCommand(cli)

0 commit comments

Comments
 (0)