Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

More cmd package unit tests #2997

Merged
merged 12 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,23 @@ func (dc *deployCmd) validate(cmd *cobra.Command, args []string) error {
return fmt.Errorf(fmt.Sprintf("specified api model does not exist (%s)", dc.apimodelPath))
}

if dc.location == "" {
return fmt.Errorf(fmt.Sprintf("--location must be specified"))
}
dc.location = helpers.NormalizeAzureRegion(dc.location)

return nil
}

func (dc *deployCmd) load(cmd *cobra.Command, args []string) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small refactor here -> split validate and load into two separate functions

var err error

apiloader := &api.Apiloader{
Translator: &i18n.Translator{
Locale: dc.locale,
},
}

if dc.location == "" {
return fmt.Errorf(fmt.Sprintf("--location must be specified"))
}
dc.location = helpers.NormalizeAzureRegion(dc.location)

dc.containerService, dc.apiVersion, err = apiloader.LoadContainerServiceFromFile(dc.apimodelPath, true, false, nil)
if err != nil {
return fmt.Errorf(fmt.Sprintf("error parsing the api model: %s", err.Error()))
Expand Down
118 changes: 118 additions & 0 deletions cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Azure/acs-engine/pkg/armhelpers"
"github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const ExampleAPIModel = `{
Expand Down Expand Up @@ -72,6 +73,123 @@ func getAPIModelWithoutServicePrincipalProfile(baseAPIModel string, useManagedId
strconv.FormatBool(useManagedIdentity))
}

func TestNewDeployCmd(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit_test_coverage++

output := newDeployCmd()
if output.Use != deployName || output.Short != deployShortDescription || output.Long != deployLongDescription {
t.Fatalf("deploy command should have use %s equal %s, short %s equal %s and long %s equal to %s", output.Use, deployName, output.Short, deployShortDescription, output.Long, versionLongDescription)
}

expectedFlags := []string{"api-model", "dns-prefix", "auto-suffix", "output-directory", "ca-private-key-path", "resource-group", "location", "force-overwrite"}
for _, f := range expectedFlags {
if output.Flags().Lookup(f) == nil {
t.Fatalf("deploy command should have flag %s", f)
}
}
}

func TestValidate(t *testing.T) {
r := &cobra.Command{}
apimodelPath := "apimodel-unit-test.json"

_, err := os.Create(apimodelPath)
if err != nil {
t.Fatalf("unable to create test apimodel path: %s", err.Error())
}
defer os.Remove(apimodelPath)

cases := []struct {
dc *deployCmd
expectedErr error
args []string
}{
{
dc: &deployCmd{
apimodelPath: "",
dnsPrefix: "test",
outputDirectory: "output/test",
forceOverwrite: false,
caCertificatePath: "test",
caPrivateKeyPath: "test",
},
args: []string{},
expectedErr: fmt.Errorf("--api-model was not supplied, nor was one specified as a positional argument"),
},
{
dc: &deployCmd{
apimodelPath: "",
dnsPrefix: "test",
outputDirectory: "output/test",
caCertificatePath: "test",
caPrivateKeyPath: "test",
},
args: []string{"wrong/path"},
expectedErr: fmt.Errorf("specified api model does not exist (wrong/path)"),
},
{
dc: &deployCmd{
apimodelPath: "",
dnsPrefix: "test",
outputDirectory: "output/test",
caCertificatePath: "test",
caPrivateKeyPath: "test",
},
args: []string{"test/apimodel.json", "some_random_stuff"},
expectedErr: fmt.Errorf("too many arguments were provided to 'deploy'"),
},
{
dc: &deployCmd{
apimodelPath: "",
dnsPrefix: "test",
outputDirectory: "output/test",
caCertificatePath: "test",
caPrivateKeyPath: "test",
},
args: []string{apimodelPath},
expectedErr: fmt.Errorf("--location must be specified"),
},
{
dc: &deployCmd{
apimodelPath: "",
dnsPrefix: "test",
outputDirectory: "output/test",
caCertificatePath: "test",
caPrivateKeyPath: "test",
location: "west europe",
},
args: []string{apimodelPath},
expectedErr: nil,
},
{
dc: &deployCmd{
apimodelPath: apimodelPath,
dnsPrefix: "test",
outputDirectory: "output/test",
caCertificatePath: "test",
caPrivateKeyPath: "test",
location: "canadaeast",
},
args: []string{},
expectedErr: nil,
},
}

for _, c := range cases {
err = c.dc.validate(r, c.args)
if err != nil && c.expectedErr != nil {
if err.Error() != c.expectedErr.Error() {
t.Fatalf("expected validate deploy command to return error %s, but instead got %s", c.expectedErr.Error(), err.Error())
}
} else {
if c.expectedErr != nil {
t.Fatalf("expected validate deploy command to return error %s, but instead got no error", c.expectedErr.Error())
} else if err != nil {
t.Fatalf("expected validate deploy command to return no error, but instead got %s", err.Error())
}

}
}
}

func TestAutofillApimodelWithoutManagedIdentityCreatesCreds(t *testing.T) {
testAutodeployCredentialHandling(t, false, "", "")
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import (
"github.com/spf13/cobra"
)

func TestNewGenerateCmd(t *testing.T) {
output := newGenerateCmd()
if output.Use != generateName || output.Short != generateShortDescription || output.Long != generateLongDescription {
t.Fatalf("generate command should have use %s equal %s, short %s equal %s and long %s equal to %s", output.Use, generateName, output.Short, generateShortDescription, output.Long, generateLongDescription)
}

expectedFlags := []string{"api-model", "output-directory", "ca-certificate-path", "ca-private-key-path", "set", "classic-mode", "no-pretty-print", "parameters-only"}
for _, f := range expectedFlags {
if output.Flags().Lookup(f) == nil {
t.Fatalf("generate command should have flag %s", f)
}
}
}

func TestGenerateCmdValidate(t *testing.T) {
g := &generateCmd{}
r := &cobra.Command{}
Expand Down
12 changes: 6 additions & 6 deletions cmd/orchestrators.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

const (
cmdName = "orchestrators"
cmdShortDescription = "Display info about supported orchestrators"
cmdLongDescription = "Display supported versions and upgrade versions for each orchestrator"
orchestratorsName = "orchestrators"
orchestratorsShortDescription = "Display info about supported orchestrators"
orchestratorsLongDescription = "Display supported versions and upgrade versions for each orchestrator"
)

type orchestratorsCmd struct {
Expand All @@ -24,9 +24,9 @@ func newOrchestratorsCmd() *cobra.Command {
oc := orchestratorsCmd{}

command := &cobra.Command{
Use: cmdName,
Short: cmdShortDescription,
Long: cmdLongDescription,
Use: orchestratorsName,
Short: orchestratorsShortDescription,
Long: orchestratorsLongDescription,
RunE: func(cmd *cobra.Command, args []string) error {
return oc.run(cmd, args)
},
Expand Down
10 changes: 10 additions & 0 deletions cmd/orchestrators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import (
)

var _ = Describe("The orchestrators command", func() {
It("should create an orchestrators command", func() {
output := newOrchestratorsCmd()

Expect(output.Use).Should(Equal(orchestratorsName))
Expect(output.Short).Should(Equal(orchestratorsShortDescription))
Expect(output.Long).Should(Equal(orchestratorsLongDescription))
Expect(output.Flags().Lookup("orchestrator")).NotTo(BeNil())
Expect(output.Flags().Lookup("version")).NotTo(BeNil())
})

It("should fail on unsupported orchestrator", func() {
command := &orchestratorsCmd{
orchestrator: "unsupported",
Expand Down