@@ -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+
7488func 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 }
0 commit comments