Skip to content

Commit ac09dcd

Browse files
committed
Tidy tests
1 parent 9f5f71e commit ac09dcd

File tree

4 files changed

+17
-32
lines changed

4 files changed

+17
-32
lines changed

internal/mode/static/nginx/config/version_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestExecuteVersion(t *testing.T) {
11-
g := NewGomegaWithT(t)
11+
g := NewWithT(t)
1212
expSubStrings := map[string]int{
1313
"return 200 42;": 1,
1414
}

internal/mode/static/nginx/runtime/manager_test.go

+14-30
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"io/fs"
7-
"os"
87
"testing"
98
"time"
109

@@ -100,7 +99,7 @@ func TestFindMainProcess(t *testing.T) {
10099

101100
for _, test := range tests {
102101
t.Run(test.name, func(t *testing.T) {
103-
g := NewGomegaWithT(t)
102+
g := NewWithT(t)
104103

105104
result, err := findMainProcess(test.ctx, test.checkFile, test.readFile, 2*time.Millisecond)
106105

@@ -115,30 +114,20 @@ func TestFindMainProcess(t *testing.T) {
115114
}
116115

117116
func TestEnsureNewNginxWorkers(t *testing.T) {
117+
previousContents := []byte("1 2 3")
118+
newContents := []byte("4 5 6")
119+
118120
readFileError := func(string) ([]byte, error) {
119121
return nil, errors.New("error")
120122
}
121123

122-
tempFileFunc := func(contents []byte) *os.File {
123-
tempFile, err := os.CreateTemp("", "tmpfile-")
124-
if err != nil {
125-
return nil
126-
}
127-
if _, err = tempFile.Write(contents); err != nil {
128-
return nil
129-
}
130-
return tempFile
124+
readFilePrevious := func(string) ([]byte, error) {
125+
return previousContents, nil
131126
}
132127

133-
previousContents := []byte("1 2 3")
134-
135-
childFileSame := tempFileFunc(previousContents)
136-
childFileDifferent := tempFileFunc([]byte("4 5 6"))
137-
138-
defer childFileSame.Close()
139-
defer os.Remove(childFileSame.Name())
140-
defer childFileDifferent.Close()
141-
defer os.Remove(childFileDifferent.Name())
128+
readFileNew := func(string) ([]byte, error) {
129+
return newContents, nil
130+
}
142131

143132
ctx := context.Background()
144133
cancellingCtx, cancel := context.WithCancel(ctx)
@@ -147,39 +136,34 @@ func TestEnsureNewNginxWorkers(t *testing.T) {
147136
tests := []struct {
148137
ctx context.Context
149138
readFile readFileFunc
150-
childFile string
151139
name string
152140
previousContents []byte
153141
expectError bool
154142
}{
155143
{
156144
ctx: ctx,
157-
readFile: os.ReadFile,
158-
childFile: childFileDifferent.Name(),
145+
readFile: readFileNew,
159146
previousContents: previousContents,
160147
expectError: false,
161148
name: "normal case",
162149
},
163150
{
164151
ctx: ctx,
165152
readFile: readFileError,
166-
childFile: childFileDifferent.Name(),
167153
previousContents: previousContents,
168154
expectError: true,
169155
name: "cannot read file",
170156
},
171157
{
172158
ctx: ctx,
173-
readFile: os.ReadFile,
174-
childFile: childFileSame.Name(),
159+
readFile: readFilePrevious,
175160
previousContents: previousContents,
176161
expectError: true,
177162
name: "no new workers",
178163
},
179164
{
180165
ctx: cancellingCtx,
181-
readFile: os.ReadFile,
182-
childFile: childFileSame.Name(),
166+
readFile: readFilePrevious,
183167
previousContents: previousContents,
184168
expectError: true,
185169
name: "context canceled",
@@ -188,11 +172,11 @@ func TestEnsureNewNginxWorkers(t *testing.T) {
188172

189173
for _, test := range tests {
190174
t.Run(test.name, func(t *testing.T) {
191-
g := NewGomegaWithT(t)
175+
g := NewWithT(t)
192176

193177
err := ensureNewNginxWorkers(
194178
test.ctx,
195-
test.childFile,
179+
"/childfile",
196180
test.previousContents,
197181
test.readFile,
198182
2*time.Millisecond,

internal/mode/static/nginx/runtime/verify.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func newVerifyClient(timeout time.Duration) *verifyClient {
4040
func (c *verifyClient) getConfigVersion() (int, error) {
4141
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
4242
defer cancel()
43+
4344
req, err := http.NewRequestWithContext(ctx, "GET", "http://config-version/version", nil)
4445
if err != nil {
4546
return 0, fmt.Errorf("error creating request: %w", err)

internal/mode/static/nginx/runtime/verify_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestVerifyClient(t *testing.T) {
6666

6767
for _, test := range tests {
6868
t.Run(test.name, func(t *testing.T) {
69-
g := NewGomegaWithT(t)
69+
g := NewWithT(t)
7070

7171
err := c.waitForCorrectVersion(test.ctx, test.expectedVersion)
7272

0 commit comments

Comments
 (0)