mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-31 18:07:17 +02:00
New integration test fixtures (#5233)
* Initial test environment implementation * linter pass * wip: update request latency test * bugfixes * Fix logic race in envoy process monitor when canceling context * skip tests using test environment on non-linux
This commit is contained in:
parent
3d958ff9c5
commit
526e2a58d6
29 changed files with 2972 additions and 101 deletions
64
internal/testenv/snippets/routes.go
Normal file
64
internal/testenv/snippets/routes.go
Normal file
|
@ -0,0 +1,64 @@
|
|||
package snippets
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/pomerium/pomerium/config"
|
||||
"github.com/pomerium/pomerium/internal/testenv"
|
||||
"github.com/pomerium/pomerium/pkg/policy/parser"
|
||||
)
|
||||
|
||||
var SimplePolicyTemplate = PolicyTemplate{
|
||||
From: "https://from-{{.Idx}}.localhost",
|
||||
To: "https://to-{{.Idx}}.localhost",
|
||||
PPL: `{"allow":{"and":["email":{"is":"user-{{.Idx}}@example.com"}]}}`,
|
||||
}
|
||||
|
||||
type PolicyTemplate struct {
|
||||
From string
|
||||
To string
|
||||
PPL string
|
||||
|
||||
// Add more fields as needed (be sure to update newPolicyFromTemplate)
|
||||
}
|
||||
|
||||
func TemplateRoutes(n int, tmpl PolicyTemplate) testenv.Modifier {
|
||||
return testenv.ModifierFunc(func(_ context.Context, cfg *config.Config) {
|
||||
for i := range n {
|
||||
cfg.Options.Policies = append(cfg.Options.Policies, newPolicyFromTemplate(i, tmpl))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func newPolicyFromTemplate(i int, pt PolicyTemplate) config.Policy {
|
||||
eval := func(in string) string {
|
||||
t := template.New("policy")
|
||||
tmpl, err := t.Parse(in)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
if err := tmpl.Execute(&out, struct{ Idx int }{i}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
||||
pplPolicy, err := parser.ParseYAML(strings.NewReader(eval(pt.PPL)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
to, err := config.ParseWeightedUrls(eval(pt.To))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return config.Policy{
|
||||
From: eval(pt.From),
|
||||
To: to,
|
||||
Policy: &config.PPLPolicy{Policy: pplPolicy},
|
||||
}
|
||||
}
|
35
internal/testenv/snippets/wait.go
Normal file
35
internal/testenv/snippets/wait.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package snippets
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/testenv"
|
||||
"github.com/pomerium/pomerium/pkg/grpcutil"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/connectivity"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
func WaitStartupComplete(env testenv.Environment, timeout ...time.Duration) time.Duration {
|
||||
start := time.Now()
|
||||
recorder := env.NewLogRecorder()
|
||||
if len(timeout) == 0 {
|
||||
timeout = append(timeout, 1*time.Minute)
|
||||
}
|
||||
ctx, ca := context.WithTimeout(env.Context(), timeout[0])
|
||||
defer ca()
|
||||
recorder.WaitForMatch(map[string]any{
|
||||
"syncer_id": "databroker",
|
||||
"syncer_type": "type.googleapis.com/pomerium.config.Config",
|
||||
"message": "listening for updates",
|
||||
}, timeout...)
|
||||
cc, err := grpc.Dial(env.DatabrokerURL().Value(),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithChainUnaryInterceptor(grpcutil.WithUnarySignedJWT(env.SharedSecret)),
|
||||
grpc.WithChainStreamInterceptor(grpcutil.WithStreamSignedJWT(env.SharedSecret)),
|
||||
)
|
||||
env.Require().NoError(err)
|
||||
env.Require().True(cc.WaitForStateChange(ctx, connectivity.Ready))
|
||||
return time.Since(start)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue