diff --git a/cmd/deploy_test.go b/cmd/deploy_test.go index 8f465fd77f..47e7d0f71b 100644 --- a/cmd/deploy_test.go +++ b/cmd/deploy_test.go @@ -13,6 +13,8 @@ import ( "github.com/ory/viper" "github.com/spf13/cobra" + apiErrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/runtime/schema" "knative.dev/func/pkg/builders" "knative.dev/func/pkg/config" fn "knative.dev/func/pkg/functions" @@ -1611,3 +1613,65 @@ func TestReDeploy_ErrorOnRegistryChangeWithoutBuild(t *testing.T) { t.Fatalf("expected error message '%v' but got '%v'", expectedError, err.Error()) } } + +// TestWarnDuringOldFuncUndeploy assures that warning is printed out when old Function's +// service is not available (is already deleted manualy or the namespace doesnt exist etc.) +// Warning: Cant undeploy Function in namespace +func TestWarnDuringOldFuncUndeploy(t *testing.T) { + var ( + root = fromTempDirectory(t) + nsOne = "nsone" + nsTwo = "nstwo" + remover = mock.NewRemover() + ) + + // Create a basic go Function + f := fn.Function{ + Runtime: "go", + Root: root, + } + _, err := fn.New().Init(f) + if err != nil { + t.Fatal(err) + } + + // Deploy the function to ns "nsone" + cmd := NewDeployCmd(NewTestClient( + fn.WithDeployer(mock.NewDeployer()), + fn.WithRegistry(TestRegistry), + fn.WithRemover(remover))) + + cmd.SetArgs([]string{fmt.Sprintf("--namespace=%s", nsOne)}) + err = cmd.Execute() + if err != nil { + t.Fatal(err) + } + + // Simulate remover error + remover.RemoveFn = func(n string) error { + return apiErrors.NewNotFound(schema.GroupResource{Group: "", Resource: "Namespace"}, nsOne) + } + + // Second Deploy with different namespace + cmd.SetArgs([]string{fmt.Sprintf("--namespace=%s", nsTwo)}) + + stdout := strings.Builder{} + cmd.SetOut(&stdout) + err = cmd.Execute() + + expectedWarning := fmt.Sprintf("Warning: Cant undeploy Function in namespace '%s' - service not found. Namespace/Service might be deleted already", nsOne) + + // ASSERT + + // Needs to pass since the error is set to nil with the warning message below + // that is printed out + if err != nil { + t.Fatal(err) + } + + // Ensure output contained warning message + if !strings.Contains(stdout.String(), expectedWarning) { + t.Log("STDOUT:\n" + stdout.String()) + t.Fatalf("Expected warning not found:\n%v", expectedWarning) + } +} diff --git a/pkg/functions/client_test.go b/pkg/functions/client_test.go index b25508b2df..6178960bf1 100644 --- a/pkg/functions/client_test.go +++ b/pkg/functions/client_test.go @@ -903,8 +903,8 @@ func TestClient_Update(t *testing.T) { } // TestClient_Deploy_RegistryUpdate ensures that deploying a Function updates -// its image member on initial deploy, and on subsequent deploys only -// if reset to it zero value. +// its image member on initial deploy, and on subsequent deploys where f.Image +// takes precedence func TestClient_Deploy_RegistryUpdate(t *testing.T) { root, rm := Mktemp(t) defer rm()