-
Notifications
You must be signed in to change notification settings - Fork 444
/
test.go
149 lines (117 loc) · 5 KB
/
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
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
package e2e
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/solo-io/gloo/test/kube2e"
"github.com/solo-io/gloo/test/kube2e/helper"
"github.com/solo-io/gloo/test/testutils"
"github.com/solo-io/gloo/test/kubernetes/testutils/actions"
"github.com/solo-io/gloo/test/kubernetes/testutils/cluster"
"github.com/solo-io/gloo/test/kubernetes/testutils/gloogateway"
"github.com/solo-io/gloo/test/kubernetes/testutils/runtime"
"github.com/solo-io/gloo/test/kubernetes/testutils/assertions"
)
// MustTestHelper returns the SoloTestHelper used for e2e tests
// The SoloTestHelper is a wrapper around `glooctl` and we should eventually phase it out
// in favor of using the exact tool that users rely on
func MustTestHelper(ctx context.Context, installation *TestInstallation) *helper.SoloTestHelper {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
rootDir := filepath.Join(cwd, "../../../../")
testHelper, err := kube2e.GetTestHelperForRootDir(ctx, rootDir, installation.Metadata.InstallNamespace)
if err != nil {
panic(err)
}
return testHelper
}
func MustTestCluster() *TestCluster {
runtimeContext := runtime.NewContext()
clusterContext := cluster.MustKindContext(runtimeContext.ClusterName)
return &TestCluster{
RuntimeContext: runtimeContext,
ClusterContext: clusterContext,
}
}
// TestCluster is the structure around a set of tests that run against a Kubernetes Cluster
// Within a TestCluster, we spin off multiple TestInstallation to test the behavior of a particular installation
type TestCluster struct {
// RuntimeContext contains the set of properties that are defined at runtime by whoever is invoking tests
RuntimeContext runtime.Context
// ClusterContext contains the metadata about the Kubernetes Cluster that is used for this TestCluster
ClusterContext *cluster.Context
// activeInstallations is the set of TestInstallation that have been created for this cluster.
// Since tests are run serially, this will only have a single entry at a time
activeInstallations map[string]*TestInstallation
}
func (c *TestCluster) RegisterTestInstallation(t *testing.T, glooGatewayContext *gloogateway.Context) *TestInstallation {
if c.activeInstallations == nil {
c.activeInstallations = make(map[string]*TestInstallation, 2)
}
installation := &TestInstallation{
// Create a reference to the TestCluster, and all of it's metadata
TestCluster: c,
// Maintain a reference to the Metadata used for this installation
Metadata: glooGatewayContext,
// ResourceClients are only available _after_ installing Gloo Gateway
ResourceClients: nil,
// Create an operations provider, and point it to the running installation
Actions: actions.NewActionsProvider().
WithClusterContext(c.ClusterContext).
WithGlooGatewayContext(glooGatewayContext),
// Create an assertions provider, and point it to the running installation
Assertions: assertions.NewProvider(t).
WithClusterContext(c.ClusterContext).
WithGlooGatewayContext(glooGatewayContext),
}
c.activeInstallations[installation.String()] = installation
return installation
}
func (c *TestCluster) UnregisterTestInstallation(installation *TestInstallation) {
delete(c.activeInstallations, installation.String())
}
// TestInstallation is the structure around a set of tests that validate behavior for an installation
// of Gloo Gateway.
type TestInstallation struct {
fmt.Stringer
// TestCluster contains the properties of the TestCluster this TestInstallation is a part of
TestCluster *TestCluster
// Metadata contains the properties used to install Gloo Gateway
Metadata *gloogateway.Context
// ResourceClients is a set of clients that can manipulate resources owned by Gloo Gateway
ResourceClients gloogateway.ResourceClients
// Actions is the entity that creates actions that can be executed by the Operator
Actions *actions.Provider
// Assertions is the entity that creates assertions that can be executed by the Operator
Assertions *assertions.Provider
}
func (i *TestInstallation) String() string {
return i.Metadata.InstallNamespace
}
func (i *TestInstallation) InstallGlooGateway(ctx context.Context, installFn func(ctx context.Context) error) {
if !testutils.ShouldSkipInstall() {
err := installFn(ctx)
i.Assertions.Require.NoError(err)
i.Assertions.EventuallyInstallationSucceeded(ctx)
}
// We can only create the ResourceClients after the CRDs exist in the Cluster
clients, err := gloogateway.NewResourceClients(ctx, i.TestCluster.ClusterContext)
i.Assertions.Require.NoError(err)
i.ResourceClients = clients
}
func (i *TestInstallation) UninstallGlooGateway(ctx context.Context, uninstallFn func(ctx context.Context) error) {
if testutils.ShouldSkipInstall() {
return
}
err := uninstallFn(ctx)
i.Assertions.Require.NoError(err)
i.Assertions.EventuallyUninstallationSucceeded(ctx)
}
// PreFailHandler is the function that is invoked if a test in the given TestInstallation fails
func (i *TestInstallation) PreFailHandler(_ context.Context) {
// Do nothing for now, we need to impelement `glooctl export report`
}