-
Notifications
You must be signed in to change notification settings - Fork 73
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
Added unit tests for pv_creator.go #166
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// 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 Ewout Prangsma | ||
// | ||
|
||
package mocks | ||
|
||
import ( | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type MockGetter interface { | ||
AsMock() *mock.Mock | ||
} | ||
|
||
// AsMock performs a typeconversion to *Mock. | ||
func AsMock(obj interface{}) *mock.Mock { | ||
return obj.(MockGetter).AsMock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// 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 Ewout Prangsma | ||
// | ||
|
||
package mocks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/arangodb/kube-arangodb/pkg/storage/provisioner" | ||
) | ||
|
||
type Provisioner interface { | ||
provisioner.API | ||
MockGetter | ||
} | ||
|
||
type provisionerMock struct { | ||
mock.Mock | ||
nodeName string | ||
available, capacity int64 | ||
localPaths map[string]struct{} | ||
} | ||
|
||
// NewProvisioner returns a new mocked provisioner | ||
func NewProvisioner(nodeName string, available, capacity int64) Provisioner { | ||
return &provisionerMock{ | ||
nodeName: nodeName, | ||
available: available, | ||
capacity: capacity, | ||
localPaths: make(map[string]struct{}), | ||
} | ||
} | ||
|
||
func (m *provisionerMock) AsMock() *mock.Mock { | ||
return &m.Mock | ||
} | ||
|
||
// GetNodeInfo fetches information from the current node. | ||
func (m *provisionerMock) GetNodeInfo(ctx context.Context) (provisioner.NodeInfo, error) { | ||
return provisioner.NodeInfo{ | ||
NodeName: m.nodeName, | ||
}, nil | ||
} | ||
|
||
// GetInfo fetches information from the filesystem containing | ||
// the given local path on the current node. | ||
func (m *provisionerMock) GetInfo(ctx context.Context, localPath string) (provisioner.Info, error) { | ||
return provisioner.Info{ | ||
NodeInfo: provisioner.NodeInfo{ | ||
NodeName: m.nodeName, | ||
}, | ||
Available: m.available, | ||
Capacity: m.capacity, | ||
}, nil | ||
} | ||
|
||
// Prepare a volume at the given local path | ||
func (m *provisionerMock) Prepare(ctx context.Context, localPath string) error { | ||
if _, found := m.localPaths[localPath]; found { | ||
return fmt.Errorf("Path already exists: %s", localPath) | ||
} | ||
m.localPaths[localPath] = struct{}{} | ||
return nil | ||
} | ||
|
||
// Remove a volume with the given local path | ||
func (m *provisionerMock) Remove(ctx context.Context, localPath string) error { | ||
if _, found := m.localPaths[localPath]; !found { | ||
return fmt.Errorf("Path not found: %s", localPath) | ||
} | ||
delete(m.localPaths, localPath) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
// | ||
// 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 Ewout Prangsma | ||
// | ||
|
||
package storage | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/arangodb/kube-arangodb/pkg/storage/provisioner" | ||
"github.com/arangodb/kube-arangodb/pkg/storage/provisioner/mocks" | ||
) | ||
|
||
// TestCreateValidEndpointList tests createValidEndpointList. | ||
func TestCreateValidEndpointList(t *testing.T) { | ||
tests := []struct { | ||
Input *v1.EndpointsList | ||
Expected []string | ||
}{ | ||
{ | ||
Input: &v1.EndpointsList{}, | ||
Expected: []string{}, | ||
}, | ||
{ | ||
Input: &v1.EndpointsList{ | ||
Items: []v1.Endpoints{ | ||
v1.Endpoints{ | ||
Subsets: []v1.EndpointSubset{ | ||
v1.EndpointSubset{ | ||
Addresses: []v1.EndpointAddress{ | ||
v1.EndpointAddress{ | ||
IP: "1.2.3.4", | ||
}, | ||
}, | ||
}, | ||
v1.EndpointSubset{ | ||
Addresses: []v1.EndpointAddress{ | ||
v1.EndpointAddress{ | ||
IP: "5.6.7.8", | ||
}, | ||
v1.EndpointAddress{ | ||
IP: "9.10.11.12", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Expected: []string{ | ||
"1.2.3.4:8929", | ||
"5.6.7.8:8929", | ||
"9.10.11.12:8929", | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
output := createValidEndpointList(test.Input) | ||
assert.Equal(t, test.Expected, output) | ||
} | ||
} | ||
|
||
// TestCreateNodeAffinity tests createNodeAffinity. | ||
func TestCreateNodeAffinity(t *testing.T) { | ||
tests := map[string]string{ | ||
"foo": "{\"requiredDuringSchedulingIgnoredDuringExecution\":{\"nodeSelectorTerms\":[{\"matchExpressions\":[{\"key\":\"kubernetes.io/hostname\",\"operator\":\"In\",\"values\":[\"foo\"]}]}]}}", | ||
"bar": "{\"requiredDuringSchedulingIgnoredDuringExecution\":{\"nodeSelectorTerms\":[{\"matchExpressions\":[{\"key\":\"kubernetes.io/hostname\",\"operator\":\"In\",\"values\":[\"bar\"]}]}]}}", | ||
} | ||
for input, expected := range tests { | ||
output, err := createNodeAffinity(input) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expected, output, "Input: '%s'", input) | ||
} | ||
} | ||
|
||
// TestCreateNodeClientMap tests createNodeClientMap. | ||
func TestCreateNodeClientMap(t *testing.T) { | ||
GB := int64(1024 * 1024 * 1024) | ||
foo := mocks.NewProvisioner("foo", 100*GB, 100*GB) | ||
bar := mocks.NewProvisioner("bar", 100*GB, 100*GB) | ||
tests := []struct { | ||
Input []provisioner.API | ||
Expected map[string]provisioner.API | ||
}{ | ||
{ | ||
Input: nil, | ||
Expected: map[string]provisioner.API{}, | ||
}, | ||
{ | ||
Input: []provisioner.API{foo, bar}, | ||
Expected: map[string]provisioner.API{ | ||
"bar": bar, | ||
"foo": foo, | ||
}, | ||
}, | ||
} | ||
ctx := context.Background() | ||
for _, test := range tests { | ||
output := createNodeClientMap(ctx, test.Input) | ||
assert.Equal(t, test.Expected, output) | ||
} | ||
} | ||
|
||
// TestGetDeploymentInfo tests getDeploymentInfo. | ||
func TestGetDeploymentInfo(t *testing.T) { | ||
tests := []struct { | ||
Input v1.PersistentVolumeClaim | ||
ExpectedDeploymentName string | ||
ExpectedRole string | ||
ExpectedEnforceAntiAffinity bool | ||
}{ | ||
{ | ||
Input: v1.PersistentVolumeClaim{}, | ||
ExpectedDeploymentName: "", | ||
ExpectedRole: "", | ||
ExpectedEnforceAntiAffinity: false, | ||
}, | ||
{ | ||
Input: v1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
"database.arangodb.com/enforce-anti-affinity": "true", | ||
}, | ||
Labels: map[string]string{ | ||
"arango_deployment": "foo", | ||
"role": "r1", | ||
}, | ||
}, | ||
}, | ||
ExpectedDeploymentName: "foo", | ||
ExpectedRole: "r1", | ||
ExpectedEnforceAntiAffinity: true, | ||
}, | ||
{ | ||
Input: v1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
"database.arangodb.com/enforce-anti-affinity": "false", | ||
}, | ||
Labels: map[string]string{ | ||
"arango_deployment": "foo", | ||
"role": "r1", | ||
}, | ||
}, | ||
}, | ||
ExpectedDeploymentName: "foo", | ||
ExpectedRole: "r1", | ||
ExpectedEnforceAntiAffinity: false, | ||
}, | ||
{ | ||
Input: v1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Annotations: map[string]string{ | ||
"database.arangodb.com/enforce-anti-affinity": "wrong", | ||
}, | ||
Labels: map[string]string{ | ||
"arango_deployment": "bar", | ||
"role": "r77", | ||
}, | ||
}, | ||
}, | ||
ExpectedDeploymentName: "bar", | ||
ExpectedRole: "r77", | ||
ExpectedEnforceAntiAffinity: false, | ||
}, | ||
} | ||
for _, test := range tests { | ||
deploymentName, role, enforceAntiAffinity := getDeploymentInfo(test.Input) | ||
assert.Equal(t, test.ExpectedDeploymentName, deploymentName) | ||
assert.Equal(t, test.ExpectedRole, role) | ||
assert.Equal(t, test.ExpectedEnforceAntiAffinity, enforceAntiAffinity) | ||
} | ||
} | ||
|
||
// TestShortHash tests shortHash. | ||
func TestShortHash(t *testing.T) { | ||
tests := map[string]string{ | ||
"foo": "0beec7", | ||
"": "da39a3", | ||
"something very very very very very looooooooooooooooooooooooooooooooong": "68ff76", | ||
} | ||
for input, expected := range tests { | ||
output := shortHash(input) | ||
assert.Equal(t, expected, output, "Input: '%s'", input) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trivial: my preference would be to have four different values instead of 100*GB for all. What if you happen to have one of parameters backwards some place in the code?