-
Notifications
You must be signed in to change notification settings - Fork 16
/
main_test.go
160 lines (150 loc) · 3.74 KB
/
main_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"context"
"flag"
"fmt"
"github.com/matryer/is"
"io/ioutil"
"os"
"testing"
"time"
)
func TestCheckVersion(t *testing.T) {
tt := []struct {
name string
majorVer int
minorVer int
err error
}{
{
name: "lower major version",
majorVer: 7,
minorVer: 2,
err: fmt.Errorf("Install libips=>'8.9'. Current version is 7.2"),
},
{
name: "lower minor version",
majorVer: 8,
minorVer: 5,
err: fmt.Errorf("Install libips=>'8.9'. Current version is 8.5"),
},
{
name: "equal version",
majorVer: 8,
minorVer: 9,
err: nil,
},
{
name: "higher version",
majorVer: 9,
minorVer: 2,
err: nil,
},
}
for _, tc := range tt {
t.Run(fmt.Sprintf("TestCheckVersionFunction: %s", tc.name), func(t *testing.T) {
is := is.NewRelaxed(t)
err := checkVipsVersion(tc.majorVer, tc.minorVer)
is.Equal(err, tc.err)
})
}
}
func TestRunServerInvalidConfigFlag(t *testing.T) {
is := is.New(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
time.AfterFunc(50*time.Millisecond, func() {
cancel()
})
err := runServer(ctx)
is.Equal(err, fmt.Errorf("Set config.yml path via -config flag."))
flag.CommandLine = flag.NewFlagSet("config", flag.ExitOnError)
configPath := "/tmp/webpserver_test.yaml"
os.Remove(configPath)
os.Args = []string{"webp-server", "-config", configPath}
err = runServer(ctx)
is.Equal(err, fmt.Errorf("Error loading config: open /tmp/webpserver_test.yaml: no such file or directory"))
}
func TestRunServerInvalidLogPath(t *testing.T) {
is := is.New(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
time.AfterFunc(50*time.Millisecond, func() {
cancel()
})
configData := []byte(`
data_directory:
/tmp/wstest/
log_path:
/tmp/wstest/sfsaf/fsfs
`)
configPath := "/tmp/webpserver_test.yaml"
err := ioutil.WriteFile(configPath, configData, 0644)
if err != nil {
t.Fatal(err)
}
defer os.Remove(configPath)
defer os.RemoveAll("/tmp/wstest/")
flag.CommandLine = flag.NewFlagSet("config", flag.ExitOnError)
os.Args = []string{"webp-server", "-config", configPath}
err = runServer(ctx)
is.Equal(err.Error(), "Could not open log file: open /tmp/wstest/sfsaf/fsfs: no such file or directory")
}
func TestRunServerGracefulShutdown(t *testing.T) {
is := is.New(t)
ctx := context.Background()
configData := []byte(`
data_directory:
/tmp/wstest/
log_path:
/tmp/wstest/wpl.log
`)
configPath := "/tmp/webpserver_test.yaml"
err := ioutil.WriteFile(configPath, configData, 0644)
if err != nil {
t.Fatal(err)
}
defer os.Remove(configPath)
defer os.RemoveAll("/tmp/wstest/")
flag.CommandLine = flag.NewFlagSet("config", flag.ExitOnError)
os.Args = []string{"webp-server", "-config", configPath}
err = ioutil.WriteFile(configPath, configData, 0644)
if err != nil {
t.Fatal(err)
}
proc, _ := os.FindProcess(os.Getpid())
time.AfterFunc(200*time.Millisecond, func() {
err := proc.Signal(os.Interrupt)
if err != nil {
t.Fatal(err)
}
})
err = runServer(ctx)
is.NoErr(err)
}
func TestRunServerInvalidAddress(t *testing.T) {
is := is.New(t)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
time.AfterFunc(50*time.Millisecond, func() {
cancel()
})
configData := []byte(`
server_address: nfsfs
data_directory:
/tmp/wstest/
log_path:
/tmp/wstest/wpl.log
`)
configPath := "/tmp/webpserver_test.yaml"
err := ioutil.WriteFile(configPath, configData, 0644)
defer os.RemoveAll("/tmp/wstest")
defer os.Remove(configPath)
if err != nil {
t.Fatal(err)
}
flag.CommandLine = flag.NewFlagSet("config", flag.ExitOnError)
os.Args = []string{"webp-server", "-config", configPath}
err = runServer(ctx)
is.Equal(err.Error(), "listen tcp4: address nfsfs: missing port in address")
}