pomerium/internal/databroker/config_source_test.go
Caleb Doxsey 5d60cff21e
databroker: refactor databroker to sync all changes (#1879)
* refactor backend, implement encrypted store

* refactor in-memory store

* wip

* wip

* wip

* add syncer test

* fix redis expiry

* fix linting issues

* fix test by skipping non-config records

* fix backoff import

* fix init issues

* fix query

* wait for initial sync before starting directory sync

* add type to SyncLatest

* add more log messages, fix deadlock in in-memory store, always return server version from SyncLatest

* update sync types and tests

* add redis tests

* skip macos in github actions

* add comments to proto

* split getBackend into separate methods

* handle errors in initVersion

* return different error for not found vs other errors in get

* use exponential backoff for redis transaction retry

* rename raw to result

* use context instead of close channel

* store type urls as constants in databroker

* use timestampb instead of ptypes

* fix group merging not waiting

* change locked names

* update GetAll to return latest record version

* add method to grpcutil to get the type url for a protobuf type
2021-02-18 15:24:33 -07:00

88 lines
1.8 KiB
Go

package databroker
import (
"context"
"net"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/anypb"
"github.com/pomerium/pomerium/config"
configpb "github.com/pomerium/pomerium/pkg/grpc/config"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
)
func TestConfigSource(t *testing.T) {
ctx, clearTimeout := context.WithTimeout(context.Background(), 5*time.Second)
defer clearTimeout()
li, err := net.Listen("tcp", "127.0.0.1:0")
if !assert.NoError(t, err) {
return
}
defer func() { _ = li.Close() }()
dataBrokerServer := New()
srv := grpc.NewServer()
databroker.RegisterDataBrokerServiceServer(srv, dataBrokerServer)
go func() { _ = srv.Serve(li) }()
cfgs := make(chan *config.Config, 10)
base := config.NewDefaultOptions()
base.DataBrokerURL = mustParse("http://" + li.Addr().String())
base.InsecureServer = true
base.GRPCInsecure = true
src := NewConfigSource(config.NewStaticSource(&config.Config{
Options: base,
}), func(cfg *config.Config) {
cfgs <- cfg
})
cfgs <- src.GetConfig()
data, _ := anypb.New(&configpb.Config{
Name: "config",
Routes: []*configpb.Route{
{
From: "https://from.example.com",
To: []string{"https://to.example.com"},
},
},
})
_, _ = dataBrokerServer.Put(ctx, &databroker.PutRequest{
Record: &databroker.Record{
Type: data.TypeUrl,
Id: "1",
Data: data,
},
})
select {
case <-ctx.Done():
assert.NoError(t, ctx.Err())
return
case cfg := <-cfgs:
assert.Len(t, cfg.Options.AdditionalPolicies, 0)
}
select {
case <-ctx.Done():
assert.NoError(t, ctx.Err())
return
case cfg := <-cfgs:
assert.Len(t, cfg.Options.AdditionalPolicies, 1)
}
}
func mustParse(raw string) *url.URL {
u, err := url.Parse(raw)
if err != nil {
panic(err)
}
return u
}