github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+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 } 76 77 func VerifyAppPackageContentsV2(appName string, files ...string) { 78 tmpZipFilepath, err := ioutil.TempFile("", "") 79 defer os.Remove(tmpZipFilepath.Name()) 80 Expect(err).ToNot(HaveOccurred()) 81 82 downloadFirstAppBits(appName, tmpZipFilepath.Name()) 83 Expect(err).ToNot(HaveOccurred()) 84 85 info, err := tmpZipFilepath.Stat() 86 Expect(err).ToNot(HaveOccurred()) 87 reader, err := ykk.NewReader(tmpZipFilepath, info.Size()) 88 Expect(err).ToNot(HaveOccurred()) 89 90 seenFiles := []string{} 91 for _, file := range reader.File { 92 seenFiles = append(seenFiles, file.Name) 93 } 94 95 Expect(seenFiles).To(ConsistOf(files)) 96 } 97 98 func downloadFirstAppBits(appName string, tmpZipFilepath string) { 99 appGUID := AppGUID(appName) 100 session := CF("curl", fmt.Sprintf("/v2/apps/%s/download", appGUID), "--output", tmpZipFilepath) 101 Eventually(session).Should(Exit(0)) 102 }