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