Skip to content

Commit 30f1bf2

Browse files
committed
Add unit tests
1 parent 34e898a commit 30f1bf2

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
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
@@ -100,6 +100,7 @@ go_test(
100100
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
101101
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
102102
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
103+
"//staging/src/k8s.io/client-go/tools/record:go_default_library",
103104
"//staging/src/k8s.io/cloud-provider:go_default_library",
104105
"//staging/src/k8s.io/cloud-provider/service/helpers:go_default_library",
105106
"//staging/src/k8s.io/legacy-cloud-providers/azure/auth:go_default_library",

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,12 @@ func (t *timedCache) Delete(key string) error {
122122
key: key,
123123
})
124124
}
125+
126+
// Set sets the data cache for the key.
127+
// It is only used for testing.
128+
func (t *timedCache) Set(key string, data interface{}) {
129+
t.store.Add(&cacheEntry{
130+
key: key,
131+
data: data,
132+
})
133+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ import (
3636
"github.com/Azure/go-autorest/autorest/to"
3737
)
3838

39+
var (
40+
errPreconditionFailedEtagMismatch = fmt.Errorf("PreconditionFailedEtagMismatch")
41+
)
42+
3943
type fakeAzureLBClient struct {
4044
mutex *sync.Mutex
4145
FakeStore map[string]map[string]network.LoadBalancer
@@ -417,13 +421,21 @@ func newFakeAzureNSGClient() *fakeAzureNSGClient {
417421
return fNSG
418422
}
419423

420-
func (fNSG *fakeAzureNSGClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, parameters network.SecurityGroup) (resp *http.Response, err error) {
424+
func (fNSG *fakeAzureNSGClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, parameters network.SecurityGroup, etag string) (resp *http.Response, err error) {
421425
fNSG.mutex.Lock()
422426
defer fNSG.mutex.Unlock()
423427

424428
if _, ok := fNSG.FakeStore[resourceGroupName]; !ok {
425429
fNSG.FakeStore[resourceGroupName] = make(map[string]network.SecurityGroup)
426430
}
431+
432+
if nsg, ok := fNSG.FakeStore[resourceGroupName][networkSecurityGroupName]; ok {
433+
if etag != "" && to.String(nsg.Etag) != "" && etag != to.String(nsg.Etag) {
434+
return &http.Response{
435+
StatusCode: http.StatusPreconditionFailed,
436+
}, errPreconditionFailedEtagMismatch
437+
}
438+
}
427439
fNSG.FakeStore[resourceGroupName][networkSecurityGroupName] = parameters
428440

429441
return nil, nil

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/types"
3232
"k8s.io/apimachinery/pkg/util/sets"
33+
"k8s.io/client-go/tools/record"
3334
servicehelpers "k8s.io/cloud-provider/service/helpers"
3435
"k8s.io/legacy-cloud-providers/azure/auth"
3536

@@ -855,6 +856,25 @@ func TestReconcileSecurityWithSourceRanges(t *testing.T) {
855856
validateSecurityGroup(t, sg, svc)
856857
}
857858

859+
func TestReconcileSecurityGroupEtagMismatch(t *testing.T) {
860+
az := getTestCloud()
861+
862+
sg := getTestSecurityGroup(az)
863+
cachedSG := *sg
864+
cachedSG.Etag = to.StringPtr("1111111-0000-0000-0000-000000000000")
865+
az.nsgCache.Set(to.String(sg.Name), &cachedSG)
866+
867+
svc1 := getTestService("servicea", v1.ProtocolTCP, 80)
868+
clusterResources := getClusterResources(az, 1, 1)
869+
lb, _ := az.reconcileLoadBalancer(testClusterName, &svc1, clusterResources.nodes, true)
870+
lbStatus, _ := az.getServiceLoadBalancerStatus(&svc1, lb)
871+
872+
newSG, err := az.reconcileSecurityGroup(testClusterName, &svc1, &lbStatus.Ingress[0].IP, true /* wantLb */)
873+
assert.Nil(t, newSG)
874+
assert.NotNil(t, err)
875+
assert.Equal(t, err, errPreconditionFailedEtagMismatch)
876+
}
877+
858878
func TestReconcilePublicIPWithNewService(t *testing.T) {
859879
az := getTestCloud()
860880
svc := getTestService("servicea", v1.ProtocolTCP, 80, 443)
@@ -958,6 +978,7 @@ func getTestCloud() (az *Cloud) {
958978
nodeResourceGroups: map[string]string{},
959979
unmanagedNodes: sets.NewString(),
960980
routeCIDRs: map[string]string{},
981+
eventRecorder: &record.FakeRecorder{},
961982
}
962983
az.DisksClient = newFakeDisksClient()
963984
az.InterfacesClient = newFakeAzureInterfacesClient()
@@ -1186,6 +1207,7 @@ func getTestSecurityGroup(az *Cloud, services ...v1.Service) *network.SecurityGr
11861207

11871208
sg := network.SecurityGroup{
11881209
Name: &az.SecurityGroupName,
1210+
Etag: to.StringPtr("0000000-0000-0000-0000-000000000000"),
11891211
SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{
11901212
SecurityRules: &rules,
11911213
},
@@ -1197,7 +1219,8 @@ func getTestSecurityGroup(az *Cloud, services ...v1.Service) *network.SecurityGr
11971219
ctx,
11981220
az.ResourceGroup,
11991221
az.SecurityGroupName,
1200-
sg)
1222+
sg,
1223+
"")
12011224

12021225
return &sg
12031226
}

0 commit comments

Comments
 (0)