Skip to content

Commit

Permalink
Add validation of the fleet underlying gameserver
Browse files Browse the repository at this point in the history
Now we perform additional validation on the Gameserver specification.
For googleforgames#765.
  • Loading branch information
aLekSer committed May 15, 2019
1 parent a1b1451 commit 3bd34f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
8 changes: 8 additions & 0 deletions pkg/apis/stable/v1alpha1/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ func (f *Fleet) Validate() ([]metav1.StatusCause, bool) {
})
}

// check gameserver specification in a fleet
gs := f.GameServerSet().GameServer()
gs.ApplyDefaults()
gsCauses, ok := gs.Validate()
if !ok {
causes = append(causes, gsCauses...)
}

return causes, len(causes) == 0
}

Expand Down
41 changes: 40 additions & 1 deletion pkg/apis/stable/v1alpha1/fleet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,30 @@ func TestSumStatusAllocatedReplicas(t *testing.T) {
assert.Equal(t, int32(5), SumStatusAllocatedReplicas([]*GameServerSet{gsSet1, gsSet2}))
}

func TestFleetGameserverSpec(t *testing.T) {
f := defaultFleet()
f.Spec.Template.Spec.Template =
corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{Name: "container", Image: "myimage"}, {Name: "container2", Image: "myimage"}},
},
}
causes, ok := f.Validate()

assert.False(t, ok)
assert.Len(t, causes, 2)
assert.Equal(t, "container", causes[0].Field)

f.Spec.Template.Spec.Container = "testing"
causes, ok = f.Validate()

assert.False(t, ok)
assert.Len(t, causes, 1)
assert.Equal(t, "Could not find a container named testing", causes[0].Message)
}

func TestFleetName(t *testing.T) {
f := Fleet{}
f := defaultFleet()

nameLen := validation.LabelValueMaxLength + 1
bytes := make([]byte, nameLen)
Expand Down Expand Up @@ -130,3 +152,20 @@ func TestSumStatusReplicas(t *testing.T) {

assert.Equal(t, int32(30), SumStatusReplicas(fixture))
}

func defaultFleet() *Fleet {
gs := GameServer{
Spec: GameServerSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "testing", Image: "testing/image"}}}}},
}
return &Fleet{
ObjectMeta: metav1.ObjectMeta{GenerateName: "simple-fleet-", Namespace: "defaultNs"},
Spec: FleetSpec{
Replicas: 2,
Template: GameServerTemplateSpec{
Spec: gs.Spec,
},
},
}
}
4 changes: 2 additions & 2 deletions test/e2e/fleetautoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestAutoscalerBasicFunctions(t *testing.T) {

// check that we are able to scale
framework.WaitForFleetAutoScalerCondition(t, fas, func(fas *v1alpha1.FleetAutoscaler) bool {
return fas.Status.ScalingLimited == false
return !fas.Status.ScalingLimited
})

// patch autoscaler to a maxReplicas count equal to current replicas count
Expand All @@ -113,7 +113,7 @@ func TestAutoscalerBasicFunctions(t *testing.T) {

// check that we are not able to scale
framework.WaitForFleetAutoScalerCondition(t, fas, func(fas *v1alpha1.FleetAutoscaler) bool {
return fas.Status.ScalingLimited == true
return fas.Status.ScalingLimited
})

// delete the allocated GameServer and watch the fleet scale down
Expand Down

0 comments on commit 3bd34f5

Please sign in to comment.