storageclass_test.go 7.2 KB
Newer Older
M
Marcin Niemira 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2019 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.
*/

M
Marcin Niemira 已提交
17 18 19
package storageclass

import (
P
Predrag Rogic 已提交
20
	"context"
M
Marcin Niemira 已提交
21 22 23
	"fmt"
	"io/ioutil"
	"os"
24
	"strings"
M
Marcin Niemira 已提交
25 26 27 28 29 30 31 32 33
	"testing"

	v1 "k8s.io/api/storage/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"
	storagev1 "k8s.io/client-go/kubernetes/typed/storage/v1"
	testing_client "k8s.io/client-go/testing"
)

M
Marcin Niemira 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
type mockStorageV1InterfaceOk struct {
	storagev1.StorageV1Interface
}
type mockStorageV1InterfaceListErr struct {
	storagev1.StorageV1Interface
}
type mockStorageV1InterfaceWithBadItem struct {
	storagev1.StorageV1Interface
}
type mockStorageClassInterfaceOk struct {
	storagev1.StorageClassInterface
}
type mockStorageClassInterfaceListErr struct {
	storagev1.StorageClassInterface
}
type mockStorageClassInterfaceWithBadItem struct {
	storagev1.StorageClassInterface
}
52

M
Marcin Niemira 已提交
53 54 55 56 57 58 59 60 61
func testStoragev1Ok() (storagev1.StorageV1Interface, error) {
	client := fake.Clientset{Fake: testing_client.Fake{}}
	return mockStorageV1InterfaceOk{client.StorageV1()}, nil
}
func testStoragev1ListErr() (storagev1.StorageV1Interface, error) {
	client := fake.Clientset{Fake: testing_client.Fake{}}
	return mockStorageV1InterfaceListErr{client.StorageV1()}, nil
}
func testStoragev1WithBadItem() (storagev1.StorageV1Interface, error) {
M
Marcin Niemira 已提交
62
	client := fake.Clientset{Fake: testing_client.Fake{}}
M
Marcin Niemira 已提交
63
	return mockStorageV1InterfaceWithBadItem{client.StorageV1()}, nil
M
Marcin Niemira 已提交
64 65
}

M
Marcin Niemira 已提交
66 67
func (mockStorageV1InterfaceOk) StorageClasses() storagev1.StorageClassInterface {
	return mockStorageClassInterfaceOk{}
M
Marcin Niemira 已提交
68 69
}

M
Marcin Niemira 已提交
70 71
func (mockStorageV1InterfaceListErr) StorageClasses() storagev1.StorageClassInterface {
	return mockStorageClassInterfaceListErr{}
M
Marcin Niemira 已提交
72 73
}

M
Marcin Niemira 已提交
74 75
func (mockStorageV1InterfaceWithBadItem) StorageClasses() storagev1.StorageClassInterface {
	return mockStorageClassInterfaceWithBadItem{}
M
Marcin Niemira 已提交
76 77
}

P
Predrag Rogic 已提交
78
func (mockStorageClassInterfaceOk) Get(ctx context.Context, name string, options metav1.GetOptions) (*v1.StorageClass, error) {
M
Marcin Niemira 已提交
79
	if strings.HasPrefix(name, "bad-class") {
M
Marcin Niemira 已提交
80 81 82 83 84 85
		return nil, fmt.Errorf("mocked error. No such class")
	}
	sc := v1.StorageClass{Provisioner: name}
	return &sc, nil
}

P
Predrag Rogic 已提交
86
func (m mockStorageClassInterfaceOk) List(ctx context.Context, opts metav1.ListOptions) (*v1.StorageClassList, error) {
M
Marcin Niemira 已提交
87 88 89 90 91
	scl := v1.StorageClassList{}
	sc := v1.StorageClass{Provisioner: "standard"}
	scl.Items = append(scl.Items, sc)
	return &scl, nil
}
92

P
Predrag Rogic 已提交
93
func (m mockStorageClassInterfaceWithBadItem) List(ctx context.Context, opts metav1.ListOptions) (*v1.StorageClassList, error) {
M
Marcin Niemira 已提交
94
	scl := v1.StorageClassList{}
M
Marcin Niemira 已提交
95 96
	sc := v1.StorageClass{Provisioner: "bad", ObjectMeta: metav1.ObjectMeta{Name: "standard"}}
	scl.Items = append(scl.Items, sc)
M
Marcin Niemira 已提交
97 98
	return &scl, nil
}
P
Predrag Rogic 已提交
99
func (mockStorageClassInterfaceListErr) List(ctx context.Context, opts metav1.ListOptions) (*v1.StorageClassList, error) {
M
Marcin Niemira 已提交
100 101
	return nil, fmt.Errorf("mocked list error")
}
M
Marcin Niemira 已提交
102

P
Predrag Rogic 已提交
103
func (mockStorageClassInterfaceOk) Update(ctx context.Context, sc *v1.StorageClass, opts metav1.UpdateOptions) (*v1.StorageClass, error) {
104 105 106
	if strings.HasPrefix(sc.Provisioner, "bad") {
		return nil, fmt.Errorf("bad provisioner")
	}
M
Marcin Niemira 已提交
107
	return &v1.StorageClass{}, nil
M
Marcin Niemira 已提交
108 109
}

P
Predrag Rogic 已提交
110
func (mockStorageClassInterfaceWithBadItem) Update(ctx context.Context, sc *v1.StorageClass, opts metav1.UpdateOptions) (*v1.StorageClass, error) {
M
Marcin Niemira 已提交
111 112 113 114 115 116 117
	if strings.HasPrefix(sc.Provisioner, "bad") {
		return nil, fmt.Errorf("bad provisioner")
	}
	return &v1.StorageClass{}, nil
}

func TestDisableDefaultStorageClass(t *testing.T) {
M
Marcin Niemira 已提交
118
	var tests = []struct {
M
Marcin Niemira 已提交
119 120 121 122
		description string
		class       string
		err         bool
		sv1Fixture  func() (storagev1.StorageV1Interface, error)
M
Marcin Niemira 已提交
123 124
	}{
		{
M
Marcin Niemira 已提交
125 126 127 128 129 130 131 132 133
			description: "ok",
			class:       "standard",
			sv1Fixture:  testStoragev1Ok,
		},
		{
			description: "no such class",
			class:       "bad-class",
			err:         true,
			sv1Fixture:  testStoragev1Ok,
M
Marcin Niemira 已提交
134 135
		},
		{
M
Marcin Niemira 已提交
136 137 138 139
			description: "bad existing class",
			class:       "bad-existing-class",
			err:         true,
			sv1Fixture:  testStoragev1Ok,
M
Marcin Niemira 已提交
140 141 142 143 144
		},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
M
Marcin Niemira 已提交
145 146
			sv1, _ := test.sv1Fixture()
			err := DisableDefaultStorageClass(sv1, test.class)
M
Marcin Niemira 已提交
147 148 149 150 151 152 153 154 155 156
			if err != nil && !test.err {
				t.Fatalf("Unexpected err: %v for test: %v", err, test.description)
			}
			if err == nil && test.err {
				t.Fatalf("Expected err for test: %v", test.description)
			}
		})
	}
}

M
Marcin Niemira 已提交
157
func TestSetDefaultStorageClass(t *testing.T) {
M
Marcin Niemira 已提交
158 159
	var tests = []struct {
		description string
M
Marcin Niemira 已提交
160
		class       string
M
Marcin Niemira 已提交
161
		err         bool
M
Marcin Niemira 已提交
162
		sv1Fixture  func() (storagev1.StorageV1Interface, error)
M
Marcin Niemira 已提交
163 164
	}{
		{
M
Marcin Niemira 已提交
165 166 167
			description: "ok (no fail)",
			class:       "standard",
			sv1Fixture:  testStoragev1Ok,
168 169
		},
		{
M
Marcin Niemira 已提交
170 171 172 173 174 175 176 177 178 179
			description: "ok (failed annotation)",
			class:       "standard",
			sv1Fixture:  testStoragev1WithBadItem,
			err:         true,
		},

		{
			description: "list error",
			class:       "standard",
			sv1Fixture:  testStoragev1ListErr,
180
			err:         true,
M
Marcin Niemira 已提交
181 182 183 184 185
		},
	}

	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
M
Marcin Niemira 已提交
186 187 188
			sv1, _ := test.sv1Fixture()

			err := SetDefaultStorageClass(sv1, test.class)
M
Marcin Niemira 已提交
189 190 191 192 193 194 195 196 197 198
			if err != nil && !test.err {
				t.Fatalf("Unexpected err: %v for test: %v", err, test.description)
			}
			if err == nil && test.err {
				t.Fatalf("Expected err for test: %v", test.description)
			}
		})
	}
}

M
Marcin Niemira 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
var mockK8sConfig = `apiVersion: v1
clusters:
- cluster:
    server: https://example.com:443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
`

func TestGetStoragev1(t *testing.T) {
217
	var tests = []struct {
M
Marcin Niemira 已提交
218 219 220
		description string
		config      string
		err         bool
221 222
	}{
		{
M
Marcin Niemira 已提交
223 224
			description: "ok",
			config:      mockK8sConfig,
225 226
		},
		{
M
Marcin Niemira 已提交
227 228 229
			description: "no valid config",
			config:      "this is not valid config",
			err:         true,
230 231
		},
	}
M
Marcin Niemira 已提交
232 233 234 235 236
	configFile, err := ioutil.TempFile("/tmp", "")
	if err != nil {
		t.Fatalf(err.Error())
	}
	defer os.Remove(configFile.Name())
237 238
	for _, test := range tests {
		t.Run(test.description, func(t *testing.T) {
M
Marcin Niemira 已提交
239
			if err := setK8SConfig(test.config, configFile.Name()); err != nil {
240 241 242
				t.Fatalf(err.Error())
			}

T
Thomas Stromberg 已提交
243 244
			// context name is hardcoded by mockK8sConfig
			_, err = GetStoragev1("minikube")
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
			if err != nil && !test.err {
				t.Fatalf("Unexpected err: %v for test: %v", err, test.description)
			}
			if err == nil && test.err {
				t.Fatalf("Expected err for test: %v", test.description)
			}
		})
	}
}

func setK8SConfig(config, kubeconfigPath string) error {
	mockK8sConfigByte := []byte(config)
	mockK8sConfigPath := kubeconfigPath
	err := ioutil.WriteFile(mockK8sConfigPath, mockK8sConfigByte, 0644)
	if err != nil {
		return fmt.Errorf("Unexpected error when writing to file %v. Error: %v", kubeconfigPath, err)
	}
	os.Setenv("KUBECONFIG", mockK8sConfigPath)
	return nil
}