From 318f9e59ae3db1551209ecc52f962daaebe9c3c0 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Thu, 30 Jul 2020 23:31:01 +0800 Subject: [PATCH] Correct rebase issue and add e2e test --- .gitignore | 4 ++- DEVELOPMENT.md | 6 ++++ cmd/rudrx/main.go | 2 +- pkg/cmd/traits.go | 13 +++---- pkg/cmd/util/template_types.go | 4 +-- pkg/test/basic_test.go | 63 ++++++++++++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 pkg/test/basic_test.go diff --git a/.gitignore b/.gitignore index 5da7ebb1f..9bf81af3d 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,6 @@ bin vendor/ # Vscode files -.vscode \ No newline at end of file +.vscode + +pkg/test/rudr diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 197d082f6..067e42491 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -64,5 +64,11 @@ workloaddefinition.core.oam.dev/deployments.apps deployment workloaddefinition.core.oam.dev/statefulsets.apps statefulsets.apps ``` +## Test +``` +$ go test ./pkg/test +ok github.com/cloud-native-application/rudrx/pkg/test 14.662s +``` + ## Make a pull request Remember to write unit-test and e2e test before making a pull request. diff --git a/cmd/rudrx/main.go b/cmd/rudrx/main.go index 8f0b55fe4..b3a648960 100644 --- a/cmd/rudrx/main.go +++ b/cmd/rudrx/main.go @@ -84,7 +84,7 @@ func newCommand() *cobra.Command { cmds.AddCommand( cmd.NewRunCommand(f, client, ioStream, os.Args[1:]), cmd.NewTraitsCommand(f, client, ioStream), - cmd.NewWorkloadsCommand(f, client, ioStream, args), + cmd.NewWorkloadsCommand(f, client, ioStream, os.Args[1:]), cmd.NewBindCommand(f, client, ioStream), cmd.NewInitCommand(f, client, ioStream), cmd.NewDeleteCommand(f, client, ioStream, os.Args[1:]), diff --git a/pkg/cmd/traits.go b/pkg/cmd/traits.go index 5d25e3959..291a2703a 100644 --- a/pkg/cmd/traits.go +++ b/pkg/cmd/traits.go @@ -32,21 +32,22 @@ func NewTraitsCommand(f cmdutil.Factory, c client.Client, ioStreams cmdutil.IOSt } func printTraitList(ctx context.Context, c client.Client, workloadName *string, ioStreams cmdutil.IOStreams) error { - traitList, err := RetrieveTraitsByWorkload(ctx, c, *workloadName) + traitList, err := RetrieveTraitsByWorkload(ctx, c, "", *workloadName) table := uitable.New() table.MaxColWidth = 60 - //if err != nil { - // errMsg := fmt.Sprintf("Listing Trait Definition hit an issue: %s", err) - // cmdutil.PrintErrorMessage(errMsg, 1) - //} + if err != nil { + return fmt.Errorf("Listing Trait Definition hit an issue: %s", err) + } table.AddRow("NAME", "Alias", "DEFINITION", "APPLIES TO", "STATUS") for _, r := range traitList { table.AddRow(r.Name, r.Short, r.Definition, r.AppliesTo, r.Status) } - fmt.Print(table.String()) + ioStreams.Info(table.String()) + + return nil } type TraitMeta struct { diff --git a/pkg/cmd/util/template_types.go b/pkg/cmd/util/template_types.go index 904106885..3d243f081 100644 --- a/pkg/cmd/util/template_types.go +++ b/pkg/cmd/util/template_types.go @@ -32,7 +32,7 @@ type Template struct { // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // Important: Run "make" to regenerate code after modifying this file Alias string `json:alias,omitempty` - Object unstructured.Unstructured `json:"object"` + Object unstructured.Unstructured `json:"object,omitempty"` LastCommandParam string `json:"lastCommandParam,omitempty"` Parameters []Parameter `json:"parameters,omitempty"` } @@ -48,7 +48,7 @@ type Parameter struct { } // ConvertTemplateJson2Object convert spec.extension to object -func ConvertTemplateJson2Object(in runtime.RawExtension) (Template, error) { +func ConvertTemplateJson2Object(in *runtime.RawExtension) (Template, error) { var t Template var extension Template err := json.Unmarshal(in.Raw, &extension) diff --git a/pkg/test/basic_test.go b/pkg/test/basic_test.go new file mode 100644 index 000000000..eece9b8d9 --- /dev/null +++ b/pkg/test/basic_test.go @@ -0,0 +1,63 @@ +package test + +import ( + "os" + "os/exec" + "path" + "testing" + + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/config" +) + +var ( + rudrPath, _ = os.Getwd() +) + +func createKubernetesClient() (client.Client, error) { + c, err := client.New(config.GetConfigOrDie(), client.Options{}) + + return c, err +} + +func TestCreateKubernetesClient(t *testing.T) { + _, err := createKubernetesClient() + if err != nil { + t.Errorf("Failed to create a Kubernetes client: %s", err) + } +} + +// TestBuildCliBinary is to build rudr binary. +func TestBuildCliBinary(t *testing.T) { + rudrPath, err := os.Getwd() + mainPath := path.Join(rudrPath, "../../cmd/rudrx/main.go") + if err != nil { + t.Errorf("Failed to build rudr binary: %s", err) + } + + cmd := exec.Command("go", "build", "-o", path.Join(rudrPath, "rudr"), mainPath) + + stdout, err := cmd.Output() + if err != nil { + t.Errorf("Failed to build rudr binary: %s", err) + } + t.Log(stdout, err) + + // TODO(zzxwill) If this failed, all other test-cases should be terminated + +} + +func Command(name string, arg ...string) *exec.Cmd { + commandName := path.Join(rudrPath, name) + return exec.Command(commandName, arg...) +} + +func TestTraitsList(t *testing.T) { + cmd := Command("rudr", []string{"traits", "list"}...) + stdout, err := cmd.Output() + t.Log(string(stdout), err) + if err != nil { + t.Errorf("Failed to list traits: %s", err) + } + +}