forked from cloudfoundry/cli-plugin-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo_index_test.go
135 lines (111 loc) · 3.91 KB
/
repo_index_test.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main_test
import (
"encoding/hex"
"io/ioutil"
"os"
"strings"
"code.cloudfoundry.org/cli-plugin-repo/sort/yamlsorter"
"code.cloudfoundry.org/cli-plugin-repo/web"
"net/url"
"crypto/sha1"
"fmt"
"net/http"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v2"
)
var _ = Describe("Database", func() {
It("correctly parses the current repo-index.yml", func() {
var plugins web.PluginsJson
b, err := ioutil.ReadFile("repo-index.yml")
Expect(err).NotTo(HaveOccurred())
err = yaml.Unmarshal(b, &plugins)
Expect(err).NotTo(HaveOccurred())
})
Describe("validations", func() {
var plugins web.PluginsJson
var pluginBytes []byte
BeforeEach(func() {
var err error
pluginBytes, err = ioutil.ReadFile("repo-index.yml")
Expect(err).NotTo(HaveOccurred())
err = yaml.Unmarshal(pluginBytes, &plugins)
Expect(err).NotTo(HaveOccurred())
})
It("the yaml file is sorted", func() {
var yamlSorter yamlsorter.YAMLSorter
sortedBytes, err := yamlSorter.Sort(pluginBytes)
Expect(err).NotTo(HaveOccurred())
Expect(sortedBytes).To(Equal(pluginBytes), "file is not sorted; please run 'go run sort/main.go repo-index.yml'.\n")
})
It("has every binary link over https", func() {
for _, plugin := range plugins.Plugins {
for _, binary := range plugin.Binaries {
url, err := url.Parse(binary.Url)
Expect(err).NotTo(HaveOccurred())
Expect(url.Scheme).To(Equal("https"))
}
}
})
It("has every version parseable by semver", func() {
for _, plugin := range plugins.Plugins {
Expect(plugin.Version).To(MatchRegexp(`^\d+\.\d+\.\d+$`), fmt.Sprintf("Plugin '%s' has a non-semver version", plugin.Name))
}
})
It("validates the platforms for every binary", func() {
for _, plugin := range plugins.Plugins {
for _, binary := range plugin.Binaries {
Expect(web.ValidPlatforms).To(
ContainElement(binary.Platform),
fmt.Sprintf(
"Plugin '%s' contains a platform '%s' that is invalid. Please use one of the following: '%s'",
plugin.Name,
binary.Platform,
strings.Join(web.ValidPlatforms, ", "),
))
}
}
})
It("requires HTTPS for all downloads", func() {
for _, plugin := range plugins.Plugins {
for _, binary := range plugin.Binaries {
Expect(binary.Url).To(
MatchRegexp("^https|ftps"),
fmt.Sprintf(
"Plugin '%s' links to a Binary's URL '%s' that cannot be downloaded over SSL (begins with https/ftps). Please provide a secure download link to your binaries. If you are unsure how to provide one, try out GitHub Releases: https://help.github.com/articles/creating-releases",
plugin.Name,
binary.Url,
))
}
}
})
It("every binary download had a matching sha1", func() {
if os.Getenv("BINARY_VALIDATION") != "true" {
Skip("Skipping SHA1 binary checking. To enable, set the BINARY_VALIDATION env variable to 'true'")
}
fmt.Println("\nRunning Binary Validations, this could take 10+ minutes")
for _, plugin := range plugins.Plugins {
for _, binary := range plugin.Binaries {
var err error
resp, err := http.Get(binary.Url)
Expect(err).NotTo(HaveOccurred())
// If there's a network error, retry exactly once for this plugin binary.
switch resp.StatusCode {
case http.StatusInternalServerError,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout:
Expect(resp.Body.Close()).To(Succeed())
resp, err = http.Get(binary.Url)
Expect(err).NotTo(HaveOccurred())
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred())
s := sha1.Sum(b)
Expect(hex.EncodeToString(s[:])).To(Equal(binary.Checksum), fmt.Sprintf("Plugin '%s' has an invalid checksum for platform '%s'\nResponse Status Code: %d\nResponse Body: %s", plugin.Name, binary.Platform, resp.StatusCode, string(b)))
}
}
})
})
})