Skip to content

Commit

Permalink
improve storage unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ObiWahn committed Mar 29, 2018
1 parent ee0c1f8 commit 04305ef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
28 changes: 11 additions & 17 deletions pkg/apis/storage/v1alpha/local_storage_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,28 @@ package v1alpha
import (
"testing"

"github.com/pkg/errors"
//"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

func Test_LocalStorageSpec_Creation(t *testing.T) {
var (
class StorageClassSpec
local LocalStorageSpec
err error
)
// Test creation of local storage spec
func TestLocalStorageSpecCreation(t *testing.T) {

class = StorageClassSpec{"SpecName", true}
local = LocalStorageSpec{StorageClass: class, LocalPath: []string{""}}
err = local.Validate()
assert.Equal(t, errors.Cause(class.Validate()), errors.Cause(err))
class := StorageClassSpec{"SpecName", true}
local := LocalStorageSpec{StorageClass: class, LocalPath: []string{""}}
assert.Error(t, local.Validate())

class = StorageClassSpec{"spec-name", true}
local = LocalStorageSpec{StorageClass: class, LocalPath: []string{""}} //is this allowed - should the paths be checked?
err = local.Validate()
assert.Equal(t, nil, errors.Cause(err))
local = LocalStorageSpec{StorageClass: class, LocalPath: []string{""}}
assert.Error(t, local.Validate(), "should fail as the empty sting is not a valid path")

class = StorageClassSpec{"spec-name", true}
local = LocalStorageSpec{StorageClass: class, LocalPath: []string{}}
err = local.Validate()
assert.Equal(t, ValidationError, errors.Cause(err)) //path empty
assert.True(t, IsValidation(local.Validate()))
}

func Test_LocalStorageSpec_Reset(t *testing.T) {
// Test reset of local storage spec
func TestLocalStorageSpecReset(t *testing.T) {
class := StorageClassSpec{"spec-name", true}
source := LocalStorageSpec{StorageClass: class, LocalPath: []string{"/a/path", "/another/path"}}
target := LocalStorageSpec{}
Expand Down
7 changes: 5 additions & 2 deletions pkg/apis/storage/v1alpha/local_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_ArangoLocalStorage_Creation(t *testing.T) {
assert.True(t, true)
func TestArangoLocalStorageCreation(t *testing.T) {
storage := ArangoLocalStorage{}
list := ArangoLocalStorageList{}
list.Items = append(list.Items, storage)
assert.Equal(t, 1, len(list.Items))
}
22 changes: 12 additions & 10 deletions pkg/apis/storage/v1alpha/storage_class_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ import (
"github.com/stretchr/testify/assert"
)

func Test_StorageClassSpec_Creation(t *testing.T) {
// test creation of storage class spec
func TestStorageClassSpecCreation(t *testing.T) {
storageClassSpec := StorageClassSpec{}
assert.True(t, nil != storageClassSpec.Validate())
assert.Error(t, storageClassSpec.Validate(), "empty name name is not allowed")

storageClassSpec = StorageClassSpec{Name: "TheSpecName", IsDefault: true} // no upper-case allowed
assert.True(t, nil != storageClassSpec.Validate())
storageClassSpec = StorageClassSpec{Name: "TheSpecName", IsDefault: true}
assert.Error(t, storageClassSpec.Validate(), "upper case letters are not allowed in resources")

storageClassSpec = StorageClassSpec{"the-spec-name", true}
assert.Equal(t, nil, storageClassSpec.Validate())
assert.NoError(t, storageClassSpec.Validate())

storageClassSpec = StorageClassSpec{} // this is invalid because it was not created with a proper name
storageClassSpec.SetDefaults("foo") // here the Name is fixed
assert.Equal(t, nil, storageClassSpec.Validate())
storageClassSpec = StorageClassSpec{} // no proper name -> invalid
storageClassSpec.SetDefaults("foo") // name is fixed -> vaild
assert.NoError(t, storageClassSpec.Validate())
}

func Test_StorageClassSpec_ResetImmutableFileds(t *testing.T) {
// test reset of storage class spec
func TestStorageClassSpecResetImmutableFileds(t *testing.T) {
specSource := StorageClassSpec{"source", true}
specTarget := StorageClassSpec{"target", true}

assert.Equal(t, "target", specTarget.Name)
rv := specSource.ResetImmutableFields("fieldPrefix-", &specTarget)
assert.Equal(t, "fieldPrefix-name", strings.Join(rv[:], ", "))
assert.Equal(t, "fieldPrefix-name", strings.Join(rv, ", "))
assert.Equal(t, "source", specTarget.Name)
}

0 comments on commit 04305ef

Please sign in to comment.