/
plugin_repo.go
165 lines (136 loc) · 5 KB
/
plugin_repo.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package helpers
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"code.cloudfoundry.org/cli/util"
"code.cloudfoundry.org/cli/util/generic"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/ghttp"
)
// Binary represents the metadata need to retrieve one of the binaries that makes up a plugin.
type Binary struct {
Checksum string `json:"checksum"`
Platform string `json:"platform"`
URL string `json:"url"`
}
// Plugin represents a plugin object provided by a plugin repo.
type Plugin struct {
Name string `json:"name"`
Version string `json:"version"`
Binaries []Binary `json:"binaries"`
}
// PluginRepository represents a repo response object describing a list of plugins available.
type PluginRepository struct {
Plugins []Plugin `json:"plugins"`
}
// PluginRepositoryServerWithPlugin represents a server used to provide downloadable plugins.
type PluginRepositoryServerWithPlugin struct {
server *Server
pluginPath string
}
// NewPluginRepositoryServer is used to configure and start a new plugin repo server, exposing the plugins contained
// in the pluginRepo.
func NewPluginRepositoryServer(pluginRepo PluginRepository) *Server {
return configurePluginRepositoryServer(NewTLSServer(), pluginRepo)
}
// NewPluginRepositoryServerWithPlugin is used to create a server to provide a single configurable_plugin
// with user provided name, version, and platform.
func NewPluginRepositoryServerWithPlugin(pluginType string, pluginName string, version string, platform string, shouldCalculateChecksum bool) *PluginRepositoryServerWithPlugin {
pluginRepoServer := PluginRepositoryServerWithPlugin{}
pluginRepoServer.Init(pluginType, pluginName, version, platform, shouldCalculateChecksum)
return &pluginRepoServer
}
// Init initializes a server to provide a single configurable_plugin with user provided name, version, and platform.
func (pluginRepoServer *PluginRepositoryServerWithPlugin) Init(pluginType string, pluginName string, version string, platform string, shouldCalculateChecksum bool) {
pluginPath := BuildConfigurablePlugin(pluginType, pluginName, version,
[]PluginCommand{
{Name: "some-command", Help: "some-command-help"},
},
)
repoServer := NewServer()
pluginRepoServer.server = repoServer
pluginRepoServer.pluginPath = pluginPath
var (
checksum []byte
err error
)
if shouldCalculateChecksum {
checksum, err = util.NewSha1Checksum(pluginPath).ComputeFileSha1()
Expect(err).NotTo(HaveOccurred())
}
baseFile := fmt.Sprintf("/%s", generic.ExecutableFilename(filepath.Base(pluginPath)))
downloadURL := fmt.Sprintf("%s%s", repoServer.URL(), baseFile)
pluginRepo := PluginRepository{
Plugins: []Plugin{
{
Name: pluginName,
Version: version,
Binaries: []Binary{
{
Checksum: fmt.Sprintf("%x", checksum),
Platform: platform,
URL: downloadURL,
},
},
},
}}
// Suppresses ginkgo server logs
repoServer.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
jsonBytes, err := json.Marshal(pluginRepo)
Expect(err).ToNot(HaveOccurred())
pluginData, err := ioutil.ReadFile(pluginPath)
Expect(err).ToNot(HaveOccurred())
repoServer.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/list"),
RespondWith(http.StatusOK, jsonBytes),
),
CombineHandlers(
VerifyRequest(http.MethodGet, "/list"),
RespondWith(http.StatusOK, jsonBytes),
),
CombineHandlers(
VerifyRequest(http.MethodGet, baseFile),
RespondWith(http.StatusOK, pluginData),
),
)
}
// PluginSize is used to get the size in bytes of the single plugin provided by the pluginRepoServer
func (pluginRepoServer *PluginRepositoryServerWithPlugin) PluginSize() int64 {
fileinfo, err := os.Stat(pluginRepoServer.pluginPath)
Expect(err).NotTo(HaveOccurred())
return fileinfo.Size()
}
// URL is used to get the pluginRepo's server url
func (pluginRepoServer *PluginRepositoryServerWithPlugin) URL() string {
return pluginRepoServer.server.URL()
}
// Close is used to destroy the repo server and cleanup any files
func (pluginRepoServer *PluginRepositoryServerWithPlugin) Cleanup() {
pluginRepoServer.server.Close()
Expect(os.RemoveAll(filepath.Dir(pluginRepoServer.pluginPath))).NotTo(HaveOccurred())
}
// NewPluginRepositoryTLSServer is used to configure and start a new TLS plugin repo server, exposing the plugins contained
// in the pluginRepo.
func NewPluginRepositoryTLSServer(pluginRepo PluginRepository) *Server {
return configurePluginRepositoryServer(NewTLSServer(), pluginRepo)
}
func configurePluginRepositoryServer(server *Server, pluginRepo PluginRepository) *Server {
// Suppresses ginkgo server logs
server.HTTPTestServer.Config.ErrorLog = log.New(&bytes.Buffer{}, "", 0)
jsonBytes, err := json.Marshal(pluginRepo)
Expect(err).ToNot(HaveOccurred())
server.AppendHandlers(
RespondWith(http.StatusOK, string(jsonBytes)),
RespondWith(http.StatusOK, string(jsonBytes)),
RespondWith(http.StatusOK, string(jsonBytes)),
RespondWith(http.StatusOK, string(jsonBytes)),
)
return server
}