From 04305ef6dfd75fb4e58dbb6f3d9f38d373a188a8 Mon Sep 17 00:00:00 2001 From: Jan Christoph Uhde Date: Thu, 29 Mar 2018 12:50:45 +0200 Subject: [PATCH] improve storage unit-tests --- .../v1alpha/local_storage_spec_test.go | 28 ++++++++----------- .../storage/v1alpha/local_storage_test.go | 7 +++-- .../v1alpha/storage_class_spec_test.go | 22 ++++++++------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/pkg/apis/storage/v1alpha/local_storage_spec_test.go b/pkg/apis/storage/v1alpha/local_storage_spec_test.go index d30a6a370..e315f2e6f 100644 --- a/pkg/apis/storage/v1alpha/local_storage_spec_test.go +++ b/pkg/apis/storage/v1alpha/local_storage_spec_test.go @@ -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{} diff --git a/pkg/apis/storage/v1alpha/local_storage_test.go b/pkg/apis/storage/v1alpha/local_storage_test.go index bb7adc212..d4b87cc56 100644 --- a/pkg/apis/storage/v1alpha/local_storage_test.go +++ b/pkg/apis/storage/v1alpha/local_storage_test.go @@ -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)) } diff --git a/pkg/apis/storage/v1alpha/storage_class_spec_test.go b/pkg/apis/storage/v1alpha/storage_class_spec_test.go index 951a1c15f..8285122cf 100644 --- a/pkg/apis/storage/v1alpha/storage_class_spec_test.go +++ b/pkg/apis/storage/v1alpha/storage_class_spec_test.go @@ -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) }