@@ -12,6 +12,7 @@ import (
12
12
"net"
13
13
"net/http"
14
14
"net/http/httptest"
15
+ "os"
15
16
"path/filepath"
16
17
"testing"
17
18
@@ -37,91 +38,30 @@ func TestNonDefaultSourceURI(t *testing.T) {
37
38
38
39
}
39
40
40
- const artifactAPIElasticAgentSearchResponse = `
41
- {
42
- "packages": {
43
- "elastic-agent-1.2.3-SNAPSHOT-darwin-aarch64.tar.gz": {
44
- "url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-darwin-aarch64.tar.gz",
45
- "sha_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-darwin-aarch64.tar.gz.sha512",
46
- "asc_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-darwin-aarch64.tar.gz.asc",
47
- "type": "tar",
48
- "architecture": "aarch64",
49
- "os": [
50
- "darwin"
51
- ]
52
- },
53
- "elastic-agent-1.2.3-SNAPSHOT-windows-x86_64.zip": {
54
- "url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-windows-x86_64.zip",
55
- "sha_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-windows-x86_64.zip.sha512",
56
- "asc_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-windows-x86_64.zip.asc",
57
- "type": "zip",
58
- "architecture": "x86_64",
59
- "os": [
60
- "windows"
61
- ]
62
- },
63
- "elastic-agent-core-1.2.3-SNAPSHOT-linux-arm64.tar.gz": {
64
- "url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/elastic-agent-core/elastic-agent-core-1.2.3-SNAPSHOT-linux-arm64.tar.gz",
65
- "sha_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/elastic-agent-core/elastic-agent-core-1.2.3-SNAPSHOT-linux-arm64.tar.gz.sha512",
66
- "asc_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/elastic-agent-core/elastic-agent-core-1.2.3-SNAPSHOT-linux-arm64.tar.gz.asc",
67
- "type": "tar",
68
- "architecture": "arm64",
69
- "os": [
70
- "linux"
71
- ]
72
- },
73
- "elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz": {
74
- "url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz",
75
- "sha_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz.sha512",
76
- "asc_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz.asc",
77
- "type": "tar",
78
- "architecture": "x86_64",
79
- "os": [
80
- "linux"
81
- ]
82
- },
83
- "elastic-agent-1.2.3-SNAPSHOT-linux-arm64.tar.gz": {
84
- "url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-arm64.tar.gz",
85
- "sha_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-arm64.tar.gz.sha512",
86
- "asc_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-arm64.tar.gz.asc",
87
- "type": "tar",
88
- "architecture": "arm64",
89
- "os": [
90
- "linux"
91
- ]
92
- },
93
- "elastic-agent-1.2.3-SNAPSHOT-darwin-x86_64.tar.gz": {
94
- "url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-darwin-x86_64.tar.gz",
95
- "sha_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-darwin-x86_64.tar.gz.sha512",
96
- "asc_url": "https://snapshots.elastic.co/1.2.3-33e8d7e1/downloads/beats/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-darwin-x86_64.tar.gz.asc",
97
- "type": "tar",
98
- "architecture": "x86_64",
99
- "os": [
100
- "darwin"
101
- ]
102
- }
103
- },
104
- "manifests": {
105
- "last-update-time": "Tue, 05 Dec 2023 15:47:06 UTC",
106
- "seconds-since-last-update": 201
107
- }
108
- }
109
- `
110
-
111
41
var agentSpec = artifact.Artifact {
112
42
Name : "Elastic Agent" ,
113
43
Cmd : "elastic-agent" ,
114
44
Artifact : "beat/elastic-agent" ,
115
45
}
116
46
117
- type downloadHttpResponse struct {
118
- statusCode int
119
- headers http.Header
120
- Body []byte
47
+ func readFile (t * testing.T , name string ) []byte {
48
+ bytes , err := os .ReadFile (name )
49
+ require .NoError (t , err )
50
+
51
+ return bytes
121
52
}
122
53
123
54
func TestDownloadVersion (t * testing.T ) {
124
-
55
+ files := map [string ][]byte {
56
+ // links for the latest snapshot
57
+ "/latest/8.14.0-SNAPSHOT.json" : readFile (t , "./testdata/latest-snapshot.json" ),
58
+ "/8.14.0-6d69ee76/downloads/beat/elastic-agent/elastic-agent-8.14.0-SNAPSHOT-linux-x86_64.tar.gz" : {},
59
+ "/8.14.0-6d69ee76/downloads/beat/elastic-agent/elastic-agent-8.14.0-SNAPSHOT-linux-x86_64.tar.gz.sha512" : {},
60
+
61
+ // links for a specific build
62
+ "/8.13.3-76ce1a63/downloads/beat/elastic-agent/elastic-agent-8.13.3-SNAPSHOT-linux-x86_64.tar.gz" : {},
63
+ "/8.13.3-76ce1a63/downloads/beat/elastic-agent/elastic-agent-8.13.3-SNAPSHOT-linux-x86_64.tar.gz.sha512" : {},
64
+ }
125
65
type fields struct {
126
66
config * artifact.Config
127
67
}
@@ -131,59 +71,33 @@ func TestDownloadVersion(t *testing.T) {
131
71
}
132
72
tests := []struct {
133
73
name string
134
- files map [string ]downloadHttpResponse
135
74
fields fields
136
75
args args
137
76
want string
138
77
wantErr assert.ErrorAssertionFunc
139
78
}{
140
79
{
141
80
name : "happy path snapshot version" ,
142
- files : map [string ]downloadHttpResponse {
143
- "/1.2.3-33e8d7e1/downloads/beat/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz" : {
144
- statusCode : http .StatusOK ,
145
- Body : []byte ("This is a fake linux elastic agent archive" ),
146
- },
147
- "/1.2.3-33e8d7e1/downloads/beat/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz.sha512" : {
148
- statusCode : http .StatusOK ,
149
- Body : []byte ("somesha512 elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz" ),
150
- },
151
- "/v1/search/1.2.3-SNAPSHOT/elastic-agent" : {
152
- statusCode : http .StatusOK ,
153
- headers : map [string ][]string {"Content-Type" : {"application/json" }},
154
- Body : []byte (artifactAPIElasticAgentSearchResponse ),
155
- },
156
- },
157
81
fields : fields {
158
82
config : & artifact.Config {
159
83
OperatingSystem : "linux" ,
160
84
Architecture : "64" ,
161
85
},
162
86
},
163
- args : args {a : agentSpec , version : agtversion .NewParsedSemVer (1 , 2 , 3 , "SNAPSHOT" , "" )},
164
- want : "elastic-agent-1.2.3 -SNAPSHOT-linux-x86_64.tar.gz" ,
87
+ args : args {a : agentSpec , version : agtversion .NewParsedSemVer (8 , 14 , 0 , "SNAPSHOT" , "" )},
88
+ want : "elastic-agent-8.14.0 -SNAPSHOT-linux-x86_64.tar.gz" ,
165
89
wantErr : assert .NoError ,
166
90
},
167
91
{
168
92
name : "happy path snapshot version with build metadata" ,
169
- files : map [string ]downloadHttpResponse {
170
- "/1.2.3-buildid/downloads/beat/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz" : {
171
- statusCode : http .StatusOK ,
172
- Body : []byte ("This is a fake linux elastic agent archive" ),
173
- },
174
- "/1.2.3-buildid/downloads/beat/elastic-agent/elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz.sha512" : {
175
- statusCode : http .StatusOK ,
176
- Body : []byte ("somesha512 elastic-agent-1.2.3-SNAPSHOT-linux-x86_64.tar.gz" ),
177
- },
178
- },
179
93
fields : fields {
180
94
config : & artifact.Config {
181
95
OperatingSystem : "linux" ,
182
96
Architecture : "64" ,
183
97
},
184
98
},
185
- args : args {a : agentSpec , version : agtversion .NewParsedSemVer (1 , 2 , 3 , "SNAPSHOT" , "buildid " )},
186
- want : "elastic-agent-1.2 .3-SNAPSHOT-linux-x86_64.tar.gz" ,
99
+ args : args {a : agentSpec , version : agtversion .NewParsedSemVer (8 , 13 , 3 , "SNAPSHOT" , "76ce1a63 " )},
100
+ want : "elastic-agent-8.13 .3-SNAPSHOT-linux-x86_64.tar.gz" ,
187
101
wantErr : assert .NoError ,
188
102
},
189
103
}
@@ -195,21 +109,15 @@ func TestDownloadVersion(t *testing.T) {
195
109
196
110
handleDownload := func (rw http.ResponseWriter , req * http.Request ) {
197
111
path := req .URL .Path
112
+ t .Logf ("incoming request for %s" , path )
198
113
199
- resp , ok := tt . files [path ]
114
+ file , ok := files [path ]
200
115
if ! ok {
201
116
rw .WriteHeader (http .StatusNotFound )
202
117
return
203
118
}
204
119
205
- for k , values := range resp .headers {
206
- for _ , v := range values {
207
- rw .Header ().Set (k , v )
208
- }
209
- }
210
-
211
- rw .WriteHeader (resp .statusCode )
212
- _ , err := io .Copy (rw , bytes .NewReader (resp .Body ))
120
+ _ , err := io .Copy (rw , bytes .NewReader (file ))
213
121
assert .NoError (t , err , "error writing out response body" )
214
122
}
215
123
server := httptest .NewTLSServer (http .HandlerFunc (handleDownload ))
0 commit comments