forked from cloudfoundry/cli-plugin-repo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo_index_test.go
112 lines (91 loc) · 3.05 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
package main_test
import (
"encoding/hex"
"io/ioutil"
"os"
"strings"
"github.com/cloudfoundry-incubator/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
BeforeEach(func() {
b, err := ioutil.ReadFile("repo-index.yml")
Expect(err).NotTo(HaveOccurred())
err = yaml.Unmarshal(b, &plugins)
Expect(err).NotTo(HaveOccurred())
})
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 {
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'", plugin.Name, binary.Platform))
}
}
})
})
})