...
1
16
17 package util
18
19 import (
20 "testing"
21
22 "k8s.io/api/core/v1"
23 )
24
25 func TestLookupContainerPortNumberByName(t *testing.T) {
26 tests := []struct {
27 name string
28 pod v1.Pod
29 portname string
30 portnum int32
31 err bool
32 }{
33 {
34 name: "test success 1",
35 pod: v1.Pod{
36 Spec: v1.PodSpec{
37 Containers: []v1.Container{
38 {
39 Ports: []v1.ContainerPort{
40 {
41 Name: "https",
42 ContainerPort: int32(443)},
43 {
44 Name: "http",
45 ContainerPort: int32(80)},
46 },
47 },
48 },
49 },
50 },
51 portname: "http",
52 portnum: int32(80),
53 err: false,
54 },
55 {
56 name: "test faulure 1",
57 pod: v1.Pod{
58 Spec: v1.PodSpec{
59 Containers: []v1.Container{
60 {
61 Ports: []v1.ContainerPort{
62 {
63 Name: "https",
64 ContainerPort: int32(443)},
65 },
66 },
67 },
68 },
69 },
70 portname: "www",
71 portnum: int32(0),
72 err: true,
73 },
74 }
75
76 for _, tt := range tests {
77 t.Run(tt.name, func(t *testing.T) {
78 portnum, err := LookupContainerPortNumberByName(tt.pod, tt.portname)
79 if err != nil {
80 if tt.err {
81 return
82 }
83
84 t.Errorf("%v: unexpected error: %v", tt.name, err)
85 return
86 }
87
88 if tt.err {
89 t.Errorf("%v: unexpected success", tt.name)
90 return
91 }
92
93 if portnum != tt.portnum {
94 t.Errorf("%v: expected port number %v; got %v", tt.name, tt.portnum, portnum)
95 }
96 })
97 }
98 }
99
View as plain text