2016-11-05 17:09:05 -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.
|
|
|
|
|
*/
|
2016-12-01 16:10:46 -08:00
|
|
|
package pool
|
2016-09-20 15:20:17 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2016-10-26 00:38:39 +02:00
|
|
|
"github.com/rook/rook/pkg/model"
|
2016-10-27 13:26:25 +02:00
|
|
|
"github.com/rook/rook/pkg/rook/test"
|
2016-09-20 15:20:17 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
2016-10-11 21:05:33 -07:00
|
|
|
const (
|
|
|
|
|
SuccessPoolCreatedMessage = "pool 'pool1' created"
|
|
|
|
|
)
|
|
|
|
|
|
2017-10-05 17:23:12 -07:00
|
|
|
func TestConfigConversion(t *testing.T) {
|
2017-09-01 12:52:14 -07:00
|
|
|
config := Config{PoolType: "foo"}
|
|
|
|
|
_, err := ConfigToModel(config)
|
2016-10-14 13:26:31 -07:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
assert.Equal(t, "invalid pool type 'foo', allowed pool types are 'replicated' and 'erasure-coded'", err.Error())
|
|
|
|
|
|
2017-10-05 17:23:12 -07:00
|
|
|
config = Config{PoolType: PoolTypeErasureCoded}
|
|
|
|
|
_, err = ConfigToModel(config)
|
2016-10-14 13:26:31 -07:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
assert.Equal(t, "both data chunks and coding chunks must be greater than zero for pool type 'erasure-coded'", err.Error())
|
|
|
|
|
|
2017-10-05 17:23:12 -07:00
|
|
|
config = Config{PoolType: PoolTypeReplicated, DataChunks: 2, CodingChunks: 1}
|
|
|
|
|
_, err = ConfigToModel(config)
|
2016-10-14 14:12:06 -07:00
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
assert.Equal(t, "both data chunks and coding chunks must be zero for pool type 'replicated'", err.Error())
|
2017-10-05 17:23:12 -07:00
|
|
|
|
|
|
|
|
config = Config{PoolType: PoolTypeReplicated, FailureDomain: "osd"}
|
|
|
|
|
result, err := ConfigToModel(config)
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, "osd", result.FailureDomain)
|
2016-10-14 14:12:06 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-14 13:26:31 -07:00
|
|
|
func TestCreatePoolReplicatedNoParams(t *testing.T) {
|
2016-10-27 13:26:25 +02:00
|
|
|
c := &test.MockRookRestClient{
|
2016-10-11 21:05:33 -07:00
|
|
|
MockCreatePool: func(actualPool model.Pool) (string, error) {
|
|
|
|
|
expectedPool := model.Pool{
|
|
|
|
|
Name: "pool1",
|
|
|
|
|
Number: 0,
|
|
|
|
|
Type: model.Replicated,
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(t, expectedPool, actualPool)
|
|
|
|
|
return SuccessPoolCreatedMessage, nil
|
2016-09-20 15:20:17 -07:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-14 13:26:31 -07:00
|
|
|
// replicated pool replica count of 0 is OK, it will get the ceph default
|
2017-09-01 12:52:14 -07:00
|
|
|
pool := model.Pool{Name: "pool1", Type: model.Replicated}
|
|
|
|
|
out, err := createPool(pool, c)
|
2016-09-20 15:20:17 -07:00
|
|
|
assert.Nil(t, err)
|
2016-10-11 21:05:33 -07:00
|
|
|
assert.Equal(t, SuccessPoolCreatedMessage, out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreatePoolReplicated(t *testing.T) {
|
2016-10-27 13:26:25 +02:00
|
|
|
c := &test.MockRookRestClient{
|
2016-10-11 21:05:33 -07:00
|
|
|
MockCreatePool: func(actualPool model.Pool) (string, error) {
|
|
|
|
|
expectedPool := model.Pool{
|
|
|
|
|
Name: "pool1",
|
|
|
|
|
Number: 0,
|
|
|
|
|
Type: model.Replicated,
|
2017-09-11 14:56:10 -07:00
|
|
|
ReplicatedConfig: model.ReplicatedPoolConfig{
|
2016-10-11 21:05:33 -07:00
|
|
|
Size: 3,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(t, expectedPool, actualPool)
|
|
|
|
|
return SuccessPoolCreatedMessage, nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 12:52:14 -07:00
|
|
|
pool := model.Pool{Name: "pool1", Type: model.Replicated}
|
2017-09-11 14:56:10 -07:00
|
|
|
pool.ReplicatedConfig.Size = 3
|
2017-09-01 12:52:14 -07:00
|
|
|
out, err := createPool(pool, c)
|
2016-10-11 21:05:33 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, SuccessPoolCreatedMessage, out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestCreatePoolErasureCoded(t *testing.T) {
|
2016-10-27 13:26:25 +02:00
|
|
|
c := &test.MockRookRestClient{
|
2016-10-11 21:05:33 -07:00
|
|
|
MockCreatePool: func(actualPool model.Pool) (string, error) {
|
|
|
|
|
expectedPool := model.Pool{
|
|
|
|
|
Name: "pool1",
|
|
|
|
|
Number: 0,
|
|
|
|
|
Type: model.ErasureCoded,
|
|
|
|
|
ErasureCodedConfig: model.ErasureCodedPoolConfig{
|
|
|
|
|
DataChunkCount: 2,
|
|
|
|
|
CodingChunkCount: 1,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(t, expectedPool, actualPool)
|
|
|
|
|
return SuccessPoolCreatedMessage, nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 12:52:14 -07:00
|
|
|
pool := model.Pool{Name: "pool1", Type: model.ErasureCoded}
|
|
|
|
|
pool.ErasureCodedConfig.DataChunkCount = 2
|
|
|
|
|
pool.ErasureCodedConfig.CodingChunkCount = 1
|
|
|
|
|
|
|
|
|
|
out, err := createPool(pool, c)
|
2016-10-11 21:05:33 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
assert.Equal(t, SuccessPoolCreatedMessage, out)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 15:20:17 -07:00
|
|
|
func TestCreatePoolFailure(t *testing.T) {
|
2016-10-27 13:26:25 +02:00
|
|
|
c := &test.MockRookRestClient{
|
2016-09-20 15:20:17 -07:00
|
|
|
MockCreatePool: func(pool model.Pool) (string, error) {
|
|
|
|
|
return "", fmt.Errorf("mock error")
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 12:52:14 -07:00
|
|
|
pool := model.Pool{Name: "pool1", Type: model.Replicated}
|
|
|
|
|
out, err := createPool(pool, c)
|
2016-09-20 15:20:17 -07:00
|
|
|
assert.NotNil(t, err)
|
2016-10-11 21:05:33 -07:00
|
|
|
assert.Equal(t, "failed to create new pool 'pool1': mock error", err.Error())
|
2016-09-20 15:20:17 -07:00
|
|
|
assert.Equal(t, "", out)
|
|
|
|
|
}
|