|
| 1 | +package info |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1" |
| 9 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params" |
| 10 | + "github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients" |
| 11 | + tcli "github.com/openshift-pipelines/pipelines-as-code/pkg/test/cli" |
| 12 | + testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients" |
| 13 | + httptesting "github.com/openshift-pipelines/pipelines-as-code/pkg/test/http" |
| 14 | + "gotest.tools/v3/assert" |
| 15 | + "gotest.tools/v3/golden" |
| 16 | + appsv1 "k8s.io/api/apps/v1" |
| 17 | + corev1 "k8s.io/api/core/v1" |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + rtesting "knative.dev/pkg/reconciler/testing" |
| 20 | +) |
| 21 | + |
| 22 | +// script kiddies, don't get too excited, this has been randomly generated with random words |
| 23 | +const fakePrivateKey = `-----BEGIN RSA PRIVATE KEY----- |
| 24 | +MIICXAIBAAKBgQC6GorZBeri0eVERMZQDFh5E1RMPjFk9AevaWr27yJse6eiUlos |
| 25 | +gY2L2vcZKLOrdvVR+TLLapIMFfg1E1qVr1iTHP3IiSCs1uW6NKDmxEQc9Uf/fG9c |
| 26 | +i56tGmTVxLkC94AvlVFmgxtWfHdP3lF2O0EcfRyIi6EIbGkWDqWQVEQG2wIDAQAB |
| 27 | +AoGAaKOd6FK0dB5Si6Uj4ERgxosAvfHGMh4n6BAc7YUd1ONeKR2myBl77eQLRaEm |
| 28 | +DMXRP+sfDVL5lUQRED62ky1JXlDc0TmdLiO+2YVyXI5Tbej0Q6wGVC25/HedguUX |
| 29 | +fw+MdKe8jsOOXVRLrJ2GfpKZ2CmOKGTm/hyrFa10TmeoTxkCQQDa4fvqZYD4vOwZ |
| 30 | +CplONnVk+PyQETj+mAyUiBnHEeLpztMImNLVwZbrmMHnBtCNx5We10oCLW+Qndfw |
| 31 | +Xi4LgliVAkEA2amSV+TZiUVQmm5j9yzon0rt1FK+cmVWfRS/JAUXyvl+Xh/J+7Gu |
| 32 | +QzoEGJNAnzkUIZuwhTfNRWlzURWYA8BVrwJAZFQhfJd6PomaTwAktU0REm9ulTrP |
| 33 | +vSNE4PBhoHX6ZOGAqfgi7AgIfYVPm+3rupE5a82TBtx8vvUa/fqtcGkW4QJAaL9t |
| 34 | +WPUeJyx/XMJxQzuOe1JA4CQt2LmiBLHeRoRY7ephgQSFXKYmed3KqNT8jWOXp5DY |
| 35 | +Q1QWaigUQdpFfNCrqwJBANLgWaJV722PhQXOCmR+INvZ7ksIhJVcq/x1l2BYOLw2 |
| 36 | +QsncVExbMiPa9Oclo5qLuTosS8qwHm1MJEytp3/SkB8= |
| 37 | +-----END RSA PRIVATE KEY-----` |
| 38 | + |
| 39 | +func TestInfo(t *testing.T) { |
| 40 | + ns1 := "ns1" |
| 41 | + ns2 := "ns2" |
| 42 | + somerepositories := []*v1alpha1.Repository{ |
| 43 | + { |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Name: "repo1", |
| 46 | + Namespace: ns1, |
| 47 | + }, |
| 48 | + Spec: v1alpha1.RepositorySpec{ |
| 49 | + URL: "https://anurl.com", |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + ObjectMeta: metav1.ObjectMeta{ |
| 54 | + Name: "repo2", |
| 55 | + Namespace: ns2, |
| 56 | + }, |
| 57 | + Spec: v1alpha1.RepositorySpec{ |
| 58 | + URL: "https://somewhere.com", |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + tests := []struct { |
| 63 | + name string |
| 64 | + wantErr bool |
| 65 | + secrets []*corev1.Secret |
| 66 | + repositories []*v1alpha1.Repository |
| 67 | + controllerLabels map[string]string |
| 68 | + controllerNs string |
| 69 | + }{ |
| 70 | + { |
| 71 | + name: "with github app", |
| 72 | + repositories: somerepositories, |
| 73 | + controllerNs: "pipelines-as-code", |
| 74 | + secrets: []*corev1.Secret{ |
| 75 | + { |
| 76 | + ObjectMeta: metav1.ObjectMeta{ |
| 77 | + Name: "pipelines-as-code-secret", |
| 78 | + Namespace: "pipelines-as-code", |
| 79 | + }, |
| 80 | + Data: map[string][]byte{ |
| 81 | + "github-application-id": []byte("12345"), |
| 82 | + "github-private-key": []byte(fakePrivateKey), |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "without github app", |
| 89 | + repositories: somerepositories, |
| 90 | + controllerNs: "pipelines-as-code", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "no repos", |
| 94 | + controllerNs: "pipelines-as-code", |
| 95 | + }, |
| 96 | + } |
| 97 | + for _, tt := range tests { |
| 98 | + t.Run(tt.name, func(t *testing.T) { |
| 99 | + namespaces := []*corev1.Namespace{ |
| 100 | + { |
| 101 | + ObjectMeta: metav1.ObjectMeta{ |
| 102 | + Name: ns1, |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + ObjectMeta: metav1.ObjectMeta{ |
| 107 | + Name: ns2, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + if tt.controllerLabels == nil { |
| 112 | + tt.controllerLabels = map[string]string{ |
| 113 | + "app.kubernetes.io/version": "testing", |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + tdata := testclient.Data{ |
| 118 | + Namespaces: namespaces, |
| 119 | + Repositories: tt.repositories, |
| 120 | + Deployments: []*appsv1.Deployment{ |
| 121 | + { |
| 122 | + ObjectMeta: metav1.ObjectMeta{ |
| 123 | + Name: "pipelines-as-code-controller", |
| 124 | + Labels: tt.controllerLabels, |
| 125 | + Namespace: tt.controllerNs, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + Secret: tt.secrets, |
| 130 | + } |
| 131 | + apiURL := "http://github.url" |
| 132 | + ghAppJSON := `{ |
| 133 | + "installations_count": 5, |
| 134 | + "description": "my beautiful app", |
| 135 | + "html_url": "http://github.url/app/myapp", |
| 136 | + "external_url": "http://myapp.url", |
| 137 | + "created_at": "2023-03-22T12:29:10Z", |
| 138 | + "name": "myapp" |
| 139 | + }` |
| 140 | + httpTestClient := httptesting.MakeHTTPTestClient(map[string]map[string]string{ |
| 141 | + apiURL + "/app": { |
| 142 | + "body": ghAppJSON, |
| 143 | + "code": "200", |
| 144 | + }, |
| 145 | + }) |
| 146 | + |
| 147 | + ctx, _ := rtesting.SetupFakeContext(t) |
| 148 | + stdata, _ := testclient.SeedTestData(t, ctx, tdata) |
| 149 | + cs := ¶ms.Run{ |
| 150 | + Clients: clients.Clients{ |
| 151 | + PipelineAsCode: stdata.PipelineAsCode, |
| 152 | + Kube: stdata.Kube, |
| 153 | + HTTP: *httpTestClient, |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + io, out := tcli.NewIOStream() |
| 158 | + err := install(ctx, cs, io, apiURL) |
| 159 | + assert.NilError(t, err) |
| 160 | + golden.Assert(t, out.String(), fmt.Sprintf("%s.golden", t.Name())) |
| 161 | + }) |
| 162 | + } |
| 163 | +} |
0 commit comments