15
15
package lifecycle
16
16
17
17
import (
18
+ "context"
19
+ "net/http"
20
+ "net/http/httptest"
18
21
"strconv"
19
22
"testing"
20
23
"time"
21
24
22
25
"github.com/stretchr/testify/assert"
23
26
"github.com/stretchr/testify/require"
27
+ "go.uber.org/zap/zaptest"
24
28
)
25
29
26
30
func TestGracefulStopCommand (t * testing.T ) {
@@ -40,7 +44,7 @@ func TestGracefulStopCommand(t *testing.T) {
40
44
41
45
for _ , tc := range testcases {
42
46
t .Run (tc .name , func (t * testing.T ) {
43
- cmd , err := RunBinary ("sh" , []string {"sleep" , "1m" })
47
+ cmd , err := RunBinary (context . TODO (), "sh" , []string {"sleep" , "1m" })
44
48
require .NoError (t , err )
45
49
require .NotNil (t , cmd )
46
50
@@ -71,7 +75,7 @@ func TestGracefulStopCommandResult(t *testing.T) {
71
75
72
76
for _ , tc := range testcases {
73
77
t .Run (tc .name , func (t * testing.T ) {
74
- cmd , err := RunBinary ("sh" , []string {"-c" , "exit " + strconv .Itoa (tc .exitCode )})
78
+ cmd , err := RunBinary (context . TODO (), "sh" , []string {"-c" , "exit " + strconv .Itoa (tc .exitCode )})
75
79
require .NoError (t , err )
76
80
require .NotNil (t , cmd )
77
81
@@ -81,3 +85,42 @@ func TestGracefulStopCommandResult(t *testing.T) {
81
85
})
82
86
}
83
87
}
88
+
89
+ func TestDownloadBinary (t * testing.T ) {
90
+ server := httpTestServer ()
91
+ defer server .Close ()
92
+
93
+ logger := zaptest .NewLogger (t )
94
+ destDir := t .TempDir ()
95
+ destFile := "test-binary"
96
+
97
+ t .Run ("successful download" , func (t * testing.T ) {
98
+ url := server .URL + "/binary"
99
+ path , err := DownloadBinary (url , destDir , destFile , logger )
100
+ require .NoError (t , err )
101
+ assert .FileExists (t , path )
102
+ })
103
+
104
+ t .Run ("file already exists" , func (t * testing.T ) {
105
+ url := server .URL + "/binary"
106
+ path , err := DownloadBinary (url , destDir , destFile , logger )
107
+ require .NoError (t , err )
108
+ assert .FileExists (t , path )
109
+
110
+ // Try downloading again, should not error and file should still exist
111
+ path , err = DownloadBinary (url , destDir , destFile , logger )
112
+ require .NoError (t , err )
113
+ assert .FileExists (t , path )
114
+ })
115
+ }
116
+
117
+ func httpTestServer () * httptest.Server {
118
+ return httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
119
+ if r .URL .Path == "/binary" {
120
+ w .WriteHeader (http .StatusOK )
121
+ w .Write ([]byte ("test binary content" ))
122
+ } else {
123
+ w .WriteHeader (http .StatusNotFound )
124
+ }
125
+ }))
126
+ }
0 commit comments