|
| 1 | +package construct_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + |
| 11 | + "github.com/cloudfoundry/bosh-windows-stemcell-builder/stembuild/construct" |
| 12 | + "github.com/cloudfoundry/bosh-windows-stemcell-builder/stembuild/remotemanager" |
| 13 | + "github.com/cloudfoundry/bosh-windows-stemcell-builder/stembuild/remotemanager/remotemanagerfakes" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = Describe("NewScriptExecutor", func() { |
| 17 | + var ( |
| 18 | + fakeRemoteManager *remotemanagerfakes.FakeRemoteManager |
| 19 | + |
| 20 | + scriptExecutor *construct.ScriptExecutor |
| 21 | + ) |
| 22 | + |
| 23 | + BeforeEach(func() { |
| 24 | + fakeRemoteManager = &remotemanagerfakes.FakeRemoteManager{} |
| 25 | + |
| 26 | + scriptExecutor = construct.NewScriptExecutor(fakeRemoteManager) |
| 27 | + }) |
| 28 | + |
| 29 | + Describe("ScriptExecutor", func() { |
| 30 | + Describe("ExecuteSetupScript", func() { |
| 31 | + It("executes setup script with correct arguments", func() { |
| 32 | + version := "11.11.11" |
| 33 | + setupFlags := []string{"SomeFlag SomeValue", "OtherFlag OtherValue"} |
| 34 | + expectedCommandInvocation := |
| 35 | + fmt.Sprintf( |
| 36 | + "powershell.exe %s -Version %s %s", |
| 37 | + `C:\provision\Setup.ps1`, |
| 38 | + version, |
| 39 | + "-SomeFlag SomeValue -OtherFlag OtherValue", |
| 40 | + ) |
| 41 | + |
| 42 | + err := scriptExecutor.ExecuteSetupScript(version, setupFlags) |
| 43 | + Expect(err).NotTo(HaveOccurred()) |
| 44 | + |
| 45 | + executeCommandCallArg := fakeRemoteManager.ExecuteCommandArgsForCall(0) |
| 46 | + |
| 47 | + Expect(executeCommandCallArg).To(Equal(expectedCommandInvocation)) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + Describe("ExecutePostRebootScript", func() { |
| 52 | + var expectedTimeout time.Duration |
| 53 | + |
| 54 | + BeforeEach(func() { |
| 55 | + expectedTimeout = 24 * time.Hour |
| 56 | + }) |
| 57 | + |
| 58 | + It("executes post-reboot script with correct arguments", func() { |
| 59 | + expectedCommandInvocation := |
| 60 | + fmt.Sprintf( |
| 61 | + "powershell.exe %s", |
| 62 | + `C:\provision\PostReboot.ps1`, |
| 63 | + ) |
| 64 | + |
| 65 | + err := scriptExecutor.ExecutePostRebootScript(expectedTimeout) |
| 66 | + Expect(err).NotTo(HaveOccurred()) |
| 67 | + |
| 68 | + executeCommandWithTimeoutCallArg, actualTimeout := fakeRemoteManager.ExecuteCommandWithTimeoutArgsForCall(0) |
| 69 | + |
| 70 | + Expect(executeCommandWithTimeoutCallArg).To(Equal(expectedCommandInvocation)) |
| 71 | + Expect(expectedTimeout).To(Equal(actualTimeout)) |
| 72 | + }) |
| 73 | + |
| 74 | + Context("when there is an error", func() { |
| 75 | + var commandExecutionErrorCode int |
| 76 | + var commandExecutionErr error |
| 77 | + |
| 78 | + BeforeEach(func() { |
| 79 | + commandExecutionErrorCode = 999 |
| 80 | + }) |
| 81 | + |
| 82 | + Context("when the error is a powershell execution error", func() { |
| 83 | + BeforeEach(func() { |
| 84 | + powershellErrorPrefix := errors.New(remotemanager.PowershellExecutionErrorMessage) |
| 85 | + commandExecutionErr = fmt.Errorf("%s: %s", powershellErrorPrefix, "a command failed to run") |
| 86 | + |
| 87 | + fakeRemoteManager.ExecuteCommandWithTimeoutReturns(commandExecutionErrorCode, commandExecutionErr) |
| 88 | + }) |
| 89 | + |
| 90 | + It("returns the error", func() { |
| 91 | + err := scriptExecutor.ExecutePostRebootScript(expectedTimeout) |
| 92 | + |
| 93 | + Expect(err).To(MatchError(commandExecutionErr)) |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + Context("when the error is a NOT powershell execution error", func() { |
| 98 | + BeforeEach(func() { |
| 99 | + commandExecutionErr = errors.New("fake-non-powershell-execution-error") |
| 100 | + }) |
| 101 | + |
| 102 | + It("wraps a non-powershell execution error", func() { |
| 103 | + fakeRemoteManager.ExecuteCommandWithTimeoutReturns(1, commandExecutionErr) |
| 104 | + |
| 105 | + err := scriptExecutor.ExecutePostRebootScript(expectedTimeout) |
| 106 | + |
| 107 | + Expect(err).To(HaveOccurred()) |
| 108 | + Expect(err.Error()).To(Equal(fmt.Sprintf("winrm connection event: %s", commandExecutionErr.Error()))) |
| 109 | + }) |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | + }) |
| 114 | +}) |
0 commit comments