forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_init_test.go
88 lines (78 loc) · 1.67 KB
/
container_init_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
package connectinject
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestHandlerContainerInit(t *testing.T) {
minimal := func() *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
annotationService: "foo",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
corev1.Container{
Name: "web",
},
corev1.Container{
Name: "web-side",
},
},
},
}
}
cases := []struct {
Name string
Pod func(*corev1.Pod) *corev1.Pod
Cmd string // Strings.Contains test
CmdNot string // Not contains
}{
{
"Only service",
func(pod *corev1.Pod) *corev1.Pod {
pod.Annotations[annotationService] = "web"
return pod
},
`service = "web"`,
`upstreams`,
},
{
"Service port specified",
func(pod *corev1.Pod) *corev1.Pod {
pod.Annotations[annotationService] = "web"
pod.Annotations[annotationPort] = "1234"
return pod
},
"port = 1234",
"",
},
{
"Upstream",
func(pod *corev1.Pod) *corev1.Pod {
pod.Annotations[annotationService] = "web"
pod.Annotations[annotationUpstreams] = "db:1234"
return pod
},
`destination_name = "db"`,
"",
},
}
for _, tt := range cases {
t.Run(tt.Name, func(t *testing.T) {
require := require.New(t)
var h Handler
container, err := h.containerInit(tt.Pod(minimal()))
require.NoError(err)
actual := strings.Join(container.Command, " ")
require.Contains(actual, tt.Cmd)
if tt.CmdNot != "" {
require.NotContains(actual, tt.CmdNot)
}
})
}
}