-
Notifications
You must be signed in to change notification settings - Fork 0
/
features_create.go
107 lines (93 loc) · 3.59 KB
/
features_create.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
package kubernetes_ctx
import (
"io/ioutil"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/xunleii/godog-kubernetes/helpers"
)
// CreateSingleResource implements the GoDoc step
// - `Kubernetes must have <ApiGroupVersionKind> '<NamespacedName>'`
// - `Kubernetes creates a new <ApiGroupVersionKind> '<NamespacedName>'`
// It creates a new resource, without any specific fields.
func CreateSingleResource(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes (?:must have|creates a new) (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)'$`,
func(groupVersionKindStr, resourceName string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, _ := helpers.NamespacedNameFrom(resourceName)
return ctx.Create(groupVersionKind, namespacedName, &unstructured.Unstructured{})
},
)
}
// CreateSingleResourceWith implements the GoDoc step
// - `Kubernetes must have <ApiGroupVersionKind> '<NamespacedName>' with <YAML>`
// - `Kubernetes creates a new <ApiGroupVersionKind> '<NamespacedName>' with <YAML>`
// It creates a new resource, with the given definition.
func CreateSingleResourceWith(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes (?:must have|creates a new) (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' with$`,
func(groupVersionKindStr, resourceName string, yamlObj helpers.YamlDocString) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, _ := helpers.NamespacedNameFrom(resourceName)
obj, err := helpers.UnmarshalYamlDocString(yamlObj)
if err != nil {
return err
}
return ctx.Create(groupVersionKind, namespacedName, &unstructured.Unstructured{Object: obj})
},
)
}
// CreateSingleResourceFrom implements the GoDoc step
// - `Kubernetes must have <ApiGroupVersionKind> '<NamespacedName>' from <filename>`
// - `Kubernetes creates a new <ApiGroupVersionKind> '<NamespacedName>' from <filename>`
// It creates a new resource, with then definition available in the given filename.
func CreateSingleResourceFrom(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes (?:must have|creates a new) (`+RxGroupVersionKind+`) '(`+RxNamespacedName+`)' from (.+)$`,
func(groupVersionKindStr, resourceName, fileName string) error {
groupVersionKind, err := helpers.GroupVersionKindFrom(groupVersionKindStr)
if err != nil {
return err
}
namespacedName, _ := helpers.NamespacedNameFrom(resourceName)
data, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
var obj unstructured.Unstructured
err = yaml.Unmarshal(data, &obj.Object)
if err != nil {
return err
}
return ctx.Create(groupVersionKind, namespacedName, &obj)
},
)
}
// CreateMultiResources implements the GoDoc step
// - `Kubernetes must have the following resources <RESOURCES_TABLE>`
// - `Kubernetes creates the following resources <RESOURCES_TABLE>`
// It creates several resources in a row, without any specific fields (useful for Namespaces).
func CreateMultiResources(ctx *FeatureContext, s ScenarioContext) {
s.Step(
`^Kubernetes (?:must have|creates) the following resources$`,
func(table helpers.ResourceTable) error {
resources, err := helpers.UnmarshalResourceTable(table)
if err != nil {
return err
}
for _, resource := range resources {
err := ctx.Create(resource.GroupVersionKind(), resource.NamespacedName(), &unstructured.Unstructured{})
if err != nil {
return err
}
}
return nil
},
)
}