-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change moves the envvars_test.go from e2e to conformance folder to use the test as a conformance test for the environment variables requirement specified in the run-time contract (https://github.com/knative/serving/blob/master/docs/runtime-contract.md) The test image is also changed to be more generic and provide a server that can be used to get information about the environment under which the container runs. Currently the iamge only exposes environment variables defined inside the container by exposing "/envvars" to fetch all the environment variables. The image folder name is also changed to "environment" to indicate this semantic change. Change also adds test/conformance/constants.go to define a way to have constants that can be shared between test-images and tests. Change also adds test/conformance/runtime_contract_types.go to define the runtime contract as go types.
- Loading branch information
1 parent
5f19022
commit d4691ad
Showing
6 changed files
with
273 additions
and
69 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
Copyright 2018 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 conformance | ||
|
||
//constants.go defines constants that are shared between test-images and conformance tests | ||
|
||
//EnvImageServerPort is the port on which the test-image server starts. | ||
// TODO: Modify this port number after https://github.com/knative/serving/issues/2258 is fixed for a stricter verification. | ||
const EnvImageServerPort = 8080 |
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,151 @@ | ||
// +build e2e | ||
|
||
/* | ||
Copyright 2018 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 conformance | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"testing" | ||
|
||
pkgTest "github.com/knative/pkg/test" | ||
"github.com/knative/pkg/test/logging" | ||
"github.com/knative/pkg/test/spoof" | ||
"github.com/knative/serving/pkg/apis/serving/v1alpha1" | ||
serviceresourcenames "github.com/knative/serving/pkg/reconciler/v1alpha1/service/resources/names" | ||
"github.com/knative/serving/test" | ||
"github.com/pkg/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
//fetchEnvVars creates the service and fetches environment variables defined inside the container. | ||
func fetchEnvVars(t *testing.T, logger *logging.BaseLogger, names* test.ResourceNames) ([]byte, error) { | ||
clients := setup(t) | ||
|
||
logger.Info("Creating a new Service") | ||
names.Service = test.AppendRandomString("yashiki", logger) | ||
svc, err := test.CreateLatestService(logger, clients, *names, test.ImagePath("environment")) | ||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("Failed to create Service: %v", err)) | ||
} | ||
names.Route = serviceresourcenames.Route(svc) | ||
names.Config = serviceresourcenames.Configuration(svc) | ||
|
||
test.CleanupOnInterrupt(func() { tearDown(clients, *names) }, logger) | ||
defer tearDown(clients, *names) | ||
|
||
var revisionName string | ||
logger.Info("The Service will be updated with the name of the Revision once it is created") | ||
err = test.WaitForServiceState(clients.ServingClient, names.Service, func(s *v1alpha1.Service) (bool, error) { | ||
if s.Status.LatestCreatedRevisionName != names.Revision { | ||
revisionName = s.Status.LatestCreatedRevisionName | ||
return true, nil | ||
} | ||
return false, nil | ||
}, "ServiceUpdatedWithRevision") | ||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("Service %s was not updated with the new revision: %v", names.Service, err)) | ||
} | ||
names.Revision = revisionName | ||
|
||
logger.Info("When the Service reports as Ready, everything should be ready.") | ||
if err := test.WaitForServiceState(clients.ServingClient, names.Service, test.IsServiceReady, "ServiceIsReady"); err != nil { | ||
return nil, errors.New(fmt.Sprintf("The Service %s was not marked as Ready to serve traffic to Revision %s: %v", names.Service, names.Revision, err)) | ||
} | ||
|
||
logger.Infof("When the Revision can have traffic routed to it, the Route is marked as Ready.") | ||
if err := test.WaitForRouteState(clients.ServingClient, names.Route, test.IsRouteReady, "RouteIsReady"); err != nil { | ||
return nil, errors.New(fmt.Sprintf("The Route %s was not marked as Ready to serve traffic: %v", names.Route, err)) | ||
} | ||
|
||
route, err := clients.ServingClient.Routes.Get(names.Route, metav1.GetOptions{}) | ||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("Error fetching Route %s: %v", names.Route, err)) | ||
} | ||
|
||
url := route.Status.Domain + "/envvars" | ||
resp, err := pkgTest.WaitForEndpointState( | ||
clients.KubeClient, | ||
logger, | ||
url, | ||
pkgTest.Retrying(func(resp *spoof.Response) (bool, error) { | ||
if resp.StatusCode == http.StatusOK { | ||
return true, nil | ||
} | ||
|
||
return true, errors.New(string(resp.Body)) | ||
}, http.StatusNotFound), | ||
"EnvVarsServesText", | ||
test.ServingFlags.ResolvableDomain) | ||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("Failed before reaching desired state : %v", err)) | ||
} | ||
|
||
return resp.Body, nil | ||
} | ||
|
||
//TestShouldEnvVars verifies environment variables that are declared as "SHOULD be set" in runtime-contract | ||
func TestShouldEnvVars(t *testing.T) { | ||
logger := logging.GetContextLogger("TestShouldEnvVars") | ||
var names test.ResourceNames | ||
resp, err := fetchEnvVars(t, logger, &names) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var respValues ShouldEnvvars | ||
if err := json.Unmarshal(resp, &respValues); err != nil { | ||
t.Fatalf("Failed to unmarshall response : %v", err) | ||
} | ||
|
||
expectedValues := ShouldEnvvars { | ||
Service: names.Service, | ||
Configuration : names.Config, | ||
Revision : names.Revision, | ||
} | ||
if respValues != expectedValues { | ||
t.Fatalf("Received response failed to match execpted response. Received: %v Expected: %v", respValues, expectedValues) | ||
} | ||
} | ||
|
||
//TestMustEnvVars verifies environment variables that are declared as "MUST be set" in runtime-contract | ||
func TestMustEnvVars(t *testing.T) { | ||
logger := logging.GetContextLogger("TestMustEnvVars") | ||
var names test.ResourceNames | ||
resp, err := fetchEnvVars(t, logger, &names) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var respValues MustEnvvars | ||
if err := json.Unmarshal(resp, &respValues); err != nil { | ||
t.Fatalf("Failed to unmarshall response : %v", err) | ||
} | ||
|
||
expectedValues := MustEnvvars { | ||
// The port value needs to match the port exposed by the test-image. | ||
// We currently control them by using a common constant, but any change needs synchronization between this check | ||
// and the value used by the test-image. | ||
Port: strconv.Itoa(EnvImageServerPort), | ||
} | ||
if respValues != expectedValues { | ||
t.Fatalf("Received response failed to match execpted response. Received: %v Expected: %v", respValues, expectedValues) | ||
} | ||
} |
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,31 @@ | ||
/* | ||
Copyright 2018 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 conformance | ||
|
||
//runtime_constract_types.go defines types that encapsulate run-time contract requirements as specified here: https://github.com/knative/serving/blob/master/docs/runtime-contract.md | ||
|
||
//ShouldEnvvars defines the environment variables that "SHOULD" be set. | ||
type ShouldEnvvars struct { | ||
Service string `json:"K_SERVICE"` | ||
Configuration string `json:"K_CONFIGURATION"` | ||
Revision string `json:"K_REVISION"` | ||
} | ||
|
||
//MustEnvvars defines environment variables that "MUST" be set. | ||
type MustEnvvars struct { | ||
Port string `json:"PORT"` | ||
} |
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,15 @@ | ||
# Environment test image | ||
|
||
This directory contains the test image used to retrieve environment information under which the container runs. This is used by conformance tests to verify Knative [run-time contract](/docs/runtime-contract.md) | ||
|
||
The image contains a simple Go webserver, `environment.go`, which by default, listens on port defined in the constant [EnvImageServerPort](/test/conformance/constants.go). | ||
|
||
Currently the server exposes: | ||
|
||
* /envvars : To provide a JSON payload containing all the environment variables set inside the container | ||
|
||
## Building | ||
|
||
For details about building and adding new images, see the [section about test | ||
images](/test/README.md#test-images). | ||
|
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,53 @@ | ||
/* | ||
Copyright 2018 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 | ||
https://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 main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"github.com/knative/serving/test/conformance" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/knative/serving/test" | ||
) | ||
|
||
func envvarsHandler(w http.ResponseWriter, r *http.Request) { | ||
envvars := make(map[string]string) | ||
for _, pair := range os.Environ() { | ||
tokens := strings.Split(pair, "=") | ||
envvars[tokens[0]] = tokens[1] | ||
} | ||
|
||
if resp, err := json.Marshal(envvars); err != nil { | ||
fmt.Fprintf(w, fmt.Sprintf("error building response : %v", err)) | ||
} else { | ||
fmt.Fprintf(w, string(resp)) | ||
} | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
log.Print("Environment test app started.") | ||
test.ListenAndServeGracefullyWithPattern(fmt.Sprintf(":%d", conformance.EnvImageServerPort), map[string]func(w http.ResponseWriter, r *http.Request) { | ||
"/envvars" : envvarsHandler, | ||
}) | ||
} | ||
|
This file was deleted.
Oops, something went wrong.