4
4
package proxy
5
5
6
6
import (
7
+ "fmt"
7
8
"testing"
8
9
9
10
"github.com/gitpod-io/gitpod/installer/pkg/common"
@@ -12,11 +13,12 @@ import (
12
13
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"
13
14
"github.com/stretchr/testify/require"
14
15
corev1 "k8s.io/api/core/v1"
16
+ "k8s.io/utils/pointer"
15
17
)
16
18
17
19
func TestServiceLoadBalancerIP (t * testing.T ) {
18
20
const loadBalancerIP = "123.456.789.0"
19
- ctx := renderContextWithProxyConfig (t , & experimental.ProxyConfig {StaticIP : loadBalancerIP })
21
+ ctx := renderContextWithProxyConfig (t , & experimental.ProxyConfig {StaticIP : loadBalancerIP }, nil )
20
22
21
23
objects , err := service (ctx )
22
24
require .NoError (t , err )
@@ -28,24 +30,98 @@ func TestServiceLoadBalancerIP(t *testing.T) {
28
30
}
29
31
30
32
func TestServiceAnnotations (t * testing.T ) {
31
- annotations := map [string ]string {"hello" : "world" }
33
+ testCases := []struct {
34
+ Name string
35
+ Annotations map [string ]string
36
+ Components * config.Components
37
+ Expect func (ctx * common.RenderContext , svc * corev1.Service , annotations map [string ]string )
38
+ }{
39
+ {
40
+ Name : "Default to LoadBalancer" ,
41
+ Annotations : map [string ]string {"hello" : "world" },
42
+ Expect : func (ctx * common.RenderContext , svc * corev1.Service , annotations map [string ]string ) {
43
+ // Check standard load balancer annotations
44
+ annotations = loadBalancerAnnotations (ctx , annotations )
32
45
33
- ctx := renderContextWithProxyConfig (t , & experimental.ProxyConfig {ServiceAnnotations : annotations })
46
+ for k , v := range annotations {
47
+ require .Equalf (t , annotations [k ], svc .Annotations [k ],
48
+ "expected to find annotation %q:%q on proxy service, but found %q:%q" , k , v , k , svc .Annotations [k ])
49
+ }
50
+ },
51
+ },
52
+ {
53
+ Name : "Set to LoadBalancer" ,
54
+ Components : & config.Components {
55
+ Proxy : & config.ProxyComponent {
56
+ Service : & config.ComponentTypeService {
57
+ ServiceType : (* corev1 .ServiceType )(pointer .String (string (corev1 .ServiceTypeLoadBalancer ))),
58
+ },
59
+ },
60
+ },
61
+ Annotations : map [string ]string {"hello" : "world" , "hello2" : "world2" },
62
+ Expect : func (ctx * common.RenderContext , svc * corev1.Service , annotations map [string ]string ) {
63
+ // Check standard load balancer annotations
64
+ annotations = loadBalancerAnnotations (ctx , annotations )
34
65
35
- objects , err := service (ctx )
36
- require .NoError (t , err )
66
+ for k , v := range annotations {
67
+ require .Equalf (t , annotations [k ], svc .Annotations [k ],
68
+ "expected to find annotation %q:%q on proxy service, but found %q:%q" , k , v , k , svc .Annotations [k ])
69
+ }
70
+ },
71
+ },
72
+ {
73
+ Name : "Set to ClusterIP" ,
74
+ Components : & config.Components {
75
+ Proxy : & config.ProxyComponent {
76
+ Service : & config.ComponentTypeService {
77
+ ServiceType : (* corev1 .ServiceType )(pointer .String (string (corev1 .ServiceTypeClusterIP ))),
78
+ },
79
+ },
80
+ },
81
+ Annotations : map [string ]string {"hello" : "world" },
82
+ Expect : func (ctx * common.RenderContext , svc * corev1.Service , annotations map [string ]string ) {
83
+ // Check standard load balancer annotations not present
84
+ lbAnnotations := loadBalancerAnnotations (ctx , make (map [string ]string , 0 ))
37
85
38
- require .Len (t , objects , 1 , "must render only one object" )
86
+ for k := range lbAnnotations {
87
+ require .NotContains (t , annotations , k )
88
+ }
39
89
40
- svc := objects [0 ].(* corev1.Service )
41
- for k , v := range annotations {
42
- require .Equalf (t , annotations [k ], svc .Annotations [k ],
43
- "expected to find annotation %q:%q on proxy service, but found %q:%q" , k , v , k , svc .Annotations [k ])
90
+ for k , v := range annotations {
91
+ require .Equalf (t , annotations [k ], svc .Annotations [k ],
92
+ "expected to find annotation %q:%q on proxy service, but found %q:%q" , k , v , k , svc .Annotations [k ])
93
+ }
94
+ },
95
+ },
96
+ }
97
+
98
+ for _ , testCase := range testCases {
99
+ t .Run (testCase .Name , func (t * testing.T ) {
100
+ ctx := renderContextWithProxyConfig (t , & experimental.ProxyConfig {ServiceAnnotations : testCase .Annotations }, testCase .Components )
101
+
102
+ objects , err := service (ctx )
103
+ require .NoError (t , err )
104
+
105
+ require .Len (t , objects , 1 , "must render only one object" )
106
+
107
+ svc := objects [0 ].(* corev1.Service )
108
+
109
+ testCase .Expect (ctx , svc , testCase .Annotations )
110
+ })
44
111
}
45
112
}
46
113
47
- func renderContextWithProxyConfig (t * testing.T , proxyConfig * experimental.ProxyConfig ) * common.RenderContext {
114
+ func loadBalancerAnnotations (ctx * common.RenderContext , annotations map [string ]string ) map [string ]string {
115
+ annotations ["external-dns.alpha.kubernetes.io/hostname" ] = fmt .Sprintf ("%s,*.%s,*.ws.%s" , ctx .Config .Domain , ctx .Config .Domain , ctx .Config .Domain )
116
+ annotations ["cloud.google.com/neg" ] = `{"exposed_ports": {"80":{},"443": {}}}`
117
+
118
+ return annotations
119
+ }
120
+
121
+ func renderContextWithProxyConfig (t * testing.T , proxyConfig * experimental.ProxyConfig , components * config.Components ) * common.RenderContext {
48
122
ctx , err := common .NewRenderContext (config.Config {
123
+ Domain : "some-domain" ,
124
+ Components : components ,
49
125
Experimental : & experimental.Config {
50
126
WebApp : & experimental.WebAppConfig {
51
127
ProxyConfig : proxyConfig ,
0 commit comments