forked from googleforgames/agones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
212 lines (185 loc) · 6.04 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2019 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 main
import (
"context"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"net/http"
"os"
"testing"
pb "agones.dev/agones/pkg/allocation/go"
allocationv1 "agones.dev/agones/pkg/apis/allocation/v1"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
corev1 "k8s.io/api/core/v1"
k8serror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
)
func TestAllocateHandler(t *testing.T) {
t.Parallel()
h := serviceHandler{
allocationCallback: func(gsa *allocationv1.GameServerAllocation) (k8sruntime.Object, error) {
return &allocationv1.GameServerAllocation{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
},
Status: allocationv1.GameServerAllocationStatus{
State: allocationv1.GameServerAllocationContention,
},
}, nil
},
}
request := &pb.AllocationRequest{
Namespace: "ns",
MultiClusterSetting: &pb.MultiClusterSetting{
Enabled: true,
},
}
response, err := h.Allocate(context.Background(), request)
if !assert.Nil(t, response) {
return
}
st, ok := status.FromError(err)
if !assert.True(t, ok) {
return
}
assert.Equal(t, st.Code(), codes.Aborted)
}
func TestAllocateHandlerReturnsError(t *testing.T) {
t.Parallel()
h := serviceHandler{
allocationCallback: func(gsa *allocationv1.GameServerAllocation) (k8sruntime.Object, error) {
return nil, k8serror.NewBadRequest("error")
},
}
request := &pb.AllocationRequest{}
_, err := h.Allocate(context.Background(), request)
if assert.Error(t, err) {
assert.Equal(t, "error", err.Error())
}
}
func TestHandlingStatus(t *testing.T) {
t.Parallel()
errorMessage := "GameServerAllocation is invalid"
h := serviceHandler{
allocationCallback: func(gsa *allocationv1.GameServerAllocation) (k8sruntime.Object, error) {
return &metav1.Status{
Status: metav1.StatusFailure,
Message: errorMessage,
Reason: metav1.StatusReasonInvalid,
Details: &metav1.StatusDetails{
Kind: "GameServerAllocation",
Group: allocationv1.SchemeGroupVersion.Group,
},
Code: http.StatusUnprocessableEntity,
}, nil
},
}
request := &pb.AllocationRequest{}
_, err := h.Allocate(context.Background(), request)
if !assert.Error(t, err, "expecting failure") {
return
}
st, ok := status.FromError(err)
if !ok {
t.Errorf("expecting status error: %v", err)
}
assert.Equal(t, 422, int(st.Code()))
assert.Contains(t, st.Message(), errorMessage)
}
func TestBadReturnType(t *testing.T) {
t.Parallel()
h := serviceHandler{
allocationCallback: func(gsa *allocationv1.GameServerAllocation) (k8sruntime.Object, error) {
return &corev1.Secret{}, nil
},
}
request := &pb.AllocationRequest{}
_, err := h.Allocate(context.Background(), request)
if !assert.Error(t, err, "expecting failure") {
return
}
st, ok := status.FromError(err)
if !ok {
t.Errorf("expecting status error: %v", err)
}
assert.Equal(t, codes.Internal, st.Code())
assert.Contains(t, st.Message(), "internal server error")
}
func TestVerifyClientCertificateSucceeds(t *testing.T) {
t.Parallel()
crt := []byte(clientCert)
certPool := x509.NewCertPool()
assert.True(t, certPool.AppendCertsFromPEM(crt))
h := serviceHandler{
caCertPool: certPool,
}
block, _ := pem.Decode(crt)
input := [][]byte{block.Bytes}
assert.Nil(t, h.verifyClientCertificate(input, nil),
"verifyClientCertificate failed.")
}
func TestVerifyClientCertificateFails(t *testing.T) {
t.Parallel()
crt := []byte(clientCert)
certPool := x509.NewCertPool()
h := serviceHandler{
caCertPool: certPool,
}
block, _ := pem.Decode(crt)
input := [][]byte{block.Bytes}
assert.Error(t, h.verifyClientCertificate(input, nil),
"verifyClientCertificate() succeeded, expected error.")
}
func TestGettingCaCert(t *testing.T) {
t.Parallel()
file, err := ioutil.TempFile(".", "*.crt")
if assert.Nil(t, err) {
defer os.Remove(file.Name()) // nolint: errcheck
_, err = file.WriteString(clientCert)
if assert.Nil(t, err) {
certPool, err := getCACertPool("./")
if assert.Nil(t, err) {
assert.Len(t, certPool.Subjects(), 1)
}
}
}
}
var clientCert = `-----BEGIN CERTIFICATE-----
MIIDuzCCAqOgAwIBAgIUduDWtqpUsp3rZhCEfUrzI05laVIwDQYJKoZIhvcNAQEL
BQAwbTELMAkGA1UEBhMCR0IxDzANBgNVBAgMBkxvbmRvbjEPMA0GA1UEBwwGTG9u
ZG9uMRgwFgYDVQQKDA9HbG9iYWwgU2VjdXJpdHkxFjAUBgNVBAsMDUlUIERlcGFy
dG1lbnQxCjAIBgNVBAMMASowHhcNMTkwNTAyMjIzMDQ3WhcNMjkwNDI5MjIzMDQ3
WjBtMQswCQYDVQQGEwJHQjEPMA0GA1UECAwGTG9uZG9uMQ8wDQYDVQQHDAZMb25k
b24xGDAWBgNVBAoMD0dsb2JhbCBTZWN1cml0eTEWMBQGA1UECwwNSVQgR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