forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_repo.go
151 lines (122 loc) · 3.87 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
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"
)
type Binary struct {
Checksum string `json:"checksum"`
Platform string `json:"platform"`
URL string `json:"url"`
}
type Plugin struct {
Name string `json:"name"`
Version string `json:"version"`
Binaries []Binary `json:"binaries"`
}
type PluginRepository struct {
Plugins []Plugin `json:"plugins"`
}
type PluginRepositoryServerWithPlugin struct {
server *Server
pluginPath string
}
func NewPluginRepositoryServer(pluginRepo PluginRepository) *Server {
return configurePluginRepositoryServer(NewTLSServer(), pluginRepo)
}
func NewPluginRepositoryServerWithPlugin(pluginName string, version string, platform string, shouldCalculateChecksum bool) *PluginRepositoryServerWithPlugin {
pluginRepoServer := PluginRepositoryServerWithPlugin{}
pluginRepoServer.Init(pluginName, version, platform, shouldCalculateChecksum)
return &pluginRepoServer
}
func (pluginRepoServer *PluginRepositoryServerWithPlugin) Init(pluginName string, version string, platform string, shouldCalculateChecksum bool) {
pluginPath := BuildConfigurablePlugin("configurable_plugin", 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),
),
)
}
func (pluginRepoServer *PluginRepositoryServerWithPlugin) PluginSize() int64 {
fileinfo, err := os.Stat(pluginRepoServer.pluginPath)
Expect(err).NotTo(HaveOccurred())
return fileinfo.Size()
}
func (pluginRepoServer *PluginRepositoryServerWithPlugin) URL() string {
return pluginRepoServer.server.URL()
}
func (pluginRepoServer *PluginRepositoryServerWithPlugin) Cleanup() {
pluginRepoServer.server.Close()
Expect(os.RemoveAll(filepath.Dir(pluginRepoServer.pluginPath))).NotTo(HaveOccurred())
}
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
}