Skip to content

Commit

Permalink
Add retrieval of additional resources
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
lynnemorrison committed Dec 11, 2024
1 parent b5dab92 commit 3c482d8
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 3 deletions.
32 changes: 29 additions & 3 deletions internal/cmd/skupper/debug/kube/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ func (cmd *CmdDebug) ValidateInput(args []string) []error {

// Validate dump file name
if len(args) < 1 {
validationErrors = append(validationErrors, fmt.Errorf("file name must be configured"))
validationErrors = append(validationErrors, fmt.Errorf("filename must be configured"))
} else if len(args) > 1 {
validationErrors = append(validationErrors, fmt.Errorf("only one argument is allowed for this command"))
} else if args[0] == "" {
validationErrors = append(validationErrors, fmt.Errorf("file name must not be empty"))
validationErrors = append(validationErrors, fmt.Errorf("filename must not be empty"))
} else {
ok, err := fileStringValidator.Evaluate(args[0])
if !ok {
validationErrors = append(validationErrors, fmt.Errorf("file name is not valid: %s", err))
validationErrors = append(validationErrors, fmt.Errorf("filename is not valid: %s", err))
} else {
cmd.fileName = args[0]
}
Expand Down Expand Up @@ -297,6 +297,32 @@ func (cmd *CmdDebug) Run() error {
}
}

routerAccessList, err := cmd.Client.RouterAccesses(cmd.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}

for _, site := range routerAccessList.Items {
s := site.DeepCopy()
err := writeObject(s, "/skupper-resource/routerAccess/"+s.Name, ".yaml", tw)
if err != nil {
return err
}
}

securedAccessList, err := cmd.Client.SecuredAccesses(cmd.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}

for _, site := range securedAccessList.Items {
s := site.DeepCopy()
err := writeObject(s, "/skupper-resource/securedAccess/"+s.Name, ".yaml", tw)
if err != nil {
return err
}
}

fmt.Println("Skupper dump details written to compressed archive: ", dumpFile)
return nil
}
Expand Down
284 changes: 284 additions & 0 deletions internal/cmd/skupper/debug/kube/debug_test.go
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
}

0 comments on commit 3c482d8

Please sign in to comment.