diff --git a/Makefile b/Makefile index e7e7151..ff96f87 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,8 @@ TEST_FUNCTIONS = \ test-logger \ redirector-test \ secret-string \ - secret-bytes + secret-bytes \ + memory-limit TEST_SECRETS = \ secret-string \ diff --git a/tests/deploy_test.go b/tests/deploy_test.go index 9031348..a5edb1a 100644 --- a/tests/deploy_test.go +++ b/tests/deploy_test.go @@ -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" @@ -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 @@ -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 { @@ -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) { diff --git a/tests/function_helpers_test.go b/tests/function_helpers_test.go index 0882d62..a8c047d 100644 --- a/tests/function_helpers_test.go +++ b/tests/function_helpers_test.go @@ -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" ) @@ -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 { @@ -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 } diff --git a/tests/invoke_test.go b/tests/invoke_test.go index 2eddd05..ec853ce 100644 --- a/tests/invoke_test.go +++ b/tests/invoke_test.go @@ -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{}, @@ -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"}, @@ -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"}, diff --git a/tests/main_test.go b/tests/main_test.go index 94b8b46..66098a0 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -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) @@ -132,6 +134,8 @@ type Config struct { // registry prefix for private registry RegistryPrefix string + + SupportCPULimits bool } func FromEnv(config *Config) {