github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/fakeservicebroker/deployment.go (about)

     1  package fakeservicebroker
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"net/http"
     9  
    10  	"code.cloudfoundry.org/cli/integration/helpers"
    11  	"github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gbytes"
    14  	. "github.com/onsi/gomega/gexec"
    15  )
    16  
    17  const (
    18  	itsOrg             = "fakeservicebroker"
    19  	itsSpace           = "integration"
    20  	defaultMemoryLimit = "256M"
    21  	defaultBrokerPath  = "../../assets/service_broker"
    22  )
    23  
    24  func (f *FakeServiceBroker) pushAppIfNecessary() {
    25  	if !f.reusable {
    26  		f.pushApp()
    27  		return
    28  	}
    29  
    30  	if f.alreadyPushedApp() {
    31  		return
    32  	}
    33  
    34  	f.pushReusableApp()
    35  }
    36  
    37  func (f *FakeServiceBroker) pushApp() {
    38  	Eventually(helpers.CF(
    39  		"push", f.name,
    40  		"-p", defaultBrokerPath,
    41  		"-m", defaultMemoryLimit,
    42  	)).Should(Exit(0))
    43  }
    44  
    45  func (f *FakeServiceBroker) pushReusableApp() {
    46  	helpers.CreateOrgAndSpaceUnlessExists(itsOrg, itsSpace)
    47  	helpers.WithRandomHomeDir(func() {
    48  		helpers.SetAPI()
    49  		helpers.LoginCF()
    50  		helpers.TargetOrgAndSpace(itsOrg, itsSpace)
    51  
    52  		f.pushApp()
    53  	})
    54  }
    55  
    56  func (f *FakeServiceBroker) alreadyPushedApp() bool {
    57  	resp, err := http.Get(f.URL("/config"))
    58  	Expect(err).ToNot(HaveOccurred())
    59  	defer resp.Body.Close()
    60  	return resp.StatusCode == http.StatusOK
    61  }
    62  
    63  func (f *FakeServiceBroker) getCatalogServiceNames() []string {
    64  	if !f.alreadyPushedApp() {
    65  		return nil
    66  	}
    67  
    68  	resp, err := http.Get(f.URL("/v2/catalog"))
    69  	Expect(err).ToNot(HaveOccurred())
    70  
    71  	defer resp.Body.Close()
    72  	body, err := ioutil.ReadAll(resp.Body)
    73  	Expect(err).ToNot(HaveOccurred())
    74  
    75  	var data map[string]interface{}
    76  	err = json.Unmarshal(body, &data)
    77  	Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("expected '%s' to be a valid json", string(body)))
    78  
    79  	var serviceNames []string
    80  	for _, value := range data["services"].([]interface{}) {
    81  		service := value.(map[string]interface{})
    82  		serviceNames = append(serviceNames, service["name"].(string))
    83  	}
    84  
    85  	return serviceNames
    86  }
    87  
    88  func generateReusableBrokerName(suffix string) string {
    89  	return fmt.Sprintf("fake-service-broker-%s%02d", suffix, ginkgo.GinkgoParallelNode())
    90  }
    91  
    92  func (f *FakeServiceBroker) register() {
    93  	if f.reusable {
    94  		f.deregister()
    95  	}
    96  
    97  	Eventually(helpers.CF("create-service-broker", f.name, f.username, f.password, f.URL())).Should(Exit(0))
    98  
    99  	f.waitForBrokerToBeRegistered()
   100  }
   101  
   102  func (f *FakeServiceBroker) registerViaV2() {
   103  	if f.reusable {
   104  		f.deregister()
   105  	}
   106  
   107  	broker := map[string]interface{}{
   108  		"name":          f.name,
   109  		"broker_url":    f.URL(),
   110  		"auth_username": f.username,
   111  		"auth_password": f.password,
   112  	}
   113  	data, err := json.Marshal(broker)
   114  	Expect(err).NotTo(HaveOccurred())
   115  
   116  	Eventually(helpers.CF("curl", "-X", "POST", "/v2/service_brokers", "-d", string(data))).Should(Exit(0))
   117  
   118  	f.waitForBrokerToBeRegistered()
   119  }
   120  
   121  func (f *FakeServiceBroker) waitForBrokerToBeRegistered() {
   122  	Eventually(helpers.CF("service-brokers")).Should(And(Exit(0), Say(f.name)))
   123  
   124  	Eventually(func() io.Reader {
   125  		session := helpers.CF("service-access", "-b", f.name)
   126  		Eventually(session).Should(Exit(0))
   127  
   128  		return session.Out
   129  	}).Should(Say(f.ServiceName()))
   130  }
   131  
   132  func (f *FakeServiceBroker) update() {
   133  	Eventually(helpers.CF("update-service-broker", f.name, "username", "password", f.URL())).Should(Exit(0))
   134  	Eventually(helpers.CF("service-brokers")).Should(And(Exit(0), Say(f.name)))
   135  }
   136  
   137  func (f *FakeServiceBroker) cleanup() {
   138  	f.deregister()
   139  	f.deleteApp()
   140  }
   141  
   142  func (f *FakeServiceBroker) deleteApp() {
   143  	if f.reusable {
   144  		return
   145  	}
   146  
   147  	Eventually(helpers.CF("delete", f.name, "-f", "-r")).Should(Exit(0))
   148  }
   149  
   150  func (f *FakeServiceBroker) deregister() {
   151  	f.purgeAllServiceOfferings(true)
   152  
   153  	Eventually(helpers.CF("delete-service-broker", f.name, "-f")).Should(Exit())
   154  }
   155  
   156  func (f *FakeServiceBroker) purgeAllServiceOfferings(ignoreFailures bool) {
   157  	for _, service := range f.getCatalogServiceNames() {
   158  		assertion := Eventually(helpers.CF("purge-service-offering", service, "-b", f.name, "-f"))
   159  
   160  		if ignoreFailures {
   161  			assertion.Should(Exit())
   162  		} else {
   163  			assertion.Should(Exit(0))
   164  		}
   165  	}
   166  }
   167  
   168  func (f *FakeServiceBroker) stopReusing() *FakeServiceBroker {
   169  	f.reusable = false
   170  	return f
   171  }