-
Notifications
You must be signed in to change notification settings - Fork 0
/
features_list.go
79 lines (68 loc) · 2.33 KB
/
features_list.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
package kubernetes_ctx
import (
"fmt"
"strings"
"github.com/thoas/go-funk"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/xunleii/godog-kubernetes/helpers"
)
// CountResources implements the GoDoc step
// - `Kubernetes has <NumberResources> <ApiGroupVersionKind>`
// It compare the current number of a specific resource with the given number.
func CountResources(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes has (\d+) (`+RxGroupVersionKind+`)$`,
func(n int, groupVersionKindStr string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
objs, err := ctx.List(groupVersionKind)
if err != nil {
return err
}
if len(objs) != n {
if len(objs) == 0 {
return fmt.Errorf("no %s found", groupVersionKindStr)
}
items := funk.Map(objs, func(obj *unstructured.Unstructured) string {
if obj.GetNamespace() == "" {
return obj.GetName()
}
return obj.GetNamespace() + "/" + obj.GetName()
})
return fmt.Errorf("%d %s found (%s)", len(objs), groupVersionKindStr, strings.Join(items.([]string), ","))
}
return nil
},
)
}
// CountNamespacedResources implements the GoDoc step
// - `Kubernetes has <NumberResources> <ApiGroupVersionKind> in namespace '<Namespace>'`
// It compare the current number of a specific resource with the given number.
func CountNamespacedResources(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes has (\d+) (`+RxGroupVersionKind+`) in namespace '(`+RxDNSChar+`+)'$`,
func(n int, groupVersionKindStr, namespace string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
objs, err := ctx.List(groupVersionKind, &client.ListOptions{Namespace: namespace})
if err != nil {
return err
}
if len(objs) != n {
if len(objs) == 0 {
return fmt.Errorf("no %s found in namespace '%s'", groupVersionKindStr, namespace)
}
items := funk.Map(objs, func(obj *unstructured.Unstructured) string {
return obj.GetNamespace() + "/" + obj.GetName()
})
return fmt.Errorf("%d %s found in namespace '%s' (%s)", len(objs), groupVersionKindStr, namespace, strings.Join(items.([]string), ","))
}
return nil
},
)
}