github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/integration/helpers/package.go (about)

     1  package helpers
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"regexp"
     9  
    10  	"code.cloudfoundry.org/ykk"
    11  
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  type Package struct {
    17  	Checksum string
    18  }
    19  
    20  func (p *Package) UnmarshalJSON(data []byte) error {
    21  	var ccPackage struct {
    22  		Data struct {
    23  			Checksum struct {
    24  				Value string `json:"value"`
    25  			} `json:"checksum"`
    26  		} `json:"data"`
    27  	}
    28  
    29  	if err := json.Unmarshal(data, &ccPackage); err != nil {
    30  		return err
    31  	}
    32  
    33  	p.Checksum = ccPackage.Data.Checksum.Value
    34  
    35  	return nil
    36  }
    37  
    38  func VerifyAppPackageContentsV3(appName string, files ...string) {
    39  	tmpZipFilepath, err := ioutil.TempFile("", "")
    40  	defer os.Remove(tmpZipFilepath.Name())
    41  	Expect(err).ToNot(HaveOccurred())
    42  
    43  	downloadFirstAppPackage(appName, tmpZipFilepath.Name())
    44  	Expect(err).ToNot(HaveOccurred())
    45  
    46  	info, err := tmpZipFilepath.Stat()
    47  	Expect(err).ToNot(HaveOccurred())
    48  	reader, err := ykk.NewReader(tmpZipFilepath, info.Size())
    49  	Expect(err).ToNot(HaveOccurred())
    50  
    51  	seenFiles := []string{}
    52  	for _, file := range reader.File {
    53  		seenFiles = append(seenFiles, file.Name)
    54  	}
    55  
    56  	Expect(seenFiles).To(ConsistOf(files))
    57  }
    58  
    59  func getFirstAppPackageGuid(appName string) string {
    60  	session := CF("v3-packages", appName)
    61  	Eventually(session).Should(Exit(0))
    62  
    63  	myRegexp, err := regexp.Compile(GUIDRegex)
    64  	Expect(err).NotTo(HaveOccurred())
    65  	matches := myRegexp.FindAll(session.Out.Contents(), -1)
    66  	packageGUID := matches[3]
    67  
    68  	return string(packageGUID)
    69  }
    70  
    71  func downloadFirstAppPackage(appName string, tmpZipFilepath string) {
    72  	appGUID := getFirstAppPackageGuid(appName)
    73  	session := CF("curl", fmt.Sprintf("/v3/packages/%s/download", appGUID), "--output", tmpZipFilepath)
    74  	Eventually(session).Should(Exit(0))
    75  	return
    76  }
    77  
    78  func VerifyAppPackageContentsV2(appName string, files ...string) {
    79  	tmpZipFilepath, err := ioutil.TempFile("", "")
    80  	defer os.Remove(tmpZipFilepath.Name())
    81  	Expect(err).ToNot(HaveOccurred())
    82  
    83  	downloadFirstAppBits(appName, tmpZipFilepath.Name())
    84  	Expect(err).ToNot(HaveOccurred())
    85  
    86  	info, err := tmpZipFilepath.Stat()
    87  	Expect(err).ToNot(HaveOccurred())
    88  	reader, err := ykk.NewReader(tmpZipFilepath, info.Size())
    89  	Expect(err).ToNot(HaveOccurred())
    90  
    91  	seenFiles := []string{}
    92  	for _, file := range reader.File {
    93  		seenFiles = append(seenFiles, file.Name)
    94  	}
    95  
    96  	Expect(seenFiles).To(ConsistOf(files))
    97  }
    98  
    99  func downloadFirstAppBits(appName string, tmpZipFilepath string) {
   100  	appGUID := AppGUID(appName)
   101  	session := CF("curl", fmt.Sprintf("/v2/apps/%s/download", appGUID), "--output", tmpZipFilepath)
   102  	Eventually(session).Should(Exit(0))
   103  	return
   104  }