Skip to content

Commit 57df625

Browse files
committed
add unit tests
1 parent 3916c4a commit 57df625

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ go_test(
9898
"azure_storage_test.go",
9999
"azure_storageaccount_test.go",
100100
"azure_test.go",
101+
"azure_utils_test.go",
101102
"azure_vmss_cache_test.go",
102103
"azure_vmss_test.go",
103104
"azure_wrap_test.go",

staging/src/k8s.io/legacy-cloud-providers/azure/azure_controller_common.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func (c *controllerCommon) AttachDisk(isManagedDisk bool, diskName, diskURI stri
142142
return -1, fmt.Errorf("failed to get azure instance id for node %q (%v)", nodeName, err)
143143
}
144144

145-
c.vmLockMap.LockEntry(string(nodeName))
146-
defer c.vmLockMap.UnlockEntry(string(nodeName))
145+
c.vmLockMap.LockEntry(strings.ToLower(string(nodeName)))
146+
defer c.vmLockMap.UnlockEntry(strings.ToLower(string(nodeName)))
147147

148148
lun, err := c.GetNextDiskLun(nodeName)
149149
if err != nil {
@@ -179,20 +179,20 @@ func (c *controllerCommon) DetachDisk(diskName, diskURI string, nodeName types.N
179179
klog.V(2).Infof("detach %v from node %q", diskURI, nodeName)
180180

181181
// make the lock here as small as possible
182-
c.vmLockMap.LockEntry(string(nodeName))
182+
c.vmLockMap.LockEntry(strings.ToLower(string(nodeName)))
183183
c.diskAttachDetachMap.Store(strings.ToLower(diskURI), "detaching")
184184
resp, err := vmset.DetachDisk(diskName, diskURI, nodeName)
185185
c.diskAttachDetachMap.Delete(strings.ToLower(diskURI))
186-
c.vmLockMap.UnlockEntry(string(nodeName))
186+
c.vmLockMap.UnlockEntry(strings.ToLower(string(nodeName)))
187187

188188
if c.cloud.CloudProviderBackoff && shouldRetryHTTPRequest(resp, err) {
189189
klog.V(2).Infof("azureDisk - update backing off: detach disk(%s, %s), err: %v", diskName, diskURI, err)
190190
retryErr := kwait.ExponentialBackoff(c.cloud.RequestBackoff(), func() (bool, error) {
191-
c.vmLockMap.LockEntry(string(nodeName))
191+
c.vmLockMap.LockEntry(strings.ToLower(string(nodeName)))
192192
c.diskAttachDetachMap.Store(strings.ToLower(diskURI), "detaching")
193193
resp, err := vmset.DetachDisk(diskName, diskURI, nodeName)
194194
c.diskAttachDetachMap.Delete(strings.ToLower(diskURI))
195-
c.vmLockMap.UnlockEntry(string(nodeName))
195+
c.vmLockMap.UnlockEntry(strings.ToLower(string(nodeName)))
196196
return c.cloud.processHTTPRetryResponse(nil, "", resp, err)
197197
})
198198
if retryErr != nil {

staging/src/k8s.io/legacy-cloud-providers/azure/azure_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// +build !providerless
22

33
/*
4-
Copyright 2018 The Kubernetes Authors.
4+
Copyright 2019 The Kubernetes Authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// +build !providerless
2+
3+
/*
4+
Copyright 2019 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package azure
20+
21+
import (
22+
"testing"
23+
"time"
24+
)
25+
26+
func TestSimpleLockEntry(t *testing.T) {
27+
testLockMap := newLockMap()
28+
29+
callbackChan1 := make(chan interface{})
30+
go testLockMap.lockAndCallback(t, "entry1", callbackChan1)
31+
ensureCallbackHappens(t, callbackChan1)
32+
}
33+
34+
func TestSimpleLockUnlockEntry(t *testing.T) {
35+
testLockMap := newLockMap()
36+
37+
callbackChan1 := make(chan interface{})
38+
go testLockMap.lockAndCallback(t, "entry1", callbackChan1)
39+
ensureCallbackHappens(t, callbackChan1)
40+
testLockMap.UnlockEntry("entry1")
41+
}
42+
43+
func TestConcurrentLockEntry(t *testing.T) {
44+
testLockMap := newLockMap()
45+
46+
callbackChan1 := make(chan interface{})
47+
callbackChan2 := make(chan interface{})
48+
49+
go testLockMap.lockAndCallback(t, "entry1", callbackChan1)
50+
ensureCallbackHappens(t, callbackChan1)
51+
52+
go testLockMap.lockAndCallback(t, "entry1", callbackChan2)
53+
ensureNoCallback(t, callbackChan2)
54+
55+
testLockMap.UnlockEntry("entry1")
56+
ensureCallbackHappens(t, callbackChan2)
57+
testLockMap.UnlockEntry("entry1")
58+
}
59+
60+
func (lm *lockMap) lockAndCallback(t *testing.T, entry string, callbackChan chan<- interface{}) {
61+
lm.LockEntry(entry)
62+
callbackChan <- true
63+
}
64+
65+
var callbackTimeout = 2 * time.Second
66+
67+
func ensureCallbackHappens(t *testing.T, callbackChan <-chan interface{}) bool {
68+
select {
69+
case <-callbackChan:
70+
return true
71+
case <-time.After(callbackTimeout):
72+
t.Fatalf("timed out waiting for callback")
73+
return false
74+
}
75+
}
76+
77+
func ensureNoCallback(t *testing.T, callbackChan <-chan interface{}) bool {
78+
select {
79+
case <-callbackChan:
80+
t.Fatalf("unexpected callback")
81+
return false
82+
case <-time.After(callbackTimeout):
83+
return true
84+
}
85+
}

0 commit comments

Comments
 (0)