github.com/sleungcy-sap/cli@v7.1.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  	commandName := "v3-packages"
    40  	if V7 {
    41  		commandName = "packages"
    42  	}
    43  	session := CF(commandName, appName)
    44  	Eventually(session).Should(Exit(0))
    45  
    46  	myRegexp, err := regexp.Compile(GUIDRegex)
    47  	Expect(err).NotTo(HaveOccurred())
    48  	matches := myRegexp.FindAll(session.Out.Contents(), -1)
    49  	packageGUID := matches[3]
    50  
    51  	return string(packageGUID)
    52  }
    53  
    54  func downloadFirstAppPackage(appName string, tmpZipFilepath string) {
    55  	appGUID := GetFirstAppPackageGuid(appName)
    56  	session := CF("curl", fmt.Sprintf("/v3/packages/%s/download", appGUID), "--output", tmpZipFilepath)
    57  	Eventually(session).Should(Exit(0))
    58  }
    59  
    60  // VerifyAppPackageContentsV2 verifies the contents of a V2 app package by downloading the package zip and
    61  // verifying the zipped files match the passed files.
    62  func VerifyAppPackageContentsV2(appName string, files ...string) {
    63  	tmpZipFilepath, err := ioutil.TempFile("", "")
    64  	defer os.Remove(tmpZipFilepath.Name())
    65  	Expect(err).ToNot(HaveOccurred())
    66  
    67  	downloadFirstAppBits(appName, tmpZipFilepath.Name())
    68  	Expect(err).ToNot(HaveOccurred())
    69  
    70  	info, err := tmpZipFilepath.Stat()
    71  	Expect(err).ToNot(HaveOccurred())
    72  	reader, err := ykk.NewReader(tmpZipFilepath, info.Size())
    73  	Expect(err).ToNot(HaveOccurred())
    74  
    75  	seenFiles := []string{}
    76  	for _, file := range reader.File {
    77  		seenFiles = append(seenFiles, file.Name)
    78  	}
    79  
    80  	Expect(seenFiles).To(ConsistOf(files))
    81  }
    82  
    83  func downloadFirstAppBits(appName string, tmpZipFilepath string) {
    84  	appGUID := AppGUID(appName)
    85  	session := CF("curl", fmt.Sprintf("/v2/apps/%s/download", appGUID), "--output", tmpZipFilepath)
    86  	Eventually(session).Should(Exit(0))
    87  }