Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 026f5a1

Browse files
committed
Add unit test.
Signed-off-by: Lantao Liu <[email protected]>
1 parent 15b30d4 commit 026f5a1

File tree

5 files changed

+270
-0
lines changed

5 files changed

+270
-0
lines changed

hack/verify-lint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ for d in $(find . -type d -a \( -iwholename './pkg*' -o -iwholename './cmd*' \))
2323
--exclude='error return value not checked.*(Close|Log|Print).*\(errcheck\)$' \
2424
--exclude='.*_test\.go:.*error return value not checked.*\(errcheck\)$' \
2525
--exclude='duplicate of.*_test.go.*\(dupl\)$' \
26+
--exclude='.*/mock_.*\.go:.*\(golint\)$' \
2627
--disable=aligncheck \
2728
--disable=gotype \
2829
--disable=gas \

pkg/server/status_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package server
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/golang/mock/gomock"
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
"golang.org/x/net/context"
27+
healthapi "google.golang.org/grpc/health/grpc_health_v1"
28+
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
29+
30+
servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing"
31+
)
32+
33+
func TestStatus(t *testing.T) {
34+
ctrl := gomock.NewController(t)
35+
defer ctrl.Finish()
36+
for desc, test := range map[string]struct {
37+
checkRes *healthapi.HealthCheckResponse
38+
checkErr error
39+
expectedRuntimeReadiness bool
40+
}{
41+
"runtime should not be ready when containerd is not serving": {
42+
checkRes: &healthapi.HealthCheckResponse{
43+
Status: healthapi.HealthCheckResponse_NOT_SERVING,
44+
},
45+
expectedRuntimeReadiness: false,
46+
},
47+
"runtime should not be ready when containerd healthcheck returns error": {
48+
checkErr: errors.New("healthcheck error"),
49+
expectedRuntimeReadiness: false,
50+
},
51+
"runtime should be ready when containerd is serving": {
52+
checkRes: &healthapi.HealthCheckResponse{
53+
Status: healthapi.HealthCheckResponse_SERVING,
54+
},
55+
expectedRuntimeReadiness: true,
56+
},
57+
} {
58+
t.Logf("TestCase %q", desc)
59+
c := newTestCRIContainerdService()
60+
ctx := context.Background()
61+
mock := servertesting.NewMockHealthClient(ctrl)
62+
mock.EXPECT().Check(ctx, &healthapi.HealthCheckRequest{}).Return(test.checkRes, test.checkErr)
63+
64+
c.healthService = mock
65+
resp, err := c.Status(ctx, &runtime.StatusRequest{})
66+
assert.NoError(t, err)
67+
require.NotNil(t, resp)
68+
runtimeReady := resp.Status.Conditions[0]
69+
networkReady := resp.Status.Conditions[1]
70+
assert.Equal(t, runtime.RuntimeReady, runtimeReady.Type)
71+
assert.Equal(t, test.expectedRuntimeReadiness, runtimeReady.Status)
72+
if !test.expectedRuntimeReadiness {
73+
assert.Equal(t, runtimeNotReadyReason, runtimeReady.Reason)
74+
assert.NotEmpty(t, runtimeReady.Message)
75+
}
76+
assert.Equal(t, runtime.NetworkReady, networkReady.Type)
77+
assert.True(t, networkReady.Status)
78+
}
79+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Automatically generated by MockGen. DO NOT EDIT!
18+
// Source: google.golang.org/grpc/health/grpc_health_v1 (interfaces: HealthClient)
19+
20+
package testing
21+
22+
import (
23+
gomock "github.com/golang/mock/gomock"
24+
context "golang.org/x/net/context"
25+
grpc "google.golang.org/grpc"
26+
grpc_health_v1 "google.golang.org/grpc/health/grpc_health_v1"
27+
)
28+
29+
// Mock of HealthClient interface
30+
type MockHealthClient struct {
31+
ctrl *gomock.Controller
32+
recorder *_MockHealthClientRecorder
33+
}
34+
35+
// Recorder for MockHealthClient (not exported)
36+
type _MockHealthClientRecorder struct {
37+
mock *MockHealthClient
38+
}
39+
40+
func NewMockHealthClient(ctrl *gomock.Controller) *MockHealthClient {
41+
mock := &MockHealthClient{ctrl: ctrl}
42+
mock.recorder = &_MockHealthClientRecorder{mock}
43+
return mock
44+
}
45+
46+
func (_m *MockHealthClient) EXPECT() *_MockHealthClientRecorder {
47+
return _m.recorder
48+
}
49+
50+
func (_m *MockHealthClient) Check(_param0 context.Context, _param1 *grpc_health_v1.HealthCheckRequest, _param2 ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) {
51+
_s := []interface{}{_param0, _param1}
52+
for _, _x := range _param2 {
53+
_s = append(_s, _x)
54+
}
55+
ret := _m.ctrl.Call(_m, "Check", _s...)
56+
ret0, _ := ret[0].(*grpc_health_v1.HealthCheckResponse)
57+
ret1, _ := ret[1].(error)
58+
return ret0, ret1
59+
}
60+
61+
func (_mr *_MockHealthClientRecorder) Check(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
62+
_s := append([]interface{}{arg0, arg1}, arg2...)
63+
return _mr.mock.ctrl.RecordCall(_mr.mock, "Check", _s...)
64+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Automatically generated by MockGen. DO NOT EDIT!
18+
// Source: github.com/containerd/containerd/api/services/version (interfaces: VersionClient)
19+
20+
package testing
21+
22+
import (
23+
version "github.com/containerd/containerd/api/services/version"
24+
gomock "github.com/golang/mock/gomock"
25+
empty "github.com/golang/protobuf/ptypes/empty"
26+
context "golang.org/x/net/context"
27+
grpc "google.golang.org/grpc"
28+
)
29+
30+
// Mock of VersionClient interface
31+
type MockVersionClient struct {
32+
ctrl *gomock.Controller
33+
recorder *_MockVersionClientRecorder
34+
}
35+
36+
// Recorder for MockVersionClient (not exported)
37+
type _MockVersionClientRecorder struct {
38+
mock *MockVersionClient
39+
}
40+
41+
func NewMockVersionClient(ctrl *gomock.Controller) *MockVersionClient {
42+
mock := &MockVersionClient{ctrl: ctrl}
43+
mock.recorder = &_MockVersionClientRecorder{mock}
44+
return mock
45+
}
46+
47+
func (_m *MockVersionClient) EXPECT() *_MockVersionClientRecorder {
48+
return _m.recorder
49+
}
50+
51+
func (_m *MockVersionClient) Version(_param0 context.Context, _param1 *empty.Empty, _param2 ...grpc.CallOption) (*version.VersionResponse, error) {
52+
_s := []interface{}{_param0, _param1}
53+
for _, _x := range _param2 {
54+
_s = append(_s, _x)
55+
}
56+
ret := _m.ctrl.Call(_m, "Version", _s...)
57+
ret0, _ := ret[0].(*version.VersionResponse)
58+
ret1, _ := ret[1].(error)
59+
return ret0, ret1
60+
}
61+
62+
func (_mr *_MockVersionClientRecorder) Version(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call {
63+
_s := append([]interface{}{arg0, arg1}, arg2...)
64+
return _mr.mock.ctrl.RecordCall(_mr.mock, "Version", _s...)
65+
}

pkg/server/version_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package server
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
versionapi "github.com/containerd/containerd/api/services/version"
24+
"github.com/golang/mock/gomock"
25+
"github.com/golang/protobuf/ptypes/empty"
26+
"github.com/stretchr/testify/assert"
27+
"golang.org/x/net/context"
28+
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1"
29+
30+
servertesting "github.com/kubernetes-incubator/cri-containerd/pkg/server/testing"
31+
)
32+
33+
func TestVersion(t *testing.T) {
34+
ctrl := gomock.NewController(t)
35+
defer ctrl.Finish()
36+
// TODO(random-liu): Check containerd version after containerd fixes its version.
37+
for desc, test := range map[string]struct {
38+
versionRes *versionapi.VersionResponse
39+
versionErr error
40+
expectErr bool
41+
}{
42+
"should return error if containerd version returns error": {
43+
versionErr: errors.New("random error"),
44+
expectErr: true,
45+
},
46+
"should not return error if containerd version returns successfully": {
47+
versionRes: &versionapi.VersionResponse{Version: "1.1.1"},
48+
expectErr: false,
49+
},
50+
} {
51+
t.Logf("TestCase %q", desc)
52+
c := newTestCRIContainerdService()
53+
ctx := context.Background()
54+
mock := servertesting.NewMockVersionClient(ctrl)
55+
mock.EXPECT().Version(ctx, &empty.Empty{}).Return(test.versionRes, test.versionErr)
56+
57+
c.versionService = mock
58+
_, err := c.Version(ctx, &runtime.VersionRequest{})
59+
assert.Equal(t, test.expectErr, err != nil)
60+
}
61+
}

0 commit comments

Comments
 (0)