From f69549c5ab1153e728f27e6c7215f1f17eec033d Mon Sep 17 00:00:00 2001 From: Ed Cook Date: Wed, 25 Sep 2019 18:01:20 +0100 Subject: [PATCH] Update ssh tests - Remove unnecessary contexts - Standardise ssh script fixtures Co-authored-by: Glen Rodgers --- commands/ssh_test.go | 248 +++++++++--------- .../{ssh_script.sh => ssh_opsman_script.sh} | 0 integration/ssh_command_test.go | 2 +- 3 files changed, 123 insertions(+), 127 deletions(-) rename integration/fixtures/{ssh_script.sh => ssh_opsman_script.sh} (100%) diff --git a/commands/ssh_test.go b/commands/ssh_test.go index 9d351be..31aed64 100644 --- a/commands/ssh_test.go +++ b/commands/ssh_test.go @@ -47,94 +47,92 @@ var _ = Describe("ssh command", func() { } }) - Context("no subcommand", func() { - JustBeforeEach(func() { - err = sshOpsManagerCommand.Execute(commandArgs) + JustBeforeEach(func() { + err = sshOpsManagerCommand.Execute(commandArgs) + }) + + When("retrieving the environment config errors", func() { + BeforeEach(func() { + envReader.ReadReturns(environment.Config{}, fmt.Errorf("env-reader-error")) }) - When("retrieving the environment config errors", func() { - BeforeEach(func() { - envReader.ReadReturns(environment.Config{}, fmt.Errorf("env-reader-error")) - }) + It("doesn't attempt to run the ssh tool", func() { + Expect(sshRunner.RunCallCount()).To(Equal(0)) + }) - It("doesn't attempt to run the ssh tool", func() { - Expect(sshRunner.RunCallCount()).To(Equal(0)) - }) + It("propagates the error", func() { + Expect(err).To(MatchError("env-reader-error")) + }) + }) - It("propagates the error", func() { - Expect(err).To(MatchError("env-reader-error")) - }) + When("retrieving the environment config is successful", func() { + BeforeEach(func() { + envReader.ReadReturns(environment.Config{Name: "env-name"}, nil) }) - When("retrieving the environment config is successful", func() { - BeforeEach(func() { - envReader.ReadReturns(environment.Config{Name: "env-name"}, nil) - }) + It("displays that the connection is being started", func() { + Expect(ui.DisplayTextCallCount()).To(Equal(2)) + Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# ssh-opsman\n")) + Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Connecting to: env-name\n")) + }) + + It("runs the ssh tool using the retrieved environment config", func() { + Expect(sshRunner.RunCallCount()).To(Equal(1)) + + environmentConfig, _, _ := sshRunner.RunArgsForCall(0) + Expect(environmentConfig).To(BeEquivalentTo(environment.Config{Name: "env-name"})) + }) - It("displays that the connection is being started", func() { - Expect(ui.DisplayTextCallCount()).To(Equal(2)) - Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# ssh-opsman\n")) - Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Connecting to: env-name\n")) + When("run with the file flag set", func() { + BeforeEach(func() { + sshOpsManagerCommand.File = true }) - It("runs the ssh tool using the retrieved environment config", func() { + It("runs the ssh tool in dry run mode", func() { Expect(sshRunner.RunCallCount()).To(Equal(1)) - environmentConfig, _, _ := sshRunner.RunArgsForCall(0) - Expect(environmentConfig).To(BeEquivalentTo(environment.Config{Name: "env-name"})) + _, dryRun, _ := sshRunner.RunArgsForCall(0) + Expect(dryRun).To(BeTrue()) }) + }) - When("run with the file flag set", func() { - BeforeEach(func() { - sshOpsManagerCommand.File = true - }) + When("run with the file flag unset", func() { + BeforeEach(func() { + sshOpsManagerCommand.File = false + }) - It("runs the ssh tool in dry run mode", func() { - Expect(sshRunner.RunCallCount()).To(Equal(1)) + It("runs the ssh tool in non-dry run mode", func() { + Expect(sshRunner.RunCallCount()).To(Equal(1)) - _, dryRun, _ := sshRunner.RunArgsForCall(0) - Expect(dryRun).To(BeTrue()) - }) + _, dryRun, _ := sshRunner.RunArgsForCall(0) + Expect(dryRun).To(BeFalse()) }) + }) - When("run with the file flag unset", func() { - BeforeEach(func() { - sshOpsManagerCommand.File = false - }) + It("runs the ssh tool with no additional args", func() { + Expect(sshRunner.RunCallCount()).To(Equal(1)) - It("runs the ssh tool in non-dry run mode", func() { - Expect(sshRunner.RunCallCount()).To(Equal(1)) + _, _, args := sshRunner.RunArgsForCall(0) + Expect(args).To(BeEmpty()) + }) - _, dryRun, _ := sshRunner.RunArgsForCall(0) - Expect(dryRun).To(BeFalse()) - }) + When("running the ssh tool is successful", func() { + BeforeEach(func() { + sshRunner.RunReturns(nil) }) - It("runs the ssh tool with no additional args", func() { - Expect(sshRunner.RunCallCount()).To(Equal(1)) - - _, _, args := sshRunner.RunArgsForCall(0) - Expect(args).To(BeEmpty()) + It("doesn't error", func() { + Expect(err).NotTo(HaveOccurred()) }) + }) - When("running the ssh tool is successful", func() { - BeforeEach(func() { - sshRunner.RunReturns(nil) - }) - - It("doesn't error", func() { - Expect(err).NotTo(HaveOccurred()) - }) + When("running the ssh tool errors", func() { + BeforeEach(func() { + sshRunner.RunReturns(fmt.Errorf("ssh-runnner-error")) }) - When("running the ssh tool errors", func() { - BeforeEach(func() { - sshRunner.RunReturns(fmt.Errorf("ssh-runnner-error")) - }) - - It("propagates the error", func() { - Expect(err).To(MatchError("ssh-runnner-error")) - }) + It("propagates the error", func() { + Expect(err).To(MatchError("ssh-runnner-error")) }) }) }) @@ -165,94 +163,92 @@ var _ = Describe("ssh command", func() { } }) - Context("no subcommand", func() { - JustBeforeEach(func() { - err = sshDirectorCommand.Execute(commandArgs) + JustBeforeEach(func() { + err = sshDirectorCommand.Execute(commandArgs) + }) + + When("retrieving the environment config errors", func() { + BeforeEach(func() { + envReader.ReadReturns(environment.Config{}, fmt.Errorf("env-reader-error")) }) - When("retrieving the environment config errors", func() { - BeforeEach(func() { - envReader.ReadReturns(environment.Config{}, fmt.Errorf("env-reader-error")) - }) + It("doesn't attempt to run the ssh tool", func() { + Expect(sshRunner.RunCallCount()).To(Equal(0)) + }) - It("doesn't attempt to run the ssh tool", func() { - Expect(sshRunner.RunCallCount()).To(Equal(0)) - }) + It("propagates the error", func() { + Expect(err).To(MatchError("env-reader-error")) + }) + }) - It("propagates the error", func() { - Expect(err).To(MatchError("env-reader-error")) - }) + When("retrieving the environment config is successful", func() { + BeforeEach(func() { + envReader.ReadReturns(environment.Config{Name: "env-name"}, nil) }) - When("retrieving the environment config is successful", func() { - BeforeEach(func() { - envReader.ReadReturns(environment.Config{Name: "env-name"}, nil) - }) + It("displays that the connection is being started", func() { + Expect(ui.DisplayTextCallCount()).To(Equal(2)) + Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# ssh-director\n")) + Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Connecting to: env-name\n")) + }) + + It("runs the ssh tool using the retrieved environment config", func() { + Expect(sshRunner.RunCallCount()).To(Equal(1)) + + environmentConfig, _, _ := sshRunner.RunArgsForCall(0) + Expect(environmentConfig).To(BeEquivalentTo(environment.Config{Name: "env-name"})) + }) - It("displays that the connection is being started", func() { - Expect(ui.DisplayTextCallCount()).To(Equal(2)) - Expect(ui.DisplayTextArgsForCall(0)).To(Equal("# ssh-director\n")) - Expect(ui.DisplayTextArgsForCall(1)).To(Equal("Connecting to: env-name\n")) + When("run with the file flag set", func() { + BeforeEach(func() { + sshDirectorCommand.File = true }) - It("runs the ssh tool using the retrieved environment config", func() { + It("runs the ssh tool in dry run mode", func() { Expect(sshRunner.RunCallCount()).To(Equal(1)) - environmentConfig, _, _ := sshRunner.RunArgsForCall(0) - Expect(environmentConfig).To(BeEquivalentTo(environment.Config{Name: "env-name"})) + _, dryRun, _ := sshRunner.RunArgsForCall(0) + Expect(dryRun).To(BeTrue()) }) + }) - When("run with the file flag set", func() { - BeforeEach(func() { - sshDirectorCommand.File = true - }) + When("run with the file flag unset", func() { + BeforeEach(func() { + sshDirectorCommand.File = false + }) - It("runs the ssh tool in dry run mode", func() { - Expect(sshRunner.RunCallCount()).To(Equal(1)) + It("runs the ssh tool in non-dry run mode", func() { + Expect(sshRunner.RunCallCount()).To(Equal(1)) - _, dryRun, _ := sshRunner.RunArgsForCall(0) - Expect(dryRun).To(BeTrue()) - }) + _, dryRun, _ := sshRunner.RunArgsForCall(0) + Expect(dryRun).To(BeFalse()) }) + }) - When("run with the file flag unset", func() { - BeforeEach(func() { - sshDirectorCommand.File = false - }) + It("runs the ssh tool with no additional args", func() { + Expect(sshRunner.RunCallCount()).To(Equal(1)) - It("runs the ssh tool in non-dry run mode", func() { - Expect(sshRunner.RunCallCount()).To(Equal(1)) + _, _, args := sshRunner.RunArgsForCall(0) + Expect(args).To(BeEmpty()) + }) - _, dryRun, _ := sshRunner.RunArgsForCall(0) - Expect(dryRun).To(BeFalse()) - }) + When("running the ssh tool is successful", func() { + BeforeEach(func() { + sshRunner.RunReturns(nil) }) - It("runs the ssh tool with no additional args", func() { - Expect(sshRunner.RunCallCount()).To(Equal(1)) - - _, _, args := sshRunner.RunArgsForCall(0) - Expect(args).To(BeEmpty()) + It("doesn't error", func() { + Expect(err).NotTo(HaveOccurred()) }) + }) - When("running the ssh tool is successful", func() { - BeforeEach(func() { - sshRunner.RunReturns(nil) - }) - - It("doesn't error", func() { - Expect(err).NotTo(HaveOccurred()) - }) + When("running the ssh tool errors", func() { + BeforeEach(func() { + sshRunner.RunReturns(fmt.Errorf("ssh-runnner-error")) }) - When("running the ssh tool errors", func() { - BeforeEach(func() { - sshRunner.RunReturns(fmt.Errorf("ssh-runnner-error")) - }) - - It("propagates the error", func() { - Expect(err).To(MatchError("ssh-runnner-error")) - }) + It("propagates the error", func() { + Expect(err).To(MatchError("ssh-runnner-error")) }) }) }) diff --git a/integration/fixtures/ssh_script.sh b/integration/fixtures/ssh_opsman_script.sh similarity index 100% rename from integration/fixtures/ssh_script.sh rename to integration/fixtures/ssh_opsman_script.sh diff --git a/integration/ssh_command_test.go b/integration/ssh_command_test.go index 2d92ade..a31f37e 100644 --- a/integration/ssh_command_test.go +++ b/integration/ssh_command_test.go @@ -37,7 +37,7 @@ var _ = Describe("SSH", func() { contents, err := ioutil.ReadFile(pathToFile) Expect(err).NotTo(HaveOccurred()) - Expect(string(contents)).To(Equal(LoadFixture("ssh_script.sh"))) + Expect(string(contents)).To(Equal(LoadFixture("ssh_opsman_script.sh"))) }) })