Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

begin to add tests for apis/storage/v1alpha #36

Merged
merged 4 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ run-unit-tests: $(GOBUILDDIR) $(SOURCES)
golang:$(GOVERSION) \
go test $(TESTVERBOSEOPTIONS) \
$(REPOPATH)/pkg/apis/deployment/v1alpha \
$(REPOPATH)/pkg/apis/storage/v1alpha \
$(REPOPATH)/pkg/deployment/reconcile \
$(REPOPATH)/pkg/deployment/resources \
$(REPOPATH)/pkg/util/k8sutil \
Expand Down
64 changes: 64 additions & 0 deletions pkg/apis/storage/v1alpha/local_storage_spec_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid the use of underscore in member names.

var (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop this and do something like class := ... on first use

class StorageClassSpec
local LocalStorageSpec
err error
)

class = StorageClassSpec{"SpecName", true}
local = LocalStorageSpec{StorageClass: class, LocalPath: []string{""}}
err = local.Validate()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't understand this and the next line.
Probably you want to use assert.Error or assert.NoError see https://godoc.org/github.com/stretchr/testify/assert#Error

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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably indicates a bug.

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)
}
33 changes: 33 additions & 0 deletions pkg/apis/storage/v1alpha/local_storage_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
55 changes: 55 additions & 0 deletions pkg/apis/storage/v1alpha/storage_class_spec_test.go
Original file line number Diff line number Diff line change
@@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert.Error


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[:], ", "))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go: no need for rv[:], just rv is enough here

assert.Equal(t, "source", specTarget.Name)
}