-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudrun): update sample service for cloud run readiness probes to support grpc #5404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ import ( | |
| "github.com/GoogleCloudPlatform/golang-samples/internal/testutil" | ||
| ) | ||
|
|
||
| func TestServiceHealth(t *testing.T) { | ||
| func TestServiceHealthHttp(t *testing.T) { | ||
| tc := testutil.EndToEndTest(t) | ||
|
|
||
| service := cloudrunci.NewService("service-health", tc.ProjectID) | ||
|
|
@@ -86,3 +86,63 @@ func TestServiceHealth(t *testing.T) { | |
| t.Fatalf("testutil.DeleteBucketIfExists: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestServiceHealthGrpc(t *testing.T) { | ||
| tc := testutil.EndToEndTest(t) | ||
|
|
||
| service := cloudrunci.NewService("service-health", tc.ProjectID) | ||
| service.Readiness = &cloudrunci.ReadinessProbe{ | ||
| TimeoutSeconds: 1, | ||
| PeriodSeconds: 1, | ||
| SuccessThreshold: 1, | ||
| FailureThreshold: 1, | ||
| GRPC: &cloudrunci.GRPCProbe{ | ||
| Port: 8081, | ||
| }, | ||
| } | ||
| service.Dir = "../service-health" | ||
| service.AsBuildpack = true | ||
| service.Platform.CommandFlags() | ||
|
|
||
| if err := service.Deploy(); err != nil { | ||
| t.Fatalf("service.Deploy %q: %v", service.Name, err) | ||
| } | ||
| defer func(service *cloudrunci.Service) { | ||
| err := service.Clean() | ||
| if err != nil { | ||
| t.Fatalf("service.Clean %q: %v", service.Name, err) | ||
| } | ||
| }(service) | ||
|
|
||
| resp, err := service.Request("GET", "/are_you_ready") | ||
| if err != nil { | ||
| t.Fatalf("request: %v", err) | ||
| } | ||
|
|
||
| out, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| t.Fatalf("io.ReadAll: %v", err) | ||
| } | ||
|
|
||
| if got, want := string(out), "HEALTHY"; got != want { | ||
| t.Errorf("body: got %q, want %q", got, want) | ||
| } | ||
|
|
||
| if got := resp.StatusCode; got != http.StatusOK { | ||
| t.Errorf("response status: got %d, want %d", got, http.StatusOK) | ||
| } | ||
|
|
||
| ctx := context.Background() | ||
| c, err := storage.NewClient(ctx) | ||
| if err != nil { | ||
| t.Fatalf("storage.NewClient: %v", err) | ||
| } | ||
| defer c.Close() | ||
| bucketName := os.Getenv("GOLANG_SAMPLES_PROJECT_ID") + "-" + service.Version() | ||
| t.Logf("Deleting bucket: %s", bucketName) | ||
|
|
||
| err = testutil.DeleteBucketIfExists(ctx, c, bucketName) | ||
| if err != nil { | ||
| t.Fatalf("testutil.DeleteBucketIfExists: %v", err) | ||
| } | ||
| } | ||
|
Comment on lines
+90
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is significant code duplication between You could define a slice of test cases, with each case specifying the readiness probe configuration, and then loop through them in a single test function using Example structure: func TestServiceHealth(t *testing.T) {
tc := testutil.EndToEndTest(t)
tests := []struct {
name string
probe *cloudrunci.ReadinessProbe
}{
{
name: "HTTP",
probe: &cloudrunci.ReadinessProbe{
// ... HTTP probe config
},
},
{
name: "gRPC",
probe: &cloudrunci.ReadinessProbe{
// ... gRPC probe config
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
// ... shared test logic using test.probe
})
}
} |
||
Uh oh!
There was an error while loading. Please reload this page.