From 9426d63ddd3ac7a3a837fc61f847f88a9ee76bf2 Mon Sep 17 00:00:00 2001 From: Lv Jiawei Date: Thu, 1 Jul 2021 12:17:01 +0800 Subject: [PATCH] Add StorageWrapper test case. (#369) --- .gitignore | 2 + pkg/yurthub/cachemanager/storage_wrapper.go | 4 +- .../cachemanager/storage_wrapper_test.go | 174 ++++++++++++++++++ 3 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 pkg/yurthub/cachemanager/storage_wrapper_test.go diff --git a/.gitignore b/.gitignore index 70e7b4c768a..3f6059de10c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,7 @@ _output gopath dockerbuild +vendor +.vscode *.out logs diff --git a/pkg/yurthub/cachemanager/storage_wrapper.go b/pkg/yurthub/cachemanager/storage_wrapper.go index 6162c1cd82e..f17eed38095 100644 --- a/pkg/yurthub/cachemanager/storage_wrapper.go +++ b/pkg/yurthub/cachemanager/storage_wrapper.go @@ -114,7 +114,7 @@ func (sw *storageWrapper) Get(key string) (runtime.Object, error) { } } - b, err := sw.store.Get(key) + b, err := sw.GetRaw(key) if err != nil { return nil, err } else if len(b) == 0 { @@ -203,7 +203,7 @@ func (sw *storageWrapper) Update(key string, obj runtime.Object) error { return err } - if err := sw.store.Update(key, buf.Bytes()); err != nil { + if err := sw.UpdateRaw(key, buf.Bytes()); err != nil { return err } diff --git a/pkg/yurthub/cachemanager/storage_wrapper_test.go b/pkg/yurthub/cachemanager/storage_wrapper_test.go new file mode 100644 index 00000000000..78a25f43385 --- /dev/null +++ b/pkg/yurthub/cachemanager/storage_wrapper_test.go @@ -0,0 +1,174 @@ +/* +Copyright 2021 The OpenYurt 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 cachemanager + +import ( + "fmt" + "os" + "testing" + "time" + + "github.com/openyurtio/openyurt/pkg/yurthub/storage" + "github.com/openyurtio/openyurt/pkg/yurthub/storage/disk" + + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" +) + +func clearDir(dir string) error { + return os.RemoveAll(dir) +} + +var testPod = runtime.Object(&v1.Pod{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "Pod", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "mypod1", + Namespace: "default", + ResourceVersion: "1", + }, +}) + +func TestStorageWrapper(t *testing.T) { + dir := fmt.Sprintf("%s-%d", rootDir, time.Now().Unix()) + + defer clearDir(dir) + + dStorage, err := disk.NewDiskStorage(dir) + if err != nil { + t.Errorf("failed to create disk storage, %v", err) + } + sWrapper := NewStorageWrapper(dStorage) + + t.Run("Test create storage", func(t *testing.T) { + err = sWrapper.Create("kubelet/pods/default/mypod1", testPod) + if err != nil { + t.Errorf("failed to create obj, %v", err) + } + obj, err := sWrapper.Get("kubelet/pods/default/mypod1") + if err != nil { + t.Errorf("failed to create obj, %v", err) + } + accessor := meta.NewAccessor() + name, _ := accessor.Name(obj) + if name != "mypod1" { + t.Errorf("the name is not expected, expect mypod1, get %s", name) + } + }) + + t.Run("Test update storage", func(t *testing.T) { + updatePod := runtime.Object(&v1.Pod{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "Pod", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "mypod1", + Namespace: "default", + ResourceVersion: "1", + Labels: map[string]string{ + "tag": "test", + }, + }, + }) + err = sWrapper.Update("kubelet/pods/default/mypod1", updatePod) + if err != nil { + t.Errorf("failed to update obj, %v", err) + } + obj, err := sWrapper.Get("kubelet/pods/default/mypod1") + if err != nil { + t.Errorf("unexpected error, %v", err) + } + accessor := meta.NewAccessor() + labels, _ := accessor.Labels(obj) + if vaule, ok := labels["tag"]; ok { + if vaule != "test" { + t.Errorf("failed to get label, expect test, get %s", vaule) + } + } else { + t.Errorf("unexpected error, the label `tag` is not existed") + } + + }) + + t.Run("Test list keys and obj", func(t *testing.T) { + // test an exist key + keys, err := sWrapper.ListKeys("kubelet/pods/default") + if err != nil { + t.Errorf("failed to list keys, %v", err) + } + if len(keys) != 1 { + t.Errorf("the length of keys is not expected, expect 1, get %d", len(keys)) + } + + // test a not exist key + _, err = sWrapper.ListKeys("kubelet/pods/test") + if err != nil { + t.Errorf("failed to list keys, %v", err) + } + + // test list obj + _, err = sWrapper.List("kubelet/pods/default") + if err != nil { + t.Errorf("failed to list obj, %v", err) + } + }) + + t.Run("Test replace obj", func(t *testing.T) { + err = sWrapper.Replace("kubelet/pods/default", map[string]runtime.Object{ + "kubelet/pods/default/mypod1": runtime.Object(&v1.Pod{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "Pod", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "mypod1", + Namespace: "default", + ResourceVersion: "1", + Labels: map[string]string{ + "tag": "test", + }, + }, + }), + }) + if err != nil { + t.Errorf("failed to replace objs, %v", err) + } + }) + + t.Run("Test delete storage", func(t *testing.T) { + err = sWrapper.Delete("kubelet/pods/default/mypod1") + if err != nil { + t.Errorf("failed to delete obj, %v", err) + } + _, err = sWrapper.Get("kubelet/pods/default/mypod1") + if err != storage.ErrStorageNotFound { + t.Errorf("unexpected error, %v", err) + } + }) + + t.Run("Test list obj in empty path", func(t *testing.T) { + _, err = sWrapper.List("kubelet/pods/default") + if err != storage.ErrStorageNotFound { + t.Errorf("failed to list obj, %v", err) + } + }) +}