github.com/argoproj/argo-events@v1.9.1/sensors/artifacts/store_test.go (about)

     1  /*
     2  Copyright 2018 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  
    17  package artifacts
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
    25  	"github.com/stretchr/testify/assert"
    26  	apiv1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/client-go/kubernetes/fake"
    29  )
    30  
    31  type FakeWorkflowArtifactReader struct{}
    32  
    33  func (f *FakeWorkflowArtifactReader) Read() ([]byte, error) {
    34  	return []byte(workflowv1alpha1), nil
    35  }
    36  
    37  func TestFetchArtifact(t *testing.T) {
    38  	reader := &FakeWorkflowArtifactReader{}
    39  	obj, err := FetchArtifact(reader)
    40  	assert.Nil(t, err)
    41  	assert.Equal(t, "argoproj.io/v1alpha1", obj.GetAPIVersion())
    42  	assert.Equal(t, "Workflow", obj.GetKind())
    43  }
    44  
    45  func TestGetCredentials(t *testing.T) {
    46  	fakeClient := fake.NewSimpleClientset()
    47  
    48  	mySecretCredentials := &apiv1.Secret{
    49  		ObjectMeta: metav1.ObjectMeta{
    50  			Name:      "test",
    51  			Namespace: "testing",
    52  		},
    53  		Data: map[string][]byte{"access": []byte("token"), "secret": []byte("value")},
    54  	}
    55  	_, err := fakeClient.CoreV1().Secrets("testing").Create(context.TODO(), mySecretCredentials, metav1.CreateOptions{})
    56  	assert.Nil(t, err)
    57  
    58  	// creds should be nil for unknown minio type
    59  	unknownArtifact := &v1alpha1.ArtifactLocation{}
    60  	creds, err := GetCredentials(unknownArtifact)
    61  	assert.Nil(t, creds)
    62  	assert.Nil(t, err)
    63  }
    64  
    65  func TestGetArtifactReader(t *testing.T) {
    66  	// test unknown failure
    67  	location := &v1alpha1.ArtifactLocation{}
    68  	creds := &Credentials{
    69  		accessKey: "access",
    70  		secretKey: "secret",
    71  	}
    72  	_, err := GetArtifactReader(location, creds)
    73  	assert.NotNil(t, err)
    74  }
    75  
    76  func TestDecodeSensor(t *testing.T) {
    77  	b, err := os.ReadFile("../../examples/sensors/multi-trigger-sensor.yaml")
    78  	assert.Nil(t, err)
    79  	_, err = decodeAndUnstructure(b)
    80  	assert.Nil(t, err)
    81  }
    82  
    83  func TestDecodeWorkflow(t *testing.T) {
    84  	_, err := decodeAndUnstructure([]byte(workflowv1alpha1))
    85  	assert.Nil(t, err)
    86  }
    87  
    88  var workflowv1alpha1 = `
    89  apiVersion: argoproj.io/v1alpha1
    90  kind: Workflow
    91  metadata:
    92    generateName: hello-world-
    93  spec:
    94    entrypoint: whalesay
    95    templates:
    96    - name: whalesay
    97      container:
    98        image: docker/whalesay:latest
    99        command: [cowsay]
   100        args: ["hello world"]
   101  `
   102  
   103  func TestDecodeDeploymentv1(t *testing.T) {
   104  	_, err := decodeAndUnstructure([]byte(deploymentv1))
   105  	assert.Nil(t, err)
   106  }
   107  
   108  var deploymentv1 = `
   109  {
   110  	"apiVersion": "apps/v1",
   111  	"kind": "Deployment",
   112  	"metadata": {
   113  	  "name": "nginx-deployment",
   114  	  "labels": {
   115  		"app": "nginx"
   116  	  }
   117  	},
   118  	"spec": {
   119  	  "replicas": 3,
   120  	  "selector": {
   121  		"matchLabels": {
   122  		  "app": "nginx"
   123  		}
   124  	  },
   125  	  "template": {
   126  		"metadata": {
   127  		  "labels": {
   128  			"app": "nginx"
   129  		  }
   130  		},
   131  		"spec": {
   132  		  "containers": [
   133  			{
   134  			  "name": "nginx",
   135  			  "image": "nginx:1.7.9",
   136  			  "ports": [
   137  				{
   138  				  "containerPort": 80
   139  				}
   140  			  ]
   141  			}
   142  		  ]
   143  		}
   144  	  }
   145  	}
   146    }
   147  `
   148  
   149  func TestDecodeJobv1(t *testing.T) {
   150  	_, err := decodeAndUnstructure([]byte(jobv1))
   151  	assert.Nil(t, err)
   152  }
   153  
   154  var jobv1 = `
   155  apiVersion: batch/v1
   156  kind: Job
   157  metadata:
   158    # Unique key of the Job instance
   159    name: example-job
   160  spec:
   161    template:
   162      metadata:
   163        name: example-job
   164      spec:
   165        containers:
   166        - name: pi
   167          image: perl
   168          command: ["perl"]
   169          args: ["-Mbignum=bpi", "-wle", "print bpi(2000)"]
   170        # Do not restart containers after they exit
   171        restartPolicy: Never
   172  `
   173  
   174  func TestDecodeUnsupported(t *testing.T) {
   175  	_, err := decodeAndUnstructure([]byte(unsupportedType))
   176  	assert.Nil(t, err)
   177  }
   178  
   179  var unsupportedType = `
   180  apiVersion: extensions/v1beta1
   181  kind: DaemonSet
   182  metadata:
   183    # Unique key of the DaemonSet instance
   184    name: daemonset-example
   185  spec:
   186    template:
   187      metadata:
   188        labels:
   189          app: daemonset-example
   190      spec:
   191        containers:
   192        # This container is run once on each Node in the cluster
   193        - name: daemonset-example
   194          image: ubuntu:trusty
   195          command:
   196          - /bin/sh
   197          args:
   198          - -c
   199          - >-
   200            while [ true ]; do
   201            echo "DaemonSet running on $(hostname)" ;
   202            sleep 10 ;
   203            done
   204  `
   205  
   206  func TestDecodeUnknown(t *testing.T) {
   207  	_, err := decodeAndUnstructure([]byte(unsupportedType))
   208  	assert.Nil(t, err, "expected nil error but got", err)
   209  }