2016-07-25 21:39:31 +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 (
|
2016-11-02 03:20:13 +00:00
|
|
|
"errors"
|
2016-10-29 00:14:33 +00:00
|
|
|
"testing"
|
2016-08-30 00:40:28 +00:00
|
|
|
"time"
|
|
|
|
|
2017-02-03 02:28:19 +00:00
|
|
|
"github.com/blang/semver"
|
2017-06-29 20:21:17 +00:00
|
|
|
dockertypes "github.com/docker/docker/api/types"
|
2016-11-02 03:20:13 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-02-03 02:28:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-11-02 03:20:13 +00:00
|
|
|
|
2017-05-19 17:57:39 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/clock"
|
2017-06-07 08:54:28 +00:00
|
|
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2016-10-14 18:52:18 +00:00
|
|
|
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
|
2017-05-03 17:46:35 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
|
2016-10-29 00:14:33 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/network"
|
2016-12-13 19:54:08 +00:00
|
|
|
nettest "k8s.io/kubernetes/pkg/kubelet/network/testing"
|
2017-02-03 02:28:19 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/util/cache"
|
2016-07-25 21:39:31 +00:00
|
|
|
)
|
|
|
|
|
2016-10-29 00:14:33 +00:00
|
|
|
// newTestNetworkPlugin returns a mock plugin that implements network.NetworkPlugin
|
2016-12-13 19:54:08 +00:00
|
|
|
func newTestNetworkPlugin(t *testing.T) *nettest.MockNetworkPlugin {
|
2016-10-29 00:14:33 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
2016-12-13 19:54:08 +00:00
|
|
|
return nettest.NewMockNetworkPlugin(ctrl)
|
2016-10-29 00:14:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:46:35 +00:00
|
|
|
func newTestDockerService() (*dockerService, *libdocker.FakeDockerClient, *clock.FakeClock) {
|
2016-08-30 00:40:28 +00:00
|
|
|
fakeClock := clock.NewFakeClock(time.Time{})
|
2017-05-03 17:46:35 +00:00
|
|
|
c := libdocker.NewFakeDockerClient().WithClock(fakeClock).WithVersion("1.11.2", "1.23")
|
2016-12-13 22:19:04 +00:00
|
|
|
pm := network.NewPluginManager(&network.NoopNetworkPlugin{})
|
2017-04-05 00:31:54 +00:00
|
|
|
return &dockerService{
|
|
|
|
client: c,
|
|
|
|
os: &containertest.FakeOS{},
|
|
|
|
network: pm,
|
|
|
|
checkpointHandler: NewTestPersistentCheckpointHandler(),
|
|
|
|
networkReady: make(map[string]bool),
|
|
|
|
}, c, fakeClock
|
2016-07-25 21:39:31 +00:00
|
|
|
}
|
2016-11-02 03:20:13 +00:00
|
|
|
|
2017-05-03 17:46:35 +00:00
|
|
|
func newTestDockerServiceWithVersionCache() (*dockerService, *libdocker.FakeDockerClient, *clock.FakeClock) {
|
2017-02-03 02:28:19 +00:00
|
|
|
ds, c, fakeClock := newTestDockerService()
|
|
|
|
ds.versionCache = cache.NewObjectCache(
|
|
|
|
func() (interface{}, error) {
|
|
|
|
return ds.getDockerVersion()
|
|
|
|
},
|
|
|
|
time.Hour*10,
|
|
|
|
)
|
|
|
|
return ds, c, fakeClock
|
|
|
|
}
|
|
|
|
|
2016-11-02 03:20:13 +00:00
|
|
|
// TestStatus tests the runtime status logic.
|
|
|
|
func TestStatus(t *testing.T) {
|
|
|
|
ds, fDocker, _ := newTestDockerService()
|
|
|
|
|
2016-11-30 07:27:27 +00:00
|
|
|
assertStatus := func(expected map[string]bool, status *runtimeapi.RuntimeStatus) {
|
2016-11-02 03:20:13 +00:00
|
|
|
conditions := status.GetConditions()
|
|
|
|
assert.Equal(t, len(expected), len(conditions))
|
|
|
|
for k, v := range expected {
|
|
|
|
for _, c := range conditions {
|
2017-01-20 01:55:37 +00:00
|
|
|
if k == c.Type {
|
|
|
|
assert.Equal(t, v, c.Status)
|
2016-11-02 03:20:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should report ready status if version returns no error.
|
|
|
|
status, err := ds.Status()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assertStatus(map[string]bool{
|
2016-11-30 07:27:27 +00:00
|
|
|
runtimeapi.RuntimeReady: true,
|
|
|
|
runtimeapi.NetworkReady: true,
|
2016-11-02 03:20:13 +00:00
|
|
|
}, status)
|
|
|
|
|
|
|
|
// Should not report ready status if version returns error.
|
|
|
|
fDocker.InjectError("version", errors.New("test error"))
|
|
|
|
status, err = ds.Status()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assertStatus(map[string]bool{
|
2016-11-30 07:27:27 +00:00
|
|
|
runtimeapi.RuntimeReady: false,
|
|
|
|
runtimeapi.NetworkReady: true,
|
2016-11-02 03:20:13 +00:00
|
|
|
}, status)
|
2016-11-03 01:23:57 +00:00
|
|
|
|
|
|
|
// Should not report ready status is network plugin returns error.
|
|
|
|
mockPlugin := newTestNetworkPlugin(t)
|
2016-12-13 22:19:04 +00:00
|
|
|
ds.network = network.NewPluginManager(mockPlugin)
|
2016-11-03 01:23:57 +00:00
|
|
|
defer mockPlugin.Finish()
|
|
|
|
mockPlugin.EXPECT().Status().Return(errors.New("network error"))
|
|
|
|
status, err = ds.Status()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assertStatus(map[string]bool{
|
2016-11-30 07:27:27 +00:00
|
|
|
runtimeapi.RuntimeReady: true,
|
|
|
|
runtimeapi.NetworkReady: false,
|
2016-11-03 01:23:57 +00:00
|
|
|
}, status)
|
2016-11-02 03:20:13 +00:00
|
|
|
}
|
2017-02-03 02:28:19 +00:00
|
|
|
|
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
ds, _, _ := newTestDockerService()
|
|
|
|
|
|
|
|
expectedVersion := &dockertypes.Version{Version: "1.11.2", APIVersion: "1.23.0"}
|
|
|
|
v, err := ds.getDockerVersion()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedVersion, v)
|
|
|
|
|
|
|
|
expectedAPIVersion := &semver.Version{Major: 1, Minor: 23, Patch: 0}
|
|
|
|
apiVersion, err := ds.getDockerAPIVersion()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedAPIVersion, apiVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIVersionWithCache(t *testing.T) {
|
|
|
|
ds, _, _ := newTestDockerServiceWithVersionCache()
|
|
|
|
|
|
|
|
expected := &semver.Version{Major: 1, Minor: 23, Patch: 0}
|
|
|
|
version, err := ds.getDockerAPIVersion()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, version)
|
|
|
|
}
|