mirror of https://github.com/knative/func.git
chore: add unit test cases for instances (#1314)
* chore: add unit test cases for instances Signed-off-by: Lance Ball <lball@redhat.com> * fixup: account for windows Signed-off-by: Lance Ball <lball@redhat.com> * fixup: account for windows for real Signed-off-by: Lance Ball <lball@redhat.com> Signed-off-by: Lance Ball <lball@redhat.com>
This commit is contained in:
parent
318d3e1621
commit
cc0fb82e89
|
@ -16,6 +16,7 @@ var (
|
|||
ErrNotRunning = errors.New("function not running")
|
||||
ErrRootRequired = errors.New("function root path is required")
|
||||
ErrEnvironmentNotFound = errors.New("environment not found")
|
||||
ErrMismatchedName = errors.New("name passed does not match name of the function at root")
|
||||
)
|
||||
|
||||
// Instances manager
|
||||
|
@ -100,9 +101,7 @@ func (s *Instances) Remote(ctx context.Context, name, root string) (Instance, er
|
|||
return Instance{}, err
|
||||
}
|
||||
if name != f.Name {
|
||||
return Instance{}, errors.New(
|
||||
"name passed does not match name of the function at root. " +
|
||||
"Try passing either name or root rather than both.")
|
||||
return Instance{}, errors.New("name passed does not match name of the function at root")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
//go:build !integration
|
||||
// +build !integration
|
||||
|
||||
package function
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
. "knative.dev/kn-plugin-func/testing"
|
||||
)
|
||||
|
||||
// TestInstances_LocalErrors tests the three possible error states for a function
|
||||
// when attempting to access a local instance.
|
||||
func TestInstances_LocalErrors(t *testing.T) {
|
||||
root, rm := Mktemp(t)
|
||||
defer rm()
|
||||
|
||||
// Create a function that will not be running
|
||||
if err := New().Create(Function{Runtime: "go", Root: root}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Load the function
|
||||
f, err := NewFunction(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
f Function
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "Not running", // Function exists but is not running
|
||||
f: f,
|
||||
want: ErrNotRunning,
|
||||
},
|
||||
{
|
||||
name: "Not initialized", // A function directory is provided, but no function exists
|
||||
f: Function{Root: "testdata/not-initialized"},
|
||||
want: ErrNotInitialized,
|
||||
},
|
||||
{
|
||||
name: "Root required", // No root directory is provided
|
||||
f: Function{},
|
||||
want: ErrRootRequired,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
i := Instances{}
|
||||
if _, err := i.Local(context.TODO(), tt.f); err != tt.want {
|
||||
t.Errorf("Local() error = %v, wantErr %v", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestInstance_RemoteErrors tests the possible error states for a function when
|
||||
// attempting to access a remote instance.
|
||||
func TestInstance_RemoteErrors(t *testing.T) {
|
||||
root, rm := Mktemp(t)
|
||||
defer rm()
|
||||
|
||||
// Create a function that will not be running
|
||||
if err := New().Create(Function{Runtime: "go", Root: root}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Load the function
|
||||
_, err := NewFunction(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var badRoot = "func.yaml: no such file or directory"
|
||||
if runtime.GOOS == "windows" {
|
||||
badRoot = "The system cannot find the path specified"
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
root string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "foo",
|
||||
root: "foo", // bad root
|
||||
want: badRoot,
|
||||
},
|
||||
{
|
||||
name: "foo", // name and root are mismatched
|
||||
root: root,
|
||||
want: "name passed does not match name of the function at root",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
i := Instances{}
|
||||
if _, err := i.Remote(context.TODO(), tt.name, tt.root); !strings.Contains(err.Error(), tt.want) {
|
||||
t.Errorf("Remote() %v error = %v, wantErr %v", "Mismatched name and root", err.Error(), tt.want)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue