From 97afe568660459b4d7e3085e76cd278c5784131e Mon Sep 17 00:00:00 2001 From: Jan Christoph Uhde Date: Thu, 8 Mar 2018 12:30:33 +0100 Subject: [PATCH 1/3] begin to add tests for `storage/v1alpha` --- Makefile | 1 + .../v1alpha/local_storage_spec_test.go | 64 +++++++++++++++++++ .../storage/v1alpha/local_storage_test.go | 33 ++++++++++ .../v1alpha/storage_class_spec_test.go | 55 ++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 pkg/apis/storage/v1alpha/local_storage_spec_test.go create mode 100644 pkg/apis/storage/v1alpha/local_storage_test.go create mode 100644 pkg/apis/storage/v1alpha/storage_class_spec_test.go diff --git a/Makefile b/Makefile index 3088e7c0e..96269f8bb 100644 --- a/Makefile +++ b/Makefile @@ -209,6 +209,7 @@ run-unit-tests: $(GOBUILDDIR) $(SOURCES) golang:$(GOVERSION) \ go test $(TESTVERBOSEOPTIONS) \ $(REPOPATH)/pkg/apis/deployment/v1alpha \ + $(REPOPATH)/pkg/apis/storage/v1alpha \ $(REPOPATH)/pkg/deployment \ $(REPOPATH)/pkg/util/k8sutil \ $(REPOPATH)/pkg/util/k8sutil/test diff --git a/pkg/apis/storage/v1alpha/local_storage_spec_test.go b/pkg/apis/storage/v1alpha/local_storage_spec_test.go new file mode 100644 index 000000000..d30a6a370 --- /dev/null +++ b/pkg/apis/storage/v1alpha/local_storage_spec_test.go @@ -0,0 +1,64 @@ +// +// DISCLAIMER +// +// Copyright 2018 ArangoDB GmbH, Cologne, Germany +// +// 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. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// +// Author Jan Christoph Uhde +// + +package v1alpha + +import ( + "testing" + + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" +) + +func Test_LocalStorageSpec_Creation(t *testing.T) { + var ( + class StorageClassSpec + local LocalStorageSpec + err error + ) + + 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{"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)) + + class = StorageClassSpec{"spec-name", true} + local = LocalStorageSpec{StorageClass: class, LocalPath: []string{}} + err = local.Validate() + assert.Equal(t, ValidationError, errors.Cause(err)) //path empty +} + +func Test_LocalStorageSpec_Reset(t *testing.T) { + class := StorageClassSpec{"spec-name", true} + source := LocalStorageSpec{StorageClass: class, LocalPath: []string{"/a/path", "/another/path"}} + target := LocalStorageSpec{} + resetImmutableFieldsResult := source.ResetImmutableFields(&target) + expected := []string{"storageClass.name", "localPath"} + assert.Equal(t, expected, resetImmutableFieldsResult) + assert.Equal(t, source.LocalPath, target.LocalPath) + assert.Equal(t, source.StorageClass.Name, target.StorageClass.Name) +} diff --git a/pkg/apis/storage/v1alpha/local_storage_test.go b/pkg/apis/storage/v1alpha/local_storage_test.go new file mode 100644 index 000000000..bb7adc212 --- /dev/null +++ b/pkg/apis/storage/v1alpha/local_storage_test.go @@ -0,0 +1,33 @@ +// +// DISCLAIMER +// +// Copyright 2018 ArangoDB GmbH, Cologne, Germany +// +// 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. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// +// Author Jan Christoph Uhde +// + +package v1alpha + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_ArangoLocalStorage_Creation(t *testing.T) { + assert.True(t, true) +} diff --git a/pkg/apis/storage/v1alpha/storage_class_spec_test.go b/pkg/apis/storage/v1alpha/storage_class_spec_test.go new file mode 100644 index 000000000..951a1c15f --- /dev/null +++ b/pkg/apis/storage/v1alpha/storage_class_spec_test.go @@ -0,0 +1,55 @@ +// +// DISCLAIMER +// +// Copyright 2018 ArangoDB GmbH, Cologne, Germany +// +// 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. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// +// Author Jan Christoph Uhde +// + +package v1alpha + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_StorageClassSpec_Creation(t *testing.T) { + storageClassSpec := StorageClassSpec{} + assert.True(t, nil != storageClassSpec.Validate()) + + storageClassSpec = StorageClassSpec{Name: "TheSpecName", IsDefault: true} // no upper-case allowed + assert.True(t, nil != storageClassSpec.Validate()) + + storageClassSpec = StorageClassSpec{"the-spec-name", true} + assert.Equal(t, nil, 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()) +} + +func Test_StorageClassSpec_ResetImmutableFileds(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, "source", specTarget.Name) +} From 8a59df6d8a9350102cc169c8cb019e7a29cad314 Mon Sep 17 00:00:00 2001 From: Jan Christoph Uhde Date: Thu, 29 Mar 2018 12:50:45 +0200 Subject: [PATCH 2/3] improve storage unit-tests --- .../v1alpha/local_storage_spec_test.go | 28 ++++++++----------- .../storage/v1alpha/local_storage_test.go | 9 ++++-- .../v1alpha/storage_class_spec_test.go | 22 ++++++++------- 3 files changed, 30 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..6bb505d04 100644 --- a/pkg/apis/storage/v1alpha/local_storage_test.go +++ b/pkg/apis/storage/v1alpha/local_storage_test.go @@ -28,6 +28,11 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_ArangoLocalStorage_Creation(t *testing.T) { - assert.True(t, true) +// test creation of arango local storage +func TestArangoLocalStorageCreation(t *testing.T) { + // REVIEW - is there something more meaningful to test + 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) } From 486163bdabb10669cc21c97e05bd5e9cd1c79fa0 Mon Sep 17 00:00:00 2001 From: Jan Christoph Uhde Date: Thu, 29 Mar 2018 14:56:21 +0200 Subject: [PATCH 3/3] delete useless test --- .../storage/v1alpha/local_storage_test.go | 38 ------------------- 1 file changed, 38 deletions(-) delete mode 100644 pkg/apis/storage/v1alpha/local_storage_test.go diff --git a/pkg/apis/storage/v1alpha/local_storage_test.go b/pkg/apis/storage/v1alpha/local_storage_test.go deleted file mode 100644 index 6bb505d04..000000000 --- a/pkg/apis/storage/v1alpha/local_storage_test.go +++ /dev/null @@ -1,38 +0,0 @@ -// -// DISCLAIMER -// -// Copyright 2018 ArangoDB GmbH, Cologne, Germany -// -// 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. -// -// Copyright holder is ArangoDB GmbH, Cologne, Germany -// -// Author Jan Christoph Uhde -// - -package v1alpha - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -// test creation of arango local storage -func TestArangoLocalStorageCreation(t *testing.T) { - // REVIEW - is there something more meaningful to test - storage := ArangoLocalStorage{} - list := ArangoLocalStorageList{} - list.Items = append(list.Items, storage) - assert.Equal(t, 1, len(list.Items)) -}