forked from gruntwork-io/kubergrunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingress_test.go
118 lines (104 loc) · 2.94 KB
/
ingress_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package kubectl
import (
"fmt"
"strings"
"testing"
"time"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/stretchr/testify/require"
)
const ExampleIngressName = "nginx-service-ingress"
func TestGetIngressReturnsErrorForNonExistantIngress(t *testing.T) {
t.Parallel()
kubeConfigPath, err := k8s.GetKubeConfigPathE(t)
require.NoError(t, err)
kubectlOptions := &KubectlOptions{ConfigPath: kubeConfigPath}
_, err = GetIngress(kubectlOptions, "kube-system", "i-dont-exist")
require.Error(t, err)
}
func TestGetIngressEReturnsCorrectIngressInCorrectNamespace(t *testing.T) {
t.Parallel()
uniqueID := strings.ToLower(random.UniqueId())
ttKubectlOptions := k8s.NewKubectlOptions("", "")
ttKubectlOptions.Namespace = uniqueID
configData := fmt.Sprintf(EXAMPLE_INGRESS_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID, uniqueID)
defer k8s.KubectlDeleteFromString(t, ttKubectlOptions, configData)
k8s.KubectlApplyFromString(t, ttKubectlOptions, configData)
kubeConfigPath, err := k8s.GetKubeConfigPathE(t)
require.NoError(t, err)
kubectlOptions := &KubectlOptions{ConfigPath: kubeConfigPath}
ingress, err := GetIngress(kubectlOptions, uniqueID, "nginx-service-ingress")
require.NoError(t, err)
require.Equal(t, ingress.Name, "nginx-service-ingress")
require.Equal(t, ingress.Namespace, uniqueID)
}
func TestWaitUntilIngressAvailableReturnsSuccessfully(t *testing.T) {
t.Parallel()
uniqueID := strings.ToLower(random.UniqueId())
ttKubectlOptions := k8s.NewKubectlOptions("", "")
ttKubectlOptions.Namespace = uniqueID
configData := fmt.Sprintf(EXAMPLE_INGRESS_DEPLOYMENT_YAML_TEMPLATE, uniqueID, uniqueID, uniqueID, uniqueID)
defer k8s.KubectlDeleteFromString(t, ttKubectlOptions, configData)
k8s.KubectlApplyFromString(t, ttKubectlOptions, configData)
kubeConfigPath, err := k8s.GetKubeConfigPathE(t)
require.NoError(t, err)
kubectlOptions := &KubectlOptions{ConfigPath: kubeConfigPath}
err = WaitUntilIngressEndpointProvisioned(kubectlOptions, uniqueID, ExampleIngressName, 60, 5*time.Second)
require.NoError(t, err)
}
const EXAMPLE_INGRESS_DEPLOYMENT_YAML_TEMPLATE = `---
apiVersion: v1
kind: Namespace
metadata:
name: %s
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: %s
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.7
ports:
- containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
name: nginx-service
namespace: %s
spec:
selector:
app: nginx
ports:
- protocol: TCP
targetPort: 80
port: 80
type: NodePort
---
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: nginx-service-ingress
namespace: %s
spec:
rules:
- http:
paths:
- path: /app
backend:
serviceName: nginx-service
servicePort: 80
`