github.com/argoproj/argo-events@v1.9.1/sensors/triggers/argo-workflow/argo-workflow_test.go (about)

     1  /*
     2  Copyright 2020 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package argo_workflow
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"os/exec"
    22  	"testing"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  	"k8s.io/client-go/dynamic"
    26  
    27  	"github.com/argoproj/argo-events/common/logging"
    28  	apicommon "github.com/argoproj/argo-events/pkg/apis/common"
    29  	"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
    30  	"github.com/stretchr/testify/assert"
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    33  	"k8s.io/apimachinery/pkg/runtime"
    34  	dynamicFake "k8s.io/client-go/dynamic/fake"
    35  	"k8s.io/client-go/kubernetes/fake"
    36  )
    37  
    38  var sensorObj = &v1alpha1.Sensor{
    39  	ObjectMeta: metav1.ObjectMeta{
    40  		Name:      "fake-sensor",
    41  		Namespace: "fake",
    42  	},
    43  	Spec: v1alpha1.SensorSpec{
    44  		Triggers: []v1alpha1.Trigger{
    45  			{
    46  				Template: &v1alpha1.TriggerTemplate{
    47  					Name: "fake-trigger",
    48  					K8s:  &v1alpha1.StandardK8STrigger{},
    49  				},
    50  			},
    51  		},
    52  	},
    53  }
    54  
    55  var (
    56  	un = newUnstructured("argoproj.io/v1alpha1", "Workflow", "fake", "test")
    57  )
    58  
    59  func newUnstructured(apiVersion, kind, namespace, name string) *unstructured.Unstructured {
    60  	return &unstructured.Unstructured{
    61  		Object: map[string]interface{}{
    62  			"apiVersion": apiVersion,
    63  			"kind":       kind,
    64  			"metadata": map[string]interface{}{
    65  				"namespace": namespace,
    66  				"name":      name,
    67  				"labels": map[string]interface{}{
    68  					"name": name,
    69  				},
    70  			},
    71  		},
    72  	}
    73  }
    74  
    75  func getFakeWfTrigger(operation v1alpha1.ArgoWorkflowOperation) *ArgoWorkflowTrigger {
    76  	runtimeScheme := runtime.NewScheme()
    77  	client := dynamicFake.NewSimpleDynamicClient(runtimeScheme)
    78  	artifact := apicommon.NewResource(un)
    79  	trigger := &v1alpha1.Trigger{
    80  		Template: &v1alpha1.TriggerTemplate{
    81  			Name: "fake",
    82  			ArgoWorkflow: &v1alpha1.ArgoWorkflowTrigger{
    83  				Source: &v1alpha1.ArtifactLocation{
    84  					Resource: &artifact,
    85  				},
    86  				Operation: operation,
    87  			},
    88  		},
    89  	}
    90  	return NewArgoWorkflowTrigger(fake.NewSimpleClientset(), client, sensorObj.DeepCopy(), trigger, logging.NewArgoEventsLogger())
    91  }
    92  
    93  func TestFetchResource(t *testing.T) {
    94  	trigger := getFakeWfTrigger("submit")
    95  	resource, err := trigger.FetchResource(context.TODO())
    96  	assert.Nil(t, err)
    97  	assert.NotNil(t, resource)
    98  	obj, ok := resource.(*unstructured.Unstructured)
    99  	assert.Equal(t, true, ok)
   100  	assert.Equal(t, "test", obj.GetName())
   101  	assert.Equal(t, "Workflow", obj.GroupVersionKind().Kind)
   102  }
   103  
   104  func TestApplyResourceParameters(t *testing.T) {
   105  
   106  }
   107  
   108  func TestExecute(t *testing.T) {
   109  	t.Run("passes trigger args as flags to argo command", func(t *testing.T) {
   110  		ctx := context.Background()
   111  		var actual string
   112  		firstArg := "--foo"
   113  		secondArg := "--bar"
   114  		trigger := storingCmdTrigger(&actual, firstArg, secondArg)
   115  
   116  		_, err := namespacedClientFrom(trigger).Namespace(un.GetNamespace()).Create(ctx, un, metav1.CreateOptions{})
   117  		assert.Nil(t, err)
   118  
   119  		_, err = trigger.Execute(ctx, nil, un)
   120  		assert.Nil(t, err)
   121  
   122  		expected := fmt.Sprintf("argo -n %s resume test %s %s", un.GetNamespace(), firstArg, secondArg)
   123  		assert.Contains(t, actual, expected)
   124  	})
   125  }
   126  
   127  func storingCmdTrigger(cmdStr *string, wfArgs ...string) *ArgoWorkflowTrigger {
   128  	trigger := getFakeWfTrigger("resume")
   129  	f := func(cmd *exec.Cmd) error {
   130  		*cmdStr = cmd.String()
   131  		return nil
   132  	}
   133  	trigger.cmdRunner = f
   134  	trigger.Trigger.Template.ArgoWorkflow.Args = wfArgs
   135  
   136  	return trigger
   137  }
   138  
   139  func namespacedClientFrom(trigger *ArgoWorkflowTrigger) dynamic.NamespaceableResourceInterface {
   140  	return trigger.DynamicClient.Resource(schema.GroupVersionResource{
   141  		Group:    "argoproj.io",
   142  		Version:  "v1alpha1",
   143  		Resource: "workflows",
   144  	})
   145  }