Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Add hyperTest and imagesTest #221

Merged
merged 1 commit into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 75 additions & 4 deletions pkg/hyper/fake_client_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package hyper

import (
"fmt"
"io"
"strings"
"sync"
"time"
Expand All @@ -36,6 +37,9 @@ type fakeClientInterface struct {
err error
containerInfoMap map[string]*types.ContainerInfo
podInfoMap map[string]*types.PodInfo
imageInfoList []*types.ImageInfo
version string
apiVersion string
}

func newFakeClientInterface(c clock.Clock) *fakeClientInterface {
Expand Down Expand Up @@ -67,6 +71,13 @@ func (f *fakeClientInterface) SetFakePod(pods []*FakePod) {
}
}

func (f *fakeClientInterface) SetVersion(version string, apiVersion string) {
f.Lock()
defer f.Unlock()
f.version = version
f.apiVersion = apiVersion
}

func (f *fakeClientInterface) CleanCalls() {
f.Lock()
defer f.Unlock()
Expand Down Expand Up @@ -153,7 +164,12 @@ func (f *fakeClientInterface) ContainerInfo(ctx context.Context, in *types.Conta
}

func (f *fakeClientInterface) ImageList(ctx context.Context, in *types.ImageListRequest, opts ...grpc.CallOption) (*types.ImageListResponse, error) {
return nil, fmt.Errorf("Not implemented")
f.Lock()
defer f.Unlock()
f.called = append(f.called, "ImageList")
return &types.ImageListResponse{
ImageList: f.imageInfoList,
}, f.err
}

func (f *fakeClientInterface) VMList(ctx context.Context, in *types.VMListRequest, opts ...grpc.CallOption) (*types.VMListResponse, error) {
Expand Down Expand Up @@ -305,16 +321,65 @@ func (f *fakeClientInterface) ServiceUpdate(ctx context.Context, in *types.Servi
return nil, fmt.Errorf("Not implemented")
}

type fakeAPIImagePullClient struct {
grpc.ClientStream
}

func (x *fakeAPIImagePullClient) Recv() (*types.ImagePullResponse, error) {
m := &types.ImagePullResponse{}
//Return "the image data has been read"
return m, io.EOF
}

func (f *fakeClientInterface) ImagePull(ctx context.Context, in *types.ImagePullRequest, opts ...grpc.CallOption) (types.PublicAPI_ImagePullClient, error) {
return nil, fmt.Errorf("Not implemented")
f.Lock()
defer f.Unlock()
f.called = append(f.called, "ImagePull")
repoSep := ":"
id := in.Tag
if strings.Index(in.Tag, ":") > 0 {
repoSep = "@"
str := strings.Split(in.Tag, ":")
id = str[1]
}
repoTags := []string{
fmt.Sprintf("%s%s%s", in.Image, repoSep, in.Tag),
}
imageInfo := &types.ImageInfo{
Id: id,
RepoTags: repoTags,
VirtualSize: 0,
}
f.imageInfoList = append(f.imageInfoList, imageInfo)
return &fakeAPIImagePullClient{}, f.err
}

func (f *fakeClientInterface) ImagePush(ctx context.Context, in *types.ImagePushRequest, opts ...grpc.CallOption) (types.PublicAPI_ImagePushClient, error) {
return nil, fmt.Errorf("Not implemented")
}

func (f *fakeClientInterface) ImageRemove(ctx context.Context, in *types.ImageRemoveRequest, opts ...grpc.CallOption) (*types.ImageRemoveResponse, error) {
return nil, fmt.Errorf("Not implemented")
f.Lock()
defer f.Unlock()
f.called = append(f.called, "ImageRemove")
tag := ""
for i, image := range f.imageInfoList {
for _, im := range image.RepoTags {
if im == in.Image {
tag = f.imageInfoList[i].Id
//In this test,one imageId has only one tag,while deleting the tag,we also delete the image
f.imageInfoList = append(f.imageInfoList[:i], f.imageInfoList[i+1:]...)
}
}
}
imageDelete := &types.ImageDelete{
Untaged: in.Image,
Deleted: tag,
}
images := []*types.ImageDelete{
imageDelete,
}
return &types.ImageRemoveResponse{Images: images}, f.err
}

func (f *fakeClientInterface) Ping(ctx context.Context, in *types.PingRequest, opts ...grpc.CallOption) (*types.PingResponse, error) {
Expand All @@ -326,7 +391,13 @@ func (f *fakeClientInterface) Info(ctx context.Context, in *types.InfoRequest, o
}

func (f *fakeClientInterface) Version(ctx context.Context, in *types.VersionRequest, opts ...grpc.CallOption) (*types.VersionResponse, error) {
return nil, fmt.Errorf("Not implemented")
f.Lock()
defer f.Unlock()
f.called = append(f.called, "Version")
return &types.VersionResponse{
Version: f.version,
ApiVersion: f.apiVersion,
}, f.err
}

// dockerTimestampToString converts the timestamp to string
Expand Down
76 changes: 76 additions & 0 deletions pkg/hyper/hyper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ limitations under the License.
package hyper

import (
"fmt"
"sync"
"testing"
"time"

cnitypes "github.com/containernetworking/cni/pkg/types"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/clock"
kubeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)

func newTestRuntime() (*Runtime, *fakeClientInterface, *clock.FakeClock) {
Expand All @@ -32,3 +38,73 @@ func newTestRuntime() (*Runtime, *fakeClientInterface, *clock.FakeClock) {
client: client,
}, publicClient, fakeClock
}

type fakeCNIPlugin struct {
sync.Mutex
name string
status error
}

func (f *fakeCNIPlugin) Status() error {
f.Lock()
defer f.Unlock()
return f.status
}

func (f *fakeCNIPlugin) Name() string {
f.Lock()
defer f.Unlock()
return f.name
}

func (f *fakeCNIPlugin) SetUpPod(podNetnsPath string, podID string, metadata *kubeapi.PodSandboxMetadata, annotations map[string]string, capabilities map[string]interface{}) (cnitypes.Result, error) {
return nil, fmt.Errorf("Not implemented")
}

func (f *fakeCNIPlugin) TearDownPod(podNetnsPath string, podID string, metadata *kubeapi.PodSandboxMetadata, annotations map[string]string, capabilities map[string]interface{}) error {
return fmt.Errorf("Not implemented")
}

func TestVersion(t *testing.T) {
r, fakeClient, _ := newTestRuntime()
kubeApiVersion := "kube-v1"
version, apiVersion := "v1", "api-v1"
//Set the version
fakeClient.SetVersion(version, apiVersion)
expected := &kubeapi.VersionResponse{
Version: kubeApiVersion,
RuntimeName: hyperRuntimeName,
RuntimeVersion: version,
RuntimeApiVersion: apiVersion,
}
//Get the version
versionEx, err := r.Version(kubeApiVersion)
assert.NoError(t, err)
assert.Equal(t, expected, versionEx)
}

func TestStatus(t *testing.T) {
r, fakeClient, _ := newTestRuntime()
runtimeStatus := true
networkStatus := true
version, apiVersion := "v1", "api-v1"
//Set the version
fakeClient.SetVersion(version, apiVersion)
r.netPlugin = &fakeCNIPlugin{
status: nil,
}
runtimeReady := &kubeapi.RuntimeCondition{
Type: kubeapi.RuntimeReady,
Status: runtimeStatus,
}
networkReady := &kubeapi.RuntimeCondition{
Type: kubeapi.NetworkReady,
Status: networkStatus,
}
conditions := []*kubeapi.RuntimeCondition{runtimeReady, networkReady}
expected := &kubeapi.RuntimeStatus{Conditions: conditions}
//Get the status
status, err := r.Status()
assert.NoError(t, err)
assert.Equal(t, expected, status)
}
116 changes: 116 additions & 0 deletions pkg/hyper/images_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright 2017 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 hyper

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

kubeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)

func TestPullImage(t *testing.T) {
r, _, _ := newTestRuntime()
imageFullName := []string{
"localhost:5000/foo/bar@sha256:12345",
"test/foo:54321",
}
for i := range imageFullName {
imageSpec := &kubeapi.ImageSpec{
Image: imageFullName[i],
}
id, err := r.PullImage(imageSpec, nil)
assert.NoError(t, err)
str := strings.Split(imageFullName[i], ":")
expected := str[len(str)-1]
assert.Equal(t, expected, id)
}

}

func TestListImage(t *testing.T) {
r, fakeClient, _ := newTestRuntime()
imageFullName := []string{
"localhost:5000/foo/bar@sha256:12345",
"test/foo:54321",
}
expected := []*kubeapi.Image{}
for i := range imageFullName {
imageSpec := &kubeapi.ImageSpec{
Image: imageFullName[i],
}
id, err := r.PullImage(imageSpec, nil)
assert.NoError(t, err)
repoTages := []string{}
repoTages = append(repoTages, imageFullName[i])
image := kubeapi.Image{
Id: id,
RepoTags: repoTages,
RepoDigests: nil,
Size_: 0,
}
expected = append(expected, &image)
}
fliter := kubeapi.ImageFilter{}
//Test list image
images, err := r.ListImages(&fliter)
assert.NoError(t, err)
assert.Equal(t, expected, images)
//Test remove image
assert.Len(t, fakeClient.imageInfoList, len(imageFullName))
image := &kubeapi.ImageSpec{
Image: "localhost:5000/foo/bar@sha256:12345",
}
err = r.RemoveImage(image)
assert.NoError(t, err)
assert.Len(t, fakeClient.imageInfoList, len(imageFullName)-1)

}

func TestImageStatus(t *testing.T) {
r, _, _ := newTestRuntime()
imageFullName := []string{
"localhost:5000/foo/bar@sha256:12345",
"test/foo:54321",
}
for i := range imageFullName {
imageSpec := &kubeapi.ImageSpec{
Image: imageFullName[i],
}
//Pull the image for test
id, err := r.PullImage(imageSpec, nil)
assert.NoError(t, err)

imageSpec = &kubeapi.ImageSpec{
Image: imageFullName[i],
}
//Get the image's status
image, err := r.ImageStatus(imageSpec)
assert.NoError(t, err)
repoTages := []string{}
repoTages = append(repoTages, imageFullName[i])
expected := &kubeapi.Image{
Id: id,
RepoTags: repoTages,
RepoDigests: nil,
Size_: 0,
}
assert.Equal(t, image, expected)
}
}