-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from rramkumar1/refactors-for-testing
Break out some helper functions for better testing + reuse
- Loading branch information
Showing
6 changed files
with
250 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
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 backendconfig | ||
|
||
import ( | ||
"strconv" | ||
|
||
apiv1 "k8s.io/api/core/v1" | ||
|
||
"k8s.io/ingress-gce/pkg/annotations" | ||
) | ||
|
||
// BackendConfigName returns the name of the BackendConfig which is associated | ||
// with the given ServicePort. | ||
func BackendConfigName(backendConfigs annotations.BackendConfigs, svcPort apiv1.ServicePort) string { | ||
configName := "" | ||
// Both port name and port number are allowed. | ||
if name, ok := backendConfigs.Ports[svcPort.Name]; ok { | ||
configName = name | ||
} else if name, ok := backendConfigs.Ports[strconv.Itoa(int(svcPort.Port))]; ok { | ||
configName = name | ||
} else if len(backendConfigs.Default) != 0 { | ||
configName = backendConfigs.Default | ||
} | ||
return configName | ||
} |
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,74 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
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 backendconfig | ||
|
||
import ( | ||
"testing" | ||
|
||
apiv1 "k8s.io/api/core/v1" | ||
|
||
"k8s.io/ingress-gce/pkg/annotations" | ||
) | ||
|
||
func TestBackendConfigName(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
backendConfigs annotations.BackendConfigs | ||
svcPort apiv1.ServicePort | ||
expected string | ||
}{ | ||
{ | ||
desc: "matched on port name", | ||
backendConfigs: annotations.BackendConfigs{ | ||
Ports: map[string]string{"http": "config-http"}, | ||
Default: "default", | ||
}, | ||
svcPort: apiv1.ServicePort{Name: "http"}, | ||
expected: "config-http", | ||
}, | ||
{ | ||
desc: "matched on port number", | ||
backendConfigs: annotations.BackendConfigs{ | ||
Ports: map[string]string{"80": "config-http"}, | ||
Default: "default", | ||
}, | ||
svcPort: apiv1.ServicePort{Name: "foo", Port: 80}, | ||
expected: "config-http", | ||
}, | ||
{ | ||
desc: "matched on default", | ||
backendConfigs: annotations.BackendConfigs{ | ||
Ports: map[string]string{"http": "config-http"}, | ||
Default: "default", | ||
}, | ||
svcPort: apiv1.ServicePort{Name: "https", Port: 443}, | ||
expected: "default", | ||
}, | ||
{ | ||
desc: "no match", | ||
backendConfigs: annotations.BackendConfigs{ | ||
Ports: map[string]string{"http": "config-http"}, | ||
}, | ||
svcPort: apiv1.ServicePort{Name: "https", Port: 443}, | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
result := BackendConfigName(tc.backendConfigs, tc.svcPort) | ||
if result != tc.expected { | ||
t.Errorf("%s: expected %s but got %s", tc.desc, tc.expected, result) | ||
} | ||
} | ||
} |
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,41 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
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 translator | ||
|
||
import ( | ||
api_v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
) | ||
|
||
// ServicePort is a helper function that retrieves a port of a Service. | ||
func ServicePort(svc api_v1.Service, port intstr.IntOrString) *api_v1.ServicePort { | ||
var svcPort *api_v1.ServicePort | ||
PortLoop: | ||
for _, p := range svc.Spec.Ports { | ||
np := p | ||
switch port.Type { | ||
case intstr.Int: | ||
if p.Port == port.IntVal { | ||
svcPort = &np | ||
break PortLoop | ||
} | ||
default: | ||
if p.Name == port.StrVal { | ||
svcPort = &np | ||
break PortLoop | ||
} | ||
} | ||
} | ||
return svcPort | ||
} |
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,92 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
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 translator | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
api_v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
) | ||
|
||
var ( | ||
testSvc = api_v1.Service{ | ||
Spec: api_v1.ServiceSpec{ | ||
Ports: []api_v1.ServicePort{ | ||
{ | ||
Name: "foo", | ||
Port: 1000, | ||
}, | ||
{ | ||
Name: "bar", | ||
Port: 1001, | ||
}, | ||
{ | ||
Name: "baz", | ||
Port: 1002, | ||
}, | ||
{ | ||
Name: "qux", | ||
Port: 1003, | ||
}, | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
func TestServicePort(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
port intstr.IntOrString | ||
expected *api_v1.ServicePort | ||
}{ | ||
{ | ||
desc: "match on port number", | ||
port: intstr.FromInt(1000), | ||
expected: &api_v1.ServicePort{ | ||
Name: "foo", | ||
Port: 1000, | ||
}, | ||
}, | ||
{ | ||
desc: "match on port name", | ||
port: intstr.FromString("foo"), | ||
expected: &api_v1.ServicePort{ | ||
Name: "foo", | ||
Port: 1000, | ||
}, | ||
}, | ||
{ | ||
desc: "match on last port number", | ||
port: intstr.FromInt(1003), | ||
expected: &api_v1.ServicePort{ | ||
Name: "qux", | ||
Port: 1003, | ||
}, | ||
}, | ||
{ | ||
desc: "no match", | ||
port: intstr.FromInt(3000), | ||
expected: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
result := ServicePort(testSvc, tc.port) | ||
if !reflect.DeepEqual(result, tc.expected) { | ||
t.Errorf("%s: expected %+v but got %+v", tc.desc, tc.expected, result) | ||
} | ||
} | ||
} |