diff --git a/pkg/scheduler/util/BUILD b/pkg/scheduler/util/BUILD index 35241c873f..57839b6708 100644 --- a/pkg/scheduler/util/BUILD +++ b/pkg/scheduler/util/BUILD @@ -17,6 +17,7 @@ go_test( "//pkg/apis/scheduling:go_default_library", "//staging/src/k8s.io/api/core/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/types:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library", ], ) diff --git a/pkg/scheduler/util/utils_test.go b/pkg/scheduler/util/utils_test.go index a452f2dc5a..62378ae99e 100644 --- a/pkg/scheduler/util/utils_test.go +++ b/pkg/scheduler/util/utils_test.go @@ -17,9 +17,11 @@ limitations under the License. package util import ( + "reflect" "testing" "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/diff" "k8s.io/kubernetes/pkg/apis/scheduling" ) @@ -303,3 +305,115 @@ func TestHostPortInfo_Check(t *testing.T) { } } } + +func TestGetContainerPorts(t *testing.T) { + tests := []struct { + pod1 *v1.Pod + pod2 *v1.Pod + expected []*v1.ContainerPort + }{ + { + pod1: &v1.Pod{ + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Ports: []v1.ContainerPort{ + { + ContainerPort: 8001, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8002, + Protocol: v1.ProtocolTCP, + }, + }, + }, + { + Ports: []v1.ContainerPort{ + { + ContainerPort: 8003, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8004, + Protocol: v1.ProtocolTCP, + }, + }, + }, + }, + }, + }, + pod2: &v1.Pod{ + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Ports: []v1.ContainerPort{ + { + ContainerPort: 8011, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8012, + Protocol: v1.ProtocolTCP, + }, + }, + }, + { + Ports: []v1.ContainerPort{ + { + ContainerPort: 8013, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8014, + Protocol: v1.ProtocolTCP, + }, + }, + }, + }, + }, + }, + expected: []*v1.ContainerPort{ + { + ContainerPort: 8001, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8002, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8003, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8004, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8011, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8012, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8013, + Protocol: v1.ProtocolTCP, + }, + { + ContainerPort: 8014, + Protocol: v1.ProtocolTCP, + }, + }, + }, + } + + for _, test := range tests { + result := GetContainerPorts(test.pod1, test.pod2) + if !reflect.DeepEqual(test.expected, result) { + t.Errorf("Got different result than expected.\nDifference detected on:\n%s", diff.ObjectGoPrintSideBySide(test.expected, result)) + } + } +}