Skip to content

Commit

Permalink
small changes to remover and ns added to its tests
Browse files Browse the repository at this point in the history
Signed-off-by: gauron99 <fridrich.david19@gmail.com>
  • Loading branch information
gauron99 committed Jan 31, 2024
1 parent 01a486b commit 80e54c7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
4 changes: 3 additions & 1 deletion cmd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"knative.dev/func/pkg/mock"
)

const namespace = "func"

// Test_NewTestClient ensures that the convenience method for
// constructing a mocked client for testing properly considers options:
// options provided to the factory constructor are considered exaustive,
Expand All @@ -26,7 +28,7 @@ func Test_NewTestClient(t *testing.T) {
client, _ := clientFn(ClientConfig{}, fn.WithDescriber(describer))

// Trigger an invocation of the mocks
err := client.Remove(context.Background(), fn.Function{Name: "test"}, true)
err := client.Remove(context.Background(), fn.Function{Name: "test", Deploy: fn.DeploySpec{Namespace: namespace}}, true)
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ func TestDelete_Default(t *testing.T) {
// function explicitly as an argument invokes the remover appropriately.
func TestDelete_ByName(t *testing.T) {
var (
root = fromTempDirectory(t)
testname = "testname" // explicit name for the function
remover = mock.NewRemover() // with a mock remover
err error
root = fromTempDirectory(t)
testname = "testname" // explicit name for the function
testnamespace = "testnamespace" // explicit namespace for the function
remover = mock.NewRemover() // with a mock remover
err error
)

// Remover fails the test if it receives the incorrect name
Expand All @@ -86,9 +87,13 @@ func TestDelete_ByName(t *testing.T) {
t.Fatal(err)
}

// simulate deployed namespace for the client Remover
f.Deploy.Namespace = testnamespace

if err = f.Write(); err != nil {
t.Fatal(err)
}

// Create a command with a client constructor fn that instantiates a client
// with a mocked remover.
cmd := NewDeleteCmd(NewTestClient(fn.WithRemover(remover)))
Expand Down
15 changes: 13 additions & 2 deletions pkg/functions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,8 @@ func (c *Client) Remove(ctx context.Context, cfg Function, deleteAll bool) error
// If name is provided, it takes precedence.
// Otherwise load the function defined at root.
functionName := cfg.Name
functionNamespace := cfg.Deploy.Namespace

if cfg.Name == "" {
f, err := NewFunction(cfg.Root)
if err != nil {
Expand All @@ -986,22 +988,31 @@ func (c *Client) Remove(ctx context.Context, cfg Function, deleteAll bool) error
return fmt.Errorf("function at %v can not be removed unless initialized. Try removing by name", f.Root)
}
functionName = f.Name
if functionNamespace == "" && f.Deploy.Namespace != "" {
functionNamespace = f.Deploy.Namespace
}
cfg = f
}

if functionName == "" {
return ErrNameRequired
}
if functionNamespace == "" {
return ErrNamespaceRequired
}

// Delete Knative Service and dependent resources in parallel
fmt.Fprintf(os.Stderr, "Removing Knative Service: %v in namespace '%v'\n", functionName, cfg.Deploy.Namespace)
fmt.Fprintf(os.Stderr, "Removing Knative Service: %v in namespace '%v'\n", functionName, functionNamespace)
errChan := make(chan error)
go func() {
errChan <- c.remover.Remove(ctx, functionName, cfg.Deploy.Namespace)
errChan <- c.remover.Remove(ctx, functionName, functionNamespace)
}()

var errResources error
if deleteAll {
fmt.Fprintf(os.Stderr, "Removing Knative Service '%v' and all dependent resources\n", functionName)
// TODO: might not be necessary
cfg.Deploy.Namespace = functionNamespace
errResources = c.pipelinesProvider.Remove(ctx, cfg)
}

Expand Down
12 changes: 2 additions & 10 deletions pkg/knative/remover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package knative
import (
"context"
"fmt"
"os"
"time"

apiErrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -22,17 +23,8 @@ type Remover struct {
}

func (remover *Remover) Remove(ctx context.Context, name, ns string) (err error) {
// if namespace is not provided for any reason, use the active namespace
// otherwise just throw an error. I dont think this should default to any namespace
// because its a remover, therefore we dont want to just assume a default namespace
// to delete a function from. Use provided, get the current one or none at all.

if ns == "" {
ns = ActiveNamespace()
}

if ns == "" {
fmt.Print("normal error in Remove, no namespace found here\n")
fmt.Fprintf(os.Stderr, "no namespace defined when trying to delete a function in knative remover")
return fn.ErrNamespaceRequired
}

Expand Down

0 comments on commit 80e54c7

Please sign in to comment.