Skip to content

Commit

Permalink
test: improve the config change detection (#809)
Browse files Browse the repository at this point in the history
* test: improve the config change detection

Fix the flaky test TestFilterManagerEncode.
Signed-off-by: spacewander <[email protected]>

* optimize implementation

Signed-off-by: spacewander <[email protected]>

---------

Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored Nov 28, 2024
1 parent 345f1d9 commit 97a14ea
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
13 changes: 12 additions & 1 deletion api/plugins/tests/integration/controlplane/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"os"
"runtime"
Expand Down Expand Up @@ -77,6 +78,15 @@ func NewControlPlane() *ControlPlane {
return cp
}

func getRandomString(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
result := make([]byte, n)
for i := range result {
result[i] = letters[rand.Intn(len(letters))]
}
return string(result)
}

func isWsl() bool {
f, err := os.Open("/proc/version")
if err != nil {
Expand Down Expand Up @@ -179,6 +189,7 @@ func (cp *ControlPlane) UseGoPluginConfig(t *testing.T, config *filtermanager.Fi
Domains: []string{"*"},
Routes: []*route.Route{
{
Name: getRandomString(8),
Match: &route.RouteMatch{
PathSpecifier: &route.RouteMatch_Path{
Path: "/detect_if_the_rds_takes_effect",
Expand All @@ -195,7 +206,7 @@ func (cp *ControlPlane) UseGoPluginConfig(t *testing.T, config *filtermanager.Fi
"fm": {
Override: &golang.RouterPlugin_Config{
Config: proto.MessageToAny(
FilterManagerConfigToTypedStruct(NewPluginConfig(nil))),
FilterManagerConfigToTypedStruct(NewSinglePluginConfig("detector", nil))),
},
},
},
Expand Down
15 changes: 11 additions & 4 deletions api/plugins/tests/integration/dataplane/data_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type DataPlane struct {
opt *Option
done chan error

latestRouteVersion string

dataPlanePort string
adminAPIPort string
}
Expand Down Expand Up @@ -476,14 +478,19 @@ func (dp *DataPlane) Grpcurl(importPath, protoFile, fullMethodName, req string)
}

func (dp *DataPlane) Configured() bool {
// TODO: this is fine for the first init of the envoy configuration.
// But it may be misleading when updating the configuration.
// Would be better to switch to Envoy's /config_dump API.
resp, err := dp.Head("/detect_if_the_rds_takes_effect", nil)
if err != nil {
return false
}
return resp.StatusCode == 200
if resp.StatusCode != 200 {
return false
}
name := resp.Header.Get("route-version")
if name == dp.latestRouteVersion {
return false
}
dp.latestRouteVersion = name
return true
}

func (dp *DataPlane) FlushCoverage() error {
Expand Down
30 changes: 30 additions & 0 deletions api/plugins/tests/integration/dataplane/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package dataplane

import (
"net/http"
"runtime/coverage"

"mosn.io/htnn/api/pkg/filtermanager/api"
Expand Down Expand Up @@ -59,6 +60,35 @@ func (f *coverageFilter) DecodeHeaders(headers api.RequestHeaderMap, endStream b
return &api.LocalResponse{Code: 200}
}

type detectorPlugin struct {
plugins.PluginMethodDefaultImpl
basePlugin
}

func (p *detectorPlugin) Factory() api.FilterFactory {
return detectorFactory
}

func detectorFactory(c interface{}, callbacks api.FilterCallbackHandler) api.Filter {
return &detectorFilter{
callbacks: callbacks,
}
}

type detectorFilter struct {
api.PassThroughFilter

callbacks api.FilterCallbackHandler
}

func (f *detectorFilter) DecodeHeaders(headers api.RequestHeaderMap, endStream bool) api.ResultAction {
hdr := http.Header{}
name := f.callbacks.StreamInfo().GetRouteName()
hdr.Add("route-version", name)
return &api.LocalResponse{Code: 200, Header: hdr}
}

func init() {
plugins.RegisterPlugin("coverage", &coveragePlugin{})
plugins.RegisterPlugin("detector", &detectorPlugin{})
}

0 comments on commit 97a14ea

Please sign in to comment.