From 81bb56146726a0a2073e9af6bcc32de606ead4f5 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Wed, 4 Apr 2018 16:11:32 +0800 Subject: [PATCH] proxy/shim: add unit tests To test built-in proxy and shim types. Signed-off-by: Peng Tao --- virtcontainers/podlist_test.go | 42 ++++++++++++++++++++++++++++++++++ virtcontainers/proxy_test.go | 15 ++++++++++++ virtcontainers/shim_test.go | 19 +++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 virtcontainers/podlist_test.go diff --git a/virtcontainers/podlist_test.go b/virtcontainers/podlist_test.go new file mode 100644 index 0000000000..2fc0d27ff4 --- /dev/null +++ b/virtcontainers/podlist_test.go @@ -0,0 +1,42 @@ +// +// Copyright (c) 2018 HyperHQ Inc. +// +// 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 virtcontainers + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPodListOperations(t *testing.T) { + p := &Pod{id: "testpodListpod"} + l := &podList{pods: make(map[string]*Pod)} + err := l.addPod(p) + assert.Nil(t, err, "addPod failed") + + err = l.addPod(p) + assert.NotNil(t, err, "add same pod should fail") + + np, err := l.lookupPod(p.id) + assert.Nil(t, err, "lookupPod failed") + assert.Equal(t, np, p, "lookupPod returns different pod %v:%v", np, p) + + _, err = l.lookupPod("some-non-existing-pod-name") + assert.NotNil(t, err, "lookupPod for non-existing pod should fail") + + l.removePod(p.id) +} diff --git a/virtcontainers/proxy_test.go b/virtcontainers/proxy_test.go index 2b466c5c12..2d4fa38fd6 100644 --- a/virtcontainers/proxy_test.go +++ b/virtcontainers/proxy_test.go @@ -52,6 +52,10 @@ func TestSetNoProxyType(t *testing.T) { testSetProxyType(t, "noProxy", NoProxyType) } +func TestSetKataBuiltInProxyType(t *testing.T) { + testSetProxyType(t, "kataBuiltInProxy", KataBuiltInProxyType) +} + func TestSetUnknownProxyType(t *testing.T) { var proxyType ProxyType @@ -97,6 +101,11 @@ func TestStringFromNoopProxyType(t *testing.T) { testStringFromProxyType(t, proxyType, "noopProxy") } +func TestStringFromKataBuiltInProxyType(t *testing.T) { + proxyType := KataBuiltInProxyType + testStringFromProxyType(t, proxyType, "kataBuiltInProxy") +} + func TestStringFromUnknownProxyType(t *testing.T) { var proxyType ProxyType testStringFromProxyType(t, proxyType, "") @@ -137,6 +146,12 @@ func TestNewProxyFromNoopProxyType(t *testing.T) { testNewProxyFromProxyType(t, proxyType, expectedProxy) } +func TestNewProxyFromKataBuiltInProxyType(t *testing.T) { + proxyType := KataBuiltInProxyType + expectedProxy := &kataBuiltInProxy{} + testNewProxyFromProxyType(t, proxyType, expectedProxy) +} + func TestNewProxyFromUnknownProxyType(t *testing.T) { var proxyType ProxyType diff --git a/virtcontainers/shim_test.go b/virtcontainers/shim_test.go index 77753be06d..73c21cd591 100644 --- a/virtcontainers/shim_test.go +++ b/virtcontainers/shim_test.go @@ -89,6 +89,11 @@ func TestStringFromNoopShimType(t *testing.T) { testStringFromShimType(t, shimType, "noopShim") } +func TestStringFromKataBuiltInShimType(t *testing.T) { + shimType := KataBuiltInShimType + testStringFromShimType(t, shimType, "kataBuiltInShim") +} + func TestStringFromUnknownShimType(t *testing.T) { var shimType ShimType testStringFromShimType(t, shimType, "") @@ -123,6 +128,12 @@ func TestNewShimFromNoopShimType(t *testing.T) { testNewShimFromShimType(t, shimType, expectedShim) } +func TestNewShimFromKataBuiltInShimType(t *testing.T) { + shimType := KataBuiltInShimType + expectedShim := &kataBuiltInShim{} + testNewShimFromShimType(t, shimType, expectedShim) +} + func TestNewShimFromUnknownShimType(t *testing.T) { var shimType ShimType @@ -170,6 +181,14 @@ func TestNewShimConfigFromNoopShimPodConfig(t *testing.T) { testNewShimConfigFromPodConfig(t, podConfig, nil) } +func TestNewShimConfigFromKataBuiltInShimPodConfig(t *testing.T) { + podConfig := PodConfig{ + ShimType: KataBuiltInShimType, + } + + testNewShimConfigFromPodConfig(t, podConfig, nil) +} + func TestNewShimConfigFromUnknownShimPodConfig(t *testing.T) { var shimType ShimType