Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Mar 13, 2019
1 parent ec19c5a commit eb01adc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
4 changes: 3 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions sensors/event-handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
var sensorStr = `apiVersion: argoproj.io/v1alpha1
kind: Sensor
metadata:
namespace: argo-events
name: test-sensor
labels:
sensors.argoproj.io/sensor-controller-instanceid: argo-events
Expand Down Expand Up @@ -78,6 +79,13 @@ spec:
image: "docker/whalesay:latest"
name: whalesay`

var podResourceList = metav1.APIResourceList{
GroupVersion: metav1.GroupVersion{Group: "", Version: "v1"}.String(),
APIResources: []metav1.APIResource{
{Kind: "Pod", Namespaced: true, Name: "pods", SingularName: "pod", Group: "", Version: "v1", Verbs: []string{"create", "get"}},
},
}

func getSensor() (*v1alpha1.Sensor, error) {
var sensor v1alpha1.Sensor
err := yaml.Unmarshal([]byte(sensorStr), &sensor)
Expand All @@ -101,15 +109,10 @@ func (m *mockHttpWriter) WriteHeader(statusCode int) {
func getsensorExecutionCtx(sensor *v1alpha1.Sensor) *sensorExecutionCtx {
kubeClientset := fake.NewSimpleClientset()
fakeDiscoveryClient := kubeClientset.Discovery().(*discoveryFake.FakeDiscovery)
resourceList := &metav1.APIResourceList{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
GroupVersion: metav1.GroupVersion{Group: "", Version: "v1"}.String(),
APIResources: []metav1.APIResource{{Kind: "Pod"}},
}
clientPool := &FakeClientPool{
kubeClientset.Fake,
Fake: kubeClientset.Fake,
}
fakeDiscoveryClient.Resources = append(fakeDiscoveryClient.Resources, resourceList)
fakeDiscoveryClient.Resources = append(fakeDiscoveryClient.Resources, &podResourceList)
return &sensorExecutionCtx{
kubeClient: kubeClientset,
discoveryClient: fakeDiscoveryClient,
Expand Down
63 changes: 36 additions & 27 deletions sensors/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
dynamicfake "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes/fake"
kTesting "k8s.io/client-go/testing"
"k8s.io/client-go/util/flowcontrol"
Expand Down Expand Up @@ -197,30 +198,23 @@ func (p *FakeClientPool) ClientForGroupVersionKind(kind schema.GroupVersionKind)
}

var testWf = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
apiVersion: v1
kind: Pod
metadata:
generateName: hello-world-
spec:
entrypoint: whalesay
templates:
- name: whalesay
container:
args:
- "hello world"
command:
- cowsay
image: "docker/whalesay:latest"
containers:
- name: whalesay
image: "docker/whalesay:latest"
`

var testTrigger = v1alpha1.Trigger{
Name: "sample",
Resource: &v1alpha1.ResourceObject{
Namespace: corev1.NamespaceDefault,
GroupVersionKind: v1alpha1.GroupVersionKind{
Group: "argoproj.io",
Version: "v1alpha1",
Kind: "workflow",
Version: "v1",
Kind: "Pod",
},
Source: v1alpha1.ArtifactLocation{
Inline: &testWf,
Expand All @@ -247,35 +241,50 @@ func TestCreateResourceObject(t *testing.T) {
testSensor, err := getSensor()
convey.So(err, convey.ShouldBeNil)
soc := getsensorExecutionCtx(testSensor)

rObj := testTrigger.Resource.DeepCopy()
fakeclient := soc.clientPool.(*FakeClientPool).Fake
dynamicClient := dynamicfake.FakeResourceClient{Resource: schema.GroupVersionResource{Version: "v1", Resource: "pods"}, Fake: &fakeclient}

convey.Convey("Given a pod", func() {
namespace := "foo"
rObj := testTrigger.Resource.DeepCopy()
rObj.Namespace = "foo"
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: "my-pod"},
ObjectMeta: metav1.ObjectMeta{Namespace: rObj.Namespace, Name: "my-pod"},
}
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod)
uObj, err := getUnstructuredPod(pod)
convey.So(err, convey.ShouldBeNil)
err = soc.createResourceObject(rObj, &unstructured.Unstructured{Object: uObj})

err = soc.createResourceObject(rObj, uObj)
convey.So(err, convey.ShouldBeNil)
pod, err = soc.kubeClient.CoreV1().Pods(namespace).Get(pod.Name, metav1.GetOptions{})

unstructuredPod, err := dynamicClient.Get(pod.Name, metav1.GetOptions{})
convey.So(err, convey.ShouldBeNil)
convey.So(pod.Namespace, convey.ShouldEqual, namespace)
convey.So(unstructuredPod.GetNamespace(), convey.ShouldEqual, rObj.Namespace)
})
convey.Convey("Given a pod without namespace, use sensor namespace", func() {
rObj := testTrigger.Resource.DeepCopy()
rObj.Namespace = ""
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "my-pod"},
ObjectMeta: metav1.ObjectMeta{Name: "my-pod-without-namespace"},
}
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod)
uObj, err := getUnstructuredPod(pod)
convey.So(err, convey.ShouldBeNil)
err = soc.createResourceObject(rObj, &unstructured.Unstructured{Object: uObj})

err = soc.createResourceObject(rObj, uObj)
convey.So(err, convey.ShouldBeNil)
pod, err = soc.kubeClient.CoreV1().Pods(testSensor.Namespace).Get(pod.Name, metav1.GetOptions{})

unstructuredPod, err := dynamicClient.Get(pod.Name, metav1.GetOptions{})
convey.So(err, convey.ShouldBeNil)
convey.So(pod.Namespace, convey.ShouldEqual, testSensor.Namespace)
convey.So(unstructuredPod.GetNamespace(), convey.ShouldEqual, testSensor.Namespace)
})
})
}

func getUnstructuredPod(pod *corev1.Pod) (*unstructured.Unstructured, error) {
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: obj}, nil
}

0 comments on commit eb01adc

Please sign in to comment.