-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa70c2b
commit 7ef795d
Showing
4 changed files
with
183 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package web | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/gravitational/teleport" | ||
"github.com/gravitational/teleport/api/client/proto" | ||
"github.com/gravitational/teleport/api/utils" | ||
"github.com/gravitational/teleport/entitlements" | ||
"github.com/jonboulle/clockwork" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// mockFeaturesClient is a mock implementation of the `featuresClient` interface | ||
// that always returns its internal features with support for entitlement compatibility on Ping | ||
type mockFeaturesClient struct { | ||
features proto.Features | ||
} | ||
|
||
func (m *mockFeaturesClient) Ping(ctx context.Context) (proto.PingResponse, error) { | ||
return proto.PingResponse{ | ||
ServerFeatures: &m.features, | ||
}, nil | ||
} | ||
|
||
func TestFeaturesWatcher(t *testing.T) { | ||
clock := clockwork.NewFakeClock() | ||
handler := &Handler{ | ||
cfg: Config{ | ||
LicenseWatchInterval: 100 * time.Millisecond, | ||
}, | ||
clock: clock, | ||
clusterFeatures: proto.Features{}, | ||
featureWatcherStop: make(chan struct{}), | ||
log: newPackageLogger(), | ||
logger: slog.Default().With(teleport.ComponentKey, teleport.ComponentWeb), | ||
} | ||
|
||
features := proto.Features{ | ||
Kubernetes: true, | ||
Entitlements: map[string]*proto.EntitlementInfo{}, | ||
AccessRequests: &proto.AccessRequestsFeature{}, | ||
} | ||
entitlements.SupportEntitlementsCompatibility(&features) | ||
|
||
client := mockFeaturesClient{ | ||
features, | ||
} | ||
// before running the service, features should match the value passed in | ||
// to the handler | ||
requireFeatures(t, clock, proto.Features{}, handler.GetClusterFeatures) | ||
|
||
go handler.startFeaturesWatcher(&client) | ||
clock.BlockUntil(1) | ||
// after starting the service, handler.GetClusterFeatures should return | ||
// values matching the client's response | ||
expected := utils.CloneProtoMsg(&features) | ||
requireFeatures(t, clock, *expected, handler.GetClusterFeatures) | ||
|
||
// update values once again and check if the features are properly updated | ||
features = proto.Features{ | ||
Kubernetes: false, | ||
Entitlements: map[string]*proto.EntitlementInfo{}, | ||
AccessRequests: &proto.AccessRequestsFeature{}, | ||
} | ||
entitlements.SupportEntitlementsCompatibility(&features) | ||
client.features = features | ||
expected = utils.CloneProtoMsg(&features) | ||
requireFeatures(t, clock, *expected, handler.GetClusterFeatures) | ||
|
||
// test entitlements explicitly | ||
features = proto.Features{ | ||
Kubernetes: false, | ||
Entitlements: map[string]*proto.EntitlementInfo{ | ||
string(entitlements.ExternalAuditStorage): {Enabled: true}, | ||
string(entitlements.AccessLists): {Enabled: true}, | ||
string(entitlements.AccessMonitoring): {Enabled: true}, | ||
string(entitlements.App): {Enabled: true}, | ||
string(entitlements.CloudAuditLogRetention): {Enabled: true}, | ||
}, | ||
AccessRequests: &proto.AccessRequestsFeature{}, | ||
} | ||
entitlements.SupportEntitlementsCompatibility(&features) | ||
client.features = features | ||
|
||
expected = &proto.Features{ | ||
Kubernetes: false, | ||
Entitlements: map[string]*proto.EntitlementInfo{ | ||
string(entitlements.ExternalAuditStorage): {Enabled: true}, | ||
string(entitlements.AccessLists): {Enabled: true}, | ||
string(entitlements.AccessMonitoring): {Enabled: true}, | ||
string(entitlements.App): {Enabled: true}, | ||
string(entitlements.CloudAuditLogRetention): {Enabled: true}, | ||
}, | ||
AccessRequests: &proto.AccessRequestsFeature{}, | ||
} | ||
entitlements.SupportEntitlementsCompatibility(expected) | ||
requireFeatures(t, clock, *expected, handler.GetClusterFeatures) | ||
|
||
// call stop and ensure it stops updating features | ||
handler.stopFeaturesWatcher() | ||
previousFeatures := utils.CloneProtoMsg(&features) | ||
// toggle some features and update the mock client | ||
features = proto.Features{ | ||
Kubernetes: !previousFeatures.Kubernetes, | ||
App: !previousFeatures.App, | ||
Entitlements: map[string]*proto.EntitlementInfo{}, | ||
AccessRequests: &proto.AccessRequestsFeature{}, | ||
} | ||
client.features = features | ||
neverFeatures(t, clock, features, handler.GetClusterFeatures) | ||
} | ||
|
||
// requireFeatures is a helper function that advances the clock, then | ||
// calls `getFeatures` every 100ms for up to 1 second, until it | ||
// returns the expected result (`want`). | ||
func requireFeatures(t *testing.T, fakeClock clockwork.FakeClock, want proto.Features, getFeatures func() proto.Features) { | ||
t.Helper() | ||
|
||
// Advance the clock so the service fetch and stores features | ||
fakeClock.Advance(1 * time.Second) | ||
|
||
require.EventuallyWithT(t, func(c *assert.CollectT) { | ||
diff := cmp.Diff(want, getFeatures()) | ||
if !assert.Empty(c, diff) { | ||
t.Logf("Feature diff (-want +got):\n%s", diff) | ||
} | ||
}, 1*time.Second, time.Millisecond*100) | ||
} | ||
|
||
func neverFeatures(t *testing.T, fakeClock clockwork.FakeClock, want proto.Features, getFeatures func() proto.Features) { | ||
t.Helper() | ||
|
||
// Advance the clock so the service fetch and stores features | ||
fakeClock.Advance(1 * time.Second) | ||
|
||
require.Never(t, func() bool { | ||
diff := cmp.Diff(want, getFeatures()) | ||
if diff == "" { | ||
return true | ||
} | ||
return false | ||
}, 1*time.Second, time.Millisecond*100) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters