forked from weaveworks/weave-gitops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crd_test.go
99 lines (85 loc) · 2.34 KB
/
crd_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
package server_test
import (
"context"
"fmt"
"testing"
. "github.com/onsi/gomega"
pb "github.com/weaveworks/weave-gitops/pkg/api/core"
"github.com/weaveworks/weave-gitops/pkg/kube"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func TestIsAvailable(t *testing.T) {
g := NewGomegaWithT(t)
c := makeGRPCServer(k8sEnv.Rest, t)
scheme, err := kube.CreateScheme()
g.Expect(err).NotTo(HaveOccurred())
ctx := context.Background()
_, err = client.New(k8sEnv.Rest, client.Options{
Scheme: scheme,
})
g.Expect(err).NotTo(HaveOccurred())
k, err := kube.NewKubeHTTPClientWithConfig(k8sEnv.Rest, "")
g.Expect(err).NotTo(HaveOccurred())
resp, err := c.IsCRDAvailable(ctx, &pb.IsCRDAvailableRequest{Name: "customobjects.example.com"})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(resp.GetClusters()).To(HaveKey("Default"))
g.Expect(resp.GetClusters()["Default"]).To(BeFalse())
newCRD(ctx, g, k,
CRDInfo{
Singular: "customobject",
Group: "example.com",
Plural: "customobjects",
Kind: "CustomObject",
})
resp, err = c.IsCRDAvailable(ctx, &pb.IsCRDAvailableRequest{Name: "customobjects.example.com"})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(resp.GetClusters()).To(HaveKey("Default"))
g.Expect(resp.GetClusters()["Default"]).To(BeTrue())
}
type CRDInfo struct {
Group string
Plural string
Singular string
Kind string
NoTest bool
}
func newCRD(
ctx context.Context,
g *GomegaWithT,
k client.Client,
info CRDInfo,
) {
resource := extv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", info.Plural, info.Group),
},
Spec: extv1.CustomResourceDefinitionSpec{
Group: info.Group,
Names: extv1.CustomResourceDefinitionNames{
Plural: info.Plural,
Singular: info.Singular,
Kind: info.Kind,
},
Scope: "Namespaced",
Versions: []extv1.CustomResourceDefinitionVersion{
{
Name: "v1beta1",
Served: true,
Storage: true,
Schema: &extv1.CustomResourceValidation{
OpenAPIV3Schema: &extv1.JSONSchemaProps{
Type: "object",
Properties: map[string]extv1.JSONSchemaProps{},
},
},
},
},
},
}
err := k.Create(ctx, &resource)
if !info.NoTest {
g.Expect(err).NotTo(HaveOccurred())
}
}