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

246 lines
10 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"
2017-12-29 00:33:36 -08:00
"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
2018-03-16 08:31:34 -07:00
func TestBlockCreateSuite(t *testing.T) {
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
installer *installer.CephInstaller
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-08-21 15:24:52 -06:00
s.op, s.kh = StartTestCluster(s.T, s.namespace, "bluestore", false, false, 1, installer.VersionMaster)
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
}
// 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"
2017-12-29 00:33:36 -08:00
defer s.tearDownTest(claimName, poolName, storageClassName, "ReadWriteOnce")
2018-08-21 15:24:52 -06:00
result, err := s.testClient.BlockClient.CreatePvc(claimName, storageClassName, "ReadWriteOnce")
2018-07-27 16:45:09 -06:00
checkOrderedSubstrings(s.T(), result, "persistentvolumeclaim", claimName, "created")
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
}
// Test case when persistentvolumeclaim is created for a valid storage class twice
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) TestCreateSamePVCTwice() {
logger.Infof("Test creating PVC(create block images) twice")
claimName := "test-twice-claim"
poolName := "test-twice-pool"
storageClassName := "rook-ceph-block"
2017-12-29 00:33:36 -08:00
defer s.tearDownTest(claimName, poolName, storageClassName, "ReadWriteOnce")
status, _ := s.kh.GetPVCStatus(defaultNamespace, claimName)
logger.Infof("PVC %s status: %s", claimName, status)
2018-03-16 08:31:34 -07:00
s.testClient.BlockClient.List(s.namespace)
2017-05-25 10:26:03 -07:00
logger.Infof("create pool and storageclass")
2018-08-21 15:24:52 -06:00
result0, err0 := s.testClient.PoolClient.Create(poolName, s.namespace, 1)
2018-07-27 16:45:09 -06:00
checkOrderedSubstrings(s.T(), result0, poolName, "created")
2017-10-20 13:18:05 -07:00
require.NoError(s.T(), err0)
2018-04-26 13:41:31 -07:00
2018-08-21 15:24:52 -06:00
result1, err1 := s.testClient.BlockClient.CreateStorageClass(poolName, storageClassName, s.namespace, true)
2018-07-27 16:45:09 -06:00
checkOrderedSubstrings(s.T(), result1, storageClassName, "created")
2017-07-21 12:07:15 -07:00
require.NoError(s.T(), err1)
2017-05-25 10:26:03 -07:00
logger.Infof("make sure storageclass is created")
2018-06-18 17:10:41 -07:00
err := s.kh.IsStorageClassPresent("rook-ceph-block")
2017-05-25 10:26:03 -07:00
require.Nil(s.T(), err)
logger.Infof("create pvc")
2018-08-21 15:24:52 -06:00
result2, err2 := s.testClient.BlockClient.CreatePvc(claimName, storageClassName, "ReadWriteOnce")
2018-07-27 16:45:09 -06:00
checkOrderedSubstrings(s.T(), result2, claimName, "created")
2017-07-21 12:07:15 -07:00
require.NoError(s.T(), err2)
2017-05-25 10:26:03 -07:00
logger.Infof("check status of PVC")
require.True(s.T(), s.kh.WaitUntilPVCIsBound(defaultNamespace, claimName))
2017-05-25 10:26:03 -07:00
2018-03-16 08:31:34 -07:00
b1, err := s.testClient.BlockClient.List(s.namespace)
assert.Nil(s.T(), err)
assert.Equal(s.T(), s.initBlockCount+1, len(b1), "Make sure new block image is created")
2017-05-25 10:26:03 -07:00
logger.Infof("Create same pvc again")
2018-08-21 15:24:52 -06:00
_, err3 := s.testClient.BlockClient.CreatePvc(claimName, storageClassName, "ReadWriteOnce")
2018-07-27 16:45:09 -06:00
checkOrderedSubstrings(s.T(), err3.Error(), claimName, "already exists")
2017-05-25 10:26:03 -07:00
logger.Infof("check status of PVC")
require.True(s.T(), s.kh.WaitUntilPVCIsBound(defaultNamespace, claimName))
2017-05-25 10:26:03 -07:00
logger.Infof("check block image count")
2018-03-16 08:31:34 -07:00
b2, _ := s.testClient.BlockClient.List(s.namespace)
assert.Equal(s.T(), len(b1), len(b2), "Make sure new block image is created")
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"
statefulSetName := "block-stateful-set"
statefulPodsName := "ststest"
defer s.statefulSetDataCleanup(defaultNamespace, poolName, storageClassName, 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
_, cbErr := s.testClient.PoolClient.CreateStorageClass(s.namespace, poolName, storageClassName)
2017-12-29 00:33:36 -08:00
assert.Nil(s.T(), cbErr)
logger.Info("Step 2 : Deploy statefulSet with 1X replication")
service, statefulset := getBlockStatefulSetAndServiceDefinition(defaultNamespace, statefulSetName, statefulPodsName, storageClassName)
_, err1 := s.kh.Clientset.CoreV1().Services(defaultNamespace).Create(service)
_, err2 := s.kh.Clientset.AppsV1beta1().StatefulSets(defaultNamespace).Create(statefulset)
assert.Nil(s.T(), err1)
assert.Nil(s.T(), err2)
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}
err1 = s.kh.Clientset.CoreV1().Services(defaultNamespace).Delete(statefulSetName, &delOpts)
err2 = s.kh.Clientset.AppsV1beta1().StatefulSets(defaultNamespace).Delete(statefulPodsName, &delOpts)
err3 := s.kh.Clientset.CoreV1().Pods(defaultNamespace).DeleteCollection(&delOpts, listOpts)
assert.Nil(s.T(), err1)
assert.Nil(s.T(), err2)
assert.Nil(s.T(), err3)
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
}
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) statefulSetDataCleanup(namespace, poolName, storageClassName, statefulSetName, statefulPodsName string) {
delOpts := metav1.DeleteOptions{}
listOpts := metav1.ListOptions{LabelSelector: "app=" + statefulSetName}
2017-12-29 00:33:36 -08:00
//Delete stateful set
s.kh.Clientset.CoreV1().Services(namespace).Delete(statefulSetName, &delOpts)
s.kh.Clientset.AppsV1beta1().StatefulSets(defaultNamespace).Delete(statefulPodsName, &delOpts)
s.kh.Clientset.CoreV1().Pods(defaultNamespace).DeleteCollection(&delOpts, listOpts)
2017-12-29 00:33:36 -08:00
//Delete all PVCs
s.kh.DeletePvcWithLabel(defaultNamespace, statefulSetName)
//Delete storageclass and pool
2018-08-21 15:24:52 -06:00
s.testClient.PoolClient.DeleteStorageClass(s.namespace, poolName, storageClassName)
2017-12-29 00:33:36 -08:00
}
2018-03-16 08:31:34 -07:00
func (s *BlockCreateSuite) tearDownTest(claimName string, poolName string, storageClassName string, accessMode string) {
2018-08-21 15:24:52 -06:00
s.testClient.BlockClient.DeletePvc(claimName, storageClassName, accessMode)
s.testClient.PoolClient.Delete(poolName, s.namespace)
s.testClient.BlockClient.DeleteStorageClass(poolName, storageClassName, 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"
defer s.tearDownTest(claimName, poolName, storageClassName, pvcAccessMode)
//create pool and storageclass
2018-08-21 15:24:52 -06:00
result0, err0 := s.testClient.PoolClient.Create(poolName, s.namespace, 1)
2018-07-27 16:45:09 -06:00
checkOrderedSubstrings(s.T(), result0, poolName, "created")
require.NoError(s.T(), err0)
2018-08-21 15:24:52 -06:00
result1, err1 := s.testClient.BlockClient.CreateStorageClass(poolName, storageClassName, s.namespace, true)
2018-07-27 16:45:09 -06:00
checkOrderedSubstrings(s.T(), result1, storageClassName, "created")
require.NoError(s.T(), err1)
//make sure storageclass is created
2018-06-18 17:10:41 -07:00
err := s.kh.IsStorageClassPresent(storageClassName)
require.Nil(s.T(), err)
//create pvc
2018-08-21 15:24:52 -06:00
result2, err2 := s.testClient.BlockClient.CreatePvc(claimName, storageClassName, pvcAccessMode)
2018-07-27 16:45:09 -06:00
checkOrderedSubstrings(s.T(), result2, claimName, "created")
require.NoError(s.T(), err2)
//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")
}