github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/integration/helpers/buildpack.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"regexp"
     9  
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  func MakeBuildpackArchive(stackName string) string {
    16  	archiveFile, err := ioutil.TempFile("", "buildpack-archive-file-")
    17  	Expect(err).ToNot(HaveOccurred())
    18  	err = archiveFile.Close()
    19  	Expect(err).ToNot(HaveOccurred())
    20  	err = os.RemoveAll(archiveFile.Name())
    21  	Expect(err).ToNot(HaveOccurred())
    22  
    23  	archiveName := archiveFile.Name() + ".zip"
    24  
    25  	dir, err := ioutil.TempDir("", "buildpack-dir-")
    26  	Expect(err).ToNot(HaveOccurred())
    27  	defer os.RemoveAll(dir)
    28  
    29  	manifest := filepath.Join(dir, "manifest.yml")
    30  	err = ioutil.WriteFile(manifest, []byte(fmt.Sprintf("stack: %s", stackName)), 0666)
    31  	Expect(err).ToNot(HaveOccurred())
    32  
    33  	err = Zipit(dir, archiveName, "")
    34  	Expect(err).ToNot(HaveOccurred())
    35  
    36  	return archiveName
    37  }
    38  
    39  func BuildpackWithStack(f func(buildpackArchive string), stackName string) {
    40  	buildpackZip := MakeBuildpackArchive(stackName)
    41  	defer os.Remove(buildpackZip)
    42  
    43  	f(buildpackZip)
    44  }
    45  
    46  func BuildpacksOutputRegex(fields BuildpackFields) string {
    47  	anyStringRegex := `\S+`
    48  	optionalStringRegex := `\S*`
    49  	anyBoolRegex := `(true|false)`
    50  	anyIntRegex := `\d+`
    51  
    52  	nameRegex := anyStringRegex
    53  	if fields.Name != "" {
    54  		nameRegex = regexp.QuoteMeta(fields.Name)
    55  	}
    56  
    57  	positionRegex := anyIntRegex
    58  	if fields.Position != "" {
    59  		positionRegex = regexp.QuoteMeta(fields.Position)
    60  	}
    61  
    62  	enabledRegex := anyBoolRegex
    63  	if fields.Enabled != "" {
    64  		enabledRegex = regexp.QuoteMeta(fields.Enabled)
    65  	}
    66  
    67  	lockedRegex := anyBoolRegex
    68  	if fields.Locked != "" {
    69  		lockedRegex = regexp.QuoteMeta(fields.Locked)
    70  	}
    71  
    72  	filenameRegex := anyStringRegex
    73  	if fields.Filename != "" {
    74  		filenameRegex = regexp.QuoteMeta(fields.Filename)
    75  	}
    76  
    77  	stackRegex := optionalStringRegex
    78  	if fields.Stack != "" {
    79  		stackRegex = regexp.QuoteMeta(fields.Stack)
    80  	}
    81  
    82  	return fmt.Sprintf(`%s\s+%s\s+%s\s+%s\s+%s\s+%s`, nameRegex, positionRegex, enabledRegex,
    83  		lockedRegex, filenameRegex, stackRegex)
    84  }
    85  
    86  type BuildpackFields struct {
    87  	Name     string
    88  	Position string
    89  	Enabled  string
    90  	Locked   string
    91  	Filename string
    92  	Stack    string
    93  }
    94  
    95  func BuildpackWithoutStack(f func(buildpackArchive string)) {
    96  	BuildpackWithStack(f, "")
    97  }
    98  
    99  func SetupBuildpackWithStack(buildpackName, stack string) {
   100  	BuildpackWithStack(func(buildpackPath string) {
   101  		session := CF("create-buildpack", buildpackName, buildpackPath, "99")
   102  		Eventually(session).Should(Say("OK"))
   103  		Eventually(session).Should(Say("OK"))
   104  		Eventually(session).Should(Exit(0))
   105  	}, stack)
   106  }
   107  
   108  func SetupBuildpackWithoutStack(buildpackName string) {
   109  	SetupBuildpackWithStack(buildpackName, "")
   110  }
   111  
   112  // DeleteBuildpackIfOnOldCCAPI deletes the buildpack if the CC API targeted
   113  // by the current test run is <= 2.80.0. Before this version, some entities
   114  // would receive and invalid next_url in paginated requests. Since our test run
   115  // now generally creates more than 50 buildpacks, we need to delete test buildpacks
   116  // after use if we are targeting and older CC API.
   117  // see https://github.com/cloudfoundry/capi-release/releases/tag/1.45.0
   118  func DeleteBuildpackIfOnOldCCAPI(buildpackName string) {
   119  	minVersion := "2.99.0"
   120  	if !IsVersionMet(minVersion) {
   121  		deleteSessions := CF("delete-buildpack", buildpackName, "-f")
   122  		Eventually(deleteSessions).Should(Exit())
   123  	}
   124  }