conversion_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 v1alpha1
  14. import (
  15. "testing"
  16. conversion "k8s.io/apimachinery/pkg/conversion"
  17. componentbaseconfig "k8s.io/component-base/config"
  18. componentbaseconfigv1alpha1 "k8s.io/component-base/config/v1alpha1"
  19. v1alpha1 "k8s.io/kube-scheduler/config/v1alpha1"
  20. config "k8s.io/kubernetes/pkg/scheduler/apis/config"
  21. )
  22. func TestV1alpha1ToConfigKubeSchedulerLeaderElectionConfiguration(t *testing.T) {
  23. configuration := &v1alpha1.KubeSchedulerLeaderElectionConfiguration{
  24. LockObjectName: "name",
  25. LockObjectNamespace: "namespace",
  26. LeaderElectionConfiguration: componentbaseconfigv1alpha1.LeaderElectionConfiguration{
  27. ResourceName: "name",
  28. ResourceNamespace: "namespace",
  29. },
  30. }
  31. emptyLockObjectNameConfig := configuration.DeepCopy()
  32. emptyLockObjectNameConfig.LockObjectName = ""
  33. emptyLockObjectNamespaceConfig := configuration.DeepCopy()
  34. emptyLockObjectNamespaceConfig.LockObjectNamespace = ""
  35. emptyResourceNameConfig := configuration.DeepCopy()
  36. emptyResourceNameConfig.ResourceName = ""
  37. emptyResourceNamespaceConfig := configuration.DeepCopy()
  38. emptyResourceNamespaceConfig.ResourceNamespace = ""
  39. differentNameConfig := configuration.DeepCopy()
  40. differentNameConfig.LockObjectName = "name1"
  41. differentNamespaceConfig := configuration.DeepCopy()
  42. differentNamespaceConfig.LockObjectNamespace = "namespace1"
  43. emptyconfig := &v1alpha1.KubeSchedulerLeaderElectionConfiguration{}
  44. scenarios := map[string]struct {
  45. expectedResourceNamespace string
  46. expectedResourceName string
  47. expectedToFailed bool
  48. config *v1alpha1.KubeSchedulerLeaderElectionConfiguration
  49. }{
  50. "both-set-same-name-and-namespace": {
  51. expectedResourceNamespace: "namespace",
  52. expectedResourceName: "name",
  53. expectedToFailed: false,
  54. config: configuration,
  55. },
  56. "not-set-lock-object-name": {
  57. expectedResourceNamespace: "namespace",
  58. expectedResourceName: "name",
  59. expectedToFailed: false,
  60. config: emptyLockObjectNameConfig,
  61. },
  62. "not-set-lock-object-namespace": {
  63. expectedResourceNamespace: "namespace",
  64. expectedResourceName: "name",
  65. expectedToFailed: false,
  66. config: emptyLockObjectNamespaceConfig,
  67. },
  68. "not-set-resource-name": {
  69. expectedResourceNamespace: "namespace",
  70. expectedResourceName: "name",
  71. expectedToFailed: false,
  72. config: emptyResourceNameConfig,
  73. },
  74. "not-set-resource-namespace": {
  75. expectedResourceNamespace: "namespace",
  76. expectedResourceName: "name",
  77. expectedToFailed: false,
  78. config: emptyResourceNamespaceConfig,
  79. },
  80. "set-different-name": {
  81. expectedResourceNamespace: "",
  82. expectedResourceName: "",
  83. expectedToFailed: true,
  84. config: differentNameConfig,
  85. },
  86. "set-different-namespace": {
  87. expectedResourceNamespace: "",
  88. expectedResourceName: "",
  89. expectedToFailed: true,
  90. config: differentNamespaceConfig,
  91. },
  92. "set-empty-name-and-namespace": {
  93. expectedResourceNamespace: "",
  94. expectedResourceName: "",
  95. expectedToFailed: false,
  96. config: emptyconfig,
  97. },
  98. }
  99. for name, scenario := range scenarios {
  100. out := &config.KubeSchedulerLeaderElectionConfiguration{}
  101. s := conversion.Scope(nil)
  102. err := Convert_v1alpha1_KubeSchedulerLeaderElectionConfiguration_To_config_KubeSchedulerLeaderElectionConfiguration(scenario.config, out, s)
  103. if err == nil && scenario.expectedToFailed {
  104. t.Errorf("Unexpected success for scenario: %s", name)
  105. }
  106. if err == nil && !scenario.expectedToFailed {
  107. if out.ResourceName != scenario.expectedResourceName {
  108. t.Errorf("Unexpected success for scenario: %s, out.ResourceName: %s, expectedResourceName: %s", name, out.ResourceName, scenario.expectedResourceName)
  109. }
  110. if out.ResourceNamespace != scenario.expectedResourceNamespace {
  111. t.Errorf("Unexpected success for scenario: %s, out.ResourceNamespace: %s, expectedResourceNamespace: %s", name, out.ResourceNamespace, scenario.expectedResourceNamespace)
  112. }
  113. }
  114. if err != nil && !scenario.expectedToFailed {
  115. t.Errorf("Unexpected failure for scenario: %s - %+v", name, err)
  116. }
  117. }
  118. }
  119. func TestConfigToV1alpha1KubeSchedulerLeaderElectionConfiguration(t *testing.T) {
  120. configuration := &config.KubeSchedulerLeaderElectionConfiguration{
  121. LeaderElectionConfiguration: componentbaseconfig.LeaderElectionConfiguration{
  122. ResourceName: "name",
  123. ResourceNamespace: "namespace",
  124. },
  125. }
  126. emptyconfig := &config.KubeSchedulerLeaderElectionConfiguration{}
  127. scenarios := map[string]struct {
  128. expectedResourceNamespace string
  129. expectedResourceName string
  130. expectedLockObjectNamespace string
  131. expectedLockObjectName string
  132. expectedToFailed bool
  133. config *config.KubeSchedulerLeaderElectionConfiguration
  134. }{
  135. "both-set-name-and-namespace": {
  136. expectedResourceNamespace: "namespace",
  137. expectedResourceName: "name",
  138. expectedLockObjectNamespace: "namespace",
  139. expectedLockObjectName: "name",
  140. expectedToFailed: false,
  141. config: configuration,
  142. },
  143. "set-empty-name-and-namespace": {
  144. expectedResourceNamespace: "",
  145. expectedResourceName: "",
  146. expectedLockObjectNamespace: "",
  147. expectedLockObjectName: "",
  148. expectedToFailed: false,
  149. config: emptyconfig,
  150. },
  151. }
  152. for name, scenario := range scenarios {
  153. out := &v1alpha1.KubeSchedulerLeaderElectionConfiguration{}
  154. s := conversion.Scope(nil)
  155. err := Convert_config_KubeSchedulerLeaderElectionConfiguration_To_v1alpha1_KubeSchedulerLeaderElectionConfiguration(scenario.config, out, s)
  156. if err == nil && scenario.expectedToFailed {
  157. t.Errorf("Unexpected success for scenario: %s", name)
  158. }
  159. if err == nil && !scenario.expectedToFailed {
  160. if out.ResourceName != scenario.expectedResourceName {
  161. t.Errorf("Unexpected success for scenario: %s, out.ResourceName: %s, expectedResourceName: %s", name, out.ResourceName, scenario.expectedResourceName)
  162. }
  163. if out.LockObjectName != scenario.expectedLockObjectName {
  164. t.Errorf("Unexpected success for scenario: %s, out.LockObjectName: %s, expectedLockObjectName: %s", name, out.LockObjectName, scenario.expectedLockObjectName)
  165. }
  166. if out.ResourceNamespace != scenario.expectedResourceNamespace {
  167. t.Errorf("Unexpected success for scenario: %s, out.ResourceNamespace: %s, expectedResourceNamespace: %s", name, out.ResourceNamespace, scenario.expectedResourceNamespace)
  168. }
  169. if out.LockObjectNamespace != scenario.expectedLockObjectNamespace {
  170. t.Errorf("Unexpected success for scenario: %s, out.LockObjectNamespace: %s, expectedLockObjectNamespace: %s", name, out.LockObjectNamespace, scenario.expectedLockObjectNamespace)
  171. }
  172. }
  173. if err != nil && !scenario.expectedToFailed {
  174. t.Errorf("Unexpected failure for scenario: %s - %+v", name, err)
  175. }
  176. }
  177. }