Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests to confirm that Fleet, Fleet Autoscaler, and Fleet Allocation apply defaults code is idempotent #3888

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/apis/agones/v1/fleet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"

"agones.dev/agones/pkg"
"agones.dev/agones/pkg/apis"
"agones.dev/agones/pkg/util/runtime"
)
Expand Down Expand Up @@ -108,6 +109,16 @@ func TestFleetApplyDefaults(t *testing.T) {
assert.Equal(t, "25%", f.Spec.Strategy.RollingUpdate.MaxSurge.String())
assert.Equal(t, apis.Packed, f.Spec.Scheduling)
assert.Equal(t, int32(0), f.Spec.Replicas)
assert.Equal(t, pkg.Version, f.ObjectMeta.Annotations[VersionAnnotation])

// Test apply defaults is idempotent -- calling ApplyDefaults more than one time does not change the original result.
f.ApplyDefaults()
assert.Equal(t, appsv1.RollingUpdateDeploymentStrategyType, f.Spec.Strategy.Type)
assert.Equal(t, "25%", f.Spec.Strategy.RollingUpdate.MaxUnavailable.String())
assert.Equal(t, "25%", f.Spec.Strategy.RollingUpdate.MaxSurge.String())
assert.Equal(t, apis.Packed, f.Spec.Scheduling)
assert.Equal(t, int32(0), f.Spec.Replicas)
assert.Equal(t, pkg.Version, f.ObjectMeta.Annotations[VersionAnnotation])
}

func TestFleetUpperBoundReplicas(t *testing.T) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/allocation/v1/gameserverallocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ func TestGameServerSelectorApplyDefaults(t *testing.T) {
assert.NotNil(t, s.Counters)
assert.NotNil(t, s.Lists)

// Test apply defaults is idempotent -- calling ApplyDefaults more than one time does not change the original result.
s.ApplyDefaults()
assert.Equal(t, agonesv1.GameServerStateReady, *s.GameServerState)
assert.Equal(t, int64(0), s.Players.MinAvailable)
assert.Equal(t, int64(0), s.Players.MaxAvailable)
assert.NotNil(t, s.Counters)
assert.NotNil(t, s.Lists)

state := agonesv1.GameServerStateAllocated
// set values
s = &GameServerSelector{
Expand All @@ -158,6 +166,19 @@ func TestGameServerSelectorApplyDefaults(t *testing.T) {
assert.Equal(t, int64(2), s.Lists["bar"].MinAvailable)
assert.Equal(t, int64(0), s.Lists["bar"].MaxAvailable)
assert.Equal(t, "", s.Lists["bar"].ContainsValue)

// Test apply defaults is idempotent -- calling ApplyDefaults more than one time does not change the original result.
s.ApplyDefaults()
assert.Equal(t, state, *s.GameServerState)
assert.Equal(t, int64(10), s.Players.MinAvailable)
assert.Equal(t, int64(20), s.Players.MaxAvailable)
assert.Equal(t, int64(0), s.Counters["foo"].MinCount)
assert.Equal(t, int64(0), s.Counters["foo"].MaxCount)
assert.Equal(t, int64(1), s.Counters["foo"].MinAvailable)
assert.Equal(t, int64(10), s.Counters["foo"].MaxAvailable)
assert.Equal(t, int64(2), s.Lists["bar"].MinAvailable)
assert.Equal(t, int64(0), s.Lists["bar"].MaxAvailable)
assert.Equal(t, "", s.Lists["bar"].ContainsValue)
}

func TestGameServerSelectorValidate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/autoscaling/v1/fleetautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const (
// FixedIntervalSyncType is a simple fixed interval based strategy for trigger autoscaling
FixedIntervalSyncType FleetAutoscalerSyncType = "FixedInterval"

defaultIntervalSyncSeconds = 30
defaultIntervalSyncSeconds int32 = 30
)

// BufferPolicy controls the desired behavior of the buffer policy.
Expand Down
18 changes: 18 additions & 0 deletions pkg/apis/autoscaling/v1/fleetautoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,24 @@ func TestFleetAutoscalerListValidateUpdate(t *testing.T) {
}
}

func TestFleetAutoscalerApplyDefaults(t *testing.T) {
fas := &FleetAutoscaler{}

// gate
assert.Nil(t, fas.Spec.Sync)

fas.ApplyDefaults()
assert.NotNil(t, fas.Spec.Sync)
assert.Equal(t, FixedIntervalSyncType, fas.Spec.Sync.Type)
assert.Equal(t, defaultIntervalSyncSeconds, fas.Spec.Sync.FixedInterval.Seconds)

// Test apply defaults is idempotent -- calling ApplyDefaults more than one time does not change the original result.
fas.ApplyDefaults()
assert.NotNil(t, fas.Spec.Sync)
assert.Equal(t, FixedIntervalSyncType, fas.Spec.Sync.Type)
assert.Equal(t, defaultIntervalSyncSeconds, fas.Spec.Sync.FixedInterval.Seconds)
}

func defaultFixture() *FleetAutoscaler {
return customFixture(BufferPolicyType)
}
Expand Down
Loading