testdriver.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testsuites
  14. import (
  15. v1 "k8s.io/api/core/v1"
  16. storagev1 "k8s.io/api/storage/v1"
  17. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  21. "k8s.io/kubernetes/test/e2e/framework/volume"
  22. "k8s.io/kubernetes/test/e2e/storage/testpatterns"
  23. )
  24. // TestDriver represents an interface for a driver to be tested in TestSuite.
  25. // Except for GetDriverInfo, all methods will be called at test runtime and thus
  26. // can use e2eskipper.Skipf, framework.Fatal, Gomega assertions, etc.
  27. type TestDriver interface {
  28. // GetDriverInfo returns DriverInfo for the TestDriver. This must be static
  29. // information.
  30. GetDriverInfo() *DriverInfo
  31. // SkipUnsupportedTest skips test if Testpattern is not
  32. // suitable to test with the TestDriver. It gets called after
  33. // parsing parameters of the test suite and before the
  34. // framework is initialized. Cheap tests that just check
  35. // parameters like the cloud provider can and should be
  36. // done in SkipUnsupportedTest to avoid setting up more
  37. // expensive resources like framework.Framework. Tests that
  38. // depend on a connection to the cluster can be done in
  39. // PrepareTest once the framework is ready.
  40. SkipUnsupportedTest(testpatterns.TestPattern)
  41. // PrepareTest is called at test execution time each time a new test case is about to start.
  42. // It sets up all necessary resources and returns the per-test configuration
  43. // plus a cleanup function that frees all allocated resources.
  44. PrepareTest(f *framework.Framework) (*PerTestConfig, func())
  45. }
  46. // TestVolume is the result of PreprovisionedVolumeTestDriver.CreateVolume.
  47. // The only common functionality is to delete it. Individual driver interfaces
  48. // have additional methods that work with volumes created by them.
  49. type TestVolume interface {
  50. DeleteVolume()
  51. }
  52. // PreprovisionedVolumeTestDriver represents an interface for a TestDriver that has pre-provisioned volume
  53. type PreprovisionedVolumeTestDriver interface {
  54. TestDriver
  55. // CreateVolume creates a pre-provisioned volume of the desired volume type.
  56. CreateVolume(config *PerTestConfig, volumeType testpatterns.TestVolType) TestVolume
  57. }
  58. // InlineVolumeTestDriver represents an interface for a TestDriver that supports InlineVolume
  59. type InlineVolumeTestDriver interface {
  60. PreprovisionedVolumeTestDriver
  61. // GetVolumeSource returns a volumeSource for inline volume.
  62. // It will set readOnly and fsType to the volumeSource, if TestDriver supports both of them.
  63. // It will return nil, if the TestDriver doesn't support either of the parameters.
  64. GetVolumeSource(readOnly bool, fsType string, testVolume TestVolume) *v1.VolumeSource
  65. }
  66. // PreprovisionedPVTestDriver represents an interface for a TestDriver that supports PreprovisionedPV
  67. type PreprovisionedPVTestDriver interface {
  68. PreprovisionedVolumeTestDriver
  69. // GetPersistentVolumeSource returns a PersistentVolumeSource with volume node affinity for pre-provisioned Persistent Volume.
  70. // It will set readOnly and fsType to the PersistentVolumeSource, if TestDriver supports both of them.
  71. // It will return nil, if the TestDriver doesn't support either of the parameters.
  72. GetPersistentVolumeSource(readOnly bool, fsType string, testVolume TestVolume) (*v1.PersistentVolumeSource, *v1.VolumeNodeAffinity)
  73. }
  74. // DynamicPVTestDriver represents an interface for a TestDriver that supports DynamicPV
  75. type DynamicPVTestDriver interface {
  76. TestDriver
  77. // GetDynamicProvisionStorageClass returns a StorageClass dynamic provision Persistent Volume.
  78. // The StorageClass must be created in the current test's namespace and have
  79. // a unique name inside that namespace because GetDynamicProvisionStorageClass might
  80. // be called more than once per test.
  81. // It will set fsType to the StorageClass, if TestDriver supports it.
  82. // It will return nil, if the TestDriver doesn't support it.
  83. GetDynamicProvisionStorageClass(config *PerTestConfig, fsType string) *storagev1.StorageClass
  84. }
  85. // EphemeralTestDriver represents an interface for a TestDriver that supports ephemeral inline volumes.
  86. type EphemeralTestDriver interface {
  87. TestDriver
  88. // GetVolume returns the volume attributes for a certain
  89. // inline ephemeral volume, enumerated starting with #0. Some
  90. // tests might require more than one volume. They can all be
  91. // the same or different, depending what the driver supports
  92. // and/or wants to test.
  93. //
  94. // For each volume, the test driver can return volume attributes,
  95. // whether the resulting volume is shared between different pods (i.e.
  96. // changes made in one pod are visible in another), and whether the
  97. // volume can be mounted read/write or only read-only.
  98. GetVolume(config *PerTestConfig, volumeNumber int) (attributes map[string]string, shared bool, readOnly bool)
  99. // GetCSIDriverName returns the name that was used when registering with
  100. // kubelet. Depending on how the driver was deployed, this can be different
  101. // from DriverInfo.Name. Starting with Kubernetes 1.16, there must also
  102. // be a CSIDriver object under the same name with a "mode" field that enables
  103. // usage of the driver for ephemeral inline volumes.
  104. GetCSIDriverName(config *PerTestConfig) string
  105. }
  106. // SnapshottableTestDriver represents an interface for a TestDriver that supports DynamicSnapshot
  107. type SnapshottableTestDriver interface {
  108. TestDriver
  109. // GetSnapshotClass returns a SnapshotClass to create snapshot.
  110. // It will return nil, if the TestDriver doesn't support it.
  111. GetSnapshotClass(config *PerTestConfig) *unstructured.Unstructured
  112. }
  113. // Capability represents a feature that a volume plugin supports
  114. type Capability string
  115. // Constants related to capability
  116. const (
  117. CapPersistence Capability = "persistence" // data is persisted across pod restarts
  118. CapBlock Capability = "block" // raw block mode
  119. CapFsGroup Capability = "fsGroup" // volume ownership via fsGroup
  120. CapExec Capability = "exec" // exec a file in the volume
  121. CapSnapshotDataSource Capability = "snapshotDataSource" // support populate data from snapshot
  122. CapPVCDataSource Capability = "pvcDataSource" // support populate data from pvc
  123. // multiple pods on a node can use the same volume concurrently;
  124. // for CSI, see:
  125. // - https://github.com/container-storage-interface/spec/pull/150
  126. // - https://github.com/container-storage-interface/spec/issues/178
  127. // - NodeStageVolume in the spec
  128. CapMultiPODs Capability = "multipods"
  129. CapRWX Capability = "RWX" // support ReadWriteMany access modes
  130. CapControllerExpansion Capability = "controllerExpansion" // support volume expansion for controller
  131. CapNodeExpansion Capability = "nodeExpansion" // support volume expansion for node
  132. CapVolumeLimits Capability = "volumeLimits" // support volume limits (can be *very* slow)
  133. CapSingleNodeVolume Capability = "singleNodeVolume" // support volume that can run on single node (like hostpath)
  134. CapTopology Capability = "topology" // support topology
  135. )
  136. // DriverInfo represents static information about a TestDriver.
  137. type DriverInfo struct {
  138. // Internal name of the driver, this is used as a display name in the test
  139. // case and test objects
  140. Name string
  141. // Fully qualified plugin name as registered in Kubernetes of the in-tree
  142. // plugin if it exists and is empty if this DriverInfo represents a CSI
  143. // Driver
  144. InTreePluginName string
  145. FeatureTag string // FeatureTag for the driver
  146. // Maximum single file size supported by this driver
  147. MaxFileSize int64
  148. // The range of disk size supported by this driver
  149. SupportedSizeRange volume.SizeRange
  150. // Map of string for supported fs type
  151. SupportedFsType sets.String
  152. // Map of string for supported mount option
  153. SupportedMountOption sets.String
  154. // [Optional] Map of string for required mount option
  155. RequiredMountOption sets.String
  156. // Map that represents plugin capabilities
  157. Capabilities map[Capability]bool
  158. // [Optional] List of access modes required for provisioning, defaults to
  159. // RWO if unset
  160. RequiredAccessModes []v1.PersistentVolumeAccessMode
  161. // [Optional] List of topology keys driver supports
  162. TopologyKeys []string
  163. // [Optional] Number of allowed topologies the driver requires.
  164. // Only relevant if TopologyKeys is set. Defaults to 1.
  165. // Example: multi-zonal disk requires at least 2 allowed topologies.
  166. NumAllowedTopologies int
  167. }
  168. // PerTestConfig represents parameters that control test execution.
  169. // One instance gets allocated for each test and is then passed
  170. // via pointer to functions involved in the test.
  171. type PerTestConfig struct {
  172. // The test driver for the test.
  173. Driver TestDriver
  174. // Some short word that gets inserted into dynamically
  175. // generated entities (pods, paths) as first part of the name
  176. // to make debugging easier. Can be the same for different
  177. // tests inside the test suite.
  178. Prefix string
  179. // The framework instance allocated for the current test.
  180. Framework *framework.Framework
  181. // If non-empty, Pods using a volume will be scheduled
  182. // according to the NodeSelection. Otherwise Kubernetes will
  183. // pick a node.
  184. ClientNodeSelection e2epod.NodeSelection
  185. // Some test drivers initialize a storage server. This is
  186. // the configuration that then has to be used to run tests.
  187. // The values above are ignored for such tests.
  188. ServerConfig *volume.TestConfig
  189. }
  190. // GetUniqueDriverName returns unique driver name that can be used parallelly in tests
  191. func (config *PerTestConfig) GetUniqueDriverName() string {
  192. return config.Driver.GetDriverInfo().Name + "-" + config.Framework.UniqueName
  193. }