9
9
"fmt"
10
10
"math/rand"
11
11
"net/http"
12
+ "net/http/httptest"
13
+ "net/url"
12
14
"os"
13
15
"testing"
14
16
"time"
@@ -19,42 +21,76 @@ import (
19
21
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact"
20
22
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
21
23
"github.com/elastic/elastic-agent/pkg/core/logger"
24
+ "github.com/elastic/elastic-agent/testing/proxytest"
22
25
)
23
26
24
27
func TestVerify (t * testing.T ) {
25
28
targetDir := t .TempDir ()
26
29
27
30
log , _ := logger .New ("" , false )
28
31
timeout := 30 * time .Second
29
- testCases := getRandomTestCases ()
32
+ testCases := getRandomTestCases ()[ 0 : 1 ]
30
33
server , pub := getElasticCoServer (t )
31
- elasticClient := getElasticCoClient (server )
32
- // artifact/download/http.Verifier uses http.DefaultClient, thus we need to
33
- // change it.
34
- http .DefaultClient = & elasticClient
35
34
36
35
config := & artifact.Config {
37
- SourceURI : source ,
36
+ SourceURI : server . URL + "/downloads" ,
38
37
TargetDirectory : targetDir ,
39
38
HTTPTransportSettings : httpcommon.HTTPTransportSettings {
40
39
Timeout : timeout ,
41
40
},
42
41
}
43
42
44
- for _ , testCase := range testCases {
45
- testName := fmt .Sprintf ("%s-binary-%s" , testCase .system , testCase .arch )
43
+ t .Run ("without proxy" , func (t * testing.T ) {
44
+ runTests (t , testCases , config , log , pub )
45
+ })
46
+
47
+ t .Run ("with proxy" , func (t * testing.T ) {
48
+ brokenServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
49
+ w .WriteHeader (http .StatusTeapot )
50
+ t .Log ("[brokenServer] wrong server, is the proxy working?" )
51
+ _ , _ = w .Write ([]byte (`wrong server, is the proxy working?` ))
52
+ }))
53
+ serverURL , err := url .Parse (server .URL )
54
+ require .NoError (t , err , "could not parse server URL \" %s\" " ,
55
+ server .URL )
56
+
57
+ proxy := proxytest .New (t ,
58
+ proxytest .WithRewriteFn (func (u * url.URL ) {
59
+ u .Host = serverURL .Host
60
+ }),
61
+ proxytest .WithRequestLog ("proxy" , func (_ string , _ ... any ) {}))
62
+
63
+ proxyURL , err := url .Parse (proxy .LocalhostURL )
64
+ require .NoError (t , err , "could not parse server URL \" %s\" " ,
65
+ server .URL )
66
+
67
+ config := * config
68
+ config .SourceURI = brokenServer .URL + "/downloads"
69
+ config .Proxy = httpcommon.HTTPClientProxySettings {
70
+ URL : (* httpcommon .ProxyURI )(proxyURL ),
71
+ }
72
+
73
+ runTests (t , testCases , & config , log , pub )
74
+ })
75
+ }
76
+
77
+ func runTests (t * testing.T , testCases []testCase , config * artifact.Config , log * logger.Logger , pub []byte ) {
78
+ for _ , tc := range testCases {
79
+ testName := fmt .Sprintf ("%s-binary-%s" , tc .system , tc .arch )
46
80
t .Run (testName , func (t * testing.T ) {
47
- config .OperatingSystem = testCase .system
48
- config .Architecture = testCase .arch
81
+ config .OperatingSystem = tc .system
82
+ config .Architecture = tc .arch
49
83
50
- upgradeDetails := details .NewDetails ("8.12.0" , details .StateRequested , "" )
51
- testClient := NewDownloaderWithClient (log , config , elasticClient , upgradeDetails )
52
- artifact , err := testClient .Download (context .Background (), beatSpec , version )
53
- if err != nil {
54
- t .Fatal (err )
55
- }
84
+ upgradeDetails := details .NewDetails (
85
+ "8.12.0" , details .StateRequested , "" )
86
+ downloader , err := NewDownloader (log , config , upgradeDetails )
87
+ require .NoError (t , err , "could not create new downloader" )
88
+
89
+ pkgPath , err := downloader .Download (context .Background (), beatSpec , version )
90
+ require .NoErrorf (t , err , "failed downloading %s v%s" ,
91
+ beatSpec .Artifact , version )
56
92
57
- _ , err = os .Stat (artifact )
93
+ _ , err = os .Stat (pkgPath )
58
94
if err != nil {
59
95
t .Fatal (err )
60
96
}
0 commit comments