Skip to content

Commit ab6f6f6

Browse files
committed
unit test for services multithreading
1 parent 9b27d5c commit ab6f6f6

File tree

2 files changed

+102
-18
lines changed

2 files changed

+102
-18
lines changed

netmaster/master/servicelb.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ func CreateServiceLB(stateDriver core.StateDriver, serviceLbCfg *intent.ConfigSe
129129
mastercfg.SvcMutex.Unlock()
130130
return err
131131
}
132-
mastercfg.SvcMutex.Unlock()
133132

134133
if providersPresent {
135134
err = SvcProviderUpdate(serviceID, false)
@@ -138,6 +137,7 @@ func CreateServiceLB(stateDriver core.StateDriver, serviceLbCfg *intent.ConfigSe
138137
return err
139138
}
140139
}
140+
mastercfg.SvcMutex.Unlock()
141141

142142
return nil
143143
}

netmaster/objApi/objapi_test.go

+101-17
Original file line numberDiff line numberDiff line change
@@ -877,22 +877,61 @@ func TestServiceProviderUpdate(t *testing.T) {
877877

878878
labels := []string{"key1=value1", "key2=value2"}
879879
port := []string{"80:8080:TCP"}
880+
ch := make(chan error, 1)
880881

881882
createNetwork(t, "yellow", "default", "vxlan", "10.1.1.0/24", "10.1.1.254")
882883
createNetwork(t, "orange", "default", "vxlan", "11.1.1.0/24", "11.1.1.254")
883884

884885
checkServiceCreate(t, "default", "yellow", "redis", port, labels, "")
885886
verifyServiceCreate(t, "default", "yellow", "redis", port, labels, "")
886887

887-
containerID := "723e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
888-
container := "657355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
888+
containerID1 := "723e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
889+
containerID2 := "823e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
890+
containerID3 := "023e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
891+
containerID4 := "123e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
892+
container1 := "657355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
893+
container2 := "757355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
894+
container3 := "857355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
895+
container4 := "957355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
896+
897+
createEP(t, "20.1.1.1", "orange", containerID1, "default", container1, labels)
898+
createEP(t, "20.1.1.2", "orange", containerID2, "default", container2, labels)
899+
createEP(t, "20.1.1.3", "orange", containerID3, "default", container3, labels)
900+
createEP(t, "20.1.1.4", "orange", containerID4, "default", container4, labels)
901+
902+
go triggerProviderUpdate(t, "20.1.1.1", "orange", containerID1, container1, "default", "start", labels, ch)
903+
go triggerProviderUpdate(t, "20.1.1.2", "orange", containerID2, container2, "default", "start", labels, ch)
904+
go triggerProviderUpdate(t, "20.1.1.3", "orange", containerID3, container3, "default", "start", labels, ch)
905+
go triggerProviderUpdate(t, "20.1.1.4", "orange", containerID4, container4, "default", "start", labels, ch)
906+
907+
for i := 0; i < 4; i++ {
908+
<-ch
909+
}
910+
911+
verifyProviderUpdate(t, "20.1.1.1", "orange", containerID1, "default", "start", "redis", labels)
912+
verifyProviderUpdate(t, "20.1.1.2", "orange", containerID2, "default", "start", "redis", labels)
913+
verifyProviderUpdate(t, "20.1.1.3", "orange", containerID3, "default", "start", "redis", labels)
914+
verifyProviderUpdate(t, "20.1.1.4", "orange", containerID4, "default", "start", "redis", labels)
915+
916+
go triggerProviderUpdate(t, "20.1.1.1", "orange", containerID1, container1, "default", "die", labels, ch)
917+
go triggerProviderUpdate(t, "20.1.1.2", "orange", containerID2, container2, "default", "die", labels, ch)
918+
go triggerProviderUpdate(t, "20.1.1.3", "orange", containerID3, container3, "default", "die", labels, ch)
919+
go triggerProviderUpdate(t, "20.1.1.4", "orange", containerID4, container4, "default", "die", labels, ch)
920+
921+
for i := 0; i < 4; i++ {
922+
<-ch
923+
}
924+
925+
verifyProviderUpdate(t, "20.1.1.1", "orange", containerID1, "default", "die", "redis", labels)
926+
verifyProviderUpdate(t, "20.1.1.2", "orange", containerID2, "default", "die", "redis", labels)
927+
verifyProviderUpdate(t, "20.1.1.3", "orange", containerID3, "default", "die", "redis", labels)
928+
verifyProviderUpdate(t, "20.1.1.4", "orange", containerID4, "default", "die", "redis", labels)
929+
930+
deleteEP(t, "orange", "default", container1)
931+
deleteEP(t, "orange", "default", container2)
932+
deleteEP(t, "orange", "default", container3)
933+
deleteEP(t, "orange", "default", container4)
889934

890-
createEP(t, "20.1.1.1", "orange", containerID, "default", container, labels)
891-
triggerProviderUpdate(t, "20.1.1.1", "orange", containerID, container, "default", "start", labels)
892-
verifyProviderUpdate(t, "20.1.1.1", "orange", containerID, "default", "start", "redis", labels)
893-
triggerProviderUpdate(t, "20.1.1.1", "orange", containerID, container, "default", "die", labels)
894-
verifyProviderUpdate(t, "20.1.1.1", "orange", containerID, "default", "die", "redis", labels)
895-
deleteEP(t, "orange", "default", container)
896935
checkServiceDelete(t, "default", "redis")
897936
verifyServiceDelete(t, "default", "redis")
898937
deleteNetwork(t, "orange", "default")
@@ -903,21 +942,60 @@ func TestServiceProviderUpdateServiceAdd(t *testing.T) {
903942

904943
labels := []string{"key1=value1", "key2=value2"}
905944
port := []string{"80:8080:TCP"}
945+
ch := make(chan error, 1)
906946

907-
containerID := "723e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
908-
container := "657355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
947+
containerID1 := "723e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
948+
containerID2 := "823e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
949+
containerID3 := "023e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
950+
containerID4 := "123e55bf5b244f47c1b184cb786a1c2ad8870cc3a3db723c49ac09f68a9d1e69"
951+
container1 := "657355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
952+
container2 := "757355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
953+
container3 := "857355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
954+
container4 := "957355bf5b244f47c1b184cb786a14535d8870cc3a3db723c49ac09f68a9d6a5"
909955

910956
createNetwork(t, "orange", "default", "vxlan", "11.1.1.0/24", "11.1.1.254")
911-
createEP(t, "20.1.1.1", "orange", containerID, "default", container, labels)
912-
triggerProviderUpdate(t, "20.1.1.1", "orange", containerID, container, "default", "start", labels)
957+
958+
createEP(t, "20.1.1.1", "orange", containerID1, "default", container1, labels)
959+
createEP(t, "20.1.1.2", "orange", containerID2, "default", container2, labels)
960+
createEP(t, "20.1.1.3", "orange", containerID3, "default", container3, labels)
961+
createEP(t, "20.1.1.4", "orange", containerID4, "default", container4, labels)
962+
963+
go triggerProviderUpdate(t, "20.1.1.1", "orange", containerID1, container1, "default", "start", labels, ch)
964+
go triggerProviderUpdate(t, "20.1.1.2", "orange", containerID2, container2, "default", "start", labels, ch)
965+
go triggerProviderUpdate(t, "20.1.1.3", "orange", containerID3, container3, "default", "start", labels, ch)
966+
go triggerProviderUpdate(t, "20.1.1.4", "orange", containerID4, container4, "default", "start", labels, ch)
967+
968+
for i := 0; i < 4; i++ {
969+
<-ch
970+
}
971+
913972
createNetwork(t, "yellow", "default", "vxlan", "10.1.1.0/24", "10.1.1.254")
914973
checkServiceCreate(t, "default", "yellow", "redis", port, labels, "")
915974
verifyServiceCreate(t, "default", "yellow", "redis", port, labels, "")
916975

917-
verifyProviderUpdate(t, "20.1.1.1", "orange", containerID, "default", "start", "redis", labels)
918-
triggerProviderUpdate(t, "20.1.1.1", "orange", containerID, container, "default", "die", labels)
919-
verifyProviderUpdate(t, "20.1.1.1", "orange", containerID, "default", "die", "redis", labels)
920-
deleteEP(t, "orange", "default", container)
976+
verifyProviderUpdate(t, "20.1.1.1", "orange", containerID1, "default", "start", "redis", labels)
977+
verifyProviderUpdate(t, "20.1.1.2", "orange", containerID2, "default", "start", "redis", labels)
978+
verifyProviderUpdate(t, "20.1.1.3", "orange", containerID3, "default", "start", "redis", labels)
979+
verifyProviderUpdate(t, "20.1.1.4", "orange", containerID4, "default", "start", "redis", labels)
980+
981+
go triggerProviderUpdate(t, "20.1.1.1", "orange", containerID1, container1, "default", "die", labels, ch)
982+
go triggerProviderUpdate(t, "20.1.1.2", "orange", containerID2, container2, "default", "die", labels, ch)
983+
go triggerProviderUpdate(t, "20.1.1.3", "orange", containerID3, container3, "default", "die", labels, ch)
984+
go triggerProviderUpdate(t, "20.1.1.4", "orange", containerID4, container4, "default", "die", labels, ch)
985+
for i := 0; i < 4; i++ {
986+
<-ch
987+
}
988+
989+
verifyProviderUpdate(t, "20.1.1.1", "orange", containerID1, "default", "die", "redis", labels)
990+
verifyProviderUpdate(t, "20.1.1.2", "orange", containerID2, "default", "die", "redis", labels)
991+
verifyProviderUpdate(t, "20.1.1.3", "orange", containerID3, "default", "die", "redis", labels)
992+
verifyProviderUpdate(t, "20.1.1.4", "orange", containerID4, "default", "die", "redis", labels)
993+
994+
deleteEP(t, "orange", "default", container1)
995+
deleteEP(t, "orange", "default", container2)
996+
deleteEP(t, "orange", "default", container3)
997+
deleteEP(t, "orange", "default", container4)
998+
921999
checkServiceDelete(t, "default", "redis")
9221000
verifyServiceDelete(t, "default", "redis")
9231001
deleteNetwork(t, "orange", "default")
@@ -935,6 +1013,7 @@ func TestServicePreferredIP(t *testing.T) {
9351013
checkServiceDelete(t, "default", "redis")
9361014
deleteNetwork(t, "yellow", "default")
9371015
}
1016+
9381017
func checkServiceCreate(t *testing.T, tenant, network, serviceName string, port []string, label []string,
9391018
preferredIP string) {
9401019

@@ -1012,7 +1091,7 @@ func verifyServiceDelete(t *testing.T, tenant, serviceName string) {
10121091
}
10131092

10141093
func triggerProviderUpdate(t *testing.T, providerIP, network, containerID, container,
1015-
tenant, event string, labels []string) {
1094+
tenant, event string, labels []string, ch chan error) {
10161095

10171096
providerUpdReq := master.SvcProvUpdateRequest{}
10181097
providerUpdReq.IPAddress = providerIP
@@ -1032,20 +1111,25 @@ func triggerProviderUpdate(t *testing.T, providerIP, network, containerID, conta
10321111

10331112
jsonStr, err := json.Marshal(providerUpdReq)
10341113
if err != nil {
1114+
ch <- err
10351115
t.Fatalf("Error converting request data(%#v) to Json. Err: %v", providerUpdReq, err)
10361116
}
10371117
url := netmasterTestURL + "/plugin/svcProviderUpdate"
10381118
// Perform HTTP POST operation
10391119
res, err := http.Post(url, "application/json", strings.NewReader(string(jsonStr)))
10401120
if err != nil {
10411121
t.Fatalf("Error during http get. Err: %v", err)
1122+
ch <- err
10421123
}
10431124

10441125
// Check the response code
10451126
if res.StatusCode != http.StatusOK {
1127+
ch <- err
10461128
t.Fatalf("HTTP error response. Status: %s, StatusCode: %d", res.Status, res.StatusCode)
10471129
}
10481130

1131+
ch <- nil
1132+
10491133
}
10501134
func verifyProviderUpdate(t *testing.T, providerIP, network, containerID,
10511135
tenant, event, service string, labels []string) {

0 commit comments

Comments
 (0)