-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathfile_test.go
229 lines (193 loc) · 6.48 KB
/
file_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package file
import (
"context"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
"sigs.k8s.io/yaml"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/envoygateway/config"
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
"github.com/envoyproxy/gateway/internal/message"
)
const (
resourcesUpdateTimeout = 1 * time.Minute
resourcesUpdateTick = 1 * time.Second
)
var healthzServerPort = 8082
type resourcesParam struct {
GatewayClassName string
GatewayName string
GatewayListenerPort string
HTTPRouteName string
BackendName string
}
func newDefaultResourcesParam() *resourcesParam {
return &resourcesParam{
GatewayClassName: "eg",
GatewayName: "eg",
GatewayListenerPort: "8888",
HTTPRouteName: "backend",
BackendName: "backend",
}
}
func newFileProviderConfig(paths []string) (*config.Server, error) {
cfg, err := config.New()
if err != nil {
return nil, err
}
cfg.EnvoyGateway.Provider = &egv1a1.EnvoyGatewayProvider{
Type: egv1a1.ProviderTypeCustom,
Custom: &egv1a1.EnvoyGatewayCustomProvider{
Resource: egv1a1.EnvoyGatewayResourceProvider{
Type: egv1a1.ResourceProviderTypeFile,
File: &egv1a1.EnvoyGatewayFileResourceProvider{
Paths: paths,
},
},
HealthzServerPort: &healthzServerPort,
},
}
return cfg, nil
}
func TestFileProvider(t *testing.T) {
watchFileBase, _ := os.MkdirTemp(os.TempDir(), "test-files-*")
watchFilePath := filepath.Join(watchFileBase, "test.yaml")
watchDirPath, _ := os.MkdirTemp(os.TempDir(), "test-dir-*")
// Prepare the watched test file.
writeResourcesFile(t, "testdata/resources.tmpl", watchFilePath, newDefaultResourcesParam())
require.FileExists(t, watchFilePath)
require.DirExists(t, watchDirPath)
cfg, err := newFileProviderConfig([]string{watchFilePath, watchDirPath})
require.NoError(t, err)
pResources := new(message.ProviderResources)
fp, err := New(cfg, pResources)
require.NoError(t, err)
// Start file provider.
go func() {
if err := fp.Start(context.Background()); err != nil {
t.Errorf("failed to start file provider: %v", err)
}
}()
// Wait for file provider to be ready.
waitFileProviderReady(t)
require.Equal(t, "gateway.envoyproxy.io/gatewayclass-controller", fp.resourcesStore.name)
t.Run("initial resource load", func(t *testing.T) {
require.NotZero(t, pResources.GatewayAPIResources.Len())
resources := pResources.GetResourcesByGatewayClass("eg")
require.NotNil(t, resources)
want := &resource.Resources{}
mustUnmarshal(t, "testdata/resources.all.yaml", want)
opts := []cmp.Option{
cmpopts.IgnoreFields(resource.Resources{}, "serviceMap"),
cmpopts.EquateEmpty(),
}
require.Empty(t, cmp.Diff(want, resources, opts...))
})
t.Run("rename the watched file then rename it back", func(t *testing.T) {
// Rename it
renameFilePath := filepath.Join(watchFileBase, "foobar.yaml")
err := os.Rename(watchFilePath, renameFilePath)
require.NoError(t, err)
require.Eventually(t, func() bool {
return pResources.GetResourcesByGatewayClass("eg") == nil
}, resourcesUpdateTimeout, resourcesUpdateTick)
// Rename it back
err = os.Rename(renameFilePath, watchFilePath)
require.NoError(t, err)
require.Eventually(t, func() bool {
return pResources.GetResourcesByGatewayClass("eg") != nil
}, resourcesUpdateTimeout, resourcesUpdateTick)
resources := pResources.GetResourcesByGatewayClass("eg")
want := &resource.Resources{}
mustUnmarshal(t, "testdata/resources.all.yaml", want)
opts := []cmp.Option{
cmpopts.IgnoreFields(resource.Resources{}, "serviceMap"),
cmpopts.EquateEmpty(),
}
require.Empty(t, cmp.Diff(want, resources, opts...))
})
t.Run("remove the watched file", func(t *testing.T) {
err := os.Remove(watchFilePath)
require.NoError(t, err)
require.Eventually(t, func() bool {
return pResources.GetResourcesByGatewayClass("eg") == nil
}, resourcesUpdateTimeout, resourcesUpdateTick)
})
t.Run("add a file in watched dir", func(t *testing.T) {
// Write a new file under watched directory.
newFilePath := filepath.Join(watchDirPath, "test.yaml")
writeResourcesFile(t, "testdata/resources.tmpl", newFilePath, newDefaultResourcesParam())
require.Eventually(t, func() bool {
return pResources.GetResourcesByGatewayClass("eg") != nil
}, resourcesUpdateTimeout, resourcesUpdateTick)
resources := pResources.GetResourcesByGatewayClass("eg")
want := &resource.Resources{}
mustUnmarshal(t, "testdata/resources.all.yaml", want)
opts := []cmp.Option{
cmpopts.IgnoreFields(resource.Resources{}, "serviceMap"),
cmpopts.EquateEmpty(),
}
require.Empty(t, cmp.Diff(want, resources, opts...))
})
t.Run("remove a file in watched dir", func(t *testing.T) {
newFilePath := filepath.Join(watchDirPath, "test.yaml")
err := os.Remove(newFilePath)
require.NoError(t, err)
require.Eventually(t, func() bool {
return pResources.GetResourcesByGatewayClass("eg") == nil
}, resourcesUpdateTimeout, resourcesUpdateTick)
})
t.Cleanup(func() {
_ = os.RemoveAll(watchFileBase)
_ = os.RemoveAll(watchDirPath)
})
}
func writeResourcesFile(t *testing.T, tmpl, dst string, params *resourcesParam) {
dstFile, err := os.Create(dst)
require.NoError(t, err)
// Write parameters into target file.
tmplFile, err := template.ParseFiles(tmpl)
require.NoError(t, err)
err = tmplFile.Execute(dstFile, params)
require.NoError(t, err)
require.NoError(t, dstFile.Close())
}
func waitFileProviderReady(t *testing.T) {
require.Eventually(t, func() bool {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/readyz", healthzServerPort))
if err != nil {
t.Logf("failed to get from heathlz server")
return false
}
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
t.Logf("failed to get body from response")
return false
}
if string(body) != "ok" {
t.Logf("the file provider is not ready yet")
return false
}
return true
}, 3*resourcesUpdateTimeout, resourcesUpdateTick)
}
func mustUnmarshal(t *testing.T, path string, out interface{}) {
t.Helper()
content, err := os.ReadFile(path)
require.NoError(t, err)
require.NoError(t, yaml.UnmarshalStrict(content, out, yaml.DisallowUnknownFields))
}