Files
my-rook-config/pkg/operator/ceph/pool/controller_test.go
T

225 lines
7.5 KiB
Go
Raw Normal View History

2017-03-27 09:12:56 -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-24 07:43:11 -07:00
package pool
2017-03-27 09:12:56 -07:00
import (
2017-10-05 17:23:12 -07:00
"fmt"
2017-03-27 09:12:56 -07:00
"testing"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
exectest "github.com/rook/rook/pkg/util/exec/test"
2017-03-27 09:12:56 -07:00
"github.com/stretchr/testify/assert"
2017-07-24 07:43:11 -07:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2017-03-27 09:12:56 -07:00
)
func TestValidatePool(t *testing.T) {
2017-10-05 17:23:12 -07:00
context := &clusterd.Context{Executor: &exectest.MockExecutor{}}
2019-11-05 17:16:08 +01:00
// not specifying some replication or EC settings is fine
2018-12-03 23:48:40 -07:00
p := cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "mypool", Namespace: "myns"}}
err := ValidatePool(context, &p)
2019-11-05 17:16:08 +01:00
assert.Nil(t, err)
2017-03-27 09:12:56 -07:00
// must specify name
2018-12-03 23:48:40 -07:00
p = cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Namespace: "myns"}}
err = ValidatePool(context, &p)
2017-03-27 09:12:56 -07:00
assert.NotNil(t, err)
// must specify namespace
2018-12-03 23:48:40 -07:00
p = cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "mypool"}}
err = ValidatePool(context, &p)
2017-03-27 09:12:56 -07:00
assert.NotNil(t, err)
// must not specify both replication and EC settings
2018-12-03 23:48:40 -07:00
p = cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "mypool", Namespace: "myns"}}
p.Spec.Replicated.Size = 1
p.Spec.ErasureCoded.CodingChunks = 2
p.Spec.ErasureCoded.DataChunks = 3
err = ValidatePool(context, &p)
2017-03-27 09:12:56 -07:00
assert.NotNil(t, err)
// succeed with replication settings
2018-12-03 23:48:40 -07:00
p = cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "mypool", Namespace: "myns"}}
p.Spec.Replicated.Size = 1
err = ValidatePool(context, &p)
2017-03-27 09:12:56 -07:00
assert.Nil(t, err)
// succeed with ec settings
2018-12-03 23:48:40 -07:00
p = cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "mypool", Namespace: "myns"}}
p.Spec.ErasureCoded.CodingChunks = 1
p.Spec.ErasureCoded.DataChunks = 2
err = ValidatePool(context, &p)
2017-03-27 09:12:56 -07:00
assert.Nil(t, err)
}
2018-01-23 11:55:52 -08:00
func TestValidateCrushProperties(t *testing.T) {
2017-10-05 17:23:12 -07:00
executor := &exectest.MockExecutor{}
context := &clusterd.Context{Executor: executor}
executor.MockExecuteCommandWithOutputFile = func(debug bool, actionName, command, outputFile string, args ...string) (string, error) {
logger.Infof("Command: %s %v", command, args)
if args[1] == "crush" && args[2] == "dump" {
2018-01-23 11:55:52 -08:00
return `{"types":[{"type_id": 0,"name": "osd"}],"buckets":[{"id": -1,"name":"default"},{"id": -2,"name":"good"}]}`, nil
2017-10-05 17:23:12 -07:00
}
return "", fmt.Errorf("unexpected ceph command '%v'", args)
}
// succeed with a failure domain that exists
2018-12-03 23:48:40 -07:00
p := &cephv1.CephBlockPool{
2018-01-23 11:55:52 -08:00
ObjectMeta: metav1.ObjectMeta{Name: "mypool", Namespace: "myns"},
Spec: cephv1.PoolSpec{
Replicated: cephv1.ReplicatedSpec{Size: 1},
2018-01-23 11:55:52 -08:00
FailureDomain: "osd",
},
}
err := ValidatePool(context, p)
2017-10-05 17:23:12 -07:00
assert.Nil(t, err)
// fail with a failure domain that doesn't exist
p.Spec.FailureDomain = "doesntexist"
2018-01-23 11:55:52 -08:00
err = ValidatePool(context, p)
2017-10-05 17:23:12 -07:00
assert.NotNil(t, err)
2018-01-23 11:55:52 -08:00
// fail with a crush root that doesn't exist
p.Spec.FailureDomain = "osd"
p.Spec.CrushRoot = "bad"
err = ValidatePool(context, p)
assert.NotNil(t, err)
// fail with a crush root that does exist
p.Spec.CrushRoot = "good"
err = ValidatePool(context, p)
assert.Nil(t, err)
2017-10-05 17:23:12 -07:00
}
2017-03-27 09:12:56 -07:00
func TestCreatePool(t *testing.T) {
executor := &exectest.MockExecutor{
MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outfile string, args ...string) (string, error) {
if command == "ceph" && args[1] == "erasure-code-profile" {
return `{"k":"2","m":"1","plugin":"jerasure","technique":"reed_sol_van"}`, nil
}
return "", nil
},
}
context := &clusterd.Context{Executor: executor}
2017-03-27 09:12:56 -07:00
2018-12-03 23:48:40 -07:00
p := &cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "mypool", Namespace: "myns"}}
p.Spec.Replicated.Size = 1
exists, err := poolExists(context, p)
2017-03-27 09:12:56 -07:00
assert.False(t, exists)
err = createPool(context, p)
2017-03-27 09:12:56 -07:00
assert.Nil(t, err)
// fail if both replication and EC are specified
p.Spec.ErasureCoded.CodingChunks = 2
p.Spec.ErasureCoded.DataChunks = 2
err = createPool(context, p)
2017-03-27 09:12:56 -07:00
assert.NotNil(t, err)
// succeed with EC
p.Spec.Replicated.Size = 0
err = createPool(context, p)
2017-03-27 09:12:56 -07:00
assert.Nil(t, err)
}
func TestUpdatePool(t *testing.T) {
// the pool did not change for properties that are updatable
old := cephv1.PoolSpec{FailureDomain: "osd", ErasureCoded: cephv1.ErasureCodedSpec{CodingChunks: 2, DataChunks: 2}}
new := cephv1.PoolSpec{FailureDomain: "host", ErasureCoded: cephv1.ErasureCodedSpec{CodingChunks: 3, DataChunks: 3}}
changed := poolChanged(old, new)
assert.False(t, changed)
// the pool changed for properties that are updatable
old = cephv1.PoolSpec{FailureDomain: "osd", Replicated: cephv1.ReplicatedSpec{Size: 1}}
new = cephv1.PoolSpec{FailureDomain: "osd", Replicated: cephv1.ReplicatedSpec{Size: 2}}
changed = poolChanged(old, new)
assert.True(t, changed)
}
2017-03-27 09:12:56 -07:00
func TestDeletePool(t *testing.T) {
2019-09-24 15:05:42 +05:30
failOnDelete := false
executor := &exectest.MockExecutor{
MockExecuteCommandWithOutputFile: func(debug bool, actionName, command, outfile string, args ...string) (string, error) {
if command == "ceph" && args[1] == "lspools" {
return `[{"poolnum":1,"poolname":"mypool"}]`, nil
} else if command == "ceph" && args[1] == "pool" && args[2] == "get" {
return `{"pool": "mypool","pool_id": 1,"size":1}`, nil
2017-03-27 09:12:56 -07:00
}
2019-09-24 15:05:42 +05:30
return "", nil
2017-03-27 09:12:56 -07:00
},
2019-09-24 15:05:42 +05:30
MockExecuteCommandWithOutput: func(debug bool, actionName string, command string, args ...string) (string, error) {
emptyPool := "{\"images\":{\"count\":0,\"provisioned_bytes\":0,\"snap_count\":0},\"trash\":{\"count\":1,\"provisioned_bytes\":2048,\"snap_count\":0}}"
p := "{\"images\":{\"count\":1,\"provisioned_bytes\":1024,\"snap_count\":0},\"trash\":{\"count\":1,\"provisioned_bytes\":2048,\"snap_count\":0}}"
logger.Infof("Command: %s %v", command, args)
if args[0] == "pool" {
if args[1] == "stats" {
if !failOnDelete {
return emptyPool, nil
}
return p, nil
}
return "", fmt.Errorf("rbd: error opening pool '%s': (2) No such file or directory", args[3])
}
return "", fmt.Errorf("unexpected rbd command '%v'", args)
},
2017-03-27 09:12:56 -07:00
}
context := &clusterd.Context{Executor: executor}
2017-03-27 09:12:56 -07:00
// delete a pool that exists
2018-12-03 23:48:40 -07:00
p := &cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "mypool", Namespace: "myns"}}
exists, err := poolExists(context, p)
2017-03-27 09:12:56 -07:00
assert.Nil(t, err)
assert.True(t, exists)
err = deletePool(context, p)
2017-03-27 09:12:56 -07:00
assert.Nil(t, err)
// succeed even if the pool doesn't exist
2018-12-03 23:48:40 -07:00
p = &cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "otherpool", Namespace: "myns"}}
exists, err = poolExists(context, p)
2017-03-27 09:12:56 -07:00
assert.Nil(t, err)
assert.False(t, exists)
err = deletePool(context, p)
2017-03-27 09:12:56 -07:00
assert.Nil(t, err)
2019-09-24 15:05:42 +05:30
// fail if images/snapshosts exist in the pool
failOnDelete = true
p = &cephv1.CephBlockPool{ObjectMeta: metav1.ObjectMeta{Name: "mypool", Namespace: "myns"}}
exists, err = poolExists(context, p)
assert.Nil(t, err)
assert.True(t, exists)
err = deletePool(context, p)
assert.NotNil(t, err)
2017-03-27 09:12:56 -07:00
}
func TestGetPoolObject(t *testing.T) {
// get a current version pool object, should return with no error and no migration needed
pool, err := getPoolObject(&cephv1.CephBlockPool{})
assert.NotNil(t, pool)
assert.Nil(t, err)
// try to get an object that isn't a pool, should return with an error
pool, err = getPoolObject(&map[string]string{})
assert.Nil(t, pool)
assert.NotNil(t, err)
}