github.com/argoproj/argo-events@v1.9.1/controllers/eventsource/resource_test.go (about)

     1  package eventsource
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	appv1 "k8s.io/api/apps/v1"
    10  	corev1 "k8s.io/api/core/v1"
    11  	"sigs.k8s.io/controller-runtime/pkg/client"
    12  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    13  
    14  	"github.com/argoproj/argo-events/common/logging"
    15  	apicommon "github.com/argoproj/argo-events/pkg/apis/common"
    16  	eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1"
    17  	"github.com/argoproj/argo-events/pkg/apis/eventsource/v1alpha1"
    18  )
    19  
    20  const (
    21  	testImage = "test-image"
    22  )
    23  
    24  var (
    25  	testLabels = map[string]string{"controller": "test-controller"}
    26  )
    27  
    28  func Test_BuildDeployment(t *testing.T) {
    29  	testEventSource := fakeEmptyEventSource()
    30  	testEventSource.Spec.HDFS = fakeHDFSEventSourceMap("test")
    31  	testEventSource.Spec.Template = &v1alpha1.Template{
    32  		ImagePullSecrets: []corev1.LocalObjectReference{
    33  			{
    34  				Name: "test",
    35  			},
    36  		},
    37  		PriorityClassName: "test-class",
    38  	}
    39  	t.Run("test build HDFS", func(t *testing.T) {
    40  		args := &AdaptorArgs{
    41  			Image:       testImage,
    42  			EventSource: testEventSource,
    43  			Labels:      testLabels,
    44  		}
    45  		deployment, err := buildDeployment(args, fakeEventBus)
    46  		assert.Nil(t, err)
    47  		assert.NotNil(t, deployment)
    48  		volumes := deployment.Spec.Template.Spec.Volumes
    49  		assert.True(t, len(volumes) > 0)
    50  		hasAuthVolume := false
    51  		hasTmpVolume := false
    52  		cmRefs, secretRefs := 0, 0
    53  		for _, vol := range volumes {
    54  			if vol.Name == "auth-volume" {
    55  				hasAuthVolume = true
    56  			}
    57  			if vol.Name == "tmp" {
    58  				hasTmpVolume = true
    59  			}
    60  			if strings.Contains(vol.Name, testEventSource.Spec.HDFS["test"].KrbCCacheSecret.Name) {
    61  				secretRefs++
    62  			}
    63  			if strings.Contains(vol.Name, testEventSource.Spec.HDFS["test"].KrbConfigConfigMap.Name) {
    64  				cmRefs++
    65  			}
    66  		}
    67  		assert.True(t, hasAuthVolume)
    68  		assert.True(t, hasTmpVolume)
    69  		assert.True(t, len(deployment.Spec.Template.Spec.ImagePullSecrets) > 0)
    70  		assert.True(t, cmRefs > 0)
    71  		assert.True(t, secretRefs > 0)
    72  		assert.Equal(t, deployment.Spec.Template.Spec.PriorityClassName, "test-class")
    73  	})
    74  
    75  	t.Run("test kafka eventbus secrets attached", func(t *testing.T) {
    76  		args := &AdaptorArgs{
    77  			Image:       testImage,
    78  			EventSource: testEventSource,
    79  			Labels:      testLabels,
    80  		}
    81  
    82  		// add secrets to kafka eventbus
    83  		testBus := fakeEventBusKafka.DeepCopy()
    84  		testBus.Spec.Kafka.TLS = &apicommon.TLSConfig{
    85  			CACertSecret: &corev1.SecretKeySelector{Key: "cert", LocalObjectReference: corev1.LocalObjectReference{Name: "tls-secret"}},
    86  		}
    87  		testBus.Spec.Kafka.SASL = &apicommon.SASLConfig{
    88  			Mechanism:      "SCRAM-SHA-512",
    89  			UserSecret:     &corev1.SecretKeySelector{Key: "username", LocalObjectReference: corev1.LocalObjectReference{Name: "sasl-secret"}},
    90  			PasswordSecret: &corev1.SecretKeySelector{Key: "password", LocalObjectReference: corev1.LocalObjectReference{Name: "sasl-secret"}},
    91  		}
    92  
    93  		deployment, err := buildDeployment(args, testBus)
    94  		assert.Nil(t, err)
    95  		assert.NotNil(t, deployment)
    96  
    97  		hasSASLSecretVolume := false
    98  		hasSASLSecretVolumeMount := false
    99  		hasTLSSecretVolume := false
   100  		hasTLSSecretVolumeMount := false
   101  		for _, volume := range deployment.Spec.Template.Spec.Volumes {
   102  			if volume.Name == "secret-sasl-secret" {
   103  				hasSASLSecretVolume = true
   104  			}
   105  			if volume.Name == "secret-tls-secret" {
   106  				hasTLSSecretVolume = true
   107  			}
   108  		}
   109  		for _, volumeMount := range deployment.Spec.Template.Spec.Containers[0].VolumeMounts {
   110  			if volumeMount.Name == "secret-sasl-secret" {
   111  				hasSASLSecretVolumeMount = true
   112  			}
   113  			if volumeMount.Name == "secret-tls-secret" {
   114  				hasTLSSecretVolumeMount = true
   115  			}
   116  		}
   117  
   118  		assert.True(t, hasSASLSecretVolume)
   119  		assert.True(t, hasSASLSecretVolumeMount)
   120  		assert.True(t, hasTLSSecretVolume)
   121  		assert.True(t, hasTLSSecretVolumeMount)
   122  	})
   123  
   124  	t.Run("test secret volume and volumemount order deterministic", func(t *testing.T) {
   125  		args := &AdaptorArgs{
   126  			Image:       testImage,
   127  			EventSource: testEventSource,
   128  			Labels:      testLabels,
   129  		}
   130  
   131  		webhooksWithSecrets := map[string]v1alpha1.WebhookEventSource{
   132  			"webhook4": {
   133  				WebhookContext: v1alpha1.WebhookContext{
   134  					URL:      "http://a.b",
   135  					Endpoint: "/webhook4",
   136  					Port:     "1234",
   137  					AuthSecret: &corev1.SecretKeySelector{
   138  						LocalObjectReference: corev1.LocalObjectReference{Name: "webhook4"},
   139  						Key:                  "secret",
   140  					},
   141  				},
   142  			},
   143  			"webhook3": {
   144  				WebhookContext: v1alpha1.WebhookContext{
   145  					URL:      "http://a.b",
   146  					Endpoint: "/webhook3",
   147  					Port:     "1234",
   148  					AuthSecret: &corev1.SecretKeySelector{
   149  						LocalObjectReference: corev1.LocalObjectReference{Name: "webhook3"},
   150  						Key:                  "secret",
   151  					},
   152  				},
   153  			},
   154  			"webhook1": {
   155  				WebhookContext: v1alpha1.WebhookContext{
   156  					URL:      "http://a.b",
   157  					Endpoint: "/webhook1",
   158  					Port:     "1234",
   159  					AuthSecret: &corev1.SecretKeySelector{
   160  						LocalObjectReference: corev1.LocalObjectReference{Name: "webhook1"},
   161  						Key:                  "secret",
   162  					},
   163  				},
   164  			},
   165  			"webhook2": {
   166  				WebhookContext: v1alpha1.WebhookContext{
   167  					URL:      "http://a.b",
   168  					Endpoint: "/webhook2",
   169  					Port:     "1234",
   170  					AuthSecret: &corev1.SecretKeySelector{
   171  						LocalObjectReference: corev1.LocalObjectReference{Name: "webhook2"},
   172  						Key:                  "secret",
   173  					},
   174  				},
   175  			},
   176  		}
   177  		args.EventSource.Spec.Webhook = webhooksWithSecrets
   178  
   179  		wantVolumeNames := []string{"auth-volume", "cm-test-cm", "secret-test-secret", "secret-webhook1", "secret-webhook2", "secret-webhook3", "secret-webhook4", "tmp"}
   180  		wantVolumeMountNames := []string{"auth-volume", "cm-test-cm", "secret-test-secret", "secret-webhook1", "secret-webhook2", "secret-webhook3", "secret-webhook4", "tmp"}
   181  
   182  		deployment, err := buildDeployment(args, fakeEventBus)
   183  		assert.Nil(t, err)
   184  		assert.NotNil(t, deployment)
   185  		gotVolumes := deployment.Spec.Template.Spec.Volumes
   186  		gotVolumeMounts := deployment.Spec.Template.Spec.Containers[0].VolumeMounts
   187  
   188  		var gotVolumeNames []string
   189  		var gotVolumeMountNames []string
   190  
   191  		for _, v := range gotVolumes {
   192  			gotVolumeNames = append(gotVolumeNames, v.Name)
   193  		}
   194  		for _, v := range gotVolumeMounts {
   195  			gotVolumeMountNames = append(gotVolumeMountNames, v.Name)
   196  		}
   197  
   198  		assert.Equal(t, len(gotVolumes), len(wantVolumeNames))
   199  		assert.Equal(t, len(gotVolumeMounts), len(wantVolumeMountNames))
   200  
   201  		for i := range gotVolumeNames {
   202  			assert.Equal(t, gotVolumeNames[i], wantVolumeNames[i])
   203  		}
   204  		for i := range gotVolumeMountNames {
   205  			assert.Equal(t, gotVolumeMountNames[i], wantVolumeMountNames[i])
   206  		}
   207  	})
   208  }
   209  
   210  func TestResourceReconcile(t *testing.T) {
   211  	testEventSource := fakeEmptyEventSource()
   212  	testEventSource.Spec.HDFS = fakeHDFSEventSourceMap("test")
   213  	t.Run("test resource reconcile without eventbus", func(t *testing.T) {
   214  		cl := fake.NewClientBuilder().Build()
   215  		args := &AdaptorArgs{
   216  			Image:       testImage,
   217  			EventSource: testEventSource,
   218  			Labels:      testLabels,
   219  		}
   220  		err := Reconcile(cl, args, logging.NewArgoEventsLogger())
   221  		assert.Error(t, err)
   222  		assert.False(t, testEventSource.Status.IsReady())
   223  	})
   224  
   225  	for _, eb := range []*eventbusv1alpha1.EventBus{fakeEventBus, fakeEventBusJetstream, fakeEventBusKafka} {
   226  		testBus := eb.DeepCopy()
   227  
   228  		t.Run("test resource reconcile with eventbus", func(t *testing.T) {
   229  			ctx := context.TODO()
   230  			cl := fake.NewClientBuilder().Build()
   231  			testBus.Status.MarkDeployed("test", "test")
   232  			testBus.Status.MarkConfigured()
   233  			err := cl.Create(ctx, testBus)
   234  			assert.Nil(t, err)
   235  			args := &AdaptorArgs{
   236  				Image:       testImage,
   237  				EventSource: testEventSource,
   238  				Labels:      testLabels,
   239  			}
   240  			err = Reconcile(cl, args, logging.NewArgoEventsLogger())
   241  			assert.Nil(t, err)
   242  			assert.True(t, testEventSource.Status.IsReady())
   243  
   244  			deployList := &appv1.DeploymentList{}
   245  			err = cl.List(ctx, deployList, &client.ListOptions{
   246  				Namespace: testNamespace,
   247  			})
   248  			assert.NoError(t, err)
   249  			assert.Equal(t, 1, len(deployList.Items))
   250  
   251  			svcList := &corev1.ServiceList{}
   252  			err = cl.List(ctx, svcList, &client.ListOptions{
   253  				Namespace: testNamespace,
   254  			})
   255  			assert.NoError(t, err)
   256  			assert.Equal(t, 0, len(svcList.Items))
   257  		})
   258  	}
   259  }