239 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			239 lines
		
	
	
		
			7.7 KiB
		
	
	
	
		
			Go
		
	
	
	
| package user
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"fmt"
 | |
| 	"testing"
 | |
| 
 | |
| 	virtuslabv1alpha1 "github.com/jenkinsci/kubernetes-operator/pkg/apis/virtuslab/v1alpha1"
 | |
| 
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| 	corev1 "k8s.io/api/core/v1"
 | |
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | |
| 	"sigs.k8s.io/controller-runtime/pkg/client/fake"
 | |
| 	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
 | |
| )
 | |
| 
 | |
| var fakePrivateKey = `-----BEGIN RSA PRIVATE KEY-----
 | |
| MIIEpAIBAAKCAQEArK4ld6i2iqW6L3jaTZaKD/v7PjDn+Ik9MXp+kvLcUw/+wEGm
 | |
| 285UwqLnDDlBhSi9nDgJ+m1XU87VCpz/DXW23R/CQcMX2xunib4wWLQqoR3CWbk3
 | |
| SwiLd8TWAvXkxdXm8fDOGAZbYK2alMV+M+9E2OpZsBUCxmb/3FAofF6JccKoJOH8
 | |
| UveRNSOx7IXPKtHFiypBhWM4l6ZjgJKm+DRIEhyvoC+pHzcum2ZEPOv+ZJDy5jXK
 | |
| ZHcNQXVnAZtCcojcjVUBw2rZms+fQ6Volv2JT71Gpykzx/rChhwNwxdAEwjLjKjL
 | |
| nBWEh/WxsS3NbM7zb4B2XGMCeWVeb/niUwpy+wIDAQABAoIBAQCjGkJNidARmYQI
 | |
| /u/DxWNWwb2H+o3BFW/1YixYBIjS9BK96cT/bR5mUZRG2XXnnpmqCsxx/AE2KfDU
 | |
| e4H1ZrB4oFzN3MaVsMNIuZnUzyhM0l0WfnmZp9KEKCm01ilmLCpdcARacPaylIej
 | |
| 6f7QcznmYUShqtbaK8OUhyoWfvz3s0VLkpBlqm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1e
 | |
| ns4JGP6f5uIuwqu/nbqPqMyDovjkGbX2znuGBcvki90Pi97XL7MMWw==
 | |
| -----END RSA PRIVATE KEY-----
 | |
| `
 | |
| 
 | |
| var fakeInvalidPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
 | |
| MIIEpAIBAAKCAQEArK4ld6i2iqW6L3jaTZaKD/v7PjDn+Ik9MXp+kvLcUw/+wEGm
 | |
| 285UwqLnDDlBhSi9nDgJ+m1XU87VCpz/DXW23R/CQcMX2xunib4wWLQqoR3CWbk3
 | |
| SwiLd8TWAvXkxdXm8fDOGAZbYK2alMV+M+9E2OpZsBUCxmb/3FAofF6JccKoJOH8
 | |
| `
 | |
| 
 | |
| func TestValidateSeedJobs(t *testing.T) {
 | |
| 	data := []struct {
 | |
| 		description    string
 | |
| 		jenkins        *virtuslabv1alpha1.Jenkins
 | |
| 		secret         *corev1.Secret
 | |
| 		expectedResult bool
 | |
| 	}{
 | |
| 		{
 | |
| 			description: "Valid with public repository and without private key",
 | |
| 			jenkins: &virtuslabv1alpha1.Jenkins{
 | |
| 				Spec: virtuslabv1alpha1.JenkinsSpec{
 | |
| 					SeedJobs: []virtuslabv1alpha1.SeedJob{
 | |
| 						{
 | |
| 							ID:               "jenkins-operator-e2e",
 | |
| 							Targets:          "cicd/jobs/*.jenkins",
 | |
| 							Description:      "Jenkins Operator e2e tests repository",
 | |
| 							RepositoryBranch: "master",
 | |
| 							RepositoryURL:    "https://github.com/VirtusLab/jenkins-operator-e2e.git",
 | |
| 						},
 | |
| 					},
 | |
| 				},
 | |
| 			},
 | |
| 			expectedResult: true,
 | |
| 		},
 | |
| 		{
 | |
| 			description: "Invalid without id",
 | |
| 			jenkins: &virtuslabv1alpha1.Jenkins{
 | |
| 				Spec: virtuslabv1alpha1.JenkinsSpec{
 | |
| 					SeedJobs: []virtuslabv1alpha1.SeedJob{
 | |
| 						{
 | |
| 							Targets:          "cicd/jobs/*.jenkins",
 | |
| 							Description:      "Jenkins Operator e2e tests repository",
 | |
| 							RepositoryBranch: "master",
 | |
| 							RepositoryURL:    "https://github.com/VirtusLab/jenkins-operator-e2e.git",
 | |
| 						},
 | |
| 					},
 | |
| 				},
 | |
| 			},
 | |
| 			expectedResult: false,
 | |
| 		},
 | |
| 		{
 | |
| 			description: "Valid with private key and secret",
 | |
| 			jenkins: &virtuslabv1alpha1.Jenkins{
 | |
| 				Spec: virtuslabv1alpha1.JenkinsSpec{
 | |
| 					SeedJobs: []virtuslabv1alpha1.SeedJob{
 | |
| 						{
 | |
| 							ID:               "jenkins-operator-e2e",
 | |
| 							Targets:          "cicd/jobs/*.jenkins",
 | |
| 							Description:      "Jenkins Operator e2e tests repository",
 | |
| 							RepositoryBranch: "master",
 | |
| 							RepositoryURL:    "https://github.com/VirtusLab/jenkins-operator-e2e.git",
 | |
| 							PrivateKey: virtuslabv1alpha1.PrivateKey{
 | |
| 								SecretKeyRef: &corev1.SecretKeySelector{
 | |
| 									LocalObjectReference: corev1.LocalObjectReference{
 | |
| 										Name: "deploy-keys",
 | |
| 									},
 | |
| 									Key: "jenkins-operator-e2e",
 | |
| 								},
 | |
| 							},
 | |
| 						},
 | |
| 					},
 | |
| 				},
 | |
| 			},
 | |
| 			secret: &corev1.Secret{
 | |
| 				TypeMeta: metav1.TypeMeta{
 | |
| 					Kind:       "Secret",
 | |
| 					APIVersion: "v1",
 | |
| 				},
 | |
| 				ObjectMeta: metav1.ObjectMeta{
 | |
| 					Name:      "deploy-keys",
 | |
| 					Namespace: "default",
 | |
| 				},
 | |
| 				Data: map[string][]byte{
 | |
| 					"jenkins-operator-e2e": []byte(fakePrivateKey),
 | |
| 				},
 | |
| 			},
 | |
| 			expectedResult: true,
 | |
| 		},
 | |
| 		{
 | |
| 			description: "Invalid private key in secret",
 | |
| 			jenkins: &virtuslabv1alpha1.Jenkins{
 | |
| 				Spec: virtuslabv1alpha1.JenkinsSpec{
 | |
| 					SeedJobs: []virtuslabv1alpha1.SeedJob{
 | |
| 						{
 | |
| 							ID:               "jenkins-operator-e2e",
 | |
| 							Targets:          "cicd/jobs/*.jenkins",
 | |
| 							Description:      "Jenkins Operator e2e tests repository",
 | |
| 							RepositoryBranch: "master",
 | |
| 							RepositoryURL:    "https://github.com/VirtusLab/jenkins-operator-e2e.git",
 | |
| 							PrivateKey: virtuslabv1alpha1.PrivateKey{
 | |
| 								SecretKeyRef: &corev1.SecretKeySelector{
 | |
| 									LocalObjectReference: corev1.LocalObjectReference{
 | |
| 										Name: "deploy-keys",
 | |
| 									},
 | |
| 									Key: "jenkins-operator-e2e",
 | |
| 								},
 | |
| 							},
 | |
| 						},
 | |
| 					},
 | |
| 				},
 | |
| 			},
 | |
| 			secret: &corev1.Secret{
 | |
| 				TypeMeta: metav1.TypeMeta{
 | |
| 					Kind:       "Secret",
 | |
| 					APIVersion: "v1",
 | |
| 				},
 | |
| 				ObjectMeta: metav1.ObjectMeta{
 | |
| 					Name:      "deploy-keys",
 | |
| 					Namespace: "default",
 | |
| 				},
 | |
| 				Data: map[string][]byte{
 | |
| 					"jenkins-operator-e2e": []byte(fakeInvalidPrivateKey),
 | |
| 				},
 | |
| 			},
 | |
| 			expectedResult: false,
 | |
| 		},
 | |
| 		{
 | |
| 			description: "Invalid with PrivateKey and empty Secret data",
 | |
| 			jenkins: &virtuslabv1alpha1.Jenkins{
 | |
| 				Spec: virtuslabv1alpha1.JenkinsSpec{
 | |
| 					SeedJobs: []virtuslabv1alpha1.SeedJob{
 | |
| 						{
 | |
| 							ID:               "jenkins-operator-e2e",
 | |
| 							Targets:          "cicd/jobs/*.jenkins",
 | |
| 							Description:      "Jenkins Operator e2e tests repository",
 | |
| 							RepositoryBranch: "master",
 | |
| 							RepositoryURL:    "https://github.com/VirtusLab/jenkins-operator-e2e.git",
 | |
| 							PrivateKey: virtuslabv1alpha1.PrivateKey{
 | |
| 								SecretKeyRef: &corev1.SecretKeySelector{
 | |
| 									LocalObjectReference: corev1.LocalObjectReference{
 | |
| 										Name: "deploy-keys",
 | |
| 									},
 | |
| 									Key: "jenkins-operator-e2e",
 | |
| 								},
 | |
| 							},
 | |
| 						},
 | |
| 					},
 | |
| 				},
 | |
| 			},
 | |
| 			secret: &corev1.Secret{
 | |
| 				TypeMeta: metav1.TypeMeta{
 | |
| 					Kind:       "Secret",
 | |
| 					APIVersion: "v1",
 | |
| 				},
 | |
| 				ObjectMeta: metav1.ObjectMeta{
 | |
| 					Name:      "deploy-keys",
 | |
| 					Namespace: "default",
 | |
| 				},
 | |
| 				Data: map[string][]byte{
 | |
| 					"jenkins-operator-e2e": []byte(""),
 | |
| 				},
 | |
| 			},
 | |
| 			expectedResult: false,
 | |
| 		},
 | |
| 		{
 | |
| 			description: "Invalid with ssh RepositoryURL and empty PrivateKey",
 | |
| 			jenkins: &virtuslabv1alpha1.Jenkins{
 | |
| 				Spec: virtuslabv1alpha1.JenkinsSpec{
 | |
| 					SeedJobs: []virtuslabv1alpha1.SeedJob{
 | |
| 						{
 | |
| 							ID:               "jenkins-operator-e2e",
 | |
| 							Targets:          "cicd/jobs/*.jenkins",
 | |
| 							Description:      "Jenkins Operator e2e tests repository",
 | |
| 							RepositoryBranch: "master",
 | |
| 							RepositoryURL:    "git@github.com:VirtusLab/jenkins-operator.git",
 | |
| 						},
 | |
| 					},
 | |
| 				},
 | |
| 			},
 | |
| 			expectedResult: false,
 | |
| 		},
 | |
| 	}
 | |
| 
 | |
| 	for _, testingData := range data {
 | |
| 		t.Run(fmt.Sprintf("Testing '%s'", testingData.description), func(t *testing.T) {
 | |
| 			fakeClient := fake.NewFakeClient()
 | |
| 			if testingData.secret != nil {
 | |
| 				err := fakeClient.Create(context.TODO(), testingData.secret)
 | |
| 				assert.NoError(t, err)
 | |
| 			}
 | |
| 			userReconcileLoop := New(fakeClient, nil, logf.ZapLogger(false), nil)
 | |
| 			result, err := userReconcileLoop.validateSeedJobs(testingData.jenkins)
 | |
| 			assert.NoError(t, err)
 | |
| 			assert.Equal(t, testingData.expectedResult, result)
 | |
| 		})
 | |
| 	}
 | |
| }
 |