Skip to content

Commit 8d2504a

Browse files
committed
add UT for snapshot controller
1 parent b4d60c9 commit 8d2504a

File tree

4 files changed

+947
-0
lines changed

4 files changed

+947
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright 2018 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 controller
18+
19+
import (
20+
crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1alpha1"
21+
"k8s.io/client-go/tools/cache"
22+
"testing"
23+
)
24+
25+
func storeVersion(t *testing.T, prefix string, c cache.Store, version string, expectedReturn bool) {
26+
content := newContent("contentName", classEmpty, "sid1-1", "vuid1-1", "volume1-1", "snapuid1-1", "snap1-1", nil, nil)
27+
content.ResourceVersion = version
28+
ret, err := storeObjectUpdate(c, content, "content")
29+
if err != nil {
30+
t.Errorf("%s: expected storeObjectUpdate to succeed, got: %v", prefix, err)
31+
}
32+
if expectedReturn != ret {
33+
t.Errorf("%s: expected storeObjectUpdate to return %v, got: %v", prefix, expectedReturn, ret)
34+
}
35+
36+
// find the stored version
37+
38+
contentObj, found, err := c.GetByKey("contentName")
39+
if err != nil {
40+
t.Errorf("expected content 'contentName' in the cache, got error instead: %v", err)
41+
}
42+
if !found {
43+
t.Errorf("expected content 'contentName' in the cache but it was not found")
44+
}
45+
content, ok := contentObj.(*crdv1.VolumeSnapshotContent)
46+
if !ok {
47+
t.Errorf("expected content in the cache, got different object instead: %#v", contentObj)
48+
}
49+
50+
if ret {
51+
if content.ResourceVersion != version {
52+
t.Errorf("expected content with version %s in the cache, got %s instead", version, content.ResourceVersion)
53+
}
54+
} else {
55+
if content.ResourceVersion == version {
56+
t.Errorf("expected content with version other than %s in the cache, got %s instead", version, content.ResourceVersion)
57+
}
58+
}
59+
}
60+
61+
// TestControllerCache tests func storeObjectUpdate()
62+
func TestControllerCache(t *testing.T) {
63+
// Cache under test
64+
c := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
65+
66+
// Store new PV
67+
storeVersion(t, "Step1", c, "1", true)
68+
// Store the same PV
69+
storeVersion(t, "Step2", c, "1", true)
70+
// Store newer PV
71+
storeVersion(t, "Step3", c, "2", true)
72+
// Store older PV - simulating old "PV updated" event or periodic sync with
73+
// old data
74+
storeVersion(t, "Step4", c, "1", false)
75+
// Store newer PV - test integer parsing ("2" > "10" as string,
76+
// while 2 < 10 as integers)
77+
storeVersion(t, "Step5", c, "10", true)
78+
}
79+
80+
func TestControllerCacheParsingError(t *testing.T) {
81+
c := cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
82+
// There must be something in the cache to compare with
83+
storeVersion(t, "Step1", c, "1", true)
84+
85+
content := newContent("contentName", classEmpty, "sid1-1", "vuid1-1", "volume1-1", "snapuid1-1", "snap1-1", nil, nil)
86+
content.ResourceVersion = "xxx"
87+
_, err := storeObjectUpdate(c, content, "content")
88+
if err == nil {
89+
t.Errorf("Expected parsing error, got nil instead")
90+
}
91+
}

0 commit comments

Comments
 (0)