object_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2021 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. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. )
  19. func TestValidateObjectStoreSpec(t *testing.T) {
  20. o := &CephObjectStore{
  21. ObjectMeta: metav1.ObjectMeta{
  22. Name: "my-store",
  23. Namespace: "rook-ceph",
  24. },
  25. Spec: ObjectStoreSpec{
  26. Gateway: GatewaySpec{
  27. Port: 1,
  28. SecurePort: 0,
  29. },
  30. },
  31. }
  32. err := ValidateObjectSpec(o)
  33. assert.NoError(t, err)
  34. // when both port and securePort are o
  35. o.Spec.Gateway.Port = 0
  36. err = ValidateObjectSpec(o)
  37. assert.Error(t, err)
  38. // when securePort is greater than 65535
  39. o.Spec.Gateway.SecurePort = 65536
  40. err = ValidateObjectSpec(o)
  41. assert.Error(t, err)
  42. // when name is empty
  43. o.ObjectMeta.Name = ""
  44. err = ValidateObjectSpec(o)
  45. assert.Error(t, err)
  46. // when namespace is empty
  47. o.ObjectMeta.Namespace = ""
  48. err = ValidateObjectSpec(o)
  49. assert.Error(t, err)
  50. }
  51. func TestIsTLSEnabled(t *testing.T) {
  52. objStore := &CephObjectStore{
  53. ObjectMeta: metav1.ObjectMeta{
  54. Name: "my-store",
  55. Namespace: "rook-ceph",
  56. },
  57. Spec: ObjectStoreSpec{
  58. Gateway: GatewaySpec{
  59. Port: 1,
  60. SecurePort: 0,
  61. },
  62. },
  63. }
  64. IsTLS := objStore.Spec.IsTLSEnabled()
  65. assert.False(t, IsTLS)
  66. // only securePort is set without certs
  67. objStore.Spec.Gateway.SecurePort = 443
  68. IsTLS = objStore.Spec.IsTLSEnabled()
  69. assert.False(t, IsTLS)
  70. // when SSLCertificateRef is set with securePort
  71. objStore.Spec.Gateway.SSLCertificateRef = "my-tls-cert"
  72. IsTLS = objStore.Spec.IsTLSEnabled()
  73. assert.True(t, IsTLS)
  74. // when service serving cert is used
  75. objStore.Spec.Gateway.SSLCertificateRef = ""
  76. objStore.Spec.Gateway.Service = &(RGWServiceSpec{Annotations: Annotations{ServiceServingCertKey: "rgw-cert"}})
  77. IsTLS = objStore.Spec.IsTLSEnabled()
  78. assert.True(t, IsTLS)
  79. // when cert are set but securePort unset
  80. objStore.Spec.Gateway.SecurePort = 0
  81. IsTLS = objStore.Spec.IsTLSEnabled()
  82. assert.False(t, IsTLS)
  83. }