Skip to content

Instantly share code, notes, and snippets.

@chuckha
Created December 23, 2019 16:18
Show Gist options
  • Save chuckha/b01acc137b8b5cd8c0daa28e0bb10636 to your computer and use it in GitHub Desktop.
Save chuckha/b01acc137b8b5cd8c0daa28e0bb10636 to your computer and use it in GitHub Desktop.
removing external dependency
/*
Copyright 2019 The Kubernetes Authors.
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 external
import (
"context"
"testing"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
func setupScheme() *runtime.Scheme {
scheme := runtime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
panic(err)
}
return scheme
}
//
//func TestCloneTemplateError(t *testing.T) {
// // TODO: could reuse this struct and by convention always set the err here and never set it in the other tests, but I think this leaves less room for human interpretation/error.
// tests := []struct {
// name string
// clusterName string
// owner *metav1.OwnerReference
// objs []runtime.Object
// ref *corev1.ObjectReference
// namespace string
// want *corev1.ObjectReference
// wantClone *unstructured.Unstructured
// err error
// }{
// {
// name: "Referenced resource missing Spec.Template fields",
// objs: []runtime.Object{templateMissingSpecTemplate},
// ref: &corev1.ObjectReference{
// Kind: templateMissingSpecTemplate.GetKind(),
// APIVersion: templateMissingSpecTemplate.GetAPIVersion(),
// Name: templateMissingSpecTemplate.GetName(),
// Namespace: templateMissingSpecTemplate.GetNamespace(),
// },
// namespace: "test",
// want: nil,
// err: nil,
// },
// {
// name: "Referenced resource not found",
// objs: []runtime.Object{},
// ref: &corev1.ObjectReference{
// Kind: testTemplate.Kind,
// APIVersion: testTemplate.APIVersion,
// Name: testTemplate.Name,
// Namespace: testTemplate.Namespace,
// },
// namespace: "test",
// want: nil,
// err: nil,
// },
// }
//}
func TestCloneTemplate(t *testing.T) {
templateRef := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "MyKindTemplate",
"apiVersion": "my.group/v1alpha3",
"metadata": map[string]interface{}{
"name": "foo",
"namespace": "test",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"hello": "world",
},
},
},
}
tests := []struct {
name string
clusterName string
owner *metav1.OwnerReference
objs []runtime.Object
ref *corev1.ObjectReference
namespace string
want *corev1.ObjectReference
wantClone *unstructured.Unstructured
}{
{
name: "Referenced resource found",
objs: []runtime.Object{templateRef},
ref: &corev1.ObjectReference{
Kind: "MyKindTemplate",
APIVersion: "my.group/v1alpha3",
Name: "foo",
Namespace: "test",
},
namespace: "test",
clusterName: "testCluster",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeClient := fake.NewFakeClientWithScheme(setupScheme(), tt.objs...)
templateCloner := TemplateCloner{}
got, err := templateCloner.CloneTemplate(
context.Background(),
fakeClient,
tt.ref,
tt.namespace,
tt.clusterName,
tt.owner,
)
if err != nil {
t.Fatal(err)
}
clone := &unstructured.Unstructured{}
clone.SetKind("MyKind")
clone.SetAPIVersion("my.group/v1alpha3")
key := client.ObjectKey{Name: got.Name, Namespace: got.Namespace}
err = fakeClient.Get(context.Background(), key, clone)
if err != nil {
t.Fatal("failed to get clone")
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment