Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions test/conformance/resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// +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 (
"fmt"
"net/http"
"strings"
"testing"

pkgTest "github.com/knative/pkg/test"
"github.com/knative/pkg/test/logging"
"github.com/knative/pkg/test/spoof"
"github.com/knative/serving/test"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)

func TestCustomResourcesLimits(t *testing.T) {
clients := setup(t)

//add test case specific name to its own logger
logger := logging.GetContextLogger("TestCustomResourcesLimits")

logger.Infof("Creating a new Route and Configuration")
resources := corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("350Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("350Mi"),
},
}

names := test.ResourceNames{
Service: test.AppendRandomString("test-resource-limits-", logger),
Image: "bloatingcow",
}

test.CleanupOnInterrupt(func() { tearDown(clients, names) }, logger)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be before CreateRunLatestServiceReady?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

defer tearDown(clients, names)

objects, err := test.CreateRunLatestServiceReady(logger, clients, &names, &test.Options{ContainerResources: resources})
if err != nil {
t.Fatalf("Failed to create initial Service %v: %v", names.Service, err)
}
domain := objects.Route.Status.Domain

want := "Moo!"
_, err = pkgTest.WaitForEndpointState(
clients.KubeClient,
logger,
domain,
pkgTest.Retrying(pkgTest.MatchesBody(want), http.StatusNotFound),
"ResourceTestServesText",
test.ServingFlags.ResolvableDomain)
if err != nil {
t.Fatalf("The endpoint for Route %s at domain %s didn't serve the expected text \"%s\": %v", names.Route, domain, want, err)
}

sendPostRequest := func(resolvableDomain bool, domain string, query string) (*spoof.Response, error) {
logger.Infof("The domain of request is %s and its query is %s", domain, query)
client, err := pkgTest.NewSpoofingClient(clients.KubeClient, logger, domain, resolvableDomain)
if err != nil {
return nil, err
}

req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s?%s", domain, query), nil)
if err != nil {
return nil, err
}
return client.Do(req)
}

pokeCowForMB := func(mb int) error {
response, err := sendPostRequest(test.ServingFlags.ResolvableDomain, domain, fmt.Sprintf("memory_in_mb=%d", mb))
if err != nil {
return err
}
if want != strings.TrimSpace(string(response.Body)) {
return fmt.Errorf("the response %q is not equal to expected response %q", string(response.Body), want)
}
return nil
}

logger.Info("Querying the application to see if the memory limits are enforced.")
if err := pokeCowForMB(100); err != nil {
t.Fatalf("Didn't get a response from bloating cow with %d MBs of Memory: %v", 100, err)
}

if err := pokeCowForMB(200); err != nil {
t.Fatalf("Didn't get a response from bloating cow with %d MBs of Memory: %v", 200, err)
}

if err := pokeCowForMB(500); err == nil {
t.Fatalf("We shouldn't have got a response from bloating cow with %d MBs of Memory: %v", 500, err)
}
}
157 changes: 0 additions & 157 deletions test/e2e/resources_test.go

This file was deleted.