Files
my-rook-config/tests/integration/ceph_block_create_test.go
T

205 lines
8.6 KiB
Go
Raw Normal View History

2017-06-26 21:52:06 -07:00
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2017-07-21 12:07:15 -07:00
package integration
2017-05-25 10:26:03 -07:00
import (
"fmt"
2017-05-25 10:26:03 -07:00
"testing"
2017-06-26 21:52:06 -07:00
"github.com/rook/rook/tests/framework/clients"
"github.com/rook/rook/tests/framework/installer"
2017-06-26 21:52:06 -07:00
"github.com/rook/rook/tests/framework/utils"
"github.com/stretchr/testify/assert"
2017-06-26 21:52:06 -07:00
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-05-25 10:26:03 -07:00
)
// Test K8s Block Image Creation Scenarios. These tests work when platform is set to Kubernetes
2017-10-12 16:03:08 -07:00
func TestCephBlockCreateSuite(t *testing.T) {
if installer.SkipTestSuite(installer.CephTestSuite) {
t.Skip()
}
2018-03-16 08:31:34 -07:00
s := new(BlockCreateSuite)
defer func(s *BlockCreateSuite) {
2017-11-21 00:37:46 -05:00
HandlePanics(recover(), s.op, s.T)
2017-11-10 12:03:43 -08:00
}(s)
suite.Run(t, s)
2017-05-25 10:26:03 -07:00
}
2018-03-16 08:31:34 -07:00
type BlockCreateSuite struct {
2017-05-25 10:26:03 -07:00
suite.Suite
2017-10-20 13:18:05 -07:00
testClient *clients.TestClient
kh *utils.K8sHelper
initBlockCount int
namespace string
2018-08-21 15:24:52 -06:00
op *TestCluster
2017-05-25 10:26:03 -07:00
}
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) SetupSuite() {
2017-05-25 10:26:03 -07:00
var err error
2017-10-12 16:03:08 -07:00
s.namespace = "block-k8s-ns"
2018-11-28 08:29:37 -07:00
mons := 1
rbdMirrorWorkers := 1
s.op, s.kh = StartTestCluster(s.T, s.namespace, "bluestore", false, false, mons, rbdMirrorWorkers, installer.VersionMaster, installer.NautilusVersion)
2018-08-21 15:24:52 -06:00
s.testClient = clients.CreateTestClient(s.kh, s.op.installer.Manifests)
2018-03-16 08:31:34 -07:00
initialBlocks, err := s.testClient.BlockClient.List(s.namespace)
2017-10-12 16:03:08 -07:00
assert.Nil(s.T(), err)
2017-07-05 12:26:18 -07:00
s.initBlockCount = len(initialBlocks)
2017-05-25 10:26:03 -07:00
}
func (s *BlockCreateSuite) AfterTest(suiteName, testName string) {
s.op.installer.CollectOperatorLog(suiteName, testName, installer.SystemNamespace(s.namespace))
}
2017-05-25 10:26:03 -07:00
// Test case when persistentvolumeclaim is created for a storage class that doesn't exist
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) TestCreatePVCWhenNoStorageClassExists() {
logger.Infof("Test creating PVC(block images) when storage class is not created")
2017-05-25 10:26:03 -07:00
// Create PVC
claimName := "test-no-storage-class-claim"
poolName := "test-no-storage-class-pool"
storageClassName := "rook-ceph-block"
reclaimPolicy := "Delete"
defer s.tearDownTest(claimName, poolName, storageClassName, reclaimPolicy, "ReadWriteOnce")
2019-07-08 00:02:31 +04:00
err := s.testClient.BlockClient.CreatePvc(claimName, storageClassName, "ReadWriteOnce", "1M")
2017-07-21 12:07:15 -07:00
require.NoError(s.T(), err)
2017-05-25 10:26:03 -07:00
// check status of PVC
pvcStatus, err := s.kh.GetPVCStatus(defaultNamespace, claimName)
2017-05-25 10:26:03 -07:00
require.Nil(s.T(), err)
2018-04-26 13:41:31 -07:00
assert.Contains(s.T(), pvcStatus, "Pending", "Makes sure PVC is in Pending state")
2017-05-25 10:26:03 -07:00
// check block image count
2018-03-16 08:31:34 -07:00
b, _ := s.testClient.BlockClient.List(s.namespace)
2017-07-05 12:26:18 -07:00
require.Equal(s.T(), s.initBlockCount, len(b), "Make sure new block image is not created")
2017-05-25 10:26:03 -07:00
}
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) TestCreatingPVCWithVariousAccessModes() {
s.CheckCreatingPVC("rwo", "ReadWriteOnce")
s.CheckCreatingPVC("rwx", "ReadWriteMany")
s.CheckCreatingPVC("rox", "ReadOnlyMany")
2017-05-25 10:26:03 -07:00
}
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) TestBlockStorageMountUnMountForStatefulSets() {
2017-12-29 00:33:36 -08:00
poolName := "stspool"
storageClassName := "stssc"
reclaimPolicy := "Delete"
2017-12-29 00:33:36 -08:00
statefulSetName := "block-stateful-set"
statefulPodsName := "ststest"
defer s.statefulSetDataCleanup(defaultNamespace, poolName, storageClassName, reclaimPolicy, statefulSetName, statefulPodsName)
2017-12-29 00:33:36 -08:00
logger.Infof("Test case when block persistent volumes are scaled up and down along with StatefulSet")
logger.Info("Step 1: Create pool and storageClass")
2018-08-21 15:24:52 -06:00
err := s.testClient.PoolClient.CreateStorageClass(s.namespace, poolName, storageClassName, reclaimPolicy)
assert.Nil(s.T(), err)
2017-12-29 00:33:36 -08:00
logger.Info("Step 2 : Deploy statefulSet with 1X replication")
service, statefulset := getBlockStatefulSetAndServiceDefinition(defaultNamespace, statefulSetName, statefulPodsName, storageClassName)
_, err = s.kh.Clientset.CoreV1().Services(defaultNamespace).Create(service)
assert.Nil(s.T(), err)
_, err = s.kh.Clientset.AppsV1().StatefulSets(defaultNamespace).Create(statefulset)
assert.Nil(s.T(), err)
require.True(s.T(), s.kh.CheckPodCountAndState(statefulSetName, defaultNamespace, 1, "Running"))
require.True(s.T(), s.kh.CheckPvcCountAndStatus(statefulSetName, defaultNamespace, 1, "Bound"))
2017-12-29 00:33:36 -08:00
logger.Info("Step 3 : Scale up replication on statefulSet")
scaleerr := s.kh.ScaleStatefulSet(statefulPodsName, defaultNamespace, 2)
assert.NoError(s.T(), scaleerr, "make sure scale up is successful")
require.True(s.T(), s.kh.CheckPodCountAndState(statefulSetName, defaultNamespace, 2, "Running"))
require.True(s.T(), s.kh.CheckPvcCountAndStatus(statefulSetName, defaultNamespace, 2, "Bound"))
2017-12-29 00:33:36 -08:00
logger.Info("Step 4 : Scale down replication on statefulSet")
scaleerr = s.kh.ScaleStatefulSet(statefulPodsName, defaultNamespace, 1)
assert.NoError(s.T(), scaleerr, "make sure scale down is successful")
require.True(s.T(), s.kh.CheckPodCountAndState(statefulSetName, defaultNamespace, 1, "Running"))
require.True(s.T(), s.kh.CheckPvcCountAndStatus(statefulSetName, defaultNamespace, 2, "Bound"))
2017-12-29 00:33:36 -08:00
logger.Info("Step 5 : Delete statefulSet")
delOpts := metav1.DeleteOptions{}
listOpts := metav1.ListOptions{LabelSelector: "app=" + statefulSetName}
err = s.kh.Clientset.CoreV1().Services(defaultNamespace).Delete(statefulSetName, &delOpts)
assert.Nil(s.T(), err)
err = s.kh.Clientset.AppsV1().StatefulSets(defaultNamespace).Delete(statefulPodsName, &delOpts)
assert.Nil(s.T(), err)
err = s.kh.Clientset.CoreV1().Pods(defaultNamespace).DeleteCollection(&delOpts, listOpts)
assert.Nil(s.T(), err)
require.True(s.T(), s.kh.WaitUntilPodWithLabelDeleted(fmt.Sprintf("app=%s", statefulSetName), defaultNamespace))
require.True(s.T(), s.kh.CheckPvcCountAndStatus(statefulSetName, defaultNamespace, 2, "Bound"))
2017-12-29 00:33:36 -08:00
}
func (s *BlockCreateSuite) statefulSetDataCleanup(namespace, poolName, storageClassName, reclaimPolicy, statefulSetName, statefulPodsName string) {
delOpts := metav1.DeleteOptions{}
listOpts := metav1.ListOptions{LabelSelector: "app=" + statefulSetName}
// Delete stateful set
s.kh.Clientset.CoreV1().Services(namespace).Delete(statefulSetName, &delOpts)
s.kh.Clientset.AppsV1().StatefulSets(defaultNamespace).Delete(statefulPodsName, &delOpts)
s.kh.Clientset.CoreV1().Pods(defaultNamespace).DeleteCollection(&delOpts, listOpts)
// Delete all PVCs
2017-12-29 00:33:36 -08:00
s.kh.DeletePvcWithLabel(defaultNamespace, statefulSetName)
// Delete storageclass and pool
s.testClient.PoolClient.DeleteStorageClass(s.namespace, poolName, storageClassName, reclaimPolicy)
2017-12-29 00:33:36 -08:00
}
func (s *BlockCreateSuite) tearDownTest(claimName string, poolName string, storageClassName string, reclaimPolicy string, accessMode string) {
2019-07-08 00:02:31 +04:00
s.testClient.BlockClient.DeletePvc(claimName, storageClassName, accessMode, "1M")
2018-08-21 15:24:52 -06:00
s.testClient.PoolClient.Delete(poolName, s.namespace)
s.testClient.BlockClient.DeleteStorageClass(poolName, storageClassName, reclaimPolicy, s.namespace)
2017-05-25 10:26:03 -07:00
}
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) TearDownSuite() {
2018-08-21 15:24:52 -06:00
s.op.Teardown()
}
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) CheckCreatingPVC(pvcName, pvcAccessMode string) {
logger.Infof("Test creating %s PVC(block images) when storage class is created", pvcAccessMode)
claimName := fmt.Sprintf("test-with-storage-class-claim-%s", pvcName)
poolName := fmt.Sprintf("test-with-storage-class-pool-%s", pvcName)
storageClassName := "rook-ceph-block"
reclaimPolicy := "Delete"
defer s.tearDownTest(claimName, poolName, storageClassName, reclaimPolicy, pvcAccessMode)
// create pool and storageclass
err := s.testClient.PoolClient.Create(poolName, s.namespace, 1)
require.NoError(s.T(), err)
err = s.testClient.BlockClient.CreateStorageClass(poolName, storageClassName, reclaimPolicy, s.namespace, true)
require.NoError(s.T(), err)
// make sure storageclass is created
err = s.kh.IsStorageClassPresent(storageClassName)
require.Nil(s.T(), err)
// create pvc
2019-07-08 00:02:31 +04:00
err = s.testClient.BlockClient.CreatePvc(claimName, storageClassName, pvcAccessMode, "1M")
require.NoError(s.T(), err)
// check status of PVC
require.True(s.T(), s.kh.WaitUntilPVCIsBound(defaultNamespace, claimName))
accessModes, err := s.kh.GetPVCAccessModes(defaultNamespace, claimName)
require.NoError(s.T(), err)
assert.Equal(s.T(), accessModes[0], v1.PersistentVolumeAccessMode(pvcAccessMode))
// check block image count
2018-03-16 08:31:34 -07:00
b, _ := s.testClient.BlockClient.List(s.namespace)
require.Equal(s.T(), s.initBlockCount+1, len(b), "Make sure new block image is created")
}