extract shared API test helpers to pkg/test

Signed-off-by: Steve Kriss <krisss@vmware.com>
pull/1595/head
Steve Kriss 2019-06-20 14:28:11 -06:00
parent 870743a28d
commit 0735ee7218
4 changed files with 599 additions and 517 deletions

File diff suppressed because it is too large Load Diff

57
pkg/test/api_server.go Normal file
View File

@ -0,0 +1,57 @@
/*
Copyright 2019 the Velero contributors.
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 test
import (
"testing"
"k8s.io/apimachinery/pkg/runtime"
discoveryfake "k8s.io/client-go/discovery/fake"
dynamicfake "k8s.io/client-go/dynamic/fake"
kubefake "k8s.io/client-go/kubernetes/fake"
"github.com/heptio/velero/pkg/generated/clientset/versioned/fake"
)
// APIServer contains in-memory fakes for all of the relevant
// Kubernetes API server clients.
type APIServer struct {
VeleroClient *fake.Clientset
KubeClient *kubefake.Clientset
DynamicClient *dynamicfake.FakeDynamicClient
DiscoveryClient *DiscoveryClient
}
// NewAPIServer constructs an APIServer with all of its clients
// initialized.
func NewAPIServer(t *testing.T) *APIServer {
t.Helper()
var (
veleroClient = fake.NewSimpleClientset()
kubeClient = kubefake.NewSimpleClientset()
dynamicClient = dynamicfake.NewSimpleDynamicClient(runtime.NewScheme())
discoveryClient = &DiscoveryClient{FakeDiscovery: kubeClient.Discovery().(*discoveryfake.FakeDiscovery)}
)
return &APIServer{
VeleroClient: veleroClient,
KubeClient: kubeClient,
DynamicClient: dynamicClient,
DiscoveryClient: discoveryClient,
}
}

View File

@ -38,10 +38,11 @@ func (c *DiscoveryClient) ServerPreferredResources() ([]*metav1.APIResourceList,
// TEST HELPERS
//
func (c *DiscoveryClient) WithResource(group, version, resource string, namespaced bool, shortNames ...string) *DiscoveryClient {
// WithAPIResource adds the API resource to the discovery client.
func (c *DiscoveryClient) WithAPIResource(resource *APIResource) *DiscoveryClient {
gv := metav1.GroupVersion{
Group: group,
Version: version,
Group: resource.Group,
Version: resource.Version,
}
var resourceList *metav1.APIResourceList
@ -61,20 +62,20 @@ func (c *DiscoveryClient) WithResource(group, version, resource string, namespac
}
for _, itm := range resourceList.APIResources {
if itm.Name == resource {
if itm.Name == resource.Name {
return c
}
}
resourceList.APIResources = append(resourceList.APIResources, metav1.APIResource{
Name: resource,
SingularName: strings.TrimSuffix(resource, "s"),
Namespaced: namespaced,
Group: group,
Version: version,
Kind: strings.Title(strings.TrimSuffix(resource, "s")),
Name: resource.Name,
SingularName: strings.TrimSuffix(resource.Name, "s"),
Namespaced: resource.Namespaced,
Group: resource.Group,
Version: resource.Version,
Kind: strings.Title(strings.TrimSuffix(resource.Name, "s")),
Verbs: metav1.Verbs([]string{"list", "create", "get", "delete"}),
ShortNames: shortNames,
ShortNames: []string{resource.ShortName},
})
return c

168
pkg/test/resources.go Normal file
View File

@ -0,0 +1,168 @@
/*
Copyright 2019 the Velero contributors.
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 test
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// APIResource stores information about a specific Kubernetes API
// resource.
type APIResource struct {
Group string
Version string
Name string
ShortName string
Namespaced bool
Items []metav1.Object
}
// GVR returns a GroupVersionResource representing the resource.
func (r *APIResource) GVR() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: r.Group,
Version: r.Version,
Resource: r.Name,
}
}
// Pods returns an APIResource describing core/v1's Pods.
func Pods(items ...metav1.Object) *APIResource {
return &APIResource{
Group: "",
Version: "v1",
Name: "pods",
ShortName: "po",
Namespaced: true,
Items: items,
}
}
func PVCs(items ...metav1.Object) *APIResource {
return &APIResource{
Group: "",
Version: "v1",
Name: "persistentvolumeclaims",
ShortName: "pvc",
Namespaced: true,
Items: items,
}
}
func PVs(items ...metav1.Object) *APIResource {
return &APIResource{
Group: "",
Version: "v1",
Name: "persistentvolumes",
ShortName: "pv",
Namespaced: false,
Items: items,
}
}
func Secrets(items ...metav1.Object) *APIResource {
return &APIResource{
Group: "",
Version: "v1",
Name: "secrets",
ShortName: "secrets",
Namespaced: true,
Items: items,
}
}
func Deployments(items ...metav1.Object) *APIResource {
return &APIResource{
Group: "apps",
Version: "v1",
Name: "deployments",
ShortName: "deploy",
Namespaced: true,
Items: items,
}
}
func ExtensionsDeployments(items ...metav1.Object) *APIResource {
return &APIResource{
Group: "extensions",
Version: "v1",
Name: "deployments",
ShortName: "deploy",
Namespaced: true,
Items: items,
}
}
func NewPod(ns, name string) *corev1.Pod {
return &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: objectMeta(ns, name),
}
}
func NewPVC(ns, name string) *corev1.PersistentVolumeClaim {
return &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{
Kind: "PersistentVolumeClaim",
APIVersion: "v1",
},
ObjectMeta: objectMeta(ns, name),
}
}
func NewPV(name string) *corev1.PersistentVolume {
return &corev1.PersistentVolume{
TypeMeta: metav1.TypeMeta{
Kind: "PersistentVolume",
APIVersion: "v1",
},
ObjectMeta: objectMeta("", name),
}
}
func NewSecret(ns, name string) *corev1.Secret {
return &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
ObjectMeta: objectMeta(ns, name),
}
}
func NewDeployment(ns, name string) *appsv1.Deployment {
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
},
ObjectMeta: objectMeta(ns, name),
}
}
func objectMeta(ns, name string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Namespace: ns,
Name: name,
}
}