make helper function return error (#3553)

This commit is contained in:
Zou Nengren
2020-04-24 05:58:34 +08:00
committed by GitHub
parent 6a3c03883d
commit 29f40a46f6

View File

@ -125,31 +125,27 @@ func startXDS(t *testing.T, xdsname string, v2c *v2Client, reqChan *testutils.Ch
// //
// It also waits and checks that the ack request contains the given version, and // It also waits and checks that the ack request contains the given version, and
// the generated nonce. // the generated nonce.
// func sendGoodResp(t *testing.T, xdsname string, fakeServer *fakeserver.Server, version int, goodResp *xdspb.DiscoveryResponse, wantReq *xdspb.DiscoveryRequest, callbackCh *testutils.Channel) (string, error) {
// TODO: make this and other helper function either consistently return error, nonce := sendXDSRespWithVersion(fakeServer.XDSResponseChan, goodResp, version)
// and fatal() in the test code, or all call t.Fatal(), and mark them as
// helper().
func sendGoodResp(t *testing.T, xdsname string, fakeServer *fakeserver.Server, version int, goodResp *xdspb.DiscoveryResponse, wantReq *xdspb.DiscoveryRequest, callbackCh *testutils.Channel) (nonce string) {
nonce = sendXDSRespWithVersion(fakeServer.XDSResponseChan, goodResp, version)
t.Logf("Good %s response pushed to fakeServer...", xdsname) t.Logf("Good %s response pushed to fakeServer...", xdsname)
if err := compareXDSRequest(fakeServer.XDSRequestChan, wantReq, strconv.Itoa(version), nonce); err != nil { if err := compareXDSRequest(fakeServer.XDSRequestChan, wantReq, strconv.Itoa(version), nonce); err != nil {
t.Fatalf("Failed to receive %s request: %v", xdsname, err) return "", fmt.Errorf("failed to receive %s request: %v", xdsname, err)
} }
t.Logf("Good %s response acked", xdsname) t.Logf("Good %s response acked", xdsname)
if _, err := callbackCh.Receive(); err != nil { if _, err := callbackCh.Receive(); err != nil {
t.Fatalf("Timeout when expecting %s update", xdsname) return "", fmt.Errorf("timeout when expecting %s update", xdsname)
} }
t.Logf("Good %s response callback executed", xdsname) t.Logf("Good %s response callback executed", xdsname)
return return nonce, nil
} }
// sendBadResp sends a bad response with the given version. This response will // sendBadResp sends a bad response with the given version. This response will
// be nacked, so we expect a request with the previous version (version-1). // be nacked, so we expect a request with the previous version (version-1).
// //
// But the nonce in request should be the new nonce. // But the nonce in request should be the new nonce.
func sendBadResp(t *testing.T, xdsname string, fakeServer *fakeserver.Server, version int, wantReq *xdspb.DiscoveryRequest) { func sendBadResp(t *testing.T, xdsname string, fakeServer *fakeserver.Server, version int, wantReq *xdspb.DiscoveryRequest) error {
var typeURL string var typeURL string
switch xdsname { switch xdsname {
case "LDS": case "LDS":
@ -167,9 +163,10 @@ func sendBadResp(t *testing.T, xdsname string, fakeServer *fakeserver.Server, ve
}, version) }, version)
t.Logf("Bad %s response pushed to fakeServer...", xdsname) t.Logf("Bad %s response pushed to fakeServer...", xdsname)
if err := compareXDSRequest(fakeServer.XDSRequestChan, wantReq, strconv.Itoa(version-1), nonce); err != nil { if err := compareXDSRequest(fakeServer.XDSRequestChan, wantReq, strconv.Itoa(version-1), nonce); err != nil {
t.Fatalf("Failed to receive %s request: %v", xdsname, err) return fmt.Errorf("failed to receive %s request: %v", xdsname, err)
} }
t.Logf("Bad %s response nacked", xdsname) t.Logf("Bad %s response nacked", xdsname)
return nil
} }
// TestV2ClientAck verifies that valid responses are acked, and invalid ones // TestV2ClientAck verifies that valid responses are acked, and invalid ones
@ -192,36 +189,60 @@ func (s) TestV2ClientAck(t *testing.T) {
// Start the watch, send a good response, and check for ack. // Start the watch, send a good response, and check for ack.
startXDS(t, "LDS", v2c, fakeServer.XDSRequestChan, goodLDSRequest, "", "") startXDS(t, "LDS", v2c, fakeServer.XDSRequestChan, goodLDSRequest, "", "")
sendGoodResp(t, "LDS", fakeServer, versionLDS, goodLDSResponse1, goodLDSRequest, cbLDS) if _, err := sendGoodResp(t, "LDS", fakeServer, versionLDS, goodLDSResponse1, goodLDSRequest, cbLDS); err != nil {
t.Fatal(err)
}
versionLDS++ versionLDS++
startXDS(t, "RDS", v2c, fakeServer.XDSRequestChan, goodRDSRequest, "", "") startXDS(t, "RDS", v2c, fakeServer.XDSRequestChan, goodRDSRequest, "", "")
sendGoodResp(t, "RDS", fakeServer, versionRDS, goodRDSResponse1, goodRDSRequest, cbRDS) if _, err := sendGoodResp(t, "RDS", fakeServer, versionRDS, goodRDSResponse1, goodRDSRequest, cbRDS); err != nil {
t.Fatal(err)
}
versionRDS++ versionRDS++
startXDS(t, "CDS", v2c, fakeServer.XDSRequestChan, goodCDSRequest, "", "") startXDS(t, "CDS", v2c, fakeServer.XDSRequestChan, goodCDSRequest, "", "")
sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS) if _, err := sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS); err != nil {
t.Fatal(err)
}
versionCDS++ versionCDS++
startXDS(t, "EDS", v2c, fakeServer.XDSRequestChan, goodEDSRequest, "", "") startXDS(t, "EDS", v2c, fakeServer.XDSRequestChan, goodEDSRequest, "", "")
sendGoodResp(t, "EDS", fakeServer, versionEDS, goodEDSResponse1, goodEDSRequest, cbEDS) if _, err := sendGoodResp(t, "EDS", fakeServer, versionEDS, goodEDSResponse1, goodEDSRequest, cbEDS); err != nil {
t.Fatal(err)
}
versionEDS++ versionEDS++
// Send a bad response, and check for nack. // Send a bad response, and check for nack.
sendBadResp(t, "LDS", fakeServer, versionLDS, goodLDSRequest) if err := sendBadResp(t, "LDS", fakeServer, versionLDS, goodLDSRequest); err != nil {
t.Fatal(err)
}
versionLDS++ versionLDS++
sendBadResp(t, "RDS", fakeServer, versionRDS, goodRDSRequest) if err := sendBadResp(t, "RDS", fakeServer, versionRDS, goodRDSRequest); err != nil {
t.Fatal(err)
}
versionRDS++ versionRDS++
sendBadResp(t, "CDS", fakeServer, versionCDS, goodCDSRequest) if err := sendBadResp(t, "CDS", fakeServer, versionCDS, goodCDSRequest); err != nil {
t.Fatal(err)
}
versionCDS++ versionCDS++
sendBadResp(t, "EDS", fakeServer, versionEDS, goodEDSRequest) if err := sendBadResp(t, "EDS", fakeServer, versionEDS, goodEDSRequest); err != nil {
t.Fatal(err)
}
versionEDS++ versionEDS++
// send another good response, and check for ack, with the new version. // send another good response, and check for ack, with the new version.
sendGoodResp(t, "LDS", fakeServer, versionLDS, goodLDSResponse1, goodLDSRequest, cbLDS) if _, err := sendGoodResp(t, "LDS", fakeServer, versionLDS, goodLDSResponse1, goodLDSRequest, cbLDS); err != nil {
t.Fatal(err)
}
versionLDS++ versionLDS++
sendGoodResp(t, "RDS", fakeServer, versionRDS, goodRDSResponse1, goodRDSRequest, cbRDS) if _, err := sendGoodResp(t, "RDS", fakeServer, versionRDS, goodRDSResponse1, goodRDSRequest, cbRDS); err != nil {
t.Fatal(err)
}
versionRDS++ versionRDS++
sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS) if _, err := sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS); err != nil {
t.Fatal(err)
}
versionCDS++ versionCDS++
sendGoodResp(t, "EDS", fakeServer, versionEDS, goodEDSResponse1, goodEDSRequest, cbEDS) if _, err := sendGoodResp(t, "EDS", fakeServer, versionEDS, goodEDSResponse1, goodEDSRequest, cbEDS); err != nil {
t.Fatal(err)
}
versionEDS++ versionEDS++
} }
@ -270,8 +291,10 @@ func (s) TestV2ClientAckNackAfterNewWatch(t *testing.T) {
// Start the watch, send a good response, and check for ack. // Start the watch, send a good response, and check for ack.
startXDS(t, "LDS", v2c, fakeServer.XDSRequestChan, goodLDSRequest, "", "") startXDS(t, "LDS", v2c, fakeServer.XDSRequestChan, goodLDSRequest, "", "")
nonce := sendGoodResp(t, "LDS", fakeServer, versionLDS, goodLDSResponse1, goodLDSRequest, cbLDS) nonce, err := sendGoodResp(t, "LDS", fakeServer, versionLDS, goodLDSResponse1, goodLDSRequest, cbLDS)
if err != nil {
t.Fatal(err)
}
// Start a new watch. The version in the new request should be the version // Start a new watch. The version in the new request should be the version
// from the previous response, thus versionLDS before ++. // from the previous response, thus versionLDS before ++.
startXDS(t, "LDS", v2c, fakeServer.XDSRequestChan, goodLDSRequest, strconv.Itoa(versionLDS), nonce) startXDS(t, "LDS", v2c, fakeServer.XDSRequestChan, goodLDSRequest, strconv.Itoa(versionLDS), nonce)
@ -291,7 +314,9 @@ func (s) TestV2ClientAckNackAfterNewWatch(t *testing.T) {
t.Logf("Bad response nacked") t.Logf("Bad response nacked")
versionLDS++ versionLDS++
sendGoodResp(t, "LDS", fakeServer, versionLDS, goodLDSResponse1, goodLDSRequest, cbLDS) if _, err := sendGoodResp(t, "LDS", fakeServer, versionLDS, goodLDSResponse1, goodLDSRequest, cbLDS); err != nil {
t.Fatal(err)
}
versionLDS++ versionLDS++
} }
@ -315,8 +340,10 @@ func (s) TestV2ClientAckNewWatchAfterCancel(t *testing.T) {
// Send a good CDS response, this function waits for the ACK with the right // Send a good CDS response, this function waits for the ACK with the right
// version. // version.
nonce := sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS) nonce, err := sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS)
if err != nil {
t.Fatal(err)
}
// Cancel the CDS watch, and start a new one. The new watch should have the // Cancel the CDS watch, and start a new one. The new watch should have the
// version from the response above. // version from the response above.
v2c.removeWatch(cdsURL, goodClusterName1) v2c.removeWatch(cdsURL, goodClusterName1)
@ -334,11 +361,15 @@ func (s) TestV2ClientAckNewWatchAfterCancel(t *testing.T) {
versionCDS++ versionCDS++
// Send a bad response with the next version. // Send a bad response with the next version.
sendBadResp(t, "CDS", fakeServer, versionCDS, goodCDSRequest) if err := sendBadResp(t, "CDS", fakeServer, versionCDS, goodCDSRequest); err != nil {
t.Fatal(err)
}
versionCDS++ versionCDS++
// send another good response, and check for ack, with the new version. // send another good response, and check for ack, with the new version.
sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS) if _, err := sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS); err != nil {
t.Fatal(err)
}
versionCDS++ versionCDS++
} }
@ -363,8 +394,10 @@ func (s) TestV2ClientAckCancelResponseRace(t *testing.T) {
t.Logf("FakeServer received %s request...", "CDS") t.Logf("FakeServer received %s request...", "CDS")
// send a good response, and check for ack, with the new version. // send a good response, and check for ack, with the new version.
nonce := sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS) nonce, err := sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS)
if err != nil {
t.Fatal(err)
}
// Cancel the watch before the next response is sent. This mimics the case // Cancel the watch before the next response is sent. This mimics the case
// watch is canceled while response is on wire. // watch is canceled while response is on wire.
v2c.removeWatch(cdsURL, goodClusterName1) v2c.removeWatch(cdsURL, goodClusterName1)
@ -401,10 +434,14 @@ func (s) TestV2ClientAckCancelResponseRace(t *testing.T) {
} }
// Send a bad response with the next version. // Send a bad response with the next version.
sendBadResp(t, "CDS", fakeServer, versionCDS, goodCDSRequest) if err := sendBadResp(t, "CDS", fakeServer, versionCDS, goodCDSRequest); err != nil {
t.Fatal(err)
}
versionCDS++ versionCDS++
// send another good response, and check for ack, with the new version. // send another good response, and check for ack, with the new version.
sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS) if _, err := sendGoodResp(t, "CDS", fakeServer, versionCDS, goodCDSResponse1, goodCDSRequest, cbCDS); err != nil {
t.Fatal(err)
}
versionCDS++ versionCDS++
} }