nfs_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright 2022 The Rook Authors. All rights reserved.
  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 v1
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. v1 "k8s.io/api/core/v1"
  18. )
  19. func TestNFSSecuritySpec_Validate(t *testing.T) {
  20. isFailing := true
  21. isOkay := false
  22. withSSSD := func(sssd *SSSDSpec) *NFSSecuritySpec {
  23. return &NFSSecuritySpec{
  24. SSSD: sssd,
  25. }
  26. }
  27. configMapVolumeSource := &ConfigFileVolumeSource{
  28. ConfigMap: &v1.ConfigMapVolumeSource{},
  29. }
  30. tests := []struct {
  31. name string
  32. security *NFSSecuritySpec
  33. wantErr bool
  34. }{
  35. {"security = nil", nil, isOkay},
  36. {"security empty", &NFSSecuritySpec{}, isOkay},
  37. {"security.sssd empty", withSSSD(&SSSDSpec{}), isFailing},
  38. {"security.sssd.sidecar empty",
  39. withSSSD(&SSSDSpec{
  40. Sidecar: &SSSDSidecar{},
  41. }),
  42. isFailing},
  43. {"security.sssd.sidecar fully specified",
  44. withSSSD(&SSSDSpec{
  45. Sidecar: &SSSDSidecar{
  46. Image: "myimage",
  47. SSSDConfigFile: SSSDSidecarConfigFile{
  48. VolumeSource: configMapVolumeSource,
  49. },
  50. },
  51. }),
  52. isOkay},
  53. {"security.sssd.sidecar missing image",
  54. withSSSD(&SSSDSpec{
  55. Sidecar: &SSSDSidecar{
  56. Image: "",
  57. SSSDConfigFile: SSSDSidecarConfigFile{
  58. VolumeSource: configMapVolumeSource,
  59. },
  60. },
  61. }),
  62. isFailing},
  63. {"security.sssd.sidecar.sssdConfigFile empty",
  64. withSSSD(&SSSDSpec{
  65. Sidecar: &SSSDSidecar{
  66. Image: "myimage",
  67. SSSDConfigFile: SSSDSidecarConfigFile{},
  68. },
  69. }),
  70. isOkay},
  71. {"security.sssd.sidecar.sssdConfigFile.volumeSource empty",
  72. withSSSD(&SSSDSpec{
  73. Sidecar: &SSSDSidecar{
  74. Image: "myimage",
  75. SSSDConfigFile: SSSDSidecarConfigFile{
  76. VolumeSource: &ConfigFileVolumeSource{},
  77. },
  78. },
  79. }),
  80. isFailing},
  81. {"security.sssd.sidecar.additionalFiles empty",
  82. withSSSD(&SSSDSpec{
  83. Sidecar: &SSSDSidecar{
  84. Image: "myimage",
  85. AdditionalFiles: []SSSDSidecarAdditionalFile{},
  86. },
  87. }),
  88. isOkay},
  89. {"security.sssd.sidecar.additionalFiles multiple valid",
  90. withSSSD(&SSSDSpec{
  91. Sidecar: &SSSDSidecar{
  92. Image: "myimage",
  93. AdditionalFiles: []SSSDSidecarAdditionalFile{
  94. {SubPath: "one", VolumeSource: configMapVolumeSource},
  95. {SubPath: "two", VolumeSource: configMapVolumeSource},
  96. {SubPath: "three", VolumeSource: configMapVolumeSource},
  97. },
  98. },
  99. }),
  100. isOkay},
  101. {"security.sssd.sidecar.additionalFiles one empty subDir",
  102. withSSSD(&SSSDSpec{
  103. Sidecar: &SSSDSidecar{
  104. Image: "myimage",
  105. AdditionalFiles: []SSSDSidecarAdditionalFile{
  106. {SubPath: "one", VolumeSource: configMapVolumeSource},
  107. {SubPath: "", VolumeSource: configMapVolumeSource},
  108. {SubPath: "three", VolumeSource: configMapVolumeSource},
  109. },
  110. },
  111. }),
  112. isFailing},
  113. {"security.sssd.sidecar.additionalFiles duplicate subDirs",
  114. withSSSD(&SSSDSpec{
  115. Sidecar: &SSSDSidecar{
  116. Image: "myimage",
  117. AdditionalFiles: []SSSDSidecarAdditionalFile{
  118. {SubPath: "one", VolumeSource: configMapVolumeSource},
  119. {SubPath: "two", VolumeSource: configMapVolumeSource},
  120. {SubPath: "one", VolumeSource: configMapVolumeSource},
  121. },
  122. },
  123. }),
  124. isFailing},
  125. {"security.sssd.sidecar.additionalFiles one vol source empty",
  126. withSSSD(&SSSDSpec{
  127. Sidecar: &SSSDSidecar{
  128. Image: "myimage",
  129. AdditionalFiles: []SSSDSidecarAdditionalFile{
  130. {SubPath: "one", VolumeSource: configMapVolumeSource},
  131. {SubPath: "", VolumeSource: &ConfigFileVolumeSource{}},
  132. {SubPath: "three", VolumeSource: configMapVolumeSource},
  133. },
  134. },
  135. }),
  136. isFailing},
  137. }
  138. for _, tt := range tests {
  139. t.Run(tt.name, func(t *testing.T) {
  140. if err := tt.security.Validate(); (err != nil) != tt.wantErr {
  141. t.Errorf("NFSSecuritySpec.Validate() error = %v, wantErr %v", err, tt.wantErr)
  142. }
  143. })
  144. }
  145. }
  146. func TestNFSSecuritySpec_KerberosEnabled(t *testing.T) {
  147. t.Run("nil security spec", func(t *testing.T) {
  148. var sec *NFSSecuritySpec
  149. assert.False(t, sec.KerberosEnabled())
  150. })
  151. t.Run("empty security spec", func(t *testing.T) {
  152. sec := &NFSSecuritySpec{}
  153. assert.False(t, sec.KerberosEnabled())
  154. })
  155. t.Run("empty kerberos spec", func(t *testing.T) {
  156. sec := &NFSSecuritySpec{
  157. Kerberos: &KerberosSpec{},
  158. }
  159. assert.True(t, sec.KerberosEnabled())
  160. })
  161. t.Run("filled in kerberos spec", func(t *testing.T) {
  162. sec := &NFSSecuritySpec{
  163. Kerberos: &KerberosSpec{
  164. PrincipalName: "mom",
  165. },
  166. }
  167. assert.True(t, sec.KerberosEnabled())
  168. })
  169. }
  170. func TestKerberosSpec_GetPrincipalName(t *testing.T) {
  171. t.Run("empty kerberos spec", func(t *testing.T) {
  172. k := &KerberosSpec{}
  173. assert.Equal(t, "nfs", k.GetPrincipalName())
  174. })
  175. t.Run("principal name nfs", func(t *testing.T) {
  176. k := &KerberosSpec{
  177. PrincipalName: "nfs",
  178. }
  179. assert.Equal(t, "nfs", k.GetPrincipalName())
  180. })
  181. t.Run("principal name set", func(t *testing.T) {
  182. k := &KerberosSpec{
  183. PrincipalName: "set",
  184. }
  185. assert.Equal(t, "set", k.GetPrincipalName())
  186. })
  187. }