operation_generator_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright 2019 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 operationexecutor
  14. import (
  15. "github.com/prometheus/client_model/go"
  16. "github.com/stretchr/testify/assert"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. "k8s.io/apimachinery/pkg/util/uuid"
  21. fakeclient "k8s.io/client-go/kubernetes/fake"
  22. "k8s.io/client-go/tools/record"
  23. "k8s.io/component-base/metrics/legacyregistry"
  24. "k8s.io/csi-translation-lib/plugins"
  25. "k8s.io/kubernetes/pkg/volume"
  26. "k8s.io/kubernetes/pkg/volume/awsebs"
  27. csitesting "k8s.io/kubernetes/pkg/volume/csi/testing"
  28. "k8s.io/kubernetes/pkg/volume/gcepd"
  29. volumetesting "k8s.io/kubernetes/pkg/volume/testing"
  30. "os"
  31. "testing"
  32. )
  33. // this method just tests the volume plugin name that's used in CompleteFunc, the same plugin is also used inside the
  34. // generated func so there is no need to test the plugin name that's used inside generated function
  35. func TestOperationGenerator_GenerateUnmapVolumeFunc_PluginName(t *testing.T) {
  36. type testcase struct {
  37. name string
  38. pluginName string
  39. pvSpec v1.PersistentVolumeSpec
  40. probVolumePlugins []volume.VolumePlugin
  41. }
  42. testcases := []testcase{
  43. {
  44. name: "gce pd plugin: csi migration disabled",
  45. pluginName: plugins.GCEPDInTreePluginName,
  46. pvSpec: v1.PersistentVolumeSpec{
  47. PersistentVolumeSource: v1.PersistentVolumeSource{
  48. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{},
  49. }},
  50. probVolumePlugins: gcepd.ProbeVolumePlugins(),
  51. },
  52. {
  53. name: "aws ebs plugin: csi migration disabled",
  54. pluginName: plugins.AWSEBSInTreePluginName,
  55. pvSpec: v1.PersistentVolumeSpec{
  56. PersistentVolumeSource: v1.PersistentVolumeSource{
  57. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{},
  58. }},
  59. probVolumePlugins: awsebs.ProbeVolumePlugins(),
  60. },
  61. }
  62. for _, tc := range testcases {
  63. expectedPluginName := tc.pluginName
  64. volumePluginMgr, tmpDir := initTestPlugins(t, tc.probVolumePlugins, tc.pluginName)
  65. defer os.RemoveAll(tmpDir)
  66. operationGenerator := getTestOperationGenerator(volumePluginMgr)
  67. pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID(string(uuid.NewUUID()))}}
  68. volumeToUnmount := getTestVolumeToUnmount(pod, tc.pvSpec, tc.pluginName)
  69. unmapVolumeFunc, e := operationGenerator.GenerateUnmapVolumeFunc(volumeToUnmount, nil)
  70. if e != nil {
  71. t.Fatalf("Error occurred while generating unmapVolumeFunc: %v", e)
  72. }
  73. metricFamilyName := "storage_operation_status_count"
  74. labelFilter := map[string]string{
  75. "status": "success",
  76. "operation_name": "unmap_volume",
  77. "volume_plugin": expectedPluginName,
  78. }
  79. // compare the relative change of the metric because of the global state of the prometheus.DefaultGatherer.Gather()
  80. storageOperationStatusCountMetricBefore := findMetricWithNameAndLabels(metricFamilyName, labelFilter)
  81. var ee error
  82. unmapVolumeFunc.CompleteFunc(&ee)
  83. storageOperationStatusCountMetricAfter := findMetricWithNameAndLabels(metricFamilyName, labelFilter)
  84. if storageOperationStatusCountMetricAfter == nil {
  85. t.Fatalf("Couldn't find the metric with name(%s) and labels(%v)", metricFamilyName, labelFilter)
  86. }
  87. if storageOperationStatusCountMetricBefore == nil {
  88. assert.Equal(t, float64(1), *storageOperationStatusCountMetricAfter.Counter.Value, tc.name)
  89. } else {
  90. metricValueDiff := *storageOperationStatusCountMetricAfter.Counter.Value - *storageOperationStatusCountMetricBefore.Counter.Value
  91. assert.Equal(t, float64(1), metricValueDiff, tc.name)
  92. }
  93. }
  94. }
  95. func findMetricWithNameAndLabels(metricFamilyName string, labelFilter map[string]string) *io_prometheus_client.Metric {
  96. metricFamily := getMetricFamily(metricFamilyName)
  97. if metricFamily == nil {
  98. return nil
  99. }
  100. for _, metric := range metricFamily.GetMetric() {
  101. if isLabelsMatchWithMetric(labelFilter, metric) {
  102. return metric
  103. }
  104. }
  105. return nil
  106. }
  107. func isLabelsMatchWithMetric(labelFilter map[string]string, metric *io_prometheus_client.Metric) bool {
  108. if len(labelFilter) != len(metric.Label) {
  109. return false
  110. }
  111. for labelName, labelValue := range labelFilter {
  112. labelFound := false
  113. for _, labelPair := range metric.Label {
  114. if labelName == *labelPair.Name && labelValue == *labelPair.Value {
  115. labelFound = true
  116. break
  117. }
  118. }
  119. if !labelFound {
  120. return false
  121. }
  122. }
  123. return true
  124. }
  125. func getTestOperationGenerator(volumePluginMgr *volume.VolumePluginMgr) OperationGenerator {
  126. fakeKubeClient := fakeclient.NewSimpleClientset()
  127. fakeRecorder := &record.FakeRecorder{}
  128. fakeHandler := volumetesting.NewBlockVolumePathHandler()
  129. operationGenerator := NewOperationGenerator(
  130. fakeKubeClient,
  131. volumePluginMgr,
  132. fakeRecorder,
  133. false,
  134. fakeHandler)
  135. return operationGenerator
  136. }
  137. func getTestVolumeToUnmount(pod *v1.Pod, pvSpec v1.PersistentVolumeSpec, pluginName string) MountedVolume {
  138. volumeSpec := &volume.Spec{
  139. PersistentVolume: &v1.PersistentVolume{
  140. Spec: pvSpec,
  141. },
  142. }
  143. volumeToUnmount := MountedVolume{
  144. VolumeName: v1.UniqueVolumeName("pd-volume"),
  145. PodUID: pod.UID,
  146. PluginName: pluginName,
  147. VolumeSpec: volumeSpec,
  148. }
  149. return volumeToUnmount
  150. }
  151. func getMetricFamily(metricFamilyName string) *io_prometheus_client.MetricFamily {
  152. metricFamilies, _ := legacyregistry.DefaultGatherer.Gather()
  153. for _, mf := range metricFamilies {
  154. if *mf.Name == metricFamilyName {
  155. return mf
  156. }
  157. }
  158. return nil
  159. }
  160. func initTestPlugins(t *testing.T, plugs []volume.VolumePlugin, pluginName string) (*volume.VolumePluginMgr, string) {
  161. client := fakeclient.NewSimpleClientset()
  162. pluginMgr, _, tmpDir := csitesting.NewTestPlugin(t, client)
  163. err := pluginMgr.InitPlugins(plugs, nil, pluginMgr.Host)
  164. if err != nil {
  165. t.Fatalf("Can't init volume plugins: %v", err)
  166. }
  167. _, e := pluginMgr.FindPluginByName(pluginName)
  168. if e != nil {
  169. t.Fatalf("Can't find the plugin by name: %s", pluginName)
  170. }
  171. return pluginMgr, tmpDir
  172. }