Skip to content

Commit

Permalink
fix: test and namespace isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
lkingland committed Feb 28, 2024
1 parent 7e95ca2 commit 2cc0396
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 180 deletions.
3 changes: 2 additions & 1 deletion cmd/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

fn "knative.dev/func/pkg/functions"
"knative.dev/func/pkg/mock"
. "knative.dev/func/pkg/testing"
)

// TestBuild_BuilderPersists ensures that the builder chosen is read from
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestBuild_RegistryOrImageRequired(t *testing.T) {
// - Push not triggered after an unsuccessful build
// - Push can be disabled
func TestBuild_Push(t *testing.T) {
root := fromTempDirectory(t)
root := FromTempDirectory(t)

f := fn.Function{
Root: root,
Expand Down
5 changes: 2 additions & 3 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewClient(cfg ClientConfig, options ...fn.Option) (*fn.Client, func()) {
var (
t = newTransport(cfg.InsecureSkipVerify) // may provide a custom impl which proxies
c = newCredentialsProvider(config.Dir(), t) // for accessing registries
d = newKnativeDeployer(cfg.Namespace, cfg.Verbose)
d = newKnativeDeployer(cfg.Verbose)
pp = newTektonPipelinesProvider(cfg.Namespace, c, cfg.Verbose)
o = []fn.Option{ // standard (shared) options for all commands
fn.WithVerbose(cfg.Verbose),
Expand Down Expand Up @@ -128,9 +128,8 @@ func newTektonPipelinesProvider(namespace string, creds docker.CredentialsProvid
return tekton.NewPipelinesProvider(options...)
}

func newKnativeDeployer(namespace string, verbose bool) fn.Deployer {
func newKnativeDeployer(verbose bool) fn.Deployer {
options := []knative.DeployerOpt{
knative.WithDeployerNamespace(namespace),
knative.WithDeployerVerbose(verbose),
knative.WithDeployerDecorator(deployDecorator{}),
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"errors"
"testing"

. "knative.dev/func/pkg/testing"
"knative.dev/func/pkg/utils"
)

// TestCreate_Execute ensures that an invocation of create with minimal settings
// and valid input completes without error; degenerate case.
func TestCreate_Execute(t *testing.T) {
_ = fromTempDirectory(t)
_ = FromTempDirectory(t)

cmd := NewCreateCmd(NewClient)
cmd.SetArgs([]string{"--language", "go", "myfunc"})
Expand All @@ -23,7 +24,7 @@ func TestCreate_Execute(t *testing.T) {
// TestCreate_NoRuntime ensures that an invocation of create must be
// done with a runtime.
func TestCreate_NoRuntime(t *testing.T) {
_ = fromTempDirectory(t)
_ = FromTempDirectory(t)

cmd := NewCreateCmd(NewClient)
cmd.SetArgs([]string{"myfunc"}) // Do not use test command args
Expand All @@ -38,7 +39,7 @@ func TestCreate_NoRuntime(t *testing.T) {
// TestCreate_WithNoRuntime ensures that an invocation of create must be
// done with one of the valid runtimes only.
func TestCreate_WithInvalidRuntime(t *testing.T) {
_ = fromTempDirectory(t)
_ = FromTempDirectory(t)

cmd := NewCreateCmd(NewClient)
cmd.SetArgs([]string{"--language", "invalid", "myfunc"})
Expand All @@ -53,7 +54,7 @@ func TestCreate_WithInvalidRuntime(t *testing.T) {
// TestCreate_InvalidTemplate ensures that an invocation of create must be
// done with one of the valid templates only.
func TestCreate_InvalidTemplate(t *testing.T) {
_ = fromTempDirectory(t)
_ = FromTempDirectory(t)

cmd := NewCreateCmd(NewClient)
cmd.SetArgs([]string{"--language", "go", "--template", "invalid", "myfunc"})
Expand All @@ -68,7 +69,7 @@ func TestCreate_InvalidTemplate(t *testing.T) {
// TestCreate_ValidatesName ensures that the create command only accepts
// DNS-1123 labels for function name.
func TestCreate_ValidatesName(t *testing.T) {
_ = fromTempDirectory(t)
_ = FromTempDirectory(t)

// Execute the command with a function name containing invalid characters and
// confirm the expected error is returned
Expand All @@ -84,7 +85,7 @@ func TestCreate_ValidatesName(t *testing.T) {
// TestCreate_ConfigOptional ensures that the system can be used without
// any additional configuration being required.
func TestCreate_ConfigOptional(t *testing.T) {
_ = fromTempDirectory(t)
_ = FromTempDirectory(t)

t.Setenv("XDG_CONFIG_HOME", t.TempDir())

Expand Down
13 changes: 7 additions & 6 deletions cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (

fn "knative.dev/func/pkg/functions"
"knative.dev/func/pkg/mock"
. "knative.dev/func/pkg/testing"
)

// TestDelete_Default ensures that the deployed function is deleted correctly
// with default options
func TestDelete_Default(t *testing.T) {
var (
root = fromTempDirectory(t)
root = FromTempDirectory(t)
namespace = "myns"
remover = mock.NewRemover()
err error
Expand Down Expand Up @@ -61,7 +62,7 @@ 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)
root = FromTempDirectory(t)
testname = "testname" // explicit name for the function
testnamespace = "testnamespace" // explicit namespace for the function
remover = mock.NewRemover() // with a mock remover
Expand Down Expand Up @@ -141,7 +142,7 @@ func TestDelete_Namespace(t *testing.T) {
// ignores the the function on disk
func TestDelete_NamespaceFlagPriority(t *testing.T) {
var (
root = fromTempDirectory(t)
root = FromTempDirectory(t)
namespace = "myns"
namespace2 = "myns2"
remover = mock.NewRemover()
Expand Down Expand Up @@ -184,7 +185,7 @@ func TestDelete_NamespaceFlagPriority(t *testing.T) {
// TestDelete_NamespaceWithoutNameFails ensures that providing wrong argument
// combination fails nice and fast (no name of the Function)
func TestDelete_NamespaceWithoutNameFails(t *testing.T) {
_ = fromTempDirectory(t)
_ = FromTempDirectory(t)

cmd := NewDeleteCmd(NewTestClient())
cmd.SetArgs([]string{"--namespace=myns"})
Expand All @@ -196,7 +197,7 @@ func TestDelete_NamespaceWithoutNameFails(t *testing.T) {
// TestDelete_ByProject ensures that running delete with a valid project as its
// context invokes remove and with the correct name (reads name from func.yaml)
func TestDelete_ByProject(t *testing.T) {
_ = fromTempDirectory(t)
_ = FromTempDirectory(t)

// Write a func.yaml config which specifies a name
funcYaml := `name: bar
Expand Down Expand Up @@ -248,7 +249,7 @@ func TestDelete_ByPath(t *testing.T) {

// A mock remover which will be sampled to ensure it is not invoked.
remover = mock.NewRemover()
root = fromTempDirectory(t)
root = FromTempDirectory(t)
err error
namespace = "func"
)
Expand Down
Loading

0 comments on commit 2cc0396

Please sign in to comment.