forked from googleforgames/agones
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of yaml and backing Go code for the CRDs for supporting Fleet Allocation Overflow tracking. Work on googleforgames#2682
- Loading branch information
1 parent
ce56e29
commit eca02c4
Showing
10 changed files
with
586 additions
and
0 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
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,208 @@ | ||
// Copyright 2022 Google LLC All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestAllocationOverflowValidate(t *testing.T) { | ||
// valid | ||
type expected struct { | ||
valid bool | ||
fields []string | ||
} | ||
|
||
fixtures := map[string]struct { | ||
ao AllocationOverflow | ||
expected | ||
}{ | ||
"empty": { | ||
ao: AllocationOverflow{}, | ||
expected: expected{ | ||
valid: true, | ||
fields: nil, | ||
}, | ||
}, | ||
"bad label name": { | ||
ao: AllocationOverflow{ | ||
Labels: map[string]string{"$$$foobar": "stuff"}, | ||
Annotations: nil, | ||
}, | ||
expected: expected{ | ||
valid: false, | ||
fields: []string{"labels"}, | ||
}, | ||
}, | ||
"bad label value": { | ||
ao: AllocationOverflow{ | ||
Labels: map[string]string{"valid": "$$$NOPE"}, | ||
Annotations: nil, | ||
}, | ||
expected: expected{ | ||
valid: false, | ||
fields: []string{"labels"}, | ||
}, | ||
}, | ||
"bad annotation name": { | ||
ao: AllocationOverflow{ | ||
Labels: nil, | ||
Annotations: map[string]string{"$$$foobar": "stuff"}, | ||
}, | ||
expected: expected{ | ||
valid: false, | ||
fields: []string{"annotations"}, | ||
}, | ||
}, | ||
"valid full": { | ||
ao: AllocationOverflow{ | ||
Labels: map[string]string{"valid": "yes", "still.valid": "check-me-out"}, | ||
Annotations: map[string]string{"icando-this": "yes, I can do all kinds of things here $$$"}, | ||
}, | ||
expected: expected{ | ||
valid: true, | ||
fields: nil, | ||
}, | ||
}, | ||
} | ||
|
||
for k, v := range fixtures { | ||
t.Run(k, func(t *testing.T) { | ||
causes, valid := v.ao.Validate() | ||
assert.Equal(t, v.expected.valid, valid, "valid") | ||
if v.expected.fields == nil { | ||
assert.Empty(t, causes) | ||
} else { | ||
for i, cause := range causes { | ||
assert.Equal(t, metav1.CauseTypeFieldValueInvalid, cause.Type) | ||
// messages come from K8s validation libraries, so testing exact matches would be brittle. | ||
assert.Contains(t, cause.Message, "Invalid value:") | ||
assert.Equal(t, v.expected.fields[i], cause.Field) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAllocationOverflowCountMatches(t *testing.T) { | ||
type expected struct { | ||
count int32 | ||
rest int | ||
} | ||
|
||
fixtures := map[string]struct { | ||
list func([]*GameServer) | ||
ao func(*AllocationOverflow) | ||
expected expected | ||
}{ | ||
"simple": { | ||
list: func(_ []*GameServer) {}, | ||
ao: func(_ *AllocationOverflow) {}, | ||
expected: expected{ | ||
count: 2, | ||
rest: 0, | ||
}, | ||
}, | ||
"label selector": { | ||
list: func(list []*GameServer) { | ||
list[0].ObjectMeta.Labels = map[string]string{"colour": "blue"} | ||
}, | ||
ao: func(ao *AllocationOverflow) { | ||
ao.Labels = map[string]string{"colour": "blue"} | ||
}, | ||
expected: expected{ | ||
count: 1, | ||
rest: 1, | ||
}, | ||
}, | ||
"annotation selector": { | ||
list: func(list []*GameServer) { | ||
list[0].ObjectMeta.Annotations = map[string]string{"colour": "green"} | ||
}, | ||
ao: func(ao *AllocationOverflow) { | ||
ao.Annotations = map[string]string{"colour": "green"} | ||
}, | ||
expected: expected{ | ||
count: 1, | ||
rest: 1, | ||
}, | ||
}, | ||
"both": { | ||
list: func(list []*GameServer) { | ||
list[0].ObjectMeta.Labels = map[string]string{"colour": "blue"} | ||
list[0].ObjectMeta.Annotations = map[string]string{"colour": "green"} | ||
}, | ||
ao: func(ao *AllocationOverflow) { | ||
ao.Labels = map[string]string{"colour": "blue"} | ||
ao.Annotations = map[string]string{"colour": "green"} | ||
}, | ||
expected: expected{ | ||
count: 1, | ||
rest: 1, | ||
}, | ||
}, | ||
} | ||
|
||
for k, v := range fixtures { | ||
t.Run(k, func(t *testing.T) { | ||
list := []*GameServer{ | ||
{ObjectMeta: metav1.ObjectMeta{Name: "g1"}, Status: GameServerStatus{State: GameServerStateAllocated}}, | ||
{ObjectMeta: metav1.ObjectMeta{Name: "g2"}, Status: GameServerStatus{State: GameServerStateAllocated}}, | ||
{ObjectMeta: metav1.ObjectMeta{Name: "g3"}, Status: GameServerStatus{State: GameServerStateReady}}, | ||
} | ||
v.list(list) | ||
ao := &AllocationOverflow{ | ||
Labels: nil, | ||
Annotations: nil, | ||
} | ||
v.ao(ao) | ||
|
||
count, rest := ao.CountMatches(list) | ||
assert.Equal(t, v.expected.count, count, "count") | ||
assert.Equal(t, v.expected.rest, len(rest), "rest") | ||
for _, gs := range rest { | ||
assert.Equal(t, GameServerStateAllocated, gs.Status.State) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAllocationOverflowApply(t *testing.T) { | ||
// check empty | ||
gs := &GameServer{} | ||
ao := AllocationOverflow{Labels: map[string]string{"colour": "green"}, Annotations: map[string]string{"colour": "blue", "map": "ice cream"}} | ||
|
||
ao.Apply(gs) | ||
|
||
require.Equal(t, ao.Annotations, gs.ObjectMeta.Annotations) | ||
require.Equal(t, ao.Labels, gs.ObjectMeta.Labels) | ||
|
||
// check append | ||
ao = AllocationOverflow{Labels: map[string]string{"version": "1.0"}, Annotations: map[string]string{"version": "1.0"}} | ||
ao.Apply(gs) | ||
|
||
require.Equal(t, map[string]string{"colour": "green", "version": "1.0"}, gs.ObjectMeta.Labels) | ||
require.Equal(t, map[string]string{"colour": "blue", "map": "ice cream", "version": "1.0"}, gs.ObjectMeta.Annotations) | ||
|
||
// check overwrite | ||
ao = AllocationOverflow{Labels: map[string]string{"colour": "red"}, Annotations: map[string]string{"colour": "green"}} | ||
ao.Apply(gs) | ||
require.Equal(t, map[string]string{"colour": "red", "version": "1.0"}, gs.ObjectMeta.Labels) | ||
require.Equal(t, map[string]string{"colour": "green", "map": "ice cream", "version": "1.0"}, gs.ObjectMeta.Annotations) | ||
} |
Oops, something went wrong.