From 8d8397bdc205f93a2aaa18b813b0fc01ebc759af Mon Sep 17 00:00:00 2001 From: Jack Francis Date: Tue, 17 Nov 2020 15:22:33 -0800 Subject: [PATCH] update UT --- cmd/deploy.go | 11 +++++------ cmd/deploy_test.go | 25 +++++++++++++++++++++++++ cmd/generate.go | 10 +++++----- cmd/generate_test.go | 21 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/cmd/deploy.go b/cmd/deploy.go index 598764ca70..9f76f9b404 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -193,6 +193,10 @@ func (dc *deployCmd) loadAPIModel() error { return errors.Wrap(err, "error parsing the api model") } + if dc.containerService.Properties.MasterProfile == nil { + return errors.New("MasterProfile can't be nil") + } + // consume dc.caCertificatePath and dc.caPrivateKeyPath if (dc.caCertificatePath != "" && dc.caPrivateKeyPath == "") || (dc.caCertificatePath == "" && dc.caPrivateKeyPath != "") { return errors.New("--ca-certificate-path and --ca-private-key-path must be specified together") @@ -255,7 +259,6 @@ func (dc *deployCmd) loadAPIModel() error { } func autofillApimodel(dc *deployCmd) error { - if dc.containerService.Properties.LinuxProfile != nil { if dc.containerService.Properties.LinuxProfile.AdminUsername == "" { log.Warnf("apimodel: no linuxProfile.adminUsername was specified. Will use 'azureuser'.") @@ -280,11 +283,7 @@ func autofillApimodel(dc *deployCmd) error { } if dc.outputDirectory == "" { - if dc.containerService.Properties.MasterProfile != nil { - dc.outputDirectory = path.Join("_output", dc.containerService.Properties.MasterProfile.DNSPrefix) - } else { - return errors.New("can't determine output directory from nil MasterProfile") - } + dc.outputDirectory = path.Join("_output", dc.containerService.Properties.MasterProfile.DNSPrefix) } if _, err := os.Stat(dc.outputDirectory); !dc.forceOverwrite && err == nil { diff --git a/cmd/deploy_test.go b/cmd/deploy_test.go index 3294df2dbd..8e8c3d3050 100644 --- a/cmd/deploy_test.go +++ b/cmd/deploy_test.go @@ -669,6 +669,31 @@ func TestDeployCmdRun(t *testing.T) { } } +func TestDeployCmdWithoutMasterProfile(t *testing.T) { + t.Parallel() + + outdir, del := makeTmpDir(t) + defer del() + + d := &deployCmd{ + client: &armhelpers.MockAKSEngineClient{}, + authProvider: &mockAuthProvider{ + authArgs: &authArgs{}, + getClientMock: &armhelpers.MockAKSEngineClient{}, + }, + apimodelPath: "../pkg/engine/testdata/simple/kubernetes.json", + outputDirectory: outdir, + forceOverwrite: true, + location: "westus", + } + d.set = []string{"masterProfile=nil"} + + err := d.loadAPIModel() + if err == nil { + t.Fatalf("expected error loading api model without MasterProfile: %s", err.Error()) + } +} + func TestLoadApiModelOnCustomCloud(t *testing.T) { t.Parallel() diff --git a/cmd/generate.go b/cmd/generate.go index fb80089dc9..0dab40fdaa 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -154,12 +154,12 @@ func (gc *generateCmd) loadAPIModel() error { return errors.Wrap(err, "error parsing the api model") } + if gc.containerService.Properties.MasterProfile == nil { + return errors.New("MasterProfile can't be nil") + } + if gc.outputDirectory == "" { - if gc.containerService.Properties.MasterProfile != nil { - gc.outputDirectory = path.Join("_output", gc.containerService.Properties.MasterProfile.DNSPrefix) - } else { - return errors.New("can't determine output directory from nil MasterProfile") - } + gc.outputDirectory = path.Join("_output", gc.containerService.Properties.MasterProfile.DNSPrefix) } // consume gc.caCertificatePath and gc.caPrivateKeyPath diff --git a/cmd/generate_test.go b/cmd/generate_test.go index 77cd8eed06..22eb728624 100644 --- a/cmd/generate_test.go +++ b/cmd/generate_test.go @@ -173,6 +173,27 @@ func TestGenerateCmdMLoadAPIModel(t *testing.T) { } } +func TestGenerateCmdMLoadAPIModelWithoutMasterProfile(t *testing.T) { + g := &generateCmd{} + r := &cobra.Command{} + + g.apimodelPath = "../pkg/engine/testdata/simple/kubernetes.json" + g.set = []string{"masterProfile=nil"} + + err := g.validate(r, []string{"../pkg/engine/testdata/simple/kubernetes.json"}) + if err != nil { + t.Fatalf("unexpected error validating api model: %s", err.Error()) + } + err = g.mergeAPIModel() + if err != nil { + t.Fatalf("unexpected error merging api model: %s", err.Error()) + } + err = g.loadAPIModel() + if err == nil { + t.Fatalf("expected error loading api model without MasterProfile: %s", err.Error()) + } +} + func TestAPIModelWithoutServicePrincipalProfileAndClientIdAndSecretInGenerateCmd(t *testing.T) { t.Parallel()