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

     1  package helpers
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  // MakeBuildpackArchive makes a simple buildpack zip for a given stack.
    16  func MakeBuildpackArchive(stackName string) string {
    17  	archiveFile, err := ioutil.TempFile("", "buildpack-archive-file-")
    18  	Expect(err).ToNot(HaveOccurred())
    19  	err = archiveFile.Close()
    20  	Expect(err).ToNot(HaveOccurred())
    21  	err = os.RemoveAll(archiveFile.Name())
    22  	Expect(err).ToNot(HaveOccurred())
    23  
    24  	archiveName := archiveFile.Name() + ".zip"
    25  
    26  	dir, err := ioutil.TempDir("", "buildpack-dir-")
    27  	Expect(err).ToNot(HaveOccurred())
    28  	defer os.RemoveAll(dir)
    29  
    30  	manifest := filepath.Join(dir, "manifest.yml")
    31  	err = ioutil.WriteFile(manifest, []byte(fmt.Sprintf("stack: %s", stackName)), 0666)
    32  	Expect(err).ToNot(HaveOccurred())
    33  
    34  	err = Zipit(dir, archiveName, "")
    35  	Expect(err).ToNot(HaveOccurred())
    36  
    37  	return archiveName
    38  }
    39  
    40  // BuildpackWithStack makes a simple buildpack for the given stack (using
    41  // MakeBuildpackArchive) and yields it to the given function, removing the zip
    42  // at the end.
    43  func BuildpackWithStack(f func(buildpackArchive string), stackName string) {
    44  	buildpackZip := MakeBuildpackArchive(stackName)
    45  	defer os.Remove(buildpackZip)
    46  
    47  	f(buildpackZip)
    48  }
    49  
    50  // BuildpackWithoutStack makes a simple buildpack without a stack (using
    51  // MakeBuildpackArchive) and yields it to the given function, removing the zip
    52  // at the end.
    53  func BuildpackWithoutStack(f func(buildpackArchive string)) {
    54  	BuildpackWithStack(f, "")
    55  }
    56  
    57  // SetupBuildpackWithStack makes and uploads a buildpack for the given stack.
    58  func SetupBuildpackWithStack(buildpackName, stack string) {
    59  	BuildpackWithStack(func(buildpackPath string) {
    60  		session := CF("create-buildpack", buildpackName, buildpackPath, "99")
    61  		Eventually(session).Should(Say("OK"))
    62  		Eventually(session).Should(Say("OK"))
    63  		Eventually(session).Should(Exit(0))
    64  	}, stack)
    65  }
    66  
    67  // SetupBuildpackWithoutStack makes and uploads a buildpack without a stack.
    68  func SetupBuildpackWithoutStack(buildpackName string) {
    69  	SetupBuildpackWithStack(buildpackName, "")
    70  }
    71  
    72  // BuildpackFields represents a buildpack, displayed in the 'cf buildpacks'
    73  // command.
    74  type BuildpackFields struct {
    75  	Position string
    76  	Name     string
    77  	Enabled  string
    78  	Locked   string
    79  	Filename string
    80  	Stack    string
    81  }
    82  
    83  // DeleteBuildpackIfOnOldCCAPI deletes the buildpack if the CC API targeted
    84  // by the current test run is <= 2.80.0. Before this version, some entities
    85  // would receive and invalid next_url in paginated requests. Since our test run
    86  // now generally creates more than 50 buildpacks, we need to delete test buildpacks
    87  // after use if we are targeting an older CC API.
    88  // see https://github.com/cloudfoundry/capi-release/releases/tag/1.45.0
    89  func DeleteBuildpackIfOnOldCCAPI(buildpackName string) {
    90  	minVersion := "2.99.0"
    91  	if !IsVersionMet(minVersion) {
    92  		deleteSessions := CF("delete-buildpack", buildpackName, "-f")
    93  		Eventually(deleteSessions).Should(Exit())
    94  	}
    95  }
    96  
    97  type Buildpack struct {
    98  	GUID  string `json:"guid"`
    99  	Name  string `json:"name"`
   100  	Stack string `json:"stack"`
   101  }
   102  
   103  type BuildpackList struct {
   104  	Buildpacks []Buildpack `json:"resources"`
   105  }
   106  
   107  func BuildpackGUIDByNameAndStack(buildpackName string, stackName string) string {
   108  	url := "/v3/buildpacks?names=" + buildpackName
   109  	if stackName != "" {
   110  		url += "&stacks=" + stackName
   111  	}
   112  	session := CF("curl", url)
   113  	bytes := session.Wait().Out.Contents()
   114  
   115  	buildpacks := BuildpackList{}
   116  	err := json.Unmarshal(bytes, &buildpacks)
   117  	Expect(err).ToNot(HaveOccurred())
   118  	Expect(len(buildpacks.Buildpacks)).To(BeNumerically(">", 0))
   119  	if stackName != "" {
   120  		return buildpacks.Buildpacks[0].GUID
   121  	}
   122  	for _, buildpack := range buildpacks.Buildpacks {
   123  		if buildpack.Stack == "" {
   124  			return buildpack.GUID
   125  		}
   126  	}
   127  	return ""
   128  }