Debian Codegraph @

pkg/util/webhook/serviceresolver_test.go

raw
1 /*
2 Copyright 2017 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package webhook
18
19 import (
20 "fmt"
21 "testing"
22 )
23
24 func TestDefaultServiceResolver(t *testing.T) {
25 scenarios := []struct {
26 serviceName string
27 serviceNamespace string
28 port int32
29 expectedOutput string
30 expectError bool
31 }{
32 // scenario 1: a service name along with a namespace resolves
33 {serviceName: "ross", serviceNamespace: "andromeda", port: 443, expectedOutput: "https://ross.andromeda.svc:443"},
34 // scenario 2: a service name without a namespace does not resolve
35 {serviceName: "ross", expectError: true},
36 // scenario 3: cannot resolve an empty service name
37 {serviceNamespace: "andromeda", expectError: true},
38 // scenario 1: a service name along with a namespace and different port resolves
39 {serviceName: "ross", serviceNamespace: "andromeda", port: 1002, expectedOutput: "https://ross.andromeda.svc:1002"},
40 }
41
42 // act
43 for index, scenario := range scenarios {
44 t.Run(fmt.Sprintf("scenario %d", index), func(t *testing.T) {
45 target := defaultServiceResolver{}
46 serviceURL, err := target.ResolveEndpoint(scenario.serviceNamespace, scenario.serviceName, scenario.port)
47
48 if err != nil && !scenario.expectError {
49 t.Errorf("unexpected error has occurred = %v", err)
50 }
51 if err == nil && scenario.expectError {
52 t.Error("expected an error but got nothing")
53 }
54 if !scenario.expectError {
55 if serviceURL.String() != scenario.expectedOutput {
56 t.Errorf("expected = %s, got = %s", scenario.expectedOutput, serviceURL.String())
57 }
58 }
59 })
60 }
61 }