2015-05-29 20:34:02 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-05-29 20:34:02 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package volume
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-17 22:21:55 +00:00
|
|
|
"strings"
|
2015-05-29 20:34:02 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-05-19 10:58:25 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
2015-09-03 03:14:26 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
2015-05-29 20:34:02 +00:00
|
|
|
)
|
|
|
|
|
2015-09-03 03:14:26 +00:00
|
|
|
func TestRecyclerSuccess(t *testing.T) {
|
|
|
|
client := &mockRecyclerClient{}
|
|
|
|
recycler := &api.Pod{
|
2015-05-29 20:34:02 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: api.PodSucceeded,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:58:25 +00:00
|
|
|
err := internalRecycleVolumeByWatchingPodUntilCompletion("pv-name", recycler, client)
|
2015-05-29 20:34:02 +00:00
|
|
|
if err != nil {
|
2015-09-03 03:14:26 +00:00
|
|
|
t.Errorf("Unexpected error watching recycler pod: %+v", err)
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
|
|
|
if !client.deletedCalled {
|
2015-09-03 03:14:26 +00:00
|
|
|
t.Errorf("Expected deferred client.Delete to be called on recycler pod")
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:14:26 +00:00
|
|
|
func TestRecyclerFailure(t *testing.T) {
|
|
|
|
client := &mockRecyclerClient{}
|
|
|
|
recycler := &api.Pod{
|
2015-05-29 20:34:02 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: api.PodFailed,
|
|
|
|
Message: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:58:25 +00:00
|
|
|
err := internalRecycleVolumeByWatchingPodUntilCompletion("pv-name", recycler, client)
|
2015-05-29 20:34:02 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected pod failure but got nil error returned")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "foo") {
|
2015-09-03 03:14:26 +00:00
|
|
|
t.Errorf("Expected pod.Status.Message %s but got %s", recycler.Status.Message, err)
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !client.deletedCalled {
|
2015-09-03 03:14:26 +00:00
|
|
|
t.Errorf("Expected deferred client.Delete to be called on recycler pod")
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:58:25 +00:00
|
|
|
func TestRecyclerAlreadyExists(t *testing.T) {
|
|
|
|
// Test that internalRecycleVolumeByWatchingPodUntilCompletion does not
|
|
|
|
// start a new recycler when an old one is already running.
|
|
|
|
|
|
|
|
// Old recycler is running and fails with "foo" error message
|
|
|
|
oldRecycler := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "recycler-test",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: api.PodFailed,
|
|
|
|
Message: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// New recycler _would_ succeed if it was run
|
|
|
|
newRecycler := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "recycler-test",
|
|
|
|
Namespace: api.NamespaceDefault,
|
|
|
|
},
|
|
|
|
Status: api.PodStatus{
|
|
|
|
Phase: api.PodSucceeded,
|
|
|
|
Message: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &mockRecyclerClient{
|
|
|
|
pod: oldRecycler,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := internalRecycleVolumeByWatchingPodUntilCompletion("pv-name", newRecycler, client)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected pod failure but got nil error returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the recycler failed with "foo" error message, i.e. it was the
|
|
|
|
// old recycler that finished and not the new one.
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "foo") {
|
|
|
|
t.Errorf("Expected pod.Status.Message %s but got %s", oldRecycler.Status.Message, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !client.deletedCalled {
|
|
|
|
t.Errorf("Expected deferred client.Delete to be called on recycler pod")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:14:26 +00:00
|
|
|
type mockRecyclerClient struct {
|
2015-05-29 20:34:02 +00:00
|
|
|
pod *api.Pod
|
|
|
|
deletedCalled bool
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:14:26 +00:00
|
|
|
func (c *mockRecyclerClient) CreatePod(pod *api.Pod) (*api.Pod, error) {
|
2016-05-19 10:58:25 +00:00
|
|
|
if c.pod == nil {
|
|
|
|
c.pod = pod
|
|
|
|
return c.pod, nil
|
|
|
|
}
|
|
|
|
// Simulate "already exists" error
|
|
|
|
return nil, errors.NewAlreadyExists(api.Resource("pods"), pod.Name)
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 03:14:26 +00:00
|
|
|
func (c *mockRecyclerClient) GetPod(name, namespace string) (*api.Pod, error) {
|
2015-05-29 20:34:02 +00:00
|
|
|
if c.pod != nil {
|
|
|
|
return c.pod, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("pod does not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:14:26 +00:00
|
|
|
func (c *mockRecyclerClient) DeletePod(name, namespace string) error {
|
2015-05-29 20:34:02 +00:00
|
|
|
c.deletedCalled = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:58:25 +00:00
|
|
|
func (c *mockRecyclerClient) WatchPod(name, namespace string, stopChannel chan struct{}) func() *api.Pod {
|
2015-05-29 20:34:02 +00:00
|
|
|
return func() *api.Pod {
|
|
|
|
return c.pod
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 03:14:26 +00:00
|
|
|
|
|
|
|
func TestCalculateTimeoutForVolume(t *testing.T) {
|
|
|
|
pv := &api.PersistentVolume{
|
|
|
|
Spec: api.PersistentVolumeSpec{
|
|
|
|
Capacity: api.ResourceList{
|
|
|
|
api.ResourceName(api.ResourceStorage): resource.MustParse("500M"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := CalculateTimeoutForVolume(50, 30, pv)
|
|
|
|
if timeout != 50 {
|
|
|
|
t.Errorf("Expected 50 for timeout but got %v", timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
pv.Spec.Capacity[api.ResourceStorage] = resource.MustParse("2Gi")
|
|
|
|
timeout = CalculateTimeoutForVolume(50, 30, pv)
|
|
|
|
if timeout != 60 {
|
|
|
|
t.Errorf("Expected 60 for timeout but got %v", timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
pv.Spec.Capacity[api.ResourceStorage] = resource.MustParse("150Gi")
|
|
|
|
timeout = CalculateTimeoutForVolume(50, 30, pv)
|
|
|
|
if timeout != 4500 {
|
|
|
|
t.Errorf("Expected 4500 for timeout but got %v", timeout)
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 08:46:59 +00:00
|
|
|
|
|
|
|
func TestGenerateVolumeName(t *testing.T) {
|
|
|
|
|
|
|
|
// Normal operation, no truncate
|
|
|
|
v1 := GenerateVolumeName("kubernetes", "pv-cinder-abcde", 255)
|
|
|
|
if v1 != "kubernetes-dynamic-pv-cinder-abcde" {
|
|
|
|
t.Errorf("Expected kubernetes-dynamic-pv-cinder-abcde, got %s", v1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate trailing "6789-dynamic"
|
|
|
|
prefix := strings.Repeat("0123456789", 9) // 90 characters prefix + 8 chars. of "-dynamic"
|
|
|
|
v2 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
|
|
|
|
expect := prefix[:84] + "-pv-cinder-abcde"
|
|
|
|
if v2 != expect {
|
|
|
|
t.Errorf("Expected %s, got %s", expect, v2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate really long cluster name
|
|
|
|
prefix = strings.Repeat("0123456789", 1000) // 10000 characters prefix
|
|
|
|
v3 := GenerateVolumeName(prefix, "pv-cinder-abcde", 100)
|
|
|
|
if v3 != expect {
|
|
|
|
t.Errorf("Expected %s, got %s", expect, v3)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|