github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/commandparser/construct_test.go (about)

     1  package commandparser_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"flag"
     7  
     8  	. "github.com/cloudfoundry-incubator/stembuild/commandparser"
     9  	"github.com/cloudfoundry-incubator/stembuild/commandparser/commandparserfakes"
    10  	"github.com/google/subcommands"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("construct", func() {
    17  	// Focus of this test is not to test the Flags.Parse functionality as much
    18  	// as to test that the command line flags values are stored in the expected
    19  	// struct variables. This adds a bit of protection when renaming flag parameters.
    20  	Describe("SetFlags", func() {
    21  
    22  		var f *flag.FlagSet
    23  		var ConstrCmd *ConstructCmd
    24  
    25  		BeforeEach(func() {
    26  			f = flag.NewFlagSet("test", flag.ContinueOnError)
    27  			ConstrCmd = &ConstructCmd{}
    28  			ConstrCmd.SetFlags(f)
    29  		})
    30  
    31  		var args = []string{
    32  			"-vm-ip", "10.0.0.5",
    33  			"-vm-username", "Admin",
    34  			"-vm-password", "some_password",
    35  			"-vcenter-url", "vcenter.example.com",
    36  			"-vcenter-username", "vCenterUsername",
    37  			"-vcenter-password", "vCenterPassword",
    38  			"-vm-inventory-path", "/my-datacenter/vm/my-folder/my-vm",
    39  			"-vcenter-ca-certs", "somecerts.txt",
    40  		}
    41  
    42  		It("stores the value of a vm user", func() {
    43  			err := f.Parse(args)
    44  			Expect(err).ToNot(HaveOccurred())
    45  			Expect(ConstrCmd.GetSourceConfig().GuestVMUsername).To(Equal("Admin"))
    46  		})
    47  
    48  		It("stores the value of a vm password ", func() {
    49  			err := f.Parse(args)
    50  			Expect(err).ToNot(HaveOccurred())
    51  			Expect(ConstrCmd.GetSourceConfig().GuestVMPassword).To(Equal("some_password"))
    52  		})
    53  
    54  		It("stores the value of the a vm IP", func() {
    55  			err := f.Parse(args)
    56  			Expect(err).ToNot(HaveOccurred())
    57  			Expect(ConstrCmd.GetSourceConfig().GuestVmIp).To(Equal("10.0.0.5"))
    58  		})
    59  
    60  		It("stores the value of vCenter url", func() {
    61  			err := f.Parse(args)
    62  			Expect(err).ToNot(HaveOccurred())
    63  			Expect(ConstrCmd.GetSourceConfig().VCenterUrl).To(Equal("vcenter.example.com"))
    64  		})
    65  
    66  		It("stores the value of vCenter username", func() {
    67  			err := f.Parse(args)
    68  			Expect(err).ToNot(HaveOccurred())
    69  			Expect(ConstrCmd.GetSourceConfig().VCenterUsername).To(Equal("vCenterUsername"))
    70  		})
    71  
    72  		It("stores the value of vCenter password", func() {
    73  			err := f.Parse(args)
    74  			Expect(err).ToNot(HaveOccurred())
    75  			Expect(ConstrCmd.GetSourceConfig().VCenterPassword).To(Equal("vCenterPassword"))
    76  		})
    77  
    78  		It("stores the value of VM inventory path", func() {
    79  			err := f.Parse(args)
    80  			Expect(err).ToNot(HaveOccurred())
    81  			Expect(ConstrCmd.GetSourceConfig().VmInventoryPath).To(Equal("/my-datacenter/vm/my-folder/my-vm"))
    82  		})
    83  
    84  		It("stores the value of VM inventory path", func() {
    85  			err := f.Parse(args)
    86  			Expect(err).ToNot(HaveOccurred())
    87  			Expect(ConstrCmd.GetSourceConfig().VmInventoryPath).To(Equal("/my-datacenter/vm/my-folder/my-vm"))
    88  		})
    89  
    90  		It("stores the value of the ca cert filepath", func() {
    91  			err := f.Parse(args)
    92  			Expect(err).ToNot(HaveOccurred())
    93  			Expect(ConstrCmd.GetSourceConfig().CaCertFile).To(Equal("somecerts.txt"))
    94  		})
    95  	})
    96  
    97  	Describe("Execute", func() {
    98  
    99  		var f *flag.FlagSet
   100  		var gf *GlobalFlags
   101  		var ConstrCmd *ConstructCmd
   102  		var emptyContext context.Context
   103  
   104  		var fakeFactory *commandparserfakes.FakeVMPreparerFactory
   105  		var fakeVmConstruct *commandparserfakes.FakeVmConstruct
   106  		var fakeValidator *commandparserfakes.FakeConstructCmdValidator
   107  		var fakeMessenger *commandparserfakes.FakeConstructMessenger
   108  		var fakeManagerFactory *commandparserfakes.FakeManagerFactory
   109  
   110  		BeforeEach(func() {
   111  			f = flag.NewFlagSet("test", flag.ExitOnError)
   112  			gf = &GlobalFlags{false, false, false}
   113  
   114  			fakeFactory = &commandparserfakes.FakeVMPreparerFactory{}
   115  			fakeVmConstruct = &commandparserfakes.FakeVmConstruct{}
   116  			fakeValidator = &commandparserfakes.FakeConstructCmdValidator{}
   117  			fakeMessenger = &commandparserfakes.FakeConstructMessenger{}
   118  			fakeManagerFactory = &commandparserfakes.FakeManagerFactory{}
   119  			fakeFactory.VMPreparerReturns(fakeVmConstruct, nil)
   120  
   121  			ConstrCmd = NewConstructCmd(context.Background(), fakeFactory, fakeManagerFactory, fakeValidator, fakeMessenger)
   122  			ConstrCmd.SetFlags(f)
   123  			ConstrCmd.GlobalFlags = gf
   124  			emptyContext = context.Background()
   125  		})
   126  
   127  		It("should execute the construct VM command", func() {
   128  			fakeValidator.PopulatedArgsReturns(true)
   129  			fakeValidator.LGPOInDirectoryReturns(true)
   130  
   131  			exitStatus := ConstrCmd.Execute(emptyContext, f)
   132  
   133  			Expect(exitStatus).To(Equal(subcommands.ExitSuccess))
   134  			Expect(fakeValidator.PopulatedArgsCallCount()).To(Equal(1))
   135  			Expect(fakeValidator.LGPOInDirectoryCallCount()).To(Equal(1))
   136  
   137  			Expect(fakeVmConstruct.PrepareVMCallCount()).To(Equal(1))
   138  		})
   139  
   140  		Context("with missing arguments", func() {
   141  			It("should return an error", func() {
   142  				fakeValidator.PopulatedArgsReturns(false)
   143  
   144  				exitStatus := ConstrCmd.Execute(emptyContext, f)
   145  
   146  				Expect(exitStatus).To(Equal(subcommands.ExitFailure))
   147  				Expect(fakeMessenger.ArgumentsNotProvidedCallCount()).To(Equal(1))
   148  			})
   149  		})
   150  
   151  		Context("with LGPO.zip not in current directory", func() {
   152  			It("should return an error", func() {
   153  				fakeValidator.PopulatedArgsReturns(true)
   154  				fakeValidator.LGPOInDirectoryReturns(false)
   155  
   156  				exitStatus := ConstrCmd.Execute(emptyContext, f)
   157  
   158  				Expect(exitStatus).To(Equal(subcommands.ExitFailure))
   159  				Expect(fakeMessenger.LGPONotFoundCallCount()).To(Equal(1))
   160  			})
   161  		})
   162  
   163  		Context("with an error during VMPrepare", func() {
   164  			It("should return an error", func() {
   165  				fakeValidator.PopulatedArgsReturns(true)
   166  				fakeValidator.LGPOInDirectoryReturns(true)
   167  				fakeVmConstruct.PrepareVMReturns(errors.New("some error"))
   168  
   169  				exitStatus := ConstrCmd.Execute(emptyContext, f)
   170  
   171  				Expect(exitStatus).To(Equal(subcommands.ExitFailure))
   172  				Expect(fakeMessenger.CannotPrepareVMCallCount()).To(Equal(1))
   173  			})
   174  		})
   175  	})
   176  })