-
Notifications
You must be signed in to change notification settings - Fork 1
/
nginx.go
177 lines (155 loc) · 4.52 KB
/
nginx.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Steadybit GmbH
package e2e
import (
"errors"
"fmt"
"github.com/steadybit/action-kit/go/action_kit_api/v2"
"github.com/steadybit/extension-kit/extutil"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
acorev1 "k8s.io/client-go/applyconfigurations/core/v1"
ametav1 "k8s.io/client-go/applyconfigurations/meta/v1"
"strings"
"testing"
"time"
)
type Nginx struct {
Minikube *Minikube
Pod metav1.Object
Service metav1.Object
}
func (n *Nginx) Deploy(podName string, opts ...func(c *acorev1.PodApplyConfiguration)) error {
cfg := &acorev1.PodApplyConfiguration{
TypeMetaApplyConfiguration: ametav1.TypeMetaApplyConfiguration{
Kind: extutil.Ptr("Pod"),
APIVersion: extutil.Ptr("v1"),
},
ObjectMetaApplyConfiguration: &ametav1.ObjectMetaApplyConfiguration{
Name: &podName,
Labels: map[string]string{"app": podName},
},
Spec: &acorev1.PodSpecApplyConfiguration{
RestartPolicy: extutil.Ptr(corev1.RestartPolicyNever),
Containers: []acorev1.ContainerApplyConfiguration{
{
Name: extutil.Ptr("nginx"),
Image: extutil.Ptr("docker.io/library/nginx:stable-alpine"),
Ports: []acorev1.ContainerPortApplyConfiguration{
{
ContainerPort: extutil.Ptr(int32(80)),
},
},
},
},
},
Status: nil,
}
for _, fn := range opts {
fn(cfg)
}
pod, err := n.Minikube.CreatePod(cfg)
if err != nil {
return err
}
n.Pod = pod
service, err := n.Minikube.CreateService(&acorev1.ServiceApplyConfiguration{
TypeMetaApplyConfiguration: ametav1.TypeMetaApplyConfiguration{
Kind: extutil.Ptr("Service"),
APIVersion: extutil.Ptr("v1"),
},
ObjectMetaApplyConfiguration: &ametav1.ObjectMetaApplyConfiguration{
Name: extutil.Ptr("nginx"),
Labels: map[string]string{"app": podName},
},
Spec: &acorev1.ServiceSpecApplyConfiguration{
Type: extutil.Ptr(corev1.ServiceTypeLoadBalancer),
Selector: n.Pod.GetLabels(),
Ports: []acorev1.ServicePortApplyConfiguration{
{
Port: extutil.Ptr(int32(80)),
Protocol: extutil.Ptr(corev1.ProtocolTCP),
},
},
},
})
if err != nil {
return err
}
n.Service = service
return nil
}
func (n *Nginx) Target() (*action_kit_api.Target, error) {
return NewContainerTarget(n.Minikube, n.Pod, "nginx")
}
func (n *Nginx) IsReachable() error {
client, err := n.Minikube.NewRestClientForService(n.Service)
if err != nil {
return err
}
defer client.Close()
_, err = client.R().Get("/")
if err != nil {
return err
}
return nil
}
func (n *Nginx) AssertIsReachable(t *testing.T, expected bool) {
t.Helper()
client, err := n.Minikube.NewRestClientForService(n.Service)
require.NoError(t, err)
defer client.Close()
Retry(t, 8, 500*time.Millisecond, func(r *R) {
_, err = client.R().Get("/")
if expected && err != nil {
r.Failed = true
_, _ = fmt.Fprintf(r.Log, "expected nginx to be reachble, but was not: %s", err)
} else if !expected && err == nil {
r.Failed = true
_, _ = fmt.Fprintf(r.Log, "expected nginx not to be reachble, but was")
}
})
}
func (n *Nginx) CanReach(url string) error {
out, err := n.Minikube.PodExec(n.Pod, "nginx", "curl", "--max-time", "2", url)
if err != nil {
return fmt.Errorf("%w: %s", err, out)
}
return nil
}
func (n *Nginx) AssertCanReach(t *testing.T, url string, expected bool) {
t.Helper()
Retry(t, 8, 500*time.Millisecond, func(r *R) {
err := n.CanReach(url)
if expected && err != nil {
r.Failed = true
_, _ = fmt.Fprintf(r.Log, "expected '%s' to be reachble from nginx, but was not: %s", url, err)
} else if !expected && err == nil {
r.Failed = true
_, _ = fmt.Fprintf(r.Log, "expecte '%s' not to be reachble from nginx, but was", url)
}
})
}
func (n *Nginx) AssertCannotReach(t *testing.T, url string, errContains string) {
t.Helper()
Retry(t, 8, 500*time.Millisecond, func(r *R) {
err := n.CanReach(url)
if err == nil {
r.Failed = true
_, _ = fmt.Fprintf(r.Log, "expected '%s' not to be reachble from nginx, but was", url)
} else if !strings.Contains(err.Error(), errContains) {
r.Failed = true
_, _ = fmt.Fprintf(r.Log, "expected '%s' not to be reachble from nginx, with error containing '%s', but was '%s'", url, errContains, err)
}
})
}
func (n *Nginx) ContainerStatus() (*corev1.ContainerStatus, error) {
return GetContainerStatus(n.Minikube, n.Pod, "nginx")
}
func (n *Nginx) Delete() error {
return errors.Join(
n.Minikube.DeletePod(n.Pod),
n.Minikube.DeleteService(n.Service),
)
}