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