-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Adding tests for Liveness probes #12497
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bbf1850
liveness handler, refactoring and basic liveness probe test
Shashankft9 3a81322
test after the first probe check
Shashankft9 c4b7e4f
goimports done
Shashankft9 db4c85b
fail for liveness added in test image
Shashankft9 44c79bd
making current tests pass
Shashankft9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
/* | ||
Copyright 2022 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package runtime | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
pkgtest "knative.dev/pkg/test" | ||
"knative.dev/pkg/test/spoof" | ||
revisionresourcenames "knative.dev/serving/pkg/reconciler/revision/resources/names" | ||
v1opts "knative.dev/serving/pkg/testing/v1" | ||
"knative.dev/serving/test" | ||
"knative.dev/serving/test/conformance/api/shared" | ||
v1test "knative.dev/serving/test/v1" | ||
) | ||
|
||
const livenessPath = "/healthz/liveness" | ||
|
||
func TestLiveness(t *testing.T) { | ||
t.Parallel() | ||
if test.ServingFlags.DisableOptionalAPI { | ||
t.Skip("Container.livenessProbe is not required by Knative Serving API Specification") | ||
} | ||
clients := test.Setup(t) | ||
|
||
var testCases = []struct { | ||
// name of the test case, which will be inserted in names of routes, configurations, etc. | ||
// Use a short name here to avoid hitting the 63-character limit in names | ||
// (e.g., "service-to-service-call-svc-cluster-local-uagkdshh-frkml-service" is too long.) | ||
name string | ||
handler corev1.Handler | ||
sleep bool | ||
}{{ | ||
name: "httpGet", | ||
handler: corev1.Handler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: livenessPath, | ||
}, | ||
}, | ||
}, { | ||
name: "httpGetAfterFirstProbe", | ||
handler: corev1.Handler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: livenessPath, | ||
}, | ||
}, | ||
sleep: true, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
names := test.ResourceNames{ | ||
Service: test.ObjectNameForTest(t), | ||
Image: test.HealthProbes, | ||
} | ||
|
||
test.EnsureTearDown(t, clients, &names) | ||
|
||
t.Log("Creating a new Service") | ||
resources, err := v1test.CreateServiceReady(t, clients, &names, | ||
v1opts.WithLivenessProbe( | ||
&corev1.Probe{ | ||
Handler: tc.handler, | ||
PeriodSeconds: 10, | ||
FailureThreshold: 1, | ||
})) | ||
if err != nil { | ||
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err) | ||
} | ||
|
||
// If true sleeping till the first kubelet probe check. | ||
if tc.sleep { | ||
time.Sleep(15 * time.Second) | ||
} | ||
url := resources.Route.Status.URL.URL() | ||
url.Path = livenessPath | ||
if _, err = pkgtest.CheckEndpointState( | ||
context.Background(), | ||
clients.KubeClient, | ||
t.Logf, | ||
url, | ||
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.LivenessText)), | ||
"livenessIsReady", | ||
test.ServingFlags.ResolvableDomain, | ||
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS), | ||
); err != nil { | ||
t.Fatalf("The endpoint for Route %s at %s didn't return success: %v", names.Route, url, err) | ||
} | ||
// Check if scaling down works even if access from liveness probe exists. | ||
if err := shared.WaitForScaleToZero(t, revisionresourcenames.Deployment(resources.Revision), clients); err != nil { | ||
t.Fatal("Could not scale to zero:", err) | ||
} | ||
}) | ||
|
||
} | ||
} | ||
|
||
// commented out because, for this to work, https://github.com/knative/pkg/issues/2407 needs to be | ||
// fixed, with another issue of a ksvc pod never properly restarting after going into a liveness | ||
// probe fail. | ||
|
||
/* | ||
func TestLivenessWithFail(t *testing.T) { | ||
t.Parallel() | ||
if test.ServingFlags.DisableOptionalAPI { | ||
t.Skip("Container.livenessProbe is not required by Knative Serving API Specification") | ||
} | ||
clients := test.Setup(t) | ||
|
||
var testCases = []struct { | ||
// name of the test case, which will be inserted in names of routes, configurations, etc. | ||
// Use a short name here to avoid hitting the 63-character limit in names | ||
// (e.g., "service-to-service-call-svc-cluster-local-uagkdshh-frkml-service" is too long.) | ||
name string | ||
handler corev1.Handler | ||
sleep bool | ||
}{{ | ||
name: "httpGet", | ||
handler: corev1.Handler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: livenessPath, | ||
}, | ||
}, | ||
}} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
names := test.ResourceNames{ | ||
Service: test.ObjectNameForTest(t), | ||
Image: test.HealthProbes, | ||
} | ||
|
||
test.EnsureTearDown(t, clients, &names) | ||
|
||
t.Log("Creating a new Service") | ||
resources, err := v1test.CreateServiceReady(t, clients, &names, | ||
v1opts.WithLivenessProbe( | ||
&corev1.Probe{ | ||
Handler: tc.handler, | ||
PeriodSeconds: 13, | ||
FailureThreshold: 1, | ||
})) | ||
if err != nil { | ||
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err) | ||
} | ||
|
||
url := resources.Route.Status.URL.URL() | ||
url.Path = livenessPath | ||
if _, err = pkgtest.WaitForEndpointState( | ||
context.Background(), | ||
clients.KubeClient, | ||
t.Logf, | ||
url, | ||
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.LivenessText + "15")), | ||
"livenessIsReady", | ||
test.ServingFlags.ResolvableDomain, | ||
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS), | ||
); err != nil { | ||
t.Fatalf("The endpoint for Route %s at %s didn't return success: %v", names.Route, url, err) | ||
} | ||
|
||
t.Log("POST to /start-failing") | ||
client, err := pkgtest.NewSpoofingClient(context.Background(), | ||
clients.KubeClient, | ||
t.Logf, | ||
url.Hostname(), | ||
test.ServingFlags.ResolvableDomain, | ||
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS)) | ||
if err != nil { | ||
t.Fatalf("Failed to create spoofing client: %v", err) | ||
} | ||
|
||
url.Path = "/start-failing" | ||
startFailing, err := http.NewRequest(http.MethodPost, url.String(), nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if _, err := client.Do(startFailing); err != nil { | ||
t.Fatalf("POST to /start-failing failed: %v", err) | ||
} | ||
|
||
url.Path = livenessPath | ||
if _, err = pkgtest.WaitForEndpointState( | ||
context.Background(), | ||
clients.KubeClient, | ||
t.Logf, | ||
url, | ||
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.MatchesBody(test.LivenessText + "15")), | ||
"livenessIsReady", | ||
test.ServingFlags.ResolvableDomain, | ||
test.AddRootCAtoTransport(context.Background(), t.Logf, clients, test.ServingFlags.HTTPS), | ||
); err != nil { | ||
t.Fatalf("The endpoint for Route %s at %s didn't return success: %v", names.Route, url, err) | ||
} | ||
|
||
// Check if scaling down works even if access from liveness probe exists. | ||
if err := shared.WaitForScaleToZero(t, revisionresourcenames.Deployment(resources.Revision), clients); err != nil { | ||
t.Fatal("Could not scale to zero:", err) | ||
} | ||
}) | ||
|
||
} | ||
} | ||
|
||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
test/test_images/readiness/README.md → test/test_images/healthprobes/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is trying to surface the bug we hit in #12462 by making sure the pod stays up for at least 15 seconds even if a liveness probe is configured, but I wouldn't have guessed that without remembering about that issue. I wonder if there's a way to do this that (a) is a bit clearer about what it's testing -- i.e. that a pod with a liveness probe is accessible while that probe returns true (and the probe actually runs), and then is restarted if it fails (b) ideally avoids a hardcoded sleep.
What about if the test image counted how many times its liveness handler had been invoked and returned that number in the response? Then we could
WaitForEndpointState
until the liveness probe had been executed at least N times (avoiding the sleep). We could then - maybe as a follow up - have the test POST to an endpoint on the test image that would cause the liveness check to fail, and assert that the container is properly restarted (this is similar to how we test readiness probes).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense - will do these changes, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@julz just for clarity, if the liveness probe fails and the kubelet does see it, even though it restarts the containers, the readiness probe fails, and so the liveness probe check will also hang forever because of that. I am not really sure why the readiness probe is failing though - checking on that. (this is without any user provided readiness probe)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might be the case where I am just adding bad livenesProbe or readinessProbe and going into the probe pitfalls, but I have tried to capture more of it here: #12571