-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx_test.go
98 lines (83 loc) · 2.1 KB
/
nginx_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
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
package nginx_test
import (
"context"
"os/exec"
"regexp"
"sync"
"testing"
"time"
// Packages
nginx "github.com/mutablelogic/go-server/pkg/handler/nginx"
cmd "github.com/mutablelogic/go-server/pkg/handler/nginx/cmd"
assert "github.com/stretchr/testify/assert"
)
func Test_nginx_001(t *testing.T) {
assert := assert.New(t)
config := nginx.Config{}
assert.NotEmpty(config.Name())
assert.NotEmpty(config.Description())
}
func Test_nginx_002(t *testing.T) {
assert := assert.New(t)
task, err := nginx.New(&nginx.Config{
BinaryPath: BinaryExec(t),
})
assert.NoError(err)
t.Log(task.Version())
}
func Test_nginx_003(t *testing.T) {
var wg sync.WaitGroup
// Create a new task
assert := assert.New(t)
task, err := nginx.New(&nginx.Config{
BinaryPath: BinaryExec(t),
})
assert.NoError(err)
// Create a cancellable context
ctx, cancel := context.WithCancel(context.Background())
// Cancel and then wait for the task to finish
defer wg.Wait()
defer cancel()
wg.Add(1)
go func() {
defer wg.Done()
if err := task.Run(ctx); err != nil {
t.Error(err)
}
}()
// Sleep until the task is running
time.Sleep(500 * time.Millisecond)
// Test server configuration
err = task.Test()
assert.NoError(err)
}
func BinaryExec(t *testing.T) string {
var version string
bin, err := exec.LookPath("nginx")
if err != nil {
t.Skip("Skipping test, nginx binary not found")
return ""
}
// TODO minimum version is 1.19.15
reVersion := regexp.MustCompile(`nginx/(\d+)\.(\d+)\.(\d+)`)
cmd, err := cmd.New(bin, "-v")
cmd.Err = func(data []byte) {
version += string(data)
}
cmd.Out = func(data []byte) {
version += string(data)
}
if err != nil {
t.Skip(err.Error())
} else if err := cmd.Run(); err != nil {
t.Skip(err.Error())
} else if args := reVersion.FindStringSubmatch(version); args == nil {
t.Skip("Missing version: " + version)
} else if v := args[1] + args[2]; v < "119" {
t.Skip("Invalid version (needs to be at least 1.19.X): " + version)
} else if v == "119" && args[3] < "15" {
t.Skip("Invalid version (needs to be >= 1.19.15): " + version)
}
// Return binary path
return bin
}