-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retrieval of additional resources
Add unit tests
- Loading branch information
1 parent
b5dab92
commit 3c482d8
Showing
2 changed files
with
313 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,285 @@ | ||
package kube | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/skupperproject/skupper/internal/cmd/skupper/common" | ||
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils" | ||
|
||
fakeclient "github.com/skupperproject/skupper/internal/kube/client/fake" | ||
"github.com/skupperproject/skupper/pkg/apis/skupper/v2alpha1" | ||
"github.com/spf13/cobra" | ||
"gotest.tools/v3/assert" | ||
appsv1 "k8s.io/api/apps/v1" | ||
v12 "k8s.io/api/core/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
restclient "k8s.io/client-go/rest" | ||
) | ||
|
||
func TestCmdDebug_ValidateInput(t *testing.T) { | ||
type test struct { | ||
name string | ||
args []string | ||
flags common.CommandDebugFlags | ||
k8sObjects []runtime.Object | ||
skupperObjects []runtime.Object | ||
skupperErrorMessage string | ||
expectedErrors []string | ||
restconfig bool | ||
} | ||
|
||
testTable := []test{ | ||
{ | ||
name: "no filename", | ||
flags: common.CommandDebugFlags{}, | ||
args: []string{}, | ||
expectedErrors: []string{"filename must be configured"}, | ||
restconfig: true, | ||
}, | ||
{ | ||
name: "too many args", | ||
flags: common.CommandDebugFlags{}, | ||
args: []string{"test", "not-valid"}, | ||
expectedErrors: []string{"only one argument is allowed for this command"}, | ||
restconfig: true, | ||
}, | ||
{ | ||
name: "too many args", | ||
flags: common.CommandDebugFlags{}, | ||
args: []string{""}, | ||
expectedErrors: []string{"filename must not be empty"}, | ||
restconfig: true, | ||
}, | ||
{ | ||
name: "rest empty", | ||
flags: common.CommandDebugFlags{}, | ||
args: []string{"test"}, | ||
expectedErrors: []string{"failed setting up command"}, | ||
restconfig: false, | ||
}, | ||
{ | ||
name: "ok", | ||
flags: common.CommandDebugFlags{}, | ||
args: []string{"test"}, | ||
expectedErrors: []string{}, | ||
restconfig: true, | ||
}, | ||
} | ||
|
||
for _, test := range testTable { | ||
t.Run(test.name, func(t *testing.T) { | ||
var rest restclient.Config | ||
cmd, err := newCmdDebugWithMocks("test", test.k8sObjects, test.skupperObjects, "") | ||
assert.Assert(t, err) | ||
if test.restconfig { | ||
cmd.Rest = &rest | ||
} | ||
cmd.Flags = &test.flags | ||
|
||
actualErrors := cmd.ValidateInput(test.args) | ||
|
||
actualErrorsMessages := utils.ErrorsToMessages(actualErrors) | ||
|
||
assert.DeepEqual(t, actualErrorsMessages, test.expectedErrors) | ||
|
||
}) | ||
} | ||
} | ||
|
||
func TestCmdDebug_Run(t *testing.T) { | ||
type test struct { | ||
name string | ||
DebugName string | ||
flags common.CommandDebugFlags | ||
k8sObjects []runtime.Object | ||
skupperObjects []runtime.Object | ||
skupperErrorMessage string | ||
errorMessage string | ||
} | ||
|
||
testTable := []test{ | ||
{ | ||
name: "default", | ||
flags: common.CommandDebugFlags{}, | ||
k8sObjects: []runtime.Object{ | ||
&appsv1.Deployment{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: "apps/v1", | ||
Kind: "Deployment", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "skupper-controller", | ||
Namespace: "test", | ||
Labels: map[string]string{ | ||
"application": "skupper-controller", | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Selector: &v1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"application": "skupper-controller", | ||
}, | ||
}, | ||
Template: v12.PodTemplateSpec{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "skupper-controller", | ||
Namespace: "test", | ||
Labels: map[string]string{ | ||
"application": "skupper-controller", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
&v12.Pod{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: "apps/v1", | ||
Kind: "Pod", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "skupper-controller-cbbd7c69c-9dc55", | ||
Namespace: "test", | ||
Labels: map[string]string{ | ||
"application": "skupper-controller", | ||
}, | ||
}, | ||
Spec: v12.PodSpec{ | ||
Containers: []v12.Container{ | ||
{ | ||
Name: "controller", | ||
}, | ||
}, | ||
}, | ||
}, | ||
&v12.Service{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: "apps/v1", | ||
Kind: "Service", | ||
}, | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "skupper-router", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v12.ConfigMap{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "skupper-router", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v12.Secret{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: "v1", | ||
Kind: "Secret", | ||
}, | ||
}, | ||
}, | ||
skupperObjects: []runtime.Object{ | ||
&v2alpha1.AccessGrant{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-token", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.AccessToken{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-token", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.Connector{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-connector", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.Listener{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-listener", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.Site{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "the-site", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.Certificate{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "link-test", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.Link{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-link", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.AttachedConnectorBinding{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-attachedConnectorBinding", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.AttachedConnector{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-attachedConnector", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.RouterAccess{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-routerAccess", | ||
Namespace: "test", | ||
}, | ||
}, | ||
&v2alpha1.SecuredAccess{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "my-securedAccess", | ||
Namespace: "test", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testTable { | ||
cmd, err := newCmdDebugWithMocks("test", test.k8sObjects, test.skupperObjects, test.skupperErrorMessage) | ||
assert.Assert(t, err) | ||
cmd.Flags = &test.flags | ||
cmd.Namespace = "test" | ||
cmd.fileName = "/tmp/test" | ||
cmd.CobraCmd = &cobra.Command{Use: "test"} | ||
defer os.Remove("/tmp/test.tar.gz") //clean up | ||
t.Run(test.name, func(t *testing.T) { | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
assert.Check(t, test.errorMessage == err.Error()) | ||
} else { | ||
assert.Check(t, err == nil) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// --- helper methods | ||
|
||
func newCmdDebugWithMocks(namespace string, k8sObjects []runtime.Object, skupperObjects []runtime.Object, fakeSkupperError string) (*CmdDebug, error) { | ||
|
||
client, err := fakeclient.NewFakeClient(namespace, k8sObjects, skupperObjects, fakeSkupperError) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cmdDebug := &CmdDebug{ | ||
Client: client.GetSkupperClient().SkupperV2alpha1(), | ||
KubeClient: client.GetKubeClient(), | ||
Namespace: namespace, | ||
} | ||
|
||
return cmdDebug, nil | ||
} |