agones.dev/agones@v1.54.0/pkg/apis/multicluster/v1/gameserverallocationpolicy_test.go (about)

     1  // Copyright 2019 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v1
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestConnectionInfoIterator(t *testing.T) {
    24  	testCases := []struct {
    25  		name      string
    26  		in        []*GameServerAllocationPolicy
    27  		want      []ClusterConnectionInfo
    28  		unordered bool
    29  	}{
    30  		{
    31  			name: "Simple test",
    32  			in: []*GameServerAllocationPolicy{
    33  				{
    34  					Spec: GameServerAllocationPolicySpec{
    35  						Priority: 1,
    36  						Weight:   100,
    37  						ConnectionInfo: ClusterConnectionInfo{
    38  							ClusterName:         "cluster1",
    39  							SecretName:          "secret-name",
    40  							AllocationEndpoints: []string{"allocation-endpoint"},
    41  							Namespace:           "ns1",
    42  							ServerCA:            []byte("c2VydmVyQ0E="),
    43  						},
    44  					},
    45  				},
    46  			},
    47  			want: []ClusterConnectionInfo{
    48  				{
    49  					ClusterName:         "cluster1",
    50  					SecretName:          "secret-name",
    51  					AllocationEndpoints: []string{"allocation-endpoint"},
    52  					Namespace:           "ns1",
    53  					ServerCA:            []byte("c2VydmVyQ0E="),
    54  				},
    55  			},
    56  		},
    57  		{
    58  			name: "Different priorities and weight same cluster",
    59  			in: []*GameServerAllocationPolicy{
    60  				{
    61  					Spec: GameServerAllocationPolicySpec{
    62  						Priority: 1,
    63  						Weight:   100,
    64  						ConnectionInfo: ClusterConnectionInfo{
    65  							ClusterName: "cluster-name",
    66  						},
    67  					},
    68  				},
    69  				{
    70  					Spec: GameServerAllocationPolicySpec{
    71  						Priority: 2,
    72  						Weight:   300,
    73  						ConnectionInfo: ClusterConnectionInfo{
    74  							ClusterName: "cluster-name",
    75  						},
    76  					},
    77  				},
    78  			},
    79  			want: []ClusterConnectionInfo{
    80  				{
    81  					ClusterName: "cluster-name",
    82  				},
    83  			},
    84  		},
    85  		{
    86  			name: "Different clusters same priority",
    87  			in: []*GameServerAllocationPolicy{
    88  				{
    89  					Spec: GameServerAllocationPolicySpec{
    90  						Priority: 1,
    91  						Weight:   300,
    92  						ConnectionInfo: ClusterConnectionInfo{
    93  							ClusterName: "cluster1",
    94  						},
    95  					},
    96  				},
    97  				{
    98  					Spec: GameServerAllocationPolicySpec{
    99  						Priority: 1,
   100  						Weight:   100,
   101  						ConnectionInfo: ClusterConnectionInfo{
   102  							ClusterName: "cluster2",
   103  						},
   104  					},
   105  				},
   106  			},
   107  			want: []ClusterConnectionInfo{
   108  				{
   109  					ClusterName: "cluster1",
   110  				},
   111  				{
   112  					ClusterName: "cluster2",
   113  				},
   114  			},
   115  			unordered: true,
   116  		},
   117  		{
   118  			name: "Different clusters different priorities",
   119  			in: []*GameServerAllocationPolicy{
   120  				{
   121  					Spec: GameServerAllocationPolicySpec{
   122  						Priority: 1,
   123  						Weight:   300,
   124  						ConnectionInfo: ClusterConnectionInfo{
   125  							ClusterName: "cluster1",
   126  						},
   127  					},
   128  				},
   129  				{
   130  					Spec: GameServerAllocationPolicySpec{
   131  						Priority: 2,
   132  						Weight:   100,
   133  						ConnectionInfo: ClusterConnectionInfo{
   134  							ClusterName: "cluster2",
   135  						},
   136  					},
   137  				},
   138  			},
   139  			want: []ClusterConnectionInfo{
   140  				{
   141  					ClusterName: "cluster1",
   142  				},
   143  				{
   144  					ClusterName: "cluster2",
   145  				},
   146  			},
   147  		},
   148  		{
   149  			name: "Different clusters repeated with different priorities",
   150  			in: []*GameServerAllocationPolicy{
   151  				{
   152  					Spec: GameServerAllocationPolicySpec{
   153  						Priority: 1,
   154  						Weight:   300,
   155  						ConnectionInfo: ClusterConnectionInfo{
   156  							ClusterName: "cluster1",
   157  						},
   158  					},
   159  				},
   160  				{
   161  					Spec: GameServerAllocationPolicySpec{
   162  						Priority: 1,
   163  						Weight:   100,
   164  						ConnectionInfo: ClusterConnectionInfo{
   165  							ClusterName: "cluster2",
   166  						},
   167  					},
   168  				},
   169  				{
   170  					Spec: GameServerAllocationPolicySpec{
   171  						Priority: 2,
   172  						Weight:   300,
   173  						ConnectionInfo: ClusterConnectionInfo{
   174  							ClusterName: "cluster1",
   175  						},
   176  					},
   177  				},
   178  				{
   179  					Spec: GameServerAllocationPolicySpec{
   180  						Priority: 2,
   181  						Weight:   100,
   182  						ConnectionInfo: ClusterConnectionInfo{
   183  							ClusterName: "cluster2",
   184  						},
   185  					},
   186  				},
   187  			},
   188  			want: []ClusterConnectionInfo{
   189  				{
   190  					ClusterName: "cluster1",
   191  				},
   192  				{
   193  					ClusterName: "cluster2",
   194  				},
   195  			},
   196  			unordered: true,
   197  		},
   198  		{
   199  			name: "Zero weight never chosen",
   200  			in: []*GameServerAllocationPolicy{
   201  				{
   202  					Spec: GameServerAllocationPolicySpec{
   203  						Priority: 1,
   204  						Weight:   0,
   205  						ConnectionInfo: ClusterConnectionInfo{
   206  							ClusterName: "cluster1",
   207  						},
   208  					},
   209  				},
   210  				{
   211  					Spec: GameServerAllocationPolicySpec{
   212  						Priority: 1,
   213  						Weight:   100,
   214  						ConnectionInfo: ClusterConnectionInfo{
   215  							ClusterName: "cluster2",
   216  						},
   217  					},
   218  				},
   219  			},
   220  			want: []ClusterConnectionInfo{
   221  				{
   222  					ClusterName: "cluster2",
   223  				},
   224  			},
   225  		},
   226  		{
   227  			name: "Multiple allocation endpoints test",
   228  			in: []*GameServerAllocationPolicy{
   229  				{
   230  					Spec: GameServerAllocationPolicySpec{
   231  						Priority: 1,
   232  						Weight:   100,
   233  						ConnectionInfo: ClusterConnectionInfo{
   234  							ClusterName:         "cluster1",
   235  							SecretName:          "secret-name",
   236  							AllocationEndpoints: []string{"alloc1", "alloc2"},
   237  							Namespace:           "ns1",
   238  							ServerCA:            []byte("c2VydmVyQ0E="),
   239  						},
   240  					},
   241  				},
   242  			},
   243  			want: []ClusterConnectionInfo{
   244  				{
   245  					ClusterName:         "cluster1",
   246  					SecretName:          "secret-name",
   247  					AllocationEndpoints: []string{"alloc1", "alloc2"},
   248  					Namespace:           "ns1",
   249  					ServerCA:            []byte("c2VydmVyQ0E="),
   250  				},
   251  			},
   252  		},
   253  		{
   254  			name: "Empty policy list",
   255  			in:   []*GameServerAllocationPolicy{},
   256  			want: nil,
   257  		},
   258  		{
   259  			name: "Nil policy list",
   260  			in:   nil,
   261  			want: nil,
   262  		},
   263  		{
   264  			name: "Same clusters and same priorities",
   265  			in: []*GameServerAllocationPolicy{
   266  				{
   267  					Spec: GameServerAllocationPolicySpec{
   268  						Priority: 1,
   269  						Weight:   100,
   270  						ConnectionInfo: ClusterConnectionInfo{
   271  							ClusterName: "cluster-name",
   272  						},
   273  					},
   274  				},
   275  				{
   276  					Spec: GameServerAllocationPolicySpec{
   277  						Priority: 1,
   278  						Weight:   300,
   279  						ConnectionInfo: ClusterConnectionInfo{
   280  							ClusterName: "cluster-name",
   281  						},
   282  					},
   283  				},
   284  			},
   285  			want: []ClusterConnectionInfo{
   286  				{
   287  					ClusterName: "cluster-name",
   288  				},
   289  			},
   290  		},
   291  	}
   292  	for _, tc := range testCases {
   293  		t.Run(tc.name, func(t *testing.T) {
   294  			var results []ClusterConnectionInfo
   295  			iterator := NewConnectionInfoIterator(tc.in)
   296  			for {
   297  				connectionInfo := iterator.Next()
   298  				if connectionInfo == nil {
   299  					break
   300  				}
   301  				results = append(results, *connectionInfo)
   302  			}
   303  
   304  			if tc.unordered {
   305  				assert.ElementsMatch(t, tc.want, results, "Failed test \"%s\"", tc.name)
   306  			} else {
   307  				assert.Equal(t, tc.want, results, "Failed test \"%s\"", tc.name)
   308  			}
   309  		})
   310  	}
   311  }
   312  
   313  func TestConnectionInfoIterator_SameClustersAndPriorities(t *testing.T) {
   314  	in := []*GameServerAllocationPolicy{
   315  		{
   316  			Spec: GameServerAllocationPolicySpec{
   317  				Priority: 444,
   318  				Weight:   100,
   319  				ConnectionInfo: ClusterConnectionInfo{
   320  					ClusterName: "cluster-name",
   321  				},
   322  			},
   323  		},
   324  		{
   325  			Spec: GameServerAllocationPolicySpec{
   326  				Priority: 444,
   327  				Weight:   300,
   328  				ConnectionInfo: ClusterConnectionInfo{
   329  					ClusterName: "cluster-name",
   330  				},
   331  			},
   332  		},
   333  	}
   334  
   335  	iterator := NewConnectionInfoIterator(in)
   336  	res := iterator.priorityToCluster[444]["cluster-name"]
   337  
   338  	// check an internal slice of policies
   339  	if assert.Equal(t, 2, len(res)) {
   340  		assert.Equal(t, "cluster-name", res[0].Spec.ConnectionInfo.ClusterName)
   341  		assert.Equal(t, int32(444), res[0].Spec.Priority)
   342  		assert.Equal(t, "cluster-name", res[1].Spec.ConnectionInfo.ClusterName)
   343  		assert.Equal(t, int32(444), res[1].Spec.Priority)
   344  	}
   345  }