-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfupdate_test.go
62 lines (53 loc) · 1.4 KB
/
selfupdate_test.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
package selfupdate
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestUpdateRestart(t *testing.T) {
if os.Getenv(restartEnvVar) != "" {
fmt.Println("We are in the restarted process! (TestUpdateRestart")
} else {
fmt.Println("Running UpdateRestart func...")
}
// run test http server, which returns current executable
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("http test server got request!")
self, err := os.Executable()
if err != nil {
panic(err)
}
src, err := os.Open(self)
if err != nil {
panic(err)
}
defer src.Close()
// send self
w.Header().Set("Content-Type", "application/octet-stream")
io.Copy(w, src)
}))
defer ts.Close()
// Set to past, or we would skip updating because we are too new
pastTS := time.Now().Add(-10 * time.Second)
selfExe, _ := executable()
assert.NoError(t, os.Chtimes(selfExe, pastTS, pastTS))
ok, err := UpdateRestart(ts.URL)
assert.NoError(t, err) // doesn't return the first call, but does the second
assert.True(t, ok) // in new process
}
func TestAge(t *testing.T) {
path, err := executable()
assert.NoError(t, err)
myAge := age(path)
if myAge < 0 {
t.Errorf("age() returned negative value: %s", myAge)
}
if myAge > 15*time.Second {
t.Errorf("age() returned too big value: %s", myAge)
}
}