github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/version/command_factory/command_factory_test.go (about) 1 package command_factory_test 2 3 import ( 4 "errors" 5 6 config_package "github.com/cloudfoundry-incubator/ltc/config" 7 "github.com/cloudfoundry-incubator/ltc/exit_handler/exit_codes" 8 "github.com/cloudfoundry-incubator/ltc/exit_handler/fake_exit_handler" 9 "github.com/cloudfoundry-incubator/ltc/terminal" 10 "github.com/cloudfoundry-incubator/ltc/test_helpers" 11 "github.com/cloudfoundry-incubator/ltc/version" 12 "github.com/cloudfoundry-incubator/ltc/version/command_factory" 13 "github.com/cloudfoundry-incubator/ltc/version/fake_version_manager" 14 "github.com/codegangsta/cli" 15 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("Version CommandFactory", func() { 22 var ( 23 config *config_package.Config 24 outputBuffer *gbytes.Buffer 25 terminalUI terminal.UI 26 fakeExitHandler *fake_exit_handler.FakeExitHandler 27 fakeVersionManager *fake_version_manager.FakeVersionManager 28 commandFactory *command_factory.VersionCommandFactory 29 ) 30 31 BeforeEach(func() { 32 config = config_package.New(nil) 33 config.SetTarget("lattice.xip.io") 34 35 outputBuffer = gbytes.NewBuffer() 36 terminalUI = terminal.NewUI(nil, outputBuffer, nil) 37 fakeExitHandler = &fake_exit_handler.FakeExitHandler{} 38 fakeVersionManager = &fake_version_manager.FakeVersionManager{} 39 fakeVersionManager.LatticeVersionReturns("some-client-lattice-sha") 40 commandFactory = command_factory.NewVersionCommandFactory( 41 config, 42 terminalUI, 43 fakeExitHandler, 44 "darwin", 45 "/fake/ltc", 46 fakeVersionManager) 47 }) 48 49 Describe("Version Command", func() { 50 var versionCommand cli.Command 51 52 BeforeEach(func() { 53 versionCommand = commandFactory.MakeVersionCommand() 54 fakeVersionManager.ServerVersionsReturns(version.ServerVersions{ 55 CFRelease: "v219", 56 CFRoutingRelease: "v220", 57 DiegoRelease: "v221", 58 GardenLinuxRelease: "v222", 59 LatticeRelease: "some-server-lattice-sha", 60 LatticeReleaseImage: "some-server-lattice-image-sha", 61 LTC: "v225", 62 Receptor: "v226", 63 }, nil) 64 }) 65 66 It("Prints the CLI and API versions", func() { 67 test_helpers.ExecuteCommandWithArgs(versionCommand, []string{}) 68 69 Expect(fakeVersionManager.ServerVersionsCallCount()).To(Equal(1)) 70 Expect(fakeVersionManager.ServerVersionsArgsForCall(0)).To(Equal("http://receptor.lattice.xip.io")) 71 72 Expect(outputBuffer).To(test_helpers.SayLine("Client: some-client-lattice-sha")) 73 Expect(outputBuffer).To(test_helpers.SayLine("Server: some-server-lattice-sha")) 74 Expect(outputBuffer).To(test_helpers.SayLine("\tImage: some-server-lattice-image-sha")) 75 Expect(outputBuffer).To(test_helpers.SayLine("\tCF: v219")) 76 Expect(outputBuffer).To(test_helpers.SayLine("\tDiego: v221")) 77 Expect(outputBuffer).To(test_helpers.SayLine("\tGarden-Linux: v222")) 78 Expect(outputBuffer).To(test_helpers.SayLine("\tRouting: v220")) 79 }) 80 81 Context("when the version manager returns an error", func() { 82 It("should print an error", func() { 83 fakeVersionManager.ServerVersionsReturns(version.ServerVersions{}, errors.New("failed")) 84 85 test_helpers.ExecuteCommandWithArgs(versionCommand, []string{}) 86 87 Expect(outputBuffer).To(test_helpers.SayLine("Error: failed")) 88 Expect(fakeVersionManager.ServerVersionsCallCount()).To(Equal(1)) 89 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.CommandFailed})) 90 }) 91 }) 92 }) 93 94 Describe("SyncCommand", func() { 95 var syncCommand cli.Command 96 97 BeforeEach(func() { 98 syncCommand = commandFactory.MakeSyncCommand() 99 }) 100 101 It("should sync ltc", func() { 102 test_helpers.ExecuteCommandWithArgs(syncCommand, []string{}) 103 104 Expect(outputBuffer).To(test_helpers.SayLine("Updated ltc to the latest version.")) 105 Expect(fakeVersionManager.SyncLTCCallCount()).To(Equal(1)) 106 actualLTCPath, actualArch, actualConfig := fakeVersionManager.SyncLTCArgsForCall(0) 107 Expect(actualLTCPath).To(Equal("/fake/ltc")) 108 Expect(actualArch).To(Equal("osx")) 109 Expect(actualConfig).To(Equal(config)) 110 }) 111 112 Context("when not targeted", func() { 113 It("should print an error", func() { 114 config.SetTarget("") 115 116 test_helpers.ExecuteCommandWithArgs(syncCommand, []string{}) 117 118 Expect(outputBuffer).To(test_helpers.SayLine("Error: Must be targeted to sync.")) 119 Expect(fakeVersionManager.SyncLTCCallCount()).To(Equal(0)) 120 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.CommandFailed})) 121 }) 122 }) 123 124 Context("when the architecture is unknown", func() { 125 It("should print an error", func() { 126 commandFactory := command_factory.NewVersionCommandFactory(config, terminalUI, fakeExitHandler, "unknown-arch", "fakeltc", fakeVersionManager) 127 syncCommand = commandFactory.MakeSyncCommand() 128 129 test_helpers.ExecuteCommandWithArgs(syncCommand, []string{}) 130 131 Expect(outputBuffer).To(test_helpers.SayLine("Error: Unknown architecture unknown-arch. Sync not supported.")) 132 Expect(fakeVersionManager.SyncLTCCallCount()).To(Equal(0)) 133 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.CommandFailed})) 134 }) 135 }) 136 137 Context("when the ltc binary can't be found", func() { 138 It("should print an error", func() { 139 commandFactory := command_factory.NewVersionCommandFactory(config, terminalUI, fakeExitHandler, "darwin", "", fakeVersionManager) 140 syncCommand = commandFactory.MakeSyncCommand() 141 142 test_helpers.ExecuteCommandWithArgs(syncCommand, []string{}) 143 144 Expect(outputBuffer).To(test_helpers.SayLine("Error: Unable to locate the ltc binary. Sync not supported.")) 145 Expect(fakeVersionManager.SyncLTCCallCount()).To(Equal(0)) 146 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.CommandFailed})) 147 }) 148 }) 149 150 Context("when SyncLTC fails", func() { 151 It("should print an error", func() { 152 fakeVersionManager.SyncLTCReturns(errors.New("failed")) 153 154 test_helpers.ExecuteCommandWithArgs(syncCommand, []string{}) 155 156 Expect(outputBuffer).To(test_helpers.SayLine("Error: failed")) 157 Expect(fakeVersionManager.SyncLTCCallCount()).To(Equal(1)) 158 Expect(fakeExitHandler.ExitCalledWith).To(Equal([]int{exit_codes.CommandFailed})) 159 }) 160 }) 161 }) 162 })