Example #1
0
	. "github.com/onsi/gomega"
)

var _ = Describe("CallCliCmd", func() {
	Describe(".Run", func() {
		var fakeCliConnection *pluginfakes.FakeCliConnection
		var callCliCommandPlugin *CliCmd

		BeforeEach(func() {
			fakeCliConnection = &pluginfakes.FakeCliConnection{}
			callCliCommandPlugin = &CliCmd{}
		})

		It("calls the cli command that is passed as an argument", func() {
			io_helpers.CaptureOutput(func() {
				callCliCommandPlugin.Run(fakeCliConnection, []string{"cli-command", "plugins", "arg1"})
			})

			Expect(fakeCliConnection.CliCommandArgsForCall(0)[0]).To(Equal("plugins"))
			Expect(fakeCliConnection.CliCommandArgsForCall(0)[1]).To(Equal("arg1"))
		})

		It("ouputs the text returned by the cli command", func() {
			fakeCliConnection.CliCommandReturns([]string{"Hi", "Mom"}, nil)
			output := io_helpers.CaptureOutput(func() {
				callCliCommandPlugin.Run(fakeCliConnection, []string{"cli-command", "plugins", "arg1"})
			})

			Expect(output[1]).To(Equal("---------- Command output from the plugin ----------"))
			Expect(output[2]).To(Equal("# 0  value:  Hi"))
			Expect(output[3]).To(Equal("# 1  value:  Mom"))
Example #2
0
var _ = Describe("UI", func() {
	var fakeLogger *tracefakes.FakePrinter
	BeforeEach(func() {
		fakeLogger = new(tracefakes.FakePrinter)
	})

	Describe("Printing message to stdout with PrintCapturingNoOutput", func() {
		It("prints strings without using the TeePrinter", func() {
			bucket := gbytes.NewBuffer()

			printer := NewTeePrinter(os.Stdout)
			printer.SetOutputBucket(bucket)

			io_helpers.SimulateStdin("", func(reader io.Reader) {
				output := io_helpers.CaptureOutput(func() {
					ui := NewUI(reader, os.Stdout, printer, fakeLogger)
					ui.PrintCapturingNoOutput("Hello")
				})

				Expect("Hello").To(Equal(strings.Join(output, "")))
				Expect(bucket.Contents()).To(HaveLen(0))
			})
		})
	})

	Describe("Printing message to stdout with Say", func() {
		It("prints strings", func() {
			io_helpers.SimulateStdin("", func(reader io.Reader) {
				output := io_helpers.CaptureOutput(func() {
					ui := NewUI(reader, os.Stdout, NewTeePrinter(os.Stdout), fakeLogger)
					ui.Say("Hello")
				})
	)
	Context("When running wildcard-apps", func() {
		BeforeEach(func() {
			appsList = make([]plugin_models.GetAppsModel, 0)
			appsList = append(appsList,
				plugin_models.GetAppsModel{"spring-music", "", "", 0, 0, 0, 0, nil},
				plugin_models.GetAppsModel{"app321", "", "", 0, 0, 0, 0, nil},
			)
			fakeCliConnection = &fakes.FakeCliConnection{}
			wildcardPlugin = &Wildcard{}
		})
		Describe("When there are matching apps", func() {
			It("prints a table containing only those apps", func() {
				fakeCliConnection.GetAppsReturns(appsList, nil)
				output := io_helpers.CaptureOutput(func() {
					wildcardPlugin.Run(fakeCliConnection, []string{"wildcard-apps", "app*"})
				})

				Expect(output).To(ContainSubstrings(
					[]string{"app321"},
				))
				Expect(output).ToNot(ContainSubstrings(
					[]string{"spring-music"},
				))
			})
		})
		Describe("When there are matching apps", func() {
			It("prints a table containing only those apps", func() {
				fakeCliConnection.GetAppsReturns(appsList, nil)
				output := io_helpers.CaptureOutput(func() {
					wildcardPlugin.Run(fakeCliConnection, []string{"wildcard-apps", "app*"})
				outputCapture := &fakes.FakeOutputCapture{}
				rpcService, err = NewRpcService(app, outputCapture, nil, nil)
				Expect(err).ToNot(HaveOccurred())

				err := rpcService.Start()
				Expect(err).ToNot(HaveOccurred())

				pingCli(rpcService.Port())
			})

			It("returns false in success if the command cannot be found", func() {
				io_helpers.CaptureOutput(func() {
					client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port())
					Expect(err).ToNot(HaveOccurred())

					var success bool
					err = client.Call("CliRpcCmd.CallCoreCommand", []string{"not_a_cmd"}, &success)
					Expect(success).To(BeFalse())
					Expect(err).ToNot(HaveOccurred())
				})
			})

			It("returns an error if a command cannot parse provided flags", func() {
				io_helpers.CaptureOutput(func() {
					client, err = rpc.Dial("tcp", "127.0.0.1:"+rpcService.Port())
					Expect(err).ToNot(HaveOccurred())

					var success bool
					err = client.Call("CliRpcCmd.CallCoreCommand", []string{"test_cmd", "-invalid_flag"}, &success)

					Expect(err).To(HaveOccurred())
		AfterEach(func() {
			fakeFirehose.Close()
		})

		Context("when invoked via 'app-nozzle'", func() {
			Context("when app name is not recognized", func() {
				BeforeEach(func() {
					fakeCliConnection.GetAppReturns(plugin_models.GetAppModel{}, errors.New("App not found"))
				})
				It("returns error message", func(done Done) {
					defer close(done)
					outputChan := make(chan []string)
					go func() {
						output := io_helpers.CaptureOutput(func() {
							nozzlerCmd.Run(fakeCliConnection, []string{"app-nozzle", "IDontExist"})
						})
						outputChan <- output
					}()

					var output []string
					Eventually(outputChan, 2).Should(Receive(&output))
					outputString := strings.Join(output, "|")

					Expect(outputString).To(ContainSubstring("App not found"))
				}, 3)

			})
			Context("when app name is valid", func() {
				BeforeEach(func() {
					fakeFirehose.AppMode = true
Example #6
0
			Ω(output).Should(Equal([]string{"SOME", "OUTPUT", "COMMAND"}))
		})

		It("Return Service Credentials From Appplication Environment", func() {
			_, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("VCAP_SERVICES", []string{""})
			Ω(err).Should(MatchError("missing service credentials for application"))

			service_creds := []string{"{\"VCAP_SERVICES\":{\"service\": [ { \"credentials\": {} } ]}}"}
			b, err := callCopyEnvCommandPlugin.ExtractCredentialsJSON("VCAP_SERVICES", service_creds)
			Ω(err).ShouldNot(HaveOccurred())
			Ω(string(b[:])).Should(Equal("{\"service\":[{\"credentials\":{}}]}"))
		})

		It("Print Service Credentials As Shell Variable", func() {
			output := io_helpers.CaptureOutput(func() {
				callCopyEnvCommandPlugin.ExportCredsAsShellVar("VCAP_SERVICES", "testing")
			})
			Ω(output[0]).Should(Equal("export VCAP_SERVICES='testing';"))
		})

		It("Silently uninstalls", func() {
			callCopyEnvCommandPlugin.Run(fakeCliConnection, []string{"CLI-MESSAGE-UNINSTALL"})
			Ω(fakeCliConnection.CliCommandWithoutTerminalOutputCallCount()).Should(Equal(0))
		})

		Context("when called with --all", func() {
			It("Extracts VCAP_APPLICATION and VCAP_SERVICE", func() {
				services := "{\"VCAP_SERVICES\":[\"services\"]}"
				application := "{\"VCAP_APPLICATION\":[\"application\"]}"
				fakeCliConnection.CliCommandWithoutTerminalOutputReturns([]string{
					services, application, "OTHER"}, nil)
Example #7
0
	"github.com/codegangsta/cli"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Help", func() {
	It("shows help for all commands", func() {
		commandFactory := createCommandFactory()

		dummyTemplate := `
{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
{{.Name}}
{{end}}{{end}}{{end}}
`
		output := io_helpers.CaptureOutput(func() {
			app.ShowHelp(dummyTemplate, createApp(commandFactory))
		})

		for _, metadata := range commandFactory.CommandMetadatas() {
			Expect(commandInOutput(metadata.Name, output)).To(BeTrue(), metadata.Name+" not in help")
		}
	})
})

func createCommandFactory() command_factory.Factory {
	fakeUI := &testterm.FakeUI{}
	configRepo := testconfig.NewRepository()
	manifestRepo := manifest.NewManifestDiskRepository()
	apiRepoLocator := api.NewRepositoryLocator(configRepo, map[string]net.Gateway{
		"auth":             net.NewUAAGateway(configRepo),
		"cloud-controller": net.NewCloudControllerGateway(configRepo, time.Now),
Example #8
0
var _ = Describe("TeePrinter", func() {
	var (
		output  []string
		printer *TeePrinter
	)

	Describe(".Print", func() {
		var bucket *gbytes.Buffer

		BeforeEach(func() {
			bucket = gbytes.NewBuffer()

			output = io_helpers.CaptureOutput(func() {
				printer = NewTeePrinter(os.Stdout)
				printer.SetOutputBucket(bucket)
				printer.Print("Hello ")
				printer.Print("Mom!")
			})
		})

		It("should delegate to fmt.Print", func() {
			Expect(output[0]).To(Equal("Hello Mom!"))
		})

		It("should save the output to the slice", func() {
			Expect(bucket).To(gbytes.Say("Hello "))
			Expect(bucket).To(gbytes.Say("Mom!"))
		})

		It("should decolorize text", func() {
			io_helpers.CaptureOutput(func() {
			Ω(md.Commands[0].HelpText).NotTo(BeNil())
		})
	})

	Describe("Run", func() {
		var fakeDroplet *fake_droplet.FakeDroplet

		BeforeEach(func() {
			fakeDroplet = &fake_droplet.FakeDroplet{}
			downloadDropletPlugin.Drop = fakeDroplet
		})

		Context("Messages", func() {
			It("prints an informative message when downloading the droplet", func() {
				output := io_helpers.CaptureOutput(func() {
					downloadDropletPlugin.Run(fakeCliConnection, goodArgs)
				})
				Ω(output[0]).To(Equal("Saving theApp's droplet to /tmp"))
			})
		})

		Context("initializer complication", func() {
			It("Should call the initializer during run", func() {
				downloadDropletPlugin.Run(fakeCliConnection, goodArgs)
				cmd, cli := fakeInitiliazer.InitializePluginArgsForCall(0)
				Ω(cmd).To(Equal(downloadDropletPlugin))
				Ω(cli).To(Equal(fakeCliConnection))
				Ω(fakeInitiliazer.InitializePluginCallCount()).Should(Equal(1))
			})

			It("Should handle errors from the initiliazers.", func() {
Example #10
0
var _ = Describe("TeePrinter", func() {
	var (
		output  []string
		printer *TeePrinter
	)

	Describe(".Print", func() {
		var bucket *[]string

		BeforeEach(func() {
			bucket = &[]string{}

			output = io_helpers.CaptureOutput(func() {
				printer = NewTeePrinter()
				printer.SetOutputBucket(bucket)
				printer.Print("Hello ")
				printer.Print("Mom!")
			})
		})

		It("should delegate to fmt.Print", func() {
			Expect(output[0]).To(Equal("Hello Mom!"))
		})

		It("should save the output to the slice", func() {
			Expect((*bucket)[0]).To(Equal("Hello "))
			Expect((*bucket)[1]).To(Equal("Mom!"))
		})

		It("should decolorize text", func() {
			bucket = &[]string{}
Example #11
0
	io_helpers "github.com/cloudfoundry/cli/testhelpers/io"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Help", func() {
	It("shows help for all commands", func() {
		dummyTemplate := `
{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
{{.Name}}
{{end}}{{end}}{{end}}
`
		output := io_helpers.CaptureOutput(func() {
			help.ShowHelp(dummyTemplate)
		})

		for _, metadata := range command_registry.Commands.Metadatas() {
			Expect(commandInOutput(metadata.Name, output)).To(BeTrue(), metadata.Name+" not in help")
		}
	})

	It("shows help for all installed plugin's commands", func() {
		config_helpers.PluginRepoDir = func() string {
			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
		}

		dummyTemplate := `
{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
{{.Name}}
Example #12
0
	testassert "github.com/cloudfoundry/cli/testhelpers/assert"
	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
	io_helpers "github.com/cloudfoundry/cli/testhelpers/io"

	. "github.com/cloudfoundry/cli/cf/terminal"
	. "github.com/cloudfoundry/cli/testhelpers/matchers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("UI", func() {
	Describe("Printing message to stdout with Say", func() {
		It("prints strings", func() {
			io_helpers.SimulateStdin("", func(reader io.Reader) {
				output := io_helpers.CaptureOutput(func() {
					ui := NewUI(reader)
					ui.Say("Hello")
				})

				Expect("Hello").To(Equal(strings.Join(output, "")))
			})
		})

		It("prints formatted strings", func() {
			io_helpers.SimulateStdin("", func(reader io.Reader) {
				output := io_helpers.CaptureOutput(func() {
					ui := NewUI(reader)
					ui.Say("Hello %s", "World!")
				})

				Expect("Hello World!").To(Equal(strings.Join(output, "")))
			})
Example #13
0
		Expect(buffer.Contents()).To(ContainSubstring("test2_cmd2"))
		Expect(buffer.Contents()).To(ContainSubstring("test2_really_long_really_long_really_long_command_name"))
	})

	It("adjusts the output format to the longest length of plugin command name", func() {
		confighelpers.PluginRepoDir = func() string {
			return filepath.Join("..", "..", "fixtures", "config", "help-plugin-test-config")
		}

		dummyTemplate := `
{{range .Commands}}{{range .CommandSubGroups}}{{range .}}
{{.Name}}%%%{{.Description}}
{{end}}{{end}}{{end}}
`
		output := io.CaptureOutput(func() {
			help.ShowHelp(os.Stdout, dummyTemplate)
		})

		cmdNameLen := len(strings.Split(output[2], "%%%")[0])

		for _, line := range output {
			if strings.TrimSpace(line) == "" {
				continue
			}

			expectedLen := len(strings.Split(line, "%%%")[0])
			Expect(cmdNameLen).To(Equal(expectedLen))
		}

	})
Example #14
0
var _ = Describe("appenv", func() {
	var fakeCliConnection *fakes.FakeCliConnection
	var appenv *AppEnv

	BeforeEach(func() {
		fakeCliConnection = &fakes.FakeCliConnection{}
		fakeCliConnection.IsLoggedInStub = func() (bool, error) { return true, nil }

		appenv = &AppEnv{}
	})

	Context("when uninstalled", func() {
		It("does nothing", func() {
			output := ioStub.CaptureOutput(func() {
				appenv.Run(fakeCliConnection, []string{"CLI-MESSAGE-UNINSTALL"})
			})

			Expect(output).To(ConsistOf([]string{""}))
		})
	})

	Context("when not logged in", func() {
		BeforeEach(func() {
			fakeCliConnection.IsLoggedInStub = func() (bool, error) { return false, nil }
		})

		It("returns an error message", func() {
			_, err := appenv.GetEnvs(fakeCliConnection, []string{"app_name"})
			Expect(err).To(MatchError("You must login first!"))
		})
Example #15
0
		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("help").SetDependency(deps, pluginCall))
	}

	BeforeEach(func() {
		ui = &testterm.FakeUI{}
		requirementsFactory = &testreq.FakeReqFactory{}
		config = &testconfig.FakePluginConfiguration{}
	})

	runCommand := func(args ...string) bool {
		return testcmd.RunCliCommand("help", args, requirementsFactory, updateCommandDependency, false)
	}

	Context("when no argument is provided", func() {
		It("prints the main help menu of the 'cf' app", func() {
			outputs := io_helpers.CaptureOutput(func() { runCommand() })

			Eventually(outputs).Should(ContainSubstrings([]string{"A command line tool to interact with Cloud Foundry"}))
			Eventually(outputs).Should(ContainSubstrings([]string{"CF_TRACE=true"}))
		})
	})

	Context("when a command name is provided as an argument", func() {
		Context("When the command exists", func() {
			It("prints the usage help for the command", func() {
				runCommand("target")

				Eventually(ui.Outputs).Should(ContainSubstrings([]string{"target - Set or view the targeted org or space"}))
			})
		})
Example #16
0
	})

	Context("when given a command name to run", func() {
		It("runs the command with that name", func() {
			for _, cmdName := range expectedCommandNames {
				app.Run([]string{"", cmdName})
				Expect(cmdRunner.cmdName).To(Equal(cmdName))
			}
		})
	})

	Context("when running 'cf --help'", func() {
		It("should output the help in our custom format", func() {

			output := io_helpers.CaptureOutput(func() {
				app.Run([]string{"", "--help"})
			})

			mergedOutput := strings.Join(output, "\n")
			Expect(mergedOutput).To(ContainSubstring("CF_TRACE=true"), "CF_TRACE=true not in help")
			Expect(mergedOutput).To(ContainSubstring("CF_PLUGIN_HOME=path/to/dir/"))

			for _, name := range expectedCommandNames {
				Expect(mergedOutput).To(ContainSubstring(name), name+" not in help")
			}
		})
	})

	Context("when the user provides an unknown command name", func() {
		It("should complain loudly and then panic", func() {
			Expect(func() {
			fakeCliConnection = &fakes.FakeCliConnection{}
			fakeCliConnection.AccessTokenReturns(ACCESS_TOKEN, nil)
			fakeCliConnection.DopplerEndpointReturns(fakeFirehose.URL(), nil)
			nozzlerCmd = &NozzlerCmd{}
		})

		AfterEach(func() {
			fakeFirehose.Close()
		})

		It("works", func(done Done) {
			defer close(done)
			outputChan := make(chan []string)
			go func() {
				output := io_helpers.CaptureOutput(func() {
					nozzlerCmd.Run(fakeCliConnection, []string{"nozzle", "--debug"})
				})
				outputChan <- output
			}()

			var output []string
			Eventually(outputChan, 2).Should(Receive(&output))
			outputString := strings.Join(output, "|")

			Expect(outputString).To(ContainSubstring("What type of firehose messages do you want to see?"))

			Expect(outputString).To(ContainSubstring("Starting the nozzle"))
			Expect(outputString).To(ContainSubstring("Hit Cmd+c to exit"))
			Expect(outputString).To(ContainSubstring("websocket: close 1000"))
			Expect(outputString).To(ContainSubstring("Log Message"))
			Expect(outputString).To(ContainSubstring("WEBSOCKET REQUEST"))