ceph_cosi_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package integration
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/rook/rook/pkg/daemon/ceph/client"
  7. "github.com/rook/rook/pkg/operator/ceph/object"
  8. rgw "github.com/rook/rook/pkg/operator/ceph/object"
  9. "github.com/rook/rook/tests/framework/clients"
  10. "github.com/rook/rook/tests/framework/installer"
  11. "github.com/rook/rook/tests/framework/utils"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/suite"
  14. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  15. )
  16. const (
  17. objectStoreCOSI = "cosi-store"
  18. cosiUser = "cosi"
  19. bucketClassName = "cosi-bucketclass"
  20. deletionPolicy = "Delete"
  21. cosiBucketName = "cosi-bucket"
  22. )
  23. func testCOSIDriver(s *suite.Suite, helper *clients.TestClient, k8sh *utils.K8sHelper, cephinstaller *installer.CephInstaller, namespace string) {
  24. if utils.IsPlatformOpenShift() {
  25. s.T().Skip("ceph cosi driver tests skipped on openshift")
  26. }
  27. t := s.T()
  28. t.Run("COSI Controller and CRD installation", func(t *testing.T) {
  29. _, err := k8sh.Kubectl("create", "-k", "github.com/kubernetes-sigs/container-object-storage-interface-api")
  30. assert.NoError(t, err, "failed to create COSI CRDs")
  31. _, err = k8sh.Kubectl("create", "-k", "github.com/kubernetes-sigs/container-object-storage-interface-controller")
  32. assert.NoError(t, err, "failed to create COSI controller")
  33. })
  34. createCephObjectStore(s.T(), helper, k8sh, cephinstaller, namespace, objectStoreCOSI, 1, false)
  35. t.Run("Creating CephCOSIDriver CRD", func(t *testing.T) {
  36. err := helper.COSIClient.CreateCOSI()
  37. assert.NoError(t, err, "failed to create Ceph COSI Driver CRD")
  38. })
  39. operatorNamespace := cephinstaller.Manifests.Settings().OperatorNamespace
  40. t.Run("Check ceph cosi driver running", func(t *testing.T) {
  41. for i := 24; i < 24 && k8sh.CheckPodCountAndState("ceph-cosi-driver", operatorNamespace, 1, "Running") == false; i++ {
  42. logger.Infof("ceph-cosi-driver is not running, trying again")
  43. k8sh.CheckPodCountAndState("ceph-cosi-driver", namespace, 1, "Running")
  44. }
  45. assert.True(t, k8sh.CheckPodCountAndState("ceph-cosi-driver", operatorNamespace, 1, "Running"))
  46. assert.NoError(t, k8sh.WaitForLabeledDeploymentsToBeReady("app=ceph-cosi-driver", operatorNamespace))
  47. })
  48. objectStoreUserSecretName := object.GenerateCephUserSecretName(objectStoreCOSI, cosiUser)
  49. t.Run("Creating BucketClass", func(t *testing.T) {
  50. err := helper.COSIClient.CreateBucketClass(bucketClassName, objectStoreUserSecretName, deletionPolicy)
  51. assert.NoError(t, err, "failed to create BucketClass")
  52. })
  53. t.Run("Creating BucketClaim", func(t *testing.T) {
  54. err := helper.COSIClient.CreateBucketClaim(cosiBucketName, bucketClassName)
  55. assert.NoError(t, err, "failed to create BucketClaim")
  56. })
  57. var cosiBucket string
  58. t.Run("Check Bucket is ready", func(t *testing.T) {
  59. _, err := k8sh.GetResource("bucketclaim", "-n", operatorNamespace, cosiBucketName)
  60. assert.NoError(t, err, "failed to get BucketClaim")
  61. cosiBucket, err = k8sh.GetResource("bucketclaim", "-n", operatorNamespace, cosiBucketName, "-o", "jsonpath={.status.bucketName}")
  62. assert.NoError(t, err, "failed to get Bucket name")
  63. i := 0
  64. var bucketReady string
  65. for i = 0; i < 4; i++ {
  66. bucketReady, err = k8sh.GetResource("bucket", cosiBucket, "-o", "jsonpath={.status.bucketReady}")
  67. if bucketReady == "true" && err == nil {
  68. break
  69. }
  70. logger.Warningf("bucket %q is not ready, retrying... bucketReady=%q, err=%v", cosiBucket, bucketReady, err)
  71. time.Sleep(5 * time.Second)
  72. }
  73. assert.NotEqual(t, 4, i)
  74. assert.Equal(t, "true", bucketReady, "Bucket is not ready")
  75. })
  76. t.Run("Check Bucket is created in Backend Ceph", func(t *testing.T) {
  77. ctx := context.TODO()
  78. // check if bucket is created in the backend
  79. context := k8sh.MakeContext()
  80. clusterInfo := client.AdminTestClusterInfo(namespace)
  81. objectStore, err := k8sh.RookClientset.CephV1().CephObjectStores(namespace).Get(ctx, objectStoreCOSI, metav1.GetOptions{})
  82. assert.Nil(t, err)
  83. rgwcontext, err := rgw.NewMultisiteContext(context, clusterInfo, objectStore)
  84. assert.Nil(t, err)
  85. var bkt rgw.ObjectBucket
  86. i := 0
  87. for i = 0; i < 4; i++ {
  88. b, code, err := rgw.GetBucket(rgwcontext, cosiBucket)
  89. if b != nil && err == nil {
  90. bkt = *b
  91. break
  92. }
  93. logger.Warningf("cannot get bucket %q, retrying... bucket: %v. code: %d, err: %v", cosiBucket, b, code, err)
  94. logger.Infof("(%d) check bucket exists, sleeping for 5 seconds ...", i)
  95. time.Sleep(5 * time.Second)
  96. }
  97. assert.NotEqual(t, 4, i)
  98. assert.Equal(t, cosiBucket, bkt.Name)
  99. })
  100. t.Run("Deleting BucketClaim", func(t *testing.T) {
  101. err := helper.COSIClient.DeleteBucketClaim(cosiBucketName, bucketClassName)
  102. assert.NoError(t, err, "failed to delete BucketClaim")
  103. })
  104. t.Run("Deleting BucketClass", func(t *testing.T) {
  105. err := helper.COSIClient.DeleteBucketClass(bucketClassName, objectStoreUserSecretName, deletionPolicy)
  106. assert.NoError(t, err, "failed to delete BucketClass")
  107. })
  108. t.Run("Deleting object user for cosi", func(t *testing.T) {
  109. err := helper.ObjectUserClient.Delete(namespace, cosiUser)
  110. assert.NoError(t, err, "failed to delete cosi user")
  111. })
  112. t.Run("delete CephObjectStore", func(t *testing.T) {
  113. deleteObjectStore(t, k8sh, namespace, objectStoreCOSI)
  114. assertObjectStoreDeletion(t, k8sh, namespace, objectStoreCOSI)
  115. })
  116. t.Run("delete CephCOSIDriver CRD", func(t *testing.T) {
  117. err := helper.COSIClient.DeleteCOSI()
  118. assert.NoError(t, err, "failed to delete Ceph COSI Driver CRD")
  119. })
  120. t.Run("delete COSI Controller", func(t *testing.T) {
  121. _, err := k8sh.Kubectl("delete", "-k", "github.com/kubernetes-sigs/container-object-storage-interface-api")
  122. assert.NoError(t, err, "failed to create COSI CRDs")
  123. _, err = k8sh.Kubectl("delete", "-k", "github.com/kubernetes-sigs/container-object-storage-interface-controller")
  124. assert.NoError(t, err, "failed to create COSI controller")
  125. })
  126. }