/
kube_test_context.go
84 lines (67 loc) · 1.76 KB
/
kube_test_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
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/
package testcommon
import (
"context"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/pkg/errors"
"github.com/Azure/azure-service-operator/v2/internal/config"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
)
type KubeGlobalContext struct {
TestContext
createBaseTestContext BaseTestContextFactory
cleanup context.CancelFunc
}
type BaseTestContextFactory func(PerTestContext, config.Values) (*KubeBaseTestContext, error)
func NewKubeContext(
useEnvTest bool,
recordReplay bool,
region string,
nameConfig *ResourceNameConfig,
) (KubeGlobalContext, error) {
var err error
var cbtc BaseTestContextFactory
var cleanup func() = func() {}
if useEnvTest {
cbtc, cleanup = createEnvtestContext()
} else {
cbtc, err = createRealKubeContext()
if err != nil {
return KubeGlobalContext{}, errors.Wrap(err, "unable to create real Kube context")
}
}
return KubeGlobalContext{
TestContext: NewTestContext(region, recordReplay, nameConfig),
createBaseTestContext: cbtc,
cleanup: cleanup,
}, nil
}
func (ctx KubeGlobalContext) Cleanup() error {
if ctx.cleanup != nil {
ctx.cleanup()
}
return nil
}
type KubeBaseTestContext struct {
PerTestContext
KubeConfig *rest.Config
}
func AsOwner(obj client.Object) *genruntime.KnownResourceReference {
return &genruntime.KnownResourceReference{
Name: obj.GetName(),
}
}
func AsKubernetesOwner(obj client.Object) *genruntime.KubernetesOwnerReference {
return &genruntime.KubernetesOwnerReference{
Name: obj.GetName(),
}
}
func AsARMIDOwner(id string) *genruntime.KnownResourceReference {
return &genruntime.KnownResourceReference{
ARMID: id,
}
}