From 9163d7ff5211a38e2638248bded9f862f56d61f8 Mon Sep 17 00:00:00 2001 From: aram price Date: Tue, 23 Jul 2024 18:05:25 -0700 Subject: [PATCH] Move construct/factory package up to construct package --- .../fake_vmpreparer_factory.go | 78 +++++++++---------- commandparser/construct.go | 4 +- commandparser/construct_test.go | 2 +- .../vmconstruct_factory.go => factory.go} | 17 ++-- construct/factory/factory_suite_test.go | 13 ---- ...struct_factory_test.go => factory_test.go} | 12 +-- main.go | 4 +- 7 files changed, 58 insertions(+), 72 deletions(-) rename construct/{factory/vmconstruct_factory.go => factory.go} (79%) delete mode 100644 construct/factory/factory_suite_test.go rename construct/{factory/vmconstruct_factory_test.go => factory_test.go} (83%) diff --git a/commandparser/commandparserfakes/fake_vmpreparer_factory.go b/commandparser/commandparserfakes/fake_vmpreparer_factory.go index fd849216..d76efdf8 100644 --- a/commandparser/commandparserfakes/fake_vmpreparer_factory.go +++ b/commandparser/commandparserfakes/fake_vmpreparer_factory.go @@ -9,17 +9,17 @@ import ( ) type FakeVMPreparerFactory struct { - VMPreparerStub func(config.SourceConfig, commandparser.VCenterManager) (commandparser.VmConstruct, error) - vMPreparerMutex sync.RWMutex - vMPreparerArgsForCall []struct { + NewStub func(config.SourceConfig, commandparser.VCenterManager) (commandparser.VmConstruct, error) + newMutex sync.RWMutex + newArgsForCall []struct { arg1 config.SourceConfig arg2 commandparser.VCenterManager } - vMPreparerReturns struct { + newReturns struct { result1 commandparser.VmConstruct result2 error } - vMPreparerReturnsOnCall map[int]struct { + newReturnsOnCall map[int]struct { result1 commandparser.VmConstruct result2 error } @@ -27,17 +27,17 @@ type FakeVMPreparerFactory struct { invocationsMutex sync.RWMutex } -func (fake *FakeVMPreparerFactory) VMPreparer(arg1 config.SourceConfig, arg2 commandparser.VCenterManager) (commandparser.VmConstruct, error) { - fake.vMPreparerMutex.Lock() - ret, specificReturn := fake.vMPreparerReturnsOnCall[len(fake.vMPreparerArgsForCall)] - fake.vMPreparerArgsForCall = append(fake.vMPreparerArgsForCall, struct { +func (fake *FakeVMPreparerFactory) New(arg1 config.SourceConfig, arg2 commandparser.VCenterManager) (commandparser.VmConstruct, error) { + fake.newMutex.Lock() + ret, specificReturn := fake.newReturnsOnCall[len(fake.newArgsForCall)] + fake.newArgsForCall = append(fake.newArgsForCall, struct { arg1 config.SourceConfig arg2 commandparser.VCenterManager }{arg1, arg2}) - stub := fake.VMPreparerStub - fakeReturns := fake.vMPreparerReturns - fake.recordInvocation("VMPreparer", []interface{}{arg1, arg2}) - fake.vMPreparerMutex.Unlock() + stub := fake.NewStub + fakeReturns := fake.newReturns + fake.recordInvocation("New", []interface{}{arg1, arg2}) + fake.newMutex.Unlock() if stub != nil { return stub(arg1, arg2) } @@ -47,46 +47,46 @@ func (fake *FakeVMPreparerFactory) VMPreparer(arg1 config.SourceConfig, arg2 com return fakeReturns.result1, fakeReturns.result2 } -func (fake *FakeVMPreparerFactory) VMPreparerCallCount() int { - fake.vMPreparerMutex.RLock() - defer fake.vMPreparerMutex.RUnlock() - return len(fake.vMPreparerArgsForCall) +func (fake *FakeVMPreparerFactory) NewCallCount() int { + fake.newMutex.RLock() + defer fake.newMutex.RUnlock() + return len(fake.newArgsForCall) } -func (fake *FakeVMPreparerFactory) VMPreparerCalls(stub func(config.SourceConfig, commandparser.VCenterManager) (commandparser.VmConstruct, error)) { - fake.vMPreparerMutex.Lock() - defer fake.vMPreparerMutex.Unlock() - fake.VMPreparerStub = stub +func (fake *FakeVMPreparerFactory) NewCalls(stub func(config.SourceConfig, commandparser.VCenterManager) (commandparser.VmConstruct, error)) { + fake.newMutex.Lock() + defer fake.newMutex.Unlock() + fake.NewStub = stub } -func (fake *FakeVMPreparerFactory) VMPreparerArgsForCall(i int) (config.SourceConfig, commandparser.VCenterManager) { - fake.vMPreparerMutex.RLock() - defer fake.vMPreparerMutex.RUnlock() - argsForCall := fake.vMPreparerArgsForCall[i] +func (fake *FakeVMPreparerFactory) NewArgsForCall(i int) (config.SourceConfig, commandparser.VCenterManager) { + fake.newMutex.RLock() + defer fake.newMutex.RUnlock() + argsForCall := fake.newArgsForCall[i] return argsForCall.arg1, argsForCall.arg2 } -func (fake *FakeVMPreparerFactory) VMPreparerReturns(result1 commandparser.VmConstruct, result2 error) { - fake.vMPreparerMutex.Lock() - defer fake.vMPreparerMutex.Unlock() - fake.VMPreparerStub = nil - fake.vMPreparerReturns = struct { +func (fake *FakeVMPreparerFactory) NewReturns(result1 commandparser.VmConstruct, result2 error) { + fake.newMutex.Lock() + defer fake.newMutex.Unlock() + fake.NewStub = nil + fake.newReturns = struct { result1 commandparser.VmConstruct result2 error }{result1, result2} } -func (fake *FakeVMPreparerFactory) VMPreparerReturnsOnCall(i int, result1 commandparser.VmConstruct, result2 error) { - fake.vMPreparerMutex.Lock() - defer fake.vMPreparerMutex.Unlock() - fake.VMPreparerStub = nil - if fake.vMPreparerReturnsOnCall == nil { - fake.vMPreparerReturnsOnCall = make(map[int]struct { +func (fake *FakeVMPreparerFactory) NewReturnsOnCall(i int, result1 commandparser.VmConstruct, result2 error) { + fake.newMutex.Lock() + defer fake.newMutex.Unlock() + fake.NewStub = nil + if fake.newReturnsOnCall == nil { + fake.newReturnsOnCall = make(map[int]struct { result1 commandparser.VmConstruct result2 error }) } - fake.vMPreparerReturnsOnCall[i] = struct { + fake.newReturnsOnCall[i] = struct { result1 commandparser.VmConstruct result2 error }{result1, result2} @@ -95,8 +95,8 @@ func (fake *FakeVMPreparerFactory) VMPreparerReturnsOnCall(i int, result1 comman func (fake *FakeVMPreparerFactory) Invocations() map[string][][]interface{} { fake.invocationsMutex.RLock() defer fake.invocationsMutex.RUnlock() - fake.vMPreparerMutex.RLock() - defer fake.vMPreparerMutex.RUnlock() + fake.newMutex.RLock() + defer fake.newMutex.RUnlock() copiedInvocations := map[string][][]interface{}{} for key, value := range fake.invocations { copiedInvocations[key] = value diff --git a/commandparser/construct.go b/commandparser/construct.go index 4702c06a..b91c2701 100644 --- a/commandparser/construct.go +++ b/commandparser/construct.go @@ -35,7 +35,7 @@ type VCenterManager interface { //counterfeiter:generate . VMPreparerFactory type VMPreparerFactory interface { - VMPreparer(config config.SourceConfig, vCenterManager VCenterManager) (VmConstruct, error) + New(config config.SourceConfig, vCenterManager VCenterManager) (VmConstruct, error) } //counterfeiter:generate . ManagerFactory @@ -154,7 +154,7 @@ func (p *ConstructCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interfac return subcommands.ExitFailure } - vmConstruct, err := p.prepFactory.VMPreparer(p.sourceConfig, vCenterManager) + vmConstruct, err := p.prepFactory.New(p.sourceConfig, vCenterManager) if err != nil { p.messenger.CannotPrepareVM(err) return subcommands.ExitFailure diff --git a/commandparser/construct_test.go b/commandparser/construct_test.go index 749587c6..80232d6f 100644 --- a/commandparser/construct_test.go +++ b/commandparser/construct_test.go @@ -157,7 +157,7 @@ var _ = Describe("construct", func() { fakeValidator = &commandparserfakes.FakeConstructCmdValidator{} fakeMessenger = &commandparserfakes.FakeConstructMessenger{} fakeManagerFactory = &commandparserfakes.FakeManagerFactory{} - fakeFactory.VMPreparerReturns(fakeVmConstruct, nil) + fakeFactory.NewReturns(fakeVmConstruct, nil) ConstrCmd = commandparser.NewConstructCmd(context.Background(), fakeFactory, fakeManagerFactory, fakeValidator, fakeMessenger) ConstrCmd.SetFlags(f) diff --git a/construct/factory/vmconstruct_factory.go b/construct/factory.go similarity index 79% rename from construct/factory/vmconstruct_factory.go rename to construct/factory.go index 5138e650..f7ef364e 100644 --- a/construct/factory/vmconstruct_factory.go +++ b/construct/factory.go @@ -1,4 +1,4 @@ -package vmconstruct_factory +package construct import ( "context" @@ -7,7 +7,6 @@ import ( "github.com/pkg/errors" "github.com/cloudfoundry/stembuild/commandparser" - "github.com/cloudfoundry/stembuild/construct" "github.com/cloudfoundry/stembuild/construct/archive" "github.com/cloudfoundry/stembuild/construct/config" "github.com/cloudfoundry/stembuild/iaas_cli" @@ -17,14 +16,14 @@ import ( "github.com/cloudfoundry/stembuild/version" ) -type VMConstructFactory struct { +type Factory struct { } -func (f *VMConstructFactory) VMPreparer(config config.SourceConfig, vCenterManager commandparser.VCenterManager) (commandparser.VmConstruct, error) { +func (f *Factory) New(config config.SourceConfig, vCenterManager commandparser.VCenterManager) (commandparser.VmConstruct, error) { runner := &iaas_cli.GovcRunner{} client := iaas_clients.NewVcenterClient(config.VCenterUsername, config.VCenterPassword, config.VCenterUrl, config.CaCertFile, runner) - messenger := construct.NewMessenger(os.Stdout) + messenger := NewMessenger(os.Stdout) ctx := context.Background() err := vCenterManager.Login(ctx) @@ -44,7 +43,7 @@ func (f *VMConstructFactory) VMPreparer(config config.SourceConfig, vCenterManag return nil, err } - winRMManager := &construct.WinRMManager{ + winRMManager := &WinRMManager{ GuestManager: guestManager, Unarchiver: &archive.Zip{}, } @@ -53,7 +52,7 @@ func (f *VMConstructFactory) VMPreparer(config config.SourceConfig, vCenterManag winRmClientFactory := remotemanager.NewWinRmClientFactory(config.GuestVmIp, config.GuestVMUsername, config.GuestVMPassword) remoteManager := remotemanager.NewWinRM(config.GuestVmIp, config.GuestVMUsername, config.GuestVMPassword, winRmClientFactory) - vmConnectionValidator := &construct.WinRMConnectionValidator{ + vmConnectionValidator := &WinRMConnectionValidator{ RemoteManager: remoteManager, } @@ -63,9 +62,9 @@ func (f *VMConstructFactory) VMPreparer(config config.SourceConfig, vCenterManag rebootWaiter := remotemanager.NewRebootWaiter(rebootPoller, rebootChecker) - scriptExecutor := construct.NewScriptExecutor(remoteManager) + scriptExecutor := NewScriptExecutor(remoteManager) - return construct.NewVMConstruct( + return NewVMConstruct( ctx, remoteManager, config.GuestVMUsername, diff --git a/construct/factory/factory_suite_test.go b/construct/factory/factory_suite_test.go deleted file mode 100644 index 5e4b7e39..00000000 --- a/construct/factory/factory_suite_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package vmconstruct_factory - -import ( - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -func TestFactory(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "VmConstruct Factory Suite") -} diff --git a/construct/factory/vmconstruct_factory_test.go b/construct/factory_test.go similarity index 83% rename from construct/factory/vmconstruct_factory_test.go rename to construct/factory_test.go index 88f274bd..3f8a877c 100644 --- a/construct/factory/vmconstruct_factory_test.go +++ b/construct/factory_test.go @@ -1,4 +1,4 @@ -package vmconstruct_factory +package construct_test import ( . "github.com/onsi/ginkgo/v2" @@ -13,14 +13,14 @@ import ( var _ = Describe("Factory", func() { Describe("GetVMPreparer", func() { var ( - factory *VMConstructFactory + factory *construct.Factory ) BeforeEach(func() { - factory = &VMConstructFactory{} + factory = &construct.Factory{} }) - It("should return a VMPreparer", func() { + It("should return a New", func() { fakeVCenterManager := &commandparserfakes.FakeVCenterManager{} sourceConfig := config.SourceConfig{ @@ -33,7 +33,7 @@ var _ = Describe("Factory", func() { VmInventoryPath: "some-vm-inventory-path", } - vmPreparer, err := factory.VMPreparer(sourceConfig, fakeVCenterManager) + vmPreparer, err := factory.New(sourceConfig, fakeVCenterManager) Expect(err).ToNot(HaveOccurred()) Expect(vmPreparer).To(BeAssignableToTypeOf(&construct.VMConstruct{})) }) @@ -45,7 +45,7 @@ var _ = Describe("Factory", func() { fakeVCenterManager.LoginReturns(loginFailure) sourceConfig := config.SourceConfig{} - vmPreparer, err := factory.VMPreparer(sourceConfig, fakeVCenterManager) + vmPreparer, err := factory.New(sourceConfig, fakeVCenterManager) Expect(vmPreparer).To(BeNil()) Expect(err).To(HaveOccurred()) diff --git a/main.go b/main.go index c9920438..017e22b2 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( "github.com/cloudfoundry/stembuild/assets" "github.com/cloudfoundry/stembuild/commandparser" - vmconstructfactory "github.com/cloudfoundry/stembuild/construct/factory" + "github.com/cloudfoundry/stembuild/construct" vcenterclientfactory "github.com/cloudfoundry/stembuild/iaas_cli/iaas_clients/factory" "github.com/cloudfoundry/stembuild/package_stemcell/packager" "github.com/cloudfoundry/stembuild/version" @@ -37,7 +37,7 @@ func main() { var gf commandparser.GlobalFlags packageCmd := commandparser.NewPackageCommand(version.NewVersionGetter(), &packager.Factory{}, &commandparser.PackageMessenger{Output: os.Stderr}) packageCmd.GlobalFlags = &gf - constructCmd := commandparser.NewConstructCmd(context.Background(), &vmconstructfactory.VMConstructFactory{}, &vcenterclientfactory.ManagerFactory{}, &commandparser.ConstructValidator{}, &commandparser.ConstructCmdMessenger{OutputChannel: os.Stderr}) + constructCmd := commandparser.NewConstructCmd(context.Background(), &construct.Factory{}, &vcenterclientfactory.ManagerFactory{}, &commandparser.ConstructValidator{}, &commandparser.ConstructCmdMessenger{OutputChannel: os.Stderr}) constructCmd.GlobalFlags = &gf var commands = make([]subcommands.Command, 0)