mirror of
https://github.com/pomerium/pomerium.git
synced 2025-06-24 05:28:16 +02:00
internal/databroker: store server version (#1121)
Storing server version when creating new server. After then, we can retrieve the version from backend when server restart. With storage backend which supports persistent, the server version won't change after restarting.
This commit is contained in:
parent
26f099b49d
commit
99785cbb5b
4 changed files with 347 additions and 178 deletions
73
internal/databroker/server_test.go
Normal file
73
internal/databroker/server_test.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package databroker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/pomerium/pomerium/internal/log"
|
||||
"github.com/pomerium/pomerium/pkg/grpc/databroker"
|
||||
"github.com/pomerium/pomerium/pkg/storage"
|
||||
)
|
||||
|
||||
func newServer(cfg *serverConfig) *Server {
|
||||
return &Server{
|
||||
version: uuid.New().String(),
|
||||
cfg: cfg,
|
||||
log: log.With().Str("service", "databroker").Logger(),
|
||||
|
||||
byType: make(map[string]storage.Backend),
|
||||
onchange: NewSignal(),
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer_initVersion(t *testing.T) {
|
||||
cfg := newServerConfig()
|
||||
t.Run("nil db", func(t *testing.T) {
|
||||
srv := newServer(cfg)
|
||||
srvVersion := uuid.New().String()
|
||||
srv.version = srvVersion
|
||||
srv.byType[recordTypeServerVersion] = nil
|
||||
srv.initVersion()
|
||||
assert.Equal(t, srvVersion, srv.version)
|
||||
})
|
||||
t.Run("new server with random version", func(t *testing.T) {
|
||||
srv := newServer(cfg)
|
||||
ctx := context.Background()
|
||||
db := srv.getDB(recordTypeServerVersion)
|
||||
r := db.Get(ctx, serverVersionKey)
|
||||
assert.Nil(t, r)
|
||||
srvVersion := uuid.New().String()
|
||||
srv.version = srvVersion
|
||||
srv.initVersion()
|
||||
assert.Equal(t, srvVersion, srv.version)
|
||||
r = db.Get(ctx, serverVersionKey)
|
||||
assert.NotNil(t, r)
|
||||
var sv databroker.ServerVersion
|
||||
assert.NoError(t, ptypes.UnmarshalAny(r.GetData(), &sv))
|
||||
assert.Equal(t, srv.version, sv.Version)
|
||||
})
|
||||
t.Run("init version twice should get the same version", func(t *testing.T) {
|
||||
srv := newServer(cfg)
|
||||
ctx := context.Background()
|
||||
db := srv.getDB(recordTypeServerVersion)
|
||||
r := db.Get(ctx, serverVersionKey)
|
||||
assert.Nil(t, r)
|
||||
srvVersion := uuid.New().String()
|
||||
srv.version = srvVersion
|
||||
srv.initVersion()
|
||||
assert.Equal(t, srvVersion, srv.version)
|
||||
r = db.Get(ctx, serverVersionKey)
|
||||
assert.NotNil(t, r)
|
||||
var sv databroker.ServerVersion
|
||||
assert.NoError(t, ptypes.UnmarshalAny(r.GetData(), &sv))
|
||||
assert.Equal(t, srv.version, sv.Version)
|
||||
|
||||
// re-init version should get the same value as above
|
||||
srv.initVersion()
|
||||
assert.Equal(t, srvVersion, srv.version)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue