forked from hetznercloud/hcloud-cloud-controller-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.go
138 lines (116 loc) · 3.39 KB
/
testing.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
package hcloud
import (
"context"
"os"
"testing"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/annotation"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/hcops"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/mocks"
"github.com/hetznercloud/hcloud-go/hcloud"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
// Setenv prepares the environment for testing the
// hcloud-cloud-controller-manager.
func Setenv(t *testing.T, args ...string) func() {
if len(args)%2 != 0 {
t.Fatal("Sentenv: uneven number of args")
}
newVars := make([]string, 0, len(args)/2)
oldEnv := make(map[string]string, len(newVars))
for i := 0; i < len(args); i += 2 {
k, v := args[i], args[i+1]
newVars = append(newVars, k)
if old, ok := os.LookupEnv(k); ok {
oldEnv[k] = old
}
if err := os.Setenv(k, v); err != nil {
t.Fatalf("Setenv failed: %v", err)
}
}
return func() {
for _, k := range newVars {
v, ok := oldEnv[k]
if !ok {
if err := os.Unsetenv(k); err != nil {
t.Errorf("Unsetenv failed: %v", err)
}
continue
}
if err := os.Setenv(k, v); err != nil {
t.Errorf("Setenv failed: %v", err)
}
}
}
}
// SkipEnv skips t if any of the passed vars is not set as an environment
// variable.
//
// SkipEnv uses os.LookupEnv. The empty string is therefore a valid value.
func SkipEnv(t *testing.T, vars ...string) {
for _, v := range vars {
if _, ok := os.LookupEnv(v); !ok {
t.Skipf("Environment variable not set: %s", v)
return
}
}
}
type LoadBalancerTestCase struct {
Name string
// Defined in test case as needed
ClusterName string
NetworkID int
ServiceUID string
ServiceAnnotations map[annotation.Name]interface{}
DisablePrivateIngressDefault bool
Nodes []*v1.Node
LB *hcloud.LoadBalancer
LBCreateResult *hcloud.LoadBalancerCreateResult
Mock func(t *testing.T, tt *LoadBalancerTestCase)
// Defines the actual test
Perform func(t *testing.T, tt *LoadBalancerTestCase)
Ctx context.Context // Set to context.Background by run if not defined in test
// Set by run
LBOps *hcops.MockLoadBalancerOps
LBClient *mocks.LoadBalancerClient
ActionClient *mocks.ActionClient
LoadBalancers *loadBalancers
Service *v1.Service
}
func (tt *LoadBalancerTestCase) run(t *testing.T) {
t.Helper()
tt.LBOps = &hcops.MockLoadBalancerOps{}
tt.LBOps.Test(t)
tt.ActionClient = &mocks.ActionClient{}
tt.ActionClient.Test(t)
tt.LBClient = &mocks.LoadBalancerClient{}
tt.LBClient.Test(t)
if tt.ClusterName == "" {
tt.ClusterName = "test-cluster"
}
tt.Service = &v1.Service{
ObjectMeta: metav1.ObjectMeta{UID: types.UID(tt.ServiceUID)},
}
for k, v := range tt.ServiceAnnotations {
if err := k.AnnotateService(tt.Service, v); err != nil {
t.Fatal(err)
}
}
if tt.Ctx == nil {
tt.Ctx = context.Background()
}
if tt.Mock != nil {
tt.Mock(t, tt)
}
tt.LoadBalancers = newLoadBalancers(tt.LBOps, tt.ActionClient, tt.DisablePrivateIngressDefault)
tt.Perform(t, tt)
tt.LBOps.AssertExpectations(t)
tt.LBClient.AssertExpectations(t)
tt.ActionClient.AssertExpectations(t)
}
func RunLoadBalancerTests(t *testing.T, tests []LoadBalancerTestCase) {
for _, tt := range tests {
tt.run(t)
}
}