Skip to content

Commit feb03d0

Browse files
authored
cmd/util/testutils: Add RunCmdAssertion test function (#330)
Adds a new function to the `testutils` package which allow easier testing to be written on the `cmd` layer. All the heavy lifting is done by `testutils.RunCmdAssertion` on the test function, namely it: 1. Initializes the `ecctl.App` singleton from a `testutils.MockCfg`. 2. Sets the cobra `cmd.Command` Err and Out to `*Bytes.Buffer`s. 3. Executes the command with the specified Args. 4. Asserts the returned errors by `cmd.Execute()` 5. Asserts the written data to both the `Err` and `Out` buffers. 6. `ecctl.Cleanup` is called before returning so no instance is set. It is important to note that global flags won't be parsed and instead, must be provided as values to `testutils.MockCfg`. As new global flags are added, the structure should be maintained to contain these. Interaction with the `stdin` hasn't been written yet, but it will be a follow-up patch. Last, the tracker utils have been modified to use `DefaultTestFrequency` when the host equals `api.DefaultMockHost` for ease of testing. Signed-off-by: Marc Lopez <marc5.12@outlook.com>
1 parent d64b9fe commit feb03d0

File tree

9 files changed

+785
-30
lines changed

9 files changed

+785
-30
lines changed

cmd/deployment/create.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
package cmddeployment
1919

2020
import (
21+
"errors"
2122
"fmt"
22-
"os"
2323

2424
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi"
2525
"github.com/elastic/cloud-sdk-go/pkg/api/deploymentapi/depresourceapi"
2626
"github.com/elastic/cloud-sdk-go/pkg/models"
27+
"github.com/elastic/cloud-sdk-go/pkg/multierror"
2728
sdkcmdutil "github.com/elastic/cloud-sdk-go/pkg/util/cmdutil"
2829
"github.com/spf13/cobra"
2930

@@ -34,7 +35,7 @@ import (
3435
var createCmd = &cobra.Command{
3536
Use: "create {--file | --size <int> --zones <string> | --topology-element <obj>}",
3637
Short: "Creates a deployment",
37-
PreRunE: cobra.MaximumNArgs(0),
38+
PreRunE: cobra.NoArgs,
3839
// Switch back to non-temp constants when reads for deployment templates are available on ESS
3940
Long: createLongTemp,
4041
Example: createExampleTemp,
@@ -65,12 +66,19 @@ var createCmd = &cobra.Command{
6566
var appsearchSize, _ = cmd.Flags().GetInt32("appsearch-size")
6667
var appsearchRefID, _ = cmd.Flags().GetString("appsearch-ref-id")
6768

69+
var skipFlagBased = cmd.Flag("deployment-template").Hidden
70+
6871
var payload *models.DeploymentCreateRequest
69-
if err := sdkcmdutil.FileOrStdin(cmd, "file"); err == nil {
70-
err := sdkcmdutil.DecodeDefinition(cmd, "file", &payload)
71-
if err != nil && err != sdkcmdutil.ErrNodefinitionLoaded {
72-
return err
73-
}
72+
if err := sdkcmdutil.FileOrStdin(cmd, "file"); err != nil && skipFlagBased {
73+
return err
74+
}
75+
76+
err := sdkcmdutil.DecodeDefinition(cmd, "file", &payload)
77+
if err := returnErrOnHidden(err, skipFlagBased); err != nil {
78+
merr := multierror.NewPrefixed("failed reading the file definition")
79+
return merr.Append(err,
80+
errors.New("could not read the specified file, please make sure it exists"),
81+
)
7482
}
7583

7684
if payload == nil {
@@ -128,10 +136,10 @@ var createCmd = &cobra.Command{
128136

129137
res, err := deploymentapi.Create(createParams)
130138
if err != nil {
131-
fmt.Fprintln(os.Stderr,
139+
fmt.Fprintln(cmd.ErrOrStderr(),
132140
"The deployment creation returned with an error. Use the displayed request ID to recreate the deployment resources",
133141
)
134-
fmt.Fprintln(os.Stderr, "Request ID:", reqID)
142+
fmt.Fprintln(cmd.ErrOrStderr(), "Request ID:", reqID)
135143
return err
136144
}
137145

@@ -144,6 +152,16 @@ var createCmd = &cobra.Command{
144152
},
145153
}
146154

155+
func returnErrOnHidden(err error, hidden bool) error {
156+
if hidden {
157+
return err
158+
}
159+
if err != nil && err != sdkcmdutil.ErrNodefinitionLoaded {
160+
return err
161+
}
162+
return nil
163+
}
164+
147165
func init() {
148166
Command.AddCommand(createCmd)
149167
createCmd.Flags().StringP("file", "f", "", "DeploymentCreateRequest file definition. See help for more information")

0 commit comments

Comments
 (0)