-
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
Update test/adding_tests.md #6345
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ This library exists partially in this directory and partially in | |
|
||
The libs in this dir can: | ||
|
||
- [Use common test flags](#use-common-test-flags) | ||
- [Get access to client objects](#get-access-to-client-objects) | ||
- [Make requests against deployed services](#make-requests-against-deployed-services) | ||
- [Check Knative Serving resources](#check-knative-serving-resources) | ||
|
@@ -40,6 +39,7 @@ The libs in this dir can: | |
|
||
See [`knative/pkg/test`](https://github.com/knative/pkg/tree/master/test) to: | ||
|
||
- [Use common test flags](#use-common-test-flags) | ||
- Output logs | ||
- Emit metrics | ||
- Ensure test cleanup | ||
|
@@ -49,52 +49,71 @@ See [`knative/pkg/test`](https://github.com/knative/pkg/tree/master/test) to: | |
These flags are useful for running against an existing cluster, making use of | ||
your existing [environment setup](../DEVELOPMENT.md#setup-your-environment). | ||
|
||
By importing `github.com/knative/serving/test` you get access to a global | ||
By importing `knative.dev/pkg/test` you get access to a global | ||
variable called `test.Flags` which holds the values of | ||
[the command line flags](./README.md#flags). | ||
|
||
```go | ||
imagePath := strings.Join([]string{test.ServingFlags.DockerRepo, image}, "/")) | ||
imagePath := strings.Join([]string{test.Flags.DockerRepo, image}, "/")) | ||
``` | ||
|
||
_See [e2e_flags.go](./e2e_flags.go)._ | ||
_See [e2e_flags.go](https://github.com/knative/pkg/blob/master/test/e2e_flags.go)._ | ||
|
||
### Get access to client objects | ||
|
||
To initialize client objects that you can use | ||
[the command line flags](#use-flags) that describe the environment: | ||
the command line flags that describe the environment: | ||
|
||
```go | ||
func setup(t *testing.T) *test.Clients { | ||
clients, err := test.NewClients(kubeconfig, cluster, namespaceName) | ||
if err != nil { | ||
t.Fatalf("Couldn't initialize clients: %v", err) | ||
} | ||
return clients | ||
import ( | ||
testing | ||
|
||
knative.dev/serving/test | ||
pkgTest "knative.dev/pkg/test" | ||
) | ||
|
||
func Setup(t *testing.T) *test.Clients { | ||
clients, err := test.NewClients(pkgTest.Flags.Kubeconfig, pkgTest.Flags.Cluster, namespaceName) | ||
if err != nil { | ||
t.Fatalf("Couldn't initialize clients: %v", err) | ||
} | ||
return clients | ||
} | ||
``` | ||
|
||
The `Clients` struct contains initialized clients for accessing: | ||
|
||
- Kubernetes objects | ||
- `Kubernetes objects` | ||
- `Services` | ||
- `Routes` | ||
- `Configurations` | ||
- `Revisions` | ||
- `Knative ingress` | ||
- `ServerlessServices` | ||
- `Istio objects` | ||
|
||
For example, to create a `Route`: | ||
|
||
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. This line needs to be updated:
-->
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. done |
||
```bash | ||
_, err = clients.ServingClient.Routes.Create(test.Route(namespaceName, routeName, configName)) | ||
```go | ||
_, err = clients.ServingClient.Routes.Create(v1test.Route( | ||
test.ResourceNames{ | ||
Route: routeName, | ||
Config: configName, | ||
})) | ||
``` | ||
_v1test is alias for package `knative.dev/serving/pkg/testing/v1`_ | ||
|
||
And you can use the client to clean up `Route` and `Configuration` resources | ||
created by your test: | ||
|
||
```go | ||
import "knative.dev/serving/test" | ||
|
||
func tearDown(clients *test.Clients) { | ||
if clients != nil { | ||
clients.Delete([]string{routeName}, []string{configName}) | ||
} | ||
if clients != nil { | ||
clients.ServingClient.Routes.Delete(routeName, nil) | ||
clients.ServingClient.Configs.Delete(configName, nil) | ||
} | ||
} | ||
``` | ||
|
||
|
@@ -105,22 +124,22 @@ _See [clients.go](./clients.go)._ | |
After deploying (i.e. creating a `Route` and a `Configuration`) an endpoint will | ||
not be ready to serve requests right away. To poll a deployed endpoint and wait | ||
for it to be in the state you want it to be in (or timeout) use | ||
`WaitForEndpointState`: | ||
`WaitForEndpointState` by importing `knative.dev/pkg/test` with alias `pkgTest`: | ||
|
||
```go | ||
err = pkgTest.WaitForEndpointState( | ||
clients.KubeClient, | ||
logger, | ||
updatedRoute.Status.Domain, | ||
pkgTest.EventuallyMatchesBody(expectedText), | ||
"SomeDescription", | ||
test.ServingFlags.ResolvableDomain) | ||
_, err := pkgTest.WaitForEndpointState( | ||
clients.KubeClient, | ||
logger, | ||
updatedRoute.Status.URL.URL(), | ||
pkgTest.EventuallyMatchesBody(expectedText), | ||
"SomeDescription", | ||
test.ServingFlags.ResolvableDomain) | ||
if err != nil { | ||
t.Fatalf("The endpoint for Route %s at domain %s didn't serve the expected text \"%s\": %v", routeName, updatedRoute.Status.Domain, expectedText, err) | ||
t.Fatalf("The endpoint for Route %s at domain %s didn't serve the expected text \"%s\": %v", routeName, updatedRoute.Status.Domain, expectedText, err) | ||
} | ||
``` | ||
|
||
This function makes use of [the environment flag `resolvableDomain`](#use-flags) | ||
This function makes use of [the environment flag `resolvableDomain`](README.md#using-a-resolvable-domain) | ||
to determine if the ingress should be used or the domain should be used | ||
directly. | ||
|
||
|
@@ -166,64 +185,68 @@ For example, you can poll a `Configuration` object to find the name of the | |
|
||
```go | ||
var revisionName string | ||
err := test.WaitForConfigurationState(clients.ServingClient, configName, func(c *v1alpha1.Configuration) (bool, error) { | ||
if c.Status.LatestCreatedRevisionName != "" { | ||
revisionName = c.Status.LatestCreatedRevisionName | ||
return true, nil | ||
} | ||
return false, nil | ||
err := v1alpha1testing.WaitForConfigurationState(clients.ServingClient, configName, func(c *v1alpha1.Configuration) (bool, error) { | ||
if c.Status.LatestCreatedRevisionName != "" { | ||
revisionName = c.Status.LatestCreatedRevisionName | ||
return true, nil | ||
} | ||
return false, nil | ||
}, "ConfigurationUpdatedWithRevision") | ||
``` | ||
|
||
_[Metrics will be emitted](#emit-metrics) for these `Wait` method tracking how | ||
long test poll for._ | ||
_v1alpha1testing is alias for package `knative.dev/serving/pkg/testing/v1alpha1`_ | ||
|
||
We also have `Check*` variants of many of these methods with identical | ||
signatures, same example: | ||
|
||
```go | ||
var revisionName string | ||
err := test.CheckConfigurationState(clients.ServingClient, configName, func(c *v1alpha1.Configuration) (bool, error) { | ||
if c.Status.LatestCreatedRevisionName != "" { | ||
revisionName = c.Status.LatestCreatedRevisionName | ||
return true, nil | ||
} | ||
return false, nil | ||
err := v1alpha1testing.CheckConfigurationState(clients.ServingClient, configName, func(c *v1alpha1.Configuration) (bool, error) { | ||
if c.Status.LatestCreatedRevisionName != "" { | ||
revisionName = c.Status.LatestCreatedRevisionName | ||
return true, nil | ||
} | ||
return false, nil | ||
}) | ||
``` | ||
|
||
_See [crd_checks.go](./crd_checks.go) and | ||
_v1alpha1testing is alias for package `knative.dev/serving/pkg/testing/v1alpha1`_ | ||
|
||
_For knative crd state, for example `Config`. You can see the code in [configuration.go](./v1alpha1/configuration.go). | ||
For kubernetes objects see | ||
[kube_checks.go](https://github.com/knative/pkg/blob/master/test/kube_checks.go)._ | ||
|
||
### Verify resource state transitions | ||
|
||
To use the [check functions](#check-knative-serving-resources) you must provide | ||
a function to check the state. Some of the expected transition states (as | ||
defined in [the Knative Serving spec](../docs/spec/spec.md)) are expressed in | ||
functions in [states.go](./states.go). | ||
defined in [the Knative Serving spec](https://github.com/knative/docs/blob/master/docs/serving/spec/knative-api-specification-1.0.md)) | ||
adrcunha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
, for example `v1alpha1/Revision` state, are expressed in | ||
function in [revision.go](./v1alpha1/revision.go). | ||
|
||
For example when a `Revision` has been created, the system will start the | ||
resources required to actually serve it, and then the `Revision` object will be | ||
updated to indicate it is ready. This can be polled with `test.IsRevisionReady`: | ||
updated to indicate it is ready. This can be polled with `v1alpha1testing.IsRevisionReady`: | ||
|
||
```go | ||
err := test.WaitForRevisionState(clients.ServingClient, revisionName, test.IsRevisionReady(revisionName)) | ||
err := v1alpha1testing.WaitForRevisionState(clients.ServingAlphaClient, revName, v1alpha1testing.IsRevisionReady, "RevisionIsReady") | ||
if err != nil { | ||
t.Fatalf("Revision %s did not become ready to serve traffic: %v", revisionName, err) | ||
t.Fatalf("The Revision %q did not become ready: %v", revName, err) | ||
} | ||
``` | ||
_v1alpha1testing is alias for package `knative.dev/serving/pkg/testing/v1alpha1`_ | ||
|
||
Once the `Revision` is created, all traffic for a `Route` should be routed to | ||
it. This can be polled with `test.AllRouteTrafficAtRevision`: | ||
it. This can be polled with `v1alpha1testing.AllRouteTrafficAtRevision`: | ||
|
||
```go | ||
err = test.WaitForRouteState(clients.ServingClient, routeName, test.AllRouteTrafficAtRevision(routeName, revisionName)) | ||
err := v1alpha1testing.CheckRouteState(clients.ServingAlphaClient, names.Route, v1alpha1testing.AllRouteTrafficAtRevision(names)) | ||
if err != nil { | ||
t.Fatalf("The Route %s was not updated to route traffic to the Revision %s: %v", routeName, revisionName, err) | ||
t.Fatalf("The Route %s was not updated to route traffic to the Revision %s: %v", names.Route, names.Revision, err) | ||
} | ||
``` | ||
|
||
_See [states.go](./states.go)._ | ||
_See [route.go](./v1alpha1/route.go)._ | ||
|
||
### Generate boilerplate CRDs | ||
|
||
|
@@ -238,15 +261,16 @@ with a randomized name: | |
|
||
```go | ||
func TestSomeAwesomeFeature(t *testing.T) { | ||
var names test.ResourceNames | ||
names.Config := test.ObjectNameForTest(t) | ||
_, err := clients.ServingClient.Create(test.Configuration(namespaceName, names, imagePath)) | ||
if err != nil { | ||
// handle error case | ||
} | ||
// more testing | ||
var names test.ResourceNames | ||
names.Config := test.ObjectNameForTest(t) | ||
_, err := clients.ServingClient.Create(test.Configuration(namespaceName, names, imagePath)) | ||
if err != nil { | ||
// handle error case | ||
} | ||
// more testing | ||
} | ||
``` | ||
_test is package `knative.dev/serving/test`_ | ||
|
||
Please expand these functions as more use cases are tested. | ||
|
||
|
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.
Should these be on the same indentation level as the last three flags?
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
knative/pkg/test
flag andknative/serving/test
flag can be same level.The
--dockerrepo
,--tag
,--ingressendpoint
belong toknative/pkg/test
. They are subset ofknative/pkg/test
flag.The last three flags are special flag for
knative/serving/test
.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.
Ah got it. Thanks for the clarification!