2016-10-08 04:35:18 +00:00
/ *
Copyright 2016 The Kubernetes Authors .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package dockershim
import (
2017-05-04 01:42:29 +00:00
"fmt"
2016-10-08 04:35:18 +00:00
"testing"
2017-06-29 20:21:17 +00:00
dockertypes "github.com/docker/docker/api/types"
2017-05-04 01:42:29 +00:00
"github.com/docker/docker/pkg/jsonmessage"
"github.com/stretchr/testify/assert"
2018-01-19 21:36:06 +00:00
"github.com/stretchr/testify/require"
2016-10-08 04:35:18 +00:00
2018-02-06 22:11:09 +00:00
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
2017-05-03 17:46:35 +00:00
"k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
2016-10-08 04:35:18 +00:00
)
func TestRemoveImage ( t * testing . T ) {
ds , fakeDocker , _ := newTestDockerService ( )
id := "1111"
2017-03-02 01:18:02 +00:00
fakeDocker . InjectImageInspects ( [ ] dockertypes . ImageInspect { { ID : id , RepoTags : [ ] string { "foo" } } } )
2018-01-19 21:36:06 +00:00
ds . RemoveImage ( getTestCTX ( ) , & runtimeapi . RemoveImageRequest { Image : & runtimeapi . ImageSpec { Image : id } } )
2017-05-03 17:46:35 +00:00
fakeDocker . AssertCallDetails ( libdocker . NewCalledDetail ( "inspect_image" , nil ) ,
libdocker . NewCalledDetail ( "remove_image" , [ ] interface { } { id , dockertypes . ImageRemoveOptions { PruneChildren : true } } ) )
2016-10-08 04:35:18 +00:00
}
func TestRemoveImageWithMultipleTags ( t * testing . T ) {
ds , fakeDocker , _ := newTestDockerService ( )
id := "1111"
2017-03-02 01:18:02 +00:00
fakeDocker . InjectImageInspects ( [ ] dockertypes . ImageInspect { { ID : id , RepoTags : [ ] string { "foo" , "bar" } } } )
2018-01-19 21:36:06 +00:00
ds . RemoveImage ( getTestCTX ( ) , & runtimeapi . RemoveImageRequest { Image : & runtimeapi . ImageSpec { Image : id } } )
2017-05-03 17:46:35 +00:00
fakeDocker . AssertCallDetails ( libdocker . NewCalledDetail ( "inspect_image" , nil ) ,
libdocker . NewCalledDetail ( "remove_image" , [ ] interface { } { "foo" , dockertypes . ImageRemoveOptions { PruneChildren : true } } ) ,
libdocker . NewCalledDetail ( "remove_image" , [ ] interface { } { "bar" , dockertypes . ImageRemoveOptions { PruneChildren : true } } ) )
2016-10-08 04:35:18 +00:00
}
2017-05-04 01:42:29 +00:00
func TestPullWithJSONError ( t * testing . T ) {
ds , fakeDocker , _ := newTestDockerService ( )
tests := map [ string ] struct {
image * runtimeapi . ImageSpec
err error
expectedError string
} {
"Json error" : {
& runtimeapi . ImageSpec { Image : "ubuntu" } ,
& jsonmessage . JSONError { Code : 50 , Message : "Json error" } ,
"Json error" ,
} ,
"Bad gateway" : {
& runtimeapi . ImageSpec { Image : "ubuntu" } ,
& jsonmessage . JSONError { Code : 502 , Message : "<!doctype html>\n<html class=\"no-js\" lang=\"\">\n <head>\n </head>\n <body>\n <h1>Oops, there was an error!</h1>\n <p>We have been contacted of this error, feel free to check out <a href=\"http://status.docker.com/\">status.docker.com</a>\n to see if there is a bigger issue.</p>\n\n </body>\n</html>" } ,
"RegistryUnavailable" ,
} ,
}
for key , test := range tests {
fakeDocker . InjectError ( "pull" , test . err )
2018-01-19 21:36:06 +00:00
_ , err := ds . PullImage ( getTestCTX ( ) , & runtimeapi . PullImageRequest { Image : test . image , Auth : & runtimeapi . AuthConfig { } } )
require . Error ( t , err , fmt . Sprintf ( "TestCase [%s]" , key ) )
2017-05-04 01:42:29 +00:00
assert . Contains ( t , err . Error ( ) , test . expectedError )
}
}