github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/helpers/package.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"regexp"
     8  
     9  	"code.cloudfoundry.org/ykk"
    10  
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  // VerifyAppPackageContentsV3 verifies the contents of a V3 app package by downloading the package zip and
    16  // verifying the zipped files match the passed files.
    17  func VerifyAppPackageContentsV3(appName string, files ...string) {
    18  	tmpZipFilepath, err := ioutil.TempFile("", "")
    19  	defer os.Remove(tmpZipFilepath.Name())
    20  	Expect(err).ToNot(HaveOccurred())
    21  
    22  	downloadFirstAppPackage(appName, tmpZipFilepath.Name())
    23  	Expect(err).ToNot(HaveOccurred())
    24  
    25  	info, err := tmpZipFilepath.Stat()
    26  	Expect(err).ToNot(HaveOccurred())
    27  	reader, err := ykk.NewReader(tmpZipFilepath, info.Size())
    28  	Expect(err).ToNot(HaveOccurred())
    29  
    30  	seenFiles := []string{}
    31  	for _, file := range reader.File {
    32  		seenFiles = append(seenFiles, file.Name)
    33  	}
    34  
    35  	Expect(seenFiles).To(ConsistOf(files))
    36  }
    37  
    38  func getFirstAppPackageGuid(appName string) string {
    39  	session := CF("v3-packages", appName)
    40  	Eventually(session).Should(Exit(0))
    41  
    42  	myRegexp, err := regexp.Compile(GUIDRegex)
    43  	Expect(err).NotTo(HaveOccurred())
    44  	matches := myRegexp.FindAll(session.Out.Contents(), -1)
    45  	packageGUID := matches[3]
    46  
    47  	return string(packageGUID)
    48  }
    49  
    50  func downloadFirstAppPackage(appName string, tmpZipFilepath string) {
    51  	appGUID := getFirstAppPackageGuid(appName)
    52  	session := CF("curl", fmt.Sprintf("/v3/packages/%s/download", appGUID), "--output", tmpZipFilepath)
    53  	Eventually(session).Should(Exit(0))
    54  }
    55  
    56  // VerifyAppPackageContentsV2 verifies the contents of a V2 app package by downloading the package zip and
    57  // verifying the zipped files match the passed files.
    58  func VerifyAppPackageContentsV2(appName string, files ...string) {
    59  	tmpZipFilepath, err := ioutil.TempFile("", "")
    60  	defer os.Remove(tmpZipFilepath.Name())
    61  	Expect(err).ToNot(HaveOccurred())
    62  
    63  	downloadFirstAppBits(appName, tmpZipFilepath.Name())
    64  	Expect(err).ToNot(HaveOccurred())
    65  
    66  	info, err := tmpZipFilepath.Stat()
    67  	Expect(err).ToNot(HaveOccurred())
    68  	reader, err := ykk.NewReader(tmpZipFilepath, info.Size())
    69  	Expect(err).ToNot(HaveOccurred())
    70  
    71  	seenFiles := []string{}
    72  	for _, file := range reader.File {
    73  		seenFiles = append(seenFiles, file.Name)
    74  	}
    75  
    76  	Expect(seenFiles).To(ConsistOf(files))
    77  }
    78  
    79  func downloadFirstAppBits(appName string, tmpZipFilepath string) {
    80  	appGUID := AppGUID(appName)
    81  	session := CF("curl", fmt.Sprintf("/v2/apps/%s/download", appGUID), "--output", tmpZipFilepath)
    82  	Eventually(session).Should(Exit(0))
    83  }