-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
137 lines (115 loc) · 3.69 KB
/
context.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
package kubernetes_ctx
import (
"context"
"fmt"
"github.com/cucumber/godog"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
RxDNSChar = `[a-z0-9\-.]`
RxGroupVersionKind = `[\w/]+`
RxNamespacedName = RxDNSChar + `+(?:/` + RxDNSChar + `+)?`
RxFieldPath = `[^=:]+?`
)
type (
// FeatureContext extends GoDoc in order to implement step definitions
// for Kubernetes.
FeatureContext struct {
ctx context.Context
scheme Scheme
client client.Client
gc func(*FeatureContext, *unstructured.Unstructured) error
}
// FeatureContextOption is some configuration that modifies options for
// the Kubernetes feature context.
FeatureContextOption interface {
ApplyToFeatureContext(*FeatureContext)
}
// Scheme abstracts the implementation of common operations on objects.
Scheme interface {
runtime.ObjectCreater
runtime.ObjectTyper
}
)
// NewFeatureContext returns a new instance of the Kubernetes feature context,
// with all step injected. If you want to choose which steps must be enabled,
// use NewEmptyFeatureContext instead.
func NewFeatureContext(s ScenarioContext, opts ...FeatureContextOption) (*FeatureContext, error) {
ctx, err := NewEmptyFeatureContext(s, opts...)
if err != nil {
return nil, err
}
CreateSingleResource(ctx, s)
CreateSingleResourceWith(ctx, s)
CreateSingleResourceFrom(ctx, s)
CreateMultiResources(ctx, s)
ResourceExists(ctx, s)
ResourceNotExists(ctx, s)
ResourceIsSimilarTo(ctx, s)
ResourceIsNotSimilarTo(ctx, s)
ResourceIsEqualTo(ctx, s)
ResourceIsNotEqualTo(ctx, s)
ResourceHasField(ctx, s)
ResourceDoesntHaveField(ctx, s)
ResourceHasFieldEqual(ctx, s)
ResourceHasFieldNotEqual(ctx, s)
ResourceHasLabel(ctx, s)
ResourceDoesntHaveLabel(ctx, s)
ResourceHasLabelEqual(ctx, s)
ResourceHasLabelNotEqual(ctx, s)
ResourceHasAnnotation(ctx, s)
ResourceDoesntHaveAnnotation(ctx, s)
ResourceHasAnnotationEqual(ctx, s)
ResourceHasAnnotationNotEqual(ctx, s)
CountResources(ctx, s)
CountNamespacedResources(ctx, s)
PatchResourceWith(ctx, s)
LabelizeResource(ctx, s)
RemoveResourceLabel(ctx, s)
UpdateResourceLabel(ctx, s)
AnnotateResource(ctx, s)
RemoveResourceAnnotation(ctx, s)
UpdateResourceAnnotation(ctx, s)
RemoveResource(ctx, s)
RemoveMultiResource(ctx, s)
return ctx, nil
}
// NewEmptyFeatureContext returns a new instance of the Kubernetes feature context,
// without any step injected.
func NewEmptyFeatureContext(s ScenarioContext, opts ...FeatureContextOption) (*FeatureContext, error) {
// preflight checks
dummy := &FeatureContext{}
for _, opt := range opts {
opt.ApplyToFeatureContext(dummy)
}
switch {
case dummy.client == nil:
return nil, fmt.Errorf("kubernetes client must be instanciated")
}
ctx := &FeatureContext{ctx: context.TODO()}
s.BeforeScenario(func(*godog.Scenario) {
for _, opt := range opts {
opt.ApplyToFeatureContext(ctx)
}
})
return ctx, nil
}
// Client returns the Kubernetes client used by the FeatureContext.
func (ctx FeatureContext) Client() client.Client { return ctx.client }
// Scheme returns the Kubernetes scheme used by the FeatureContext.
func (ctx FeatureContext) Scheme() Scheme { return ctx.scheme }
// GoContext returns the golang context used by the FeatureContext.
func (ctx FeatureContext) GoContext() context.Context { return ctx.ctx }
// GarbageCollector returns the garbage collector implementation used
// by the FeatureContext.
func (ctx FeatureContext) GarbageCollector() func(*FeatureContext, *unstructured.Unstructured) error {
return ctx.gc
}
func (ctx *FeatureContext) callGC(obj *unstructured.Unstructured) error {
if ctx.gc == nil {
return nil
}
return ctx.gc(ctx, obj)
}