|
| 1 | +package params |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients" |
| 7 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" |
| 8 | + appsv1 "k8s.io/api/apps/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/runtime" |
| 11 | + "k8s.io/client-go/kubernetes/fake" |
| 12 | + rtesting "knative.dev/pkg/reconciler/testing" |
| 13 | + |
| 14 | + "gotest.tools/v3/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestGetInstallLocation(t *testing.T) { |
| 18 | + const testNamespace = "pac-namespace" |
| 19 | + const versionLabel = "v1.2.3" |
| 20 | + originalInstallNamespaces := info.InstallNamespaces |
| 21 | + defer func() { |
| 22 | + info.InstallNamespaces = originalInstallNamespaces |
| 23 | + }() |
| 24 | + |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + installNamespaces []string |
| 28 | + deployments []runtime.Object |
| 29 | + expectedNS string |
| 30 | + expectedVersion string |
| 31 | + wantErr bool |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "deployment with version label", |
| 35 | + installNamespaces: []string{testNamespace}, |
| 36 | + deployments: []runtime.Object{ |
| 37 | + &appsv1.Deployment{ |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: "pipelines-as-code-controller", |
| 40 | + Namespace: testNamespace, |
| 41 | + Labels: map[string]string{ |
| 42 | + "app.kubernetes.io/version": versionLabel, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + expectedNS: testNamespace, |
| 48 | + expectedVersion: versionLabel, |
| 49 | + wantErr: false, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "deployment without version label", |
| 53 | + installNamespaces: []string{testNamespace}, |
| 54 | + deployments: []runtime.Object{ |
| 55 | + &appsv1.Deployment{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Name: "pipelines-as-code-controller", |
| 58 | + Namespace: testNamespace, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + expectedNS: testNamespace, |
| 63 | + expectedVersion: "unknown", |
| 64 | + wantErr: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "no deployments found", |
| 68 | + installNamespaces: []string{"ns1", "ns2"}, |
| 69 | + deployments: []runtime.Object{}, |
| 70 | + expectedNS: "", |
| 71 | + expectedVersion: "", |
| 72 | + wantErr: true, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + ctx, _ := rtesting.SetupFakeContext(t) |
| 79 | + info.InstallNamespaces = tt.installNamespaces |
| 80 | + kubeClient := fake.NewSimpleClientset(tt.deployments...) |
| 81 | + |
| 82 | + run := &Run{ |
| 83 | + Clients: clients.Clients{ |
| 84 | + Kube: kubeClient, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + ns, version, err := GetInstallLocation(ctx, run) |
| 89 | + if tt.wantErr { |
| 90 | + assert.Assert(t, err != nil, "expected error but got nil") |
| 91 | + } else { |
| 92 | + assert.NilError(t, err) |
| 93 | + assert.Equal(t, ns, tt.expectedNS) |
| 94 | + assert.Equal(t, version, tt.expectedVersion) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments