github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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 GetFirstAppPackageGuid(appName string) string {
    39  	session := CF("v3-packages", appName)
    40  	Eventually(session).Should(Exit(0))
    41  
    42  	// myRegexp, err := regexp.Compile("([a-f0-9-])\\s+ready")
    43  	myRegexp, err := regexp.Compile(GUIDRegex)
    44  	Expect(err).NotTo(HaveOccurred())
    45  	matches := myRegexp.FindAll(session.Out.Contents(), -1)
    46  	packageGUID := matches[3]
    47  
    48  	return string(packageGUID)
    49  }
    50  
    51  func DownloadFirstAppPackage(appName string, tmpZipFilepath string) {
    52  	packageGUID := GetFirstAppPackageGuid(appName)
    53  	session := CF("curl", fmt.Sprintf("/v3/packages/%s/download", packageGUID), "--output", tmpZipFilepath)
    54  	Eventually(session).Should(Exit(0))
    55  	return
    56  }
    57  
    58  func VerifyAppPackageContents(appName string, files ...string) {
    59  	tmpZipFilepath, err := ioutil.TempFile("", "")
    60  	defer os.Remove(tmpZipFilepath.Name())
    61  	Expect(err).ToNot(HaveOccurred())
    62  
    63  	DownloadFirstAppPackage(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  }