-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexternal.go
194 lines (163 loc) · 5.6 KB
/
external.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package external
import (
"flag"
"github.com/pkg/errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/wongma7/csi-certify/pkg/certify/utils"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/kubernetes/test/e2e/storage/testpatterns"
"k8s.io/kubernetes/test/e2e/storage/testsuites"
"io/ioutil"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
type DriverDefParameter struct {
}
var RunCustomTestDriver = true
var DriverDefParam DriverDefParameter
func init() {
flag.Var(&DriverDefParam, "driverdef", "name of a .yaml or .json file that defines a driver for storage testing, can be used more than once")
}
func (d DriverDefParameter) String() string {
return "<.yaml or .json file>"
}
func (d DriverDefParameter) Set(filename string) error {
RunCustomTestDriver = false
driver, err := d.loadDriverDefinition(filename)
if err != nil {
return err
}
if driver.DriverInfo.Name == "" {
return errors.Errorf("%q: DriverInfo.Name not set", filename)
}
description := "External Storage " + testsuites.GetDriverNameWithFeatureTags(driver)
Describe(description, func() {
testsuites.DefineTestSuite(driver, utils.CSITestSuites)
})
return nil
}
func (d DriverDefParameter) loadDriverDefinition(filename string) (*driverDefinition, error) {
if filename == "" {
return nil, errors.New("missing file name")
}
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
// Some reasonable defaults follow.
driver := &driverDefinition{
utils.DriverDefinition{
DriverInfo: testsuites.DriverInfo{
SupportedFsType: sets.NewString(
"", // Default fsType
),
},
ClaimSize: "5Gi",
},
}
// TODO: strict checking of the file content once https://github.com/kubernetes/kubernetes/pull/71589
// or something similar is merged.
if err := runtime.DecodeInto(legacyscheme.Codecs.UniversalDecoder(), data, &driver.DriverDefinition); err != nil {
return nil, errors.Wrap(err, filename)
}
return driver, nil
}
var _ testsuites.TestDriver = &driverDefinition{}
// We have to implement the interface because dynamic PV may or may
// not be supported. driverDefinition.SkipUnsupportedTest checks that
// based on the actual driver definition.
var _ testsuites.DynamicPVTestDriver = &driverDefinition{}
// Same for snapshotting.
var _ testsuites.SnapshottableTestDriver = &driverDefinition{}
// runtime.DecodeInto needs a runtime.Object but doesn't do any
// deserialization of it and therefore none of the methods below need
// an implementation.
var _ runtime.Object = &driverDefinition{}
type driverDefinition struct {
utils.DriverDefinition
}
func (d *driverDefinition) GetDriverInfo() *testsuites.DriverInfo {
return &d.DriverInfo
}
func (d *driverDefinition) SkipUnsupportedTest(pattern testpatterns.TestPattern) {
supported := false
// TODO (?): add support for more volume types
switch pattern.VolType {
case testpatterns.DynamicPV:
if d.StorageClass.FromName || d.StorageClass.FromFile != "" {
supported = true
}
}
if !supported {
framework.Skipf("Driver %q does not support volume type %q - skipping", d.DriverInfo.Name, pattern.VolType)
}
supported = false
switch pattern.SnapshotType {
case "":
supported = true
case testpatterns.DynamicCreatedSnapshot:
if d.SnapshotClass.FromName {
supported = true
}
}
if !supported {
framework.Skipf("Driver %q does not support snapshot type %q - skipping", d.DriverInfo.Name, pattern.SnapshotType)
}
}
func (d *driverDefinition) GetDynamicProvisionStorageClass(config *testsuites.PerTestConfig, fsType string) *storagev1.StorageClass {
f := config.Framework
if d.StorageClass.FromName {
provisioner := d.DriverInfo.Name
parameters := map[string]string{}
ns := f.Namespace.Name
suffix := provisioner + "-sc"
if fsType != "" {
parameters["csi.storage.k8s.io/fstype"] = fsType
}
return testsuites.GetStorageClass(provisioner, parameters, nil, ns, suffix)
}
items, err := f.LoadFromManifests(d.StorageClass.FromFile)
Expect(err).NotTo(HaveOccurred(), "load storage class from %s", d.StorageClass.FromFile)
Expect(len(items)).To(Equal(1), "exactly one item from %s", d.StorageClass.FromFile)
err = f.PatchItems(items...)
Expect(err).NotTo(HaveOccurred(), "patch items")
sc, ok := items[0].(*storagev1.StorageClass)
Expect(ok).To(BeTrue(), "storage class from %s", d.StorageClass.FromFile)
// Ensure that we can load more than once as required for
// GetDynamicProvisionStorageClass by adding a random suffix.
sc.Name = names.SimpleNameGenerator.GenerateName(sc.Name + "-")
if fsType != "" {
if sc.Parameters == nil {
sc.Parameters = map[string]string{}
}
sc.Parameters["csi.storage.k8s.io/fstype"] = fsType
}
return sc
}
func (d *driverDefinition) GetSnapshotClass(config *testsuites.PerTestConfig) *unstructured.Unstructured {
if !d.SnapshotClass.FromName {
framework.Skipf("Driver %q does not support snapshotting - skipping", d.DriverInfo.Name)
}
snapshotter := d.DriverInfo.Name
parameters := map[string]string{}
ns := config.Framework.Namespace.Name
suffix := snapshotter + "-vsc"
return testsuites.GetSnapshotClass(snapshotter, parameters, ns, suffix)
}
func (d *driverDefinition) GetClaimSize() string {
return d.ClaimSize
}
func (d *driverDefinition) PrepareTest(f *framework.Framework) (*testsuites.PerTestConfig, func()) {
config := &testsuites.PerTestConfig{
Driver: d,
Prefix: "external",
Framework: f,
ClientNodeName: d.ClientNodeName,
}
return config, func() {}
}