Skip to content

Commit

Permalink
Update ssh tests
Browse files Browse the repository at this point in the history
- Remove unnecessary contexts
- Standardise ssh script fixtures

Co-authored-by: Glen Rodgers <[email protected]>
  • Loading branch information
edwardecook and gmrodgers committed Sep 25, 2019
1 parent 764ea8f commit f69549c
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 127 deletions.
248 changes: 122 additions & 126 deletions commands/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
})
})
Expand Down Expand Up @@ -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"))
})
})
})
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion integration/ssh_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
})
})

Expand Down

0 comments on commit f69549c

Please sign in to comment.