Skip to content

Commit 777bcf2

Browse files
committed
chore: additional tests
1 parent c74ead4 commit 777bcf2

File tree

3 files changed

+173
-2
lines changed

3 files changed

+173
-2
lines changed

enterprise/coderd/coderdenttest/coderdenttest.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ type LicenseOptions struct {
183183
Features license.Features
184184
}
185185

186+
func (opts *LicenseOptions) WithIssuedAt(now time.Time) *LicenseOptions {
187+
opts.IssuedAt = now
188+
return opts
189+
}
190+
186191
func (opts *LicenseOptions) Expired(now time.Time) *LicenseOptions {
187192
opts.NotBefore = now.Add(time.Hour * 24 * -4) // needs to be before the grace period
188193
opts.ExpiresAt = now.Add(time.Hour * 24 * -2)
@@ -214,6 +219,14 @@ func (opts *LicenseOptions) UserLimit(limit int64) *LicenseOptions {
214219
return opts.Feature(codersdk.FeatureUserLimit, limit)
215220
}
216221

222+
func (opts *LicenseOptions) ManagedAgentLimit(soft int64, hard int64) *LicenseOptions {
223+
// These don't use named or exported feature names, see
224+
// enterprise/coderd/license/license.go.
225+
opts = opts.Feature(codersdk.FeatureName("managed_agent_limit_soft"), soft)
226+
opts = opts.Feature(codersdk.FeatureName("managed_agent_limit_hard"), hard)
227+
return opts
228+
}
229+
217230
func (opts *LicenseOptions) Feature(name codersdk.FeatureName, value int64) *LicenseOptions {
218231
if opts.Features == nil {
219232
opts.Features = license.Features{}

enterprise/coderd/license/license.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,10 @@ func LicensesEntitlements(
439439
if hardLimit > softLimit && softLimit > 0 {
440440
softWarningThreshold = softLimit + int64(float64(hardLimit-softLimit)*0.75)
441441
}
442-
if managedAgentCount > *agentLimit.Limit {
442+
if managedAgentCount >= *agentLimit.Limit {
443443
entitlements.Warnings = append(entitlements.Warnings,
444444
"You have built more workspaces with managed agents than your license allows. Further managed agent builds will be blocked.")
445-
} else if managedAgentCount > softWarningThreshold {
445+
} else if managedAgentCount >= softWarningThreshold {
446446
entitlements.Warnings = append(entitlements.Warnings,
447447
"You are approaching the managed agent limit in your license. Please refer to the Deployment Licenses page for more information.")
448448
}

enterprise/coderd/license/license_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,164 @@ func TestLicenseEntitlements(t *testing.T) {
888888
entitlements.Features[codersdk.FeatureCustomRoles].Entitlement)
889889
},
890890
},
891+
{
892+
Name: "ManagedAgentLimit",
893+
Licenses: []*coderdenttest.LicenseOptions{
894+
enterpriseLicense().UserLimit(100).ManagedAgentLimit(100, 200),
895+
},
896+
Arguments: license.FeatureArguments{
897+
ManagedAgentCountFn: func(ctx context.Context, from time.Time, to time.Time) (int64, error) {
898+
// 175 will generate a warning as it's over 75% of the
899+
// difference between the soft and hard limit.
900+
return 174, nil
901+
},
902+
},
903+
AssertEntitlements: func(t *testing.T, entitlements codersdk.Entitlements) {
904+
assertNoErrors(t, entitlements)
905+
assertNoWarnings(t, entitlements)
906+
feature := entitlements.Features[codersdk.FeatureManagedAgentLimit]
907+
assert.Equal(t, codersdk.EntitlementEntitled, feature.Entitlement)
908+
assert.True(t, feature.Enabled)
909+
assert.Equal(t, int64(100), *feature.SoftLimit)
910+
assert.Equal(t, int64(200), *feature.Limit)
911+
assert.Equal(t, int64(174), *feature.Actual)
912+
},
913+
},
914+
{
915+
Name: "ManagedAgentLimitWithGrace",
916+
Licenses: []*coderdenttest.LicenseOptions{
917+
// Add another license that is not entitled to managed agents to
918+
// suppress warnings for other features.
919+
enterpriseLicense().
920+
UserLimit(100).
921+
WithIssuedAt(time.Now().Add(-time.Hour * 2)),
922+
enterpriseLicense().
923+
UserLimit(100).
924+
ManagedAgentLimit(100, 100).
925+
WithIssuedAt(time.Now().Add(-time.Hour * 1)).
926+
GracePeriod(time.Now()),
927+
},
928+
Arguments: license.FeatureArguments{
929+
ManagedAgentCountFn: func(ctx context.Context, from time.Time, to time.Time) (int64, error) {
930+
// When the soft and hard limit are equal, the warning is
931+
// triggered at 75% of the hard limit.
932+
return 74, nil
933+
},
934+
},
935+
AssertEntitlements: func(t *testing.T, entitlements codersdk.Entitlements) {
936+
assertNoErrors(t, entitlements)
937+
assertNoWarnings(t, entitlements)
938+
feature := entitlements.Features[codersdk.FeatureManagedAgentLimit]
939+
assert.Equal(t, codersdk.EntitlementGracePeriod, feature.Entitlement)
940+
assert.True(t, feature.Enabled)
941+
assert.Equal(t, int64(100), *feature.SoftLimit)
942+
assert.Equal(t, int64(100), *feature.Limit)
943+
assert.Equal(t, int64(74), *feature.Actual)
944+
},
945+
},
946+
{
947+
Name: "ManagedAgentLimitWithExpired",
948+
Licenses: []*coderdenttest.LicenseOptions{
949+
// Add another license that is not entitled to managed agents to
950+
// suppress warnings for other features.
951+
enterpriseLicense().
952+
UserLimit(100).
953+
WithIssuedAt(time.Now().Add(-time.Hour * 2)),
954+
enterpriseLicense().
955+
UserLimit(100).
956+
ManagedAgentLimit(100, 200).
957+
WithIssuedAt(time.Now().Add(-time.Hour * 1)).
958+
Expired(time.Now()),
959+
},
960+
Arguments: license.FeatureArguments{
961+
ManagedAgentCountFn: func(ctx context.Context, from time.Time, to time.Time) (int64, error) {
962+
return 10, nil
963+
},
964+
},
965+
AssertEntitlements: func(t *testing.T, entitlements codersdk.Entitlements) {
966+
feature := entitlements.Features[codersdk.FeatureManagedAgentLimit]
967+
assert.Equal(t, codersdk.EntitlementNotEntitled, feature.Entitlement)
968+
assert.False(t, feature.Enabled)
969+
assert.Nil(t, feature.SoftLimit)
970+
assert.Nil(t, feature.Limit)
971+
assert.Nil(t, feature.Actual)
972+
},
973+
},
974+
{
975+
Name: "ManagedAgentLimitWarning/ApproachingLimit/DifferentSoftAndHardLimit",
976+
Licenses: []*coderdenttest.LicenseOptions{
977+
enterpriseLicense().
978+
UserLimit(100).
979+
ManagedAgentLimit(100, 200),
980+
},
981+
Arguments: license.FeatureArguments{
982+
ManagedAgentCountFn: func(ctx context.Context, from time.Time, to time.Time) (int64, error) {
983+
return 175, nil
984+
},
985+
},
986+
AssertEntitlements: func(t *testing.T, entitlements codersdk.Entitlements) {
987+
assert.Len(t, entitlements.Warnings, 1)
988+
assert.Equal(t, "You are approaching the managed agent limit in your license. Please refer to the Deployment Licenses page for more information.", entitlements.Warnings[0])
989+
assertNoErrors(t, entitlements)
990+
991+
feature := entitlements.Features[codersdk.FeatureManagedAgentLimit]
992+
assert.Equal(t, codersdk.EntitlementEntitled, feature.Entitlement)
993+
assert.True(t, feature.Enabled)
994+
assert.Equal(t, int64(100), *feature.SoftLimit)
995+
assert.Equal(t, int64(200), *feature.Limit)
996+
assert.Equal(t, int64(175), *feature.Actual)
997+
},
998+
},
999+
{
1000+
Name: "ManagedAgentLimitWarning/ApproachingLimit/EqualSoftAndHardLimit",
1001+
Licenses: []*coderdenttest.LicenseOptions{
1002+
enterpriseLicense().
1003+
UserLimit(100).
1004+
ManagedAgentLimit(100, 100),
1005+
},
1006+
Arguments: license.FeatureArguments{
1007+
ManagedAgentCountFn: func(ctx context.Context, from time.Time, to time.Time) (int64, error) {
1008+
return 75, nil
1009+
},
1010+
},
1011+
AssertEntitlements: func(t *testing.T, entitlements codersdk.Entitlements) {
1012+
assert.Len(t, entitlements.Warnings, 1)
1013+
assert.Equal(t, "You are approaching the managed agent limit in your license. Please refer to the Deployment Licenses page for more information.", entitlements.Warnings[0])
1014+
assertNoErrors(t, entitlements)
1015+
1016+
feature := entitlements.Features[codersdk.FeatureManagedAgentLimit]
1017+
assert.Equal(t, codersdk.EntitlementEntitled, feature.Entitlement)
1018+
assert.True(t, feature.Enabled)
1019+
assert.Equal(t, int64(100), *feature.SoftLimit)
1020+
assert.Equal(t, int64(100), *feature.Limit)
1021+
assert.Equal(t, int64(75), *feature.Actual)
1022+
},
1023+
},
1024+
{
1025+
Name: "ManagedAgentLimitWarning/BreachedLimit",
1026+
Licenses: []*coderdenttest.LicenseOptions{
1027+
enterpriseLicense().
1028+
UserLimit(100).
1029+
ManagedAgentLimit(100, 200),
1030+
},
1031+
Arguments: license.FeatureArguments{
1032+
ManagedAgentCountFn: func(ctx context.Context, from time.Time, to time.Time) (int64, error) {
1033+
return 200, nil
1034+
},
1035+
},
1036+
AssertEntitlements: func(t *testing.T, entitlements codersdk.Entitlements) {
1037+
assert.Len(t, entitlements.Warnings, 1)
1038+
assert.Equal(t, "You have built more workspaces with managed agents than your license allows. Further managed agent builds will be blocked.", entitlements.Warnings[0])
1039+
assertNoErrors(t, entitlements)
1040+
1041+
feature := entitlements.Features[codersdk.FeatureManagedAgentLimit]
1042+
assert.Equal(t, codersdk.EntitlementEntitled, feature.Entitlement)
1043+
assert.True(t, feature.Enabled)
1044+
assert.Equal(t, int64(100), *feature.SoftLimit)
1045+
assert.Equal(t, int64(200), *feature.Limit)
1046+
assert.Equal(t, int64(200), *feature.Actual)
1047+
},
1048+
},
8911049
}
8921050

8931051
for _, tc := range testCases {

0 commit comments

Comments
 (0)