github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/integration/construct/construct_test.go (about) 1 package construct_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "regexp" 9 "time" 10 11 "github.com/cloudfoundry-incubator/stembuild/test/helpers" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 . "github.com/onsi/gomega/gbytes" 15 . "github.com/onsi/gomega/gexec" 16 ) 17 18 const ( 19 constructOutputTimeout = 60 * time.Second 20 shutdownTimeout = 5 * time.Minute 21 ) 22 23 var _ = Describe("stembuild construct", func() { 24 var workingDir string 25 26 BeforeEach(func() { 27 var err error 28 workingDir, err = os.Getwd() 29 Expect(err).ToNot(HaveOccurred()) 30 }) 31 32 AfterEach(func() { 33 _ = os.Remove(filepath.Join(workingDir, "LGPO.zip")) 34 }) 35 Context("run successfully", func() { 36 BeforeEach(func() { 37 err := CopyFile(filepath.Join(workingDir, "assets", "LGPO.zip"), filepath.Join(workingDir, "LGPO.zip")) 38 Expect(err).ToNot(HaveOccurred()) 39 }) 40 41 It("successfully exits when vm becomes powered off", func() { 42 session := helpers.Stembuild(stembuildExecutable, "construct", "-vm-ip", conf.TargetIP, "-vm-username", conf.VMUsername, "-vm-password", conf.VMPassword, "-vcenter-url", conf.VCenterURL, "-vcenter-username", conf.VCenterUsername, "-vcenter-password", conf.VCenterPassword, "-vm-inventory-path", conf.VMInventoryPath, "-vcenter-ca-certs", conf.VCenterCACert) 43 44 Eventually(session, shutdownTimeout).Should(Exit(0)) 45 }) 46 47 It("transfers LGPO and StemcellAutomation archives, unarchive them and execute automation script", func() { 48 session := helpers.Stembuild(stembuildExecutable, "construct", "-vm-ip", conf.TargetIP, "-vm-username", conf.VMUsername, "-vm-password", conf.VMPassword, "-vcenter-url", conf.VCenterURL, "-vcenter-username", conf.VCenterUsername, "-vcenter-password", conf.VCenterPassword, "-vm-inventory-path", conf.VMInventoryPath, "-vcenter-ca-certs", conf.VCenterCACert) 49 50 Eventually(session.Out, constructOutputTimeout).Should(Say(`mock stemcell automation script executed`)) 51 }) 52 53 It("executes post-reboot automation script", func() { 54 session := helpers.Stembuild(stembuildExecutable, "construct", "-vm-ip", conf.TargetIP, "-vm-username", conf.VMUsername, "-vm-password", conf.VMPassword, "-vcenter-url", conf.VCenterURL, "-vcenter-username", conf.VCenterUsername, "-vcenter-password", conf.VCenterPassword, "-vm-inventory-path", conf.VMInventoryPath, "-vcenter-ca-certs", conf.VCenterCACert) 55 56 Eventually(session.Out, constructOutputTimeout*5).Should(Say(`mock stemcell automation post-reboot script executed`)) 57 }) 58 59 It("extracts the WinRM BOSH powershell script and executes it successfully on the guest VM", func() { 60 session := helpers.Stembuild(stembuildExecutable, "construct", "-vm-ip", conf.TargetIP, "-vm-username", conf.VMUsername, "-vm-password", conf.VMPassword, "-vcenter-url", conf.VCenterURL, "-vcenter-username", conf.VCenterUsername, "-vcenter-password", conf.VCenterPassword, "-vm-inventory-path", conf.VMInventoryPath, "-vcenter-ca-certs", conf.VCenterCACert) 61 62 Eventually(session.Out, constructOutputTimeout).Should(Say(`Attempting to enable WinRM on the guest vm...WinRm enabled on the guest VM`)) 63 }) 64 65 It("handles special characters", func() { 66 isAlphaNumeric, err := regexp.Compile("[a-zA-Z0-9]+") 67 Expect(err).ToNot(HaveOccurred()) 68 69 if isAlphaNumeric.MatchString(conf.VCenterUsername) && isAlphaNumeric.MatchString(conf.VCenterPassword) { 70 Skip("vCenter username or password must contain special characters") 71 } 72 session := helpers.Stembuild(stembuildExecutable, "construct", "-vm-ip", conf.TargetIP, "-vm-username", conf.VMUsername, "-vm-password", conf.VMPassword, "-vcenter-url", conf.VCenterURL, "-vcenter-username", conf.VCenterUsername, "-vcenter-password", conf.VCenterPassword, "-vm-inventory-path", conf.VMInventoryPath, "-vcenter-ca-certs", conf.VCenterCACert) 73 74 Eventually(session, constructOutputTimeout).Should(Exit(0)) 75 Eventually(session.Out).Should(Say(`mock stemcell automation script executed`)) 76 }) 77 78 It("successfully runs even when a user has logged in", func() { 79 fmt.Printf("Logged on VM for test has IP: %s\n", conf.LoggedInVMIP) 80 81 revertSnapshot(conf.LoggedInVMIpath, conf.LoggedInVMSnapshot) 82 83 waitForVmToBeReady(conf.LoggedInVMIP, conf.VMUsername, conf.VMPassword) 84 time.Sleep(30 * time.Second) 85 86 session := helpers.Stembuild(stembuildExecutable, "construct", 87 "-vm-ip", conf.LoggedInVMIP, 88 "-vm-username", conf.VMUsername, 89 "-vm-password", conf.VMPassword, 90 "-vcenter-url", conf.VCenterURL, 91 "-vcenter-ca-certs", conf.VCenterCACert, 92 "-vcenter-username", conf.VCenterUsername, 93 "-vcenter-password", conf.VCenterPassword, 94 "-vm-inventory-path", conf.LoggedInVMIpath) 95 96 Eventually(session, shutdownTimeout).Should(Exit(0)) 97 }) 98 }) 99 100 It("fails with an appropriate error when LGPO is missing", func() { 101 session := helpers.Stembuild(stembuildExecutable, "construct", "-vm-ip", conf.TargetIP, "-vm-username", conf.VMUsername, "-vm-password", conf.VMPassword, "-vcenter-url", conf.VCenterURL, "-vcenter-username", conf.VCenterUsername, "-vcenter-password", conf.VCenterPassword, "-vm-inventory-path", conf.VMInventoryPath, "-vcenter-ca-certs", conf.VCenterCACert) 102 103 Eventually(session, constructOutputTimeout).Should(Exit(1)) 104 Eventually(session.Err).Should(Say(`Could not find LGPO.zip in the current directory`)) 105 }) 106 107 It("does not exit when the target VM has not powered off", func() { 108 err := CopyFile(filepath.Join(workingDir, "assets", "LGPO.zip"), filepath.Join(workingDir, "LGPO.zip")) 109 Expect(err).ToNot(HaveOccurred()) 110 111 fakeStemcellAutomationShutdownDelay := 45 * time.Second 112 113 session := helpers.Stembuild(stembuildExecutable, "construct", "-vm-ip", conf.TargetIP, "-vm-username", conf.VMUsername, "-vm-password", conf.VMPassword, "-vcenter-url", conf.VCenterURL, "-vcenter-username", conf.VCenterUsername, "-vcenter-password", conf.VCenterPassword, "-vm-inventory-path", conf.VMInventoryPath, "-vcenter-ca-certs", conf.VCenterCACert) 114 115 Consistently(session, fakeStemcellAutomationShutdownDelay-5*time.Second).Should(Not(Exit())) 116 }) 117 }) 118 119 func CopyFile(src string, dest string) error { 120 input, err := ioutil.ReadFile(src) 121 if err != nil { 122 fmt.Println(err) 123 return err 124 } 125 err = ioutil.WriteFile(dest, input, 0644) 126 if err != nil { 127 fmt.Println("Error creating file") 128 fmt.Println(err) 129 return err 130 } 131 132 return err 133 }