Skip to content
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

Include Test case for Limits #83

Merged
merged 1 commit into from
Apr 6, 2022
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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ TEST_FUNCTIONS = \
test-logger \
redirector-test \
secret-string \
secret-bytes
secret-bytes \
memory-limit

TEST_SECRETS = \
secret-string \
Expand Down
37 changes: 33 additions & 4 deletions tests/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const someAnnotationJson = `{
}`

func Test_Deploy_MetaData(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

imagePath := config.RegistryPrefix + "/" + "functions/alpine:latest"
Expand Down Expand Up @@ -92,6 +92,21 @@ func Test_Deploy_MetaData(t *testing.T) {
Namespace: config.DefaultNamespace,
},
},
{
name: "Deploy with memory limit",
function: types.FunctionDeployment{
Image: imagePath,
Service: "memory-limit",
EnvProcess: "env",
Annotations: &map[string]string{},
Labels: &map[string]string{},
Namespace: config.DefaultNamespace,
Limits: &types.FunctionResources{
Memory: "5Mi",
CPU: "100m",
},
},
},
}

// Add Test case, if CERTIFIER_NAMESPACES defined
Expand Down Expand Up @@ -133,7 +148,7 @@ func Test_Deploy_MetaData(t *testing.T) {
for namespace, expected := range listCases {
actual, err := config.Client.ListFunctions(ctx, namespace)
if err != nil {
t.Fatalf("unable to List function in namspace: %s", err)
t.Fatalf("unable to List function in namspace %s: %s", namespace, err)
}

for _, actualF := range actual {
Expand Down Expand Up @@ -228,8 +243,22 @@ func compareDeployAndStatus(deploy types.FunctionDeployment, status types.Functi
return fmt.Errorf("incorrect Secrets: %s", err)
}

if !reflect.DeepEqual(deploy.Limits, status.Limits) {
return fmt.Errorf("got %v, expected Limits %v", status.Limits, deploy.Limits)
if deploy.Limits != nil {
if status.Limits == nil {
return fmt.Errorf("got nil, expected Limits %v", deploy.Limits)
}

if deploy.Limits.Memory != status.Limits.Memory {
return fmt.Errorf("got %s, expected Requested Limit %s", status.Limits.Memory, deploy.Limits.Memory)
}

if config.SupportCPULimits && deploy.Limits.CPU != status.Limits.CPU {
return fmt.Errorf("got %s, expected Requested Limit %s", status.Limits.CPU, deploy.Limits.CPU)
}
}

if deploy.Limits == nil && status.Limits != nil {
return fmt.Errorf("got %v, expected nil", status.Limits)
}

if !reflect.DeepEqual(deploy.Requests, status.Requests) {
Expand Down
13 changes: 12 additions & 1 deletion tests/function_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

sdk "github.com/openfaas/faas-cli/proxy"
"github.com/openfaas/faas-cli/stack"
"github.com/openfaas/faas-provider/types"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func copyNamespacesTest(cases []FunctionTestCase) []FunctionTestCase {
cases = append(cases, cnCases...)
return cases
}
return make([]FunctionTestCase, 0)
return cases
}

func createDeploymentSpec(test FunctionTestCase) *sdk.DeployFunctionSpec {
Expand All @@ -103,6 +104,16 @@ func createDeploymentSpec(test FunctionTestCase) *sdk.DeployFunctionSpec {
functionRequest.Labels = *test.function.Labels
}

if test.function.Limits != nil {
limits := *test.function.Limits
functionRequest.FunctionResourceRequest = sdk.FunctionResourceRequest{
Limits: &stack.FunctionResources{
Memory: limits.Memory,
CPU: limits.CPU,
},
}
}

return functionRequest
}

Expand Down
8 changes: 4 additions & 4 deletions tests/invoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func invokeWithCustomEnvVarsAndQueryString(t *testing.T, functionRequest *sdk.De

func Test_Invoke(t *testing.T) {
t.Logf("Gateway: %s", config.Gateway)

imagePrefix := config.RegistryPrefix + "/"
cases := []FunctionTestCase{
{
name: "Invoke test with different verbs",
function: types.FunctionDeployment{
Image: "functions/alpine:latest",
Image: imagePrefix + "functions/alpine:latest",
Service: "env-test-verbs",
EnvProcess: "env",
EnvVars: map[string]string{},
Expand All @@ -89,7 +89,7 @@ func Test_Invoke(t *testing.T) {
{
name: "Invoke propogates redirect to the caller",
function: types.FunctionDeployment{
Image: "theaxer/redirector:latest",
Image: imagePrefix + "theaxer/redirector:latest",
Service: "redirector-test",
EnvProcess: "./handler",
EnvVars: map[string]string{"destination": "http://example.com"},
Expand All @@ -99,7 +99,7 @@ func Test_Invoke(t *testing.T) {
{
name: "Invoke with custom env vars and query string",
function: types.FunctionDeployment{
Image: "functions/alpine:latest",
Image: imagePrefix + "functions/alpine:latest",
Service: "env-test",
EnvProcess: "env",
EnvVars: map[string]string{"custom_env": "custom_env_value"},
Expand Down
4 changes: 4 additions & 0 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func TestMain(m *testing.M) {
config.SecretUpdate = false
}

config.SupportCPULimits = config.ProviderName != faasdProviderName

prettyConfig, err := json.MarshalIndent(config, "", "\t")
if err != nil {
log.Fatalf("Config Pretty Print Failed with %s", err)
Expand Down Expand Up @@ -132,6 +134,8 @@ type Config struct {

// registry prefix for private registry
RegistryPrefix string

SupportCPULimits bool
}

func FromEnv(config *Config) {
Expand Down