@@ -2,6 +2,7 @@ package container
22
33import (
44 "context"
5+ "fmt"
56 "io"
67 "io/ioutil"
78 "os"
@@ -10,6 +11,7 @@ import (
1011 "testing"
1112
1213 "github.com/docker/cli/internal/test"
14+ "github.com/docker/cli/internal/test/notary"
1315 "github.com/docker/docker/api/types"
1416 "github.com/docker/docker/api/types/container"
1517 "github.com/docker/docker/api/types/network"
@@ -119,6 +121,51 @@ func TestCreateContainerPullsImageIfMissing(t *testing.T) {
119121 assert .Check (t , is .Contains (stderr , "Unable to find image 'does-not-exist-locally:latest' locally" ))
120122}
121123
124+ func TestNewCreateCommandWithContentTrustErrors (t * testing.T ) {
125+ testCases := []struct {
126+ name string
127+ args []string
128+ expectedError string
129+ notaryFunc test.NotaryClientFuncType
130+ }{
131+ {
132+ name : "offline-notary-server" ,
133+ notaryFunc : notary .GetOfflineNotaryRepository ,
134+ expectedError : "client is offline" ,
135+ args : []string {"image:tag" },
136+ },
137+ {
138+ name : "uninitialized-notary-server" ,
139+ notaryFunc : notary .GetUninitializedNotaryRepository ,
140+ expectedError : "remote trust data does not exist" ,
141+ args : []string {"image:tag" },
142+ },
143+ {
144+ name : "empty-notary-server" ,
145+ notaryFunc : notary .GetEmptyTargetsNotaryRepository ,
146+ expectedError : "No valid trust data for tag" ,
147+ args : []string {"image:tag" },
148+ },
149+ }
150+ for _ , tc := range testCases {
151+ cli := test .NewFakeCli (& fakeClient {
152+ createContainerFunc : func (config * container.Config ,
153+ hostConfig * container.HostConfig ,
154+ networkingConfig * network.NetworkingConfig ,
155+ containerName string ,
156+ ) (container.ContainerCreateCreatedBody , error ) {
157+ return container.ContainerCreateCreatedBody {}, fmt .Errorf ("shouldn't try to pull image" )
158+ },
159+ }, test .EnableContentTrust )
160+ cli .SetNotaryClient (tc .notaryFunc )
161+ cmd := NewCreateCommand (cli )
162+ cmd .SetOutput (ioutil .Discard )
163+ cmd .SetArgs (tc .args )
164+ err := cmd .Execute ()
165+ assert .ErrorContains (t , err , tc .expectedError )
166+ }
167+ }
168+
122169type fakeNotFound struct {}
123170
124171func (f fakeNotFound ) NotFound () bool { return true }
0 commit comments