github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/helpers/servicebrokerstub/app_deploy.go (about) 1 package servicebrokerstub 2 3 import ( 4 "fmt" 5 "net/http" 6 "os" 7 "regexp" 8 "strings" 9 "sync" 10 "time" 11 12 "github.com/onsi/ginkgo" 13 14 "code.cloudfoundry.org/cli/integration/helpers" 15 . "github.com/onsi/gomega" 16 ) 17 18 const ( 19 appNamePrefix = "hydrabroker" 20 appOrg = "fakeservicebroker" 21 appSpace = "integration" 22 defaultMemoryLimit = "32M" 23 pathToApp = "../../assets/hydrabroker" 24 ) 25 26 var mutex sync.Mutex 27 28 func ensureAppIsDeployed() { 29 if !appResponds() { 30 ensureAppIsPushed() 31 Eventually(appResponds).Should(BeTrue()) 32 } 33 } 34 35 func appResponds() bool { 36 resp, err := http.Head(appURL()) 37 Expect(err).ToNot(HaveOccurred()) 38 defer resp.Body.Close() 39 return resp.StatusCode == http.StatusNoContent 40 } 41 42 func ensureAppIsPushed() { 43 appExists := func() bool { 44 session := helpers.CF("app", "--guid", appName()) 45 session.Wait() 46 return session.ExitCode() == 0 47 } 48 49 pushApp := func() bool { 50 session := helpers.CF( 51 "push", appName(), 52 "-p", pathToApp, 53 "-m", defaultMemoryLimit, 54 "-b", "https://github.com/cloudfoundry/go-buildpack.git", // Some legacy envs have buildpack that's too old 55 ) 56 session.Wait() 57 return session.ExitCode() == 0 58 } 59 60 cleanupAppsFromPreviousRuns := func() { 61 session := helpers.CF("apps").Wait() 62 63 if session.ExitCode() == 0 { 64 matchingApps := regexp.MustCompile(fmt.Sprintf(`%s-\d+`, appNamePrefix)). 65 FindAllString(string(session.Out.Contents()), -1) 66 67 for _, app := range matchingApps { 68 if app != appName() { 69 session := helpers.CF("delete", app, "-f") 70 session.Wait() 71 } 72 } 73 } 74 } 75 76 // mutex protects from goroutines, and we retry later to protect from other test processes 77 mutex.Lock() 78 defer mutex.Unlock() 79 80 helpers.CreateOrgAndSpaceUnlessExists(appOrg, appSpace) 81 helpers.WithRandomHomeDir(func() { 82 helpers.SetAPI() 83 helpers.LoginCF() 84 helpers.TargetOrgAndSpace(appOrg, appSpace) 85 86 cleanupAppsFromPreviousRuns() 87 88 ok := false 89 for attempts := 0; attempts < 5 && !ok; attempts++ { 90 ok = appExists() 91 if !ok { 92 ok = pushApp() 93 } 94 if !ok { 95 time.Sleep(time.Second) 96 } 97 } 98 99 Expect(ok).To(BeTrue(), "Failed to push app") 100 }) 101 } 102 103 func appURL(paths ...string) string { 104 return fmt.Sprintf("http://%s.%s%s", appName(), helpers.DefaultSharedDomain(), strings.Join(paths, "")) 105 } 106 107 func appName() string { 108 id := ginkgo.GinkgoRandomSeed() 109 if len(os.Getenv("REUSE_SERVICE_BROKER_APP")) > 0 { 110 id = 0 111 } 112 return fmt.Sprintf("%s-%010d", appNamePrefix, id) 113 }