k3s/pkg/volume/util_test.go

131 lines
3.3 KiB
Go
Raw Normal View History

2015-05-29 20:34:02 +00:00
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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"
"strings"
2015-05-29 20:34:02 +00:00
"testing"
2015-08-05 22:03:47 +00:00
"k8s.io/kubernetes/pkg/api"
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{
2015-09-03 03:14:26 +00:00
Name: "recycler-test",
2015-05-29 20:34:02 +00:00
Namespace: api.NamespaceDefault,
},
Status: api.PodStatus{
Phase: api.PodSucceeded,
},
}
2015-09-03 03:14:26 +00:00
err := internalRecycleVolumeByWatchingPodUntilCompletion(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{
2015-09-03 03:14:26 +00:00
Name: "recycler-test",
2015-05-29 20:34:02 +00:00
Namespace: api.NamespaceDefault,
},
Status: api.PodStatus{
Phase: api.PodFailed,
Message: "foo",
},
}
2015-09-03 03:14:26 +00:00
err := internalRecycleVolumeByWatchingPodUntilCompletion(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
}
}
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) {
2015-05-29 20:34:02 +00:00
c.pod = pod
return c.pod, nil
}
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
}
2015-09-03 03:14:26 +00:00
func (c *mockRecyclerClient) WatchPod(name, namespace, resourceVersion 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)
}
}