@@ -2,11 +2,11 @@ package settings
22
33import (
44 "net/http"
5- "net/http/httptest"
65 "sync"
76 "testing"
87
98 hubtypes "github.com/openshift-pipelines/pipelines-as-code/pkg/hub/vars"
9+ testhttp "github.com/openshift-pipelines/pipelines-as-code/pkg/test/http"
1010 "go.uber.org/zap"
1111 zapobserver "go.uber.org/zap/zaptest/observer"
1212 "gotest.tools/v3/assert"
@@ -20,17 +20,37 @@ func TestGetCatalogHub(t *testing.T) {
2020 URL : "https://foo.com" ,
2121 Name : "tekton" ,
2222 })
23+
24+ // URLs for mocked HTTP responses
25+ const (
26+ artifactHubURL = "https://artifacthub.example.com"
27+ tektonHubURL = "https://tektonhub.example.com"
28+ )
29+
30+ // Mock HTTP client for API-based type detection
31+ mockHTTPClient := testhttp .MakeHTTPTestClient (map [string ]map [string ]string {
32+ artifactHubURL + "/api/v1/stats" : {
33+ "code" : "200" ,
34+ },
35+ tektonHubURL + "/api/v1/stats" : {
36+ "code" : "404" ,
37+ },
38+ })
39+
2340 tests := []struct {
24- name string
25- config map [string ]string
26- numCatalogs int
27- wantLog string
28- hubCatalogs * sync.Map
41+ name string
42+ config map [string ]string
43+ numCatalogs int
44+ wantLog string
45+ hubCatalogs * sync.Map
46+ wantCustomType map [string ]string
47+ httpClient * http.Client
2948 }{
3049 {
3150 name : "good/default catalog" ,
3251 numCatalogs : 1 ,
3352 hubCatalogs : & sync.Map {},
53+ httpClient : mockHTTPClient ,
3454 },
3555 {
3656 name : "good/custom catalog" ,
@@ -42,6 +62,7 @@ func TestGetCatalogHub(t *testing.T) {
4262 numCatalogs : 2 ,
4363 hubCatalogs : & sync.Map {},
4464 wantLog : "CONFIG: setting custom hub custom, catalog https://foo.com" ,
65+ httpClient : mockHTTPClient ,
4566 },
4667 {
4768 name : "good/custom catalog with same data" ,
@@ -53,6 +74,7 @@ func TestGetCatalogHub(t *testing.T) {
5374 numCatalogs : 2 ,
5475 hubCatalogs : & hubCatalog ,
5576 wantLog : "" ,
77+ httpClient : mockHTTPClient ,
5678 },
5779 {
5880 name : "good/custom catalog with different data" ,
@@ -64,6 +86,7 @@ func TestGetCatalogHub(t *testing.T) {
6486 numCatalogs : 2 ,
6587 hubCatalogs : & hubCatalog ,
6688 wantLog : "CONFIG: setting custom hub custom, catalog https://bar.com" ,
89+ httpClient : mockHTTPClient ,
6790 },
6891 {
6992 name : "good/custom catalog with initialization" ,
@@ -75,6 +98,7 @@ func TestGetCatalogHub(t *testing.T) {
7598 numCatalogs : 2 ,
7699 hubCatalogs : nil ,
77100 wantLog : "CONFIG: setting custom hub custom, catalog https://foo.com" ,
101+ httpClient : mockHTTPClient ,
78102 },
79103 {
80104 name : "bad/missing keys custom catalog" ,
@@ -85,6 +109,7 @@ func TestGetCatalogHub(t *testing.T) {
85109 numCatalogs : 1 ,
86110 hubCatalogs : & sync.Map {},
87111 wantLog : "CONFIG: hub 1 should have the key catalog-1-url, skipping catalog configuration" ,
112+ httpClient : mockHTTPClient ,
88113 },
89114 {
90115 name : "bad/missing value for custom catalog" ,
@@ -96,6 +121,7 @@ func TestGetCatalogHub(t *testing.T) {
96121 numCatalogs : 1 ,
97122 hubCatalogs : & sync.Map {},
98123 wantLog : "CONFIG: hub 1 catalog configuration have empty value for key catalog-1-url, skipping catalog configuration" ,
124+ httpClient : mockHTTPClient ,
99125 },
100126 {
101127 name : "bad/custom catalog called https" ,
@@ -107,6 +133,7 @@ func TestGetCatalogHub(t *testing.T) {
107133 numCatalogs : 1 ,
108134 hubCatalogs : & sync.Map {},
109135 wantLog : "CONFIG: custom hub catalog name cannot be https, skipping catalog configuration" ,
136+ httpClient : mockHTTPClient ,
110137 },
111138 {
112139 name : "bad/invalid url" ,
@@ -118,6 +145,7 @@ func TestGetCatalogHub(t *testing.T) {
118145 numCatalogs : 1 ,
119146 hubCatalogs : & sync.Map {},
120147 wantLog : "catalog url /u1!@1!@#$afoo.com is not valid, skipping catalog configuration" ,
148+ httpClient : mockHTTPClient ,
121149 },
122150 {
123151 name : "multiple catalogs with different types" ,
@@ -134,6 +162,7 @@ func TestGetCatalogHub(t *testing.T) {
134162 numCatalogs : 3 , // default + 2 custom
135163 hubCatalogs : & sync.Map {},
136164 wantLog : "CONFIG: setting custom hub tektonhub, catalog https://tektonhub.com" ,
165+ httpClient : mockHTTPClient ,
137166 },
138167 {
139168 name : "invalid hub type" ,
@@ -143,6 +172,35 @@ func TestGetCatalogHub(t *testing.T) {
143172 numCatalogs : 1 ,
144173 hubCatalogs : & sync.Map {},
145174 wantLog : `CONFIG: invalid hub type invalid, defaulting to artifacthub` ,
175+ httpClient : mockHTTPClient ,
176+ },
177+ {
178+ name : "custom catalog type detection via API - success (ArtifactHub)" ,
179+ config : map [string ]string {
180+ "catalog-1-id" : "example-ah" ,
181+ "catalog-1-url" : artifactHubURL ,
182+ "catalog-1-name" : "artifact" ,
183+ },
184+ numCatalogs : 2 ,
185+ hubCatalogs : & sync.Map {},
186+ wantCustomType : map [string ]string {
187+ "example-ah" : hubtypes .ArtifactHubType ,
188+ },
189+ httpClient : mockHTTPClient ,
190+ },
191+ {
192+ name : "custom catalog type detection via API - failure (TektonHub)" ,
193+ config : map [string ]string {
194+ "catalog-1-id" : "example-th" ,
195+ "catalog-1-url" : tektonHubURL ,
196+ "catalog-1-name" : "tekton" ,
197+ },
198+ numCatalogs : 2 ,
199+ hubCatalogs : & sync.Map {},
200+ wantCustomType : map [string ]string {
201+ "example-th" : hubtypes .TektonHubType ,
202+ },
203+ httpClient : mockHTTPClient ,
146204 },
147205 }
148206 for _ , tt := range tests {
@@ -152,7 +210,7 @@ func TestGetCatalogHub(t *testing.T) {
152210 if tt .config == nil {
153211 tt .config = map [string ]string {}
154212 }
155- catalogs := getHubCatalogs (fakelogger , tt .hubCatalogs , tt .config )
213+ catalogs := getHubCatalogs (fakelogger , tt .hubCatalogs , tt .config , tt . httpClient )
156214 length := 0
157215 catalogs .Range (func (_ , _ any ) bool {
158216 length ++
@@ -162,6 +220,13 @@ func TestGetCatalogHub(t *testing.T) {
162220 if tt .wantLog != "" {
163221 assert .Assert (t , len (catcher .FilterMessageSnippet (tt .wantLog ).TakeAll ()) > 0 , "could not find log message: got " , catcher )
164222 }
223+ for catalogID , expectedType := range tt .wantCustomType {
224+ value , ok := catalogs .Load (catalogID )
225+ assert .Assert (t , ok , "catalog %s should exist" , catalogID )
226+ catalog , ok := value .(HubCatalog )
227+ assert .Assert (t , ok , "catalog %s should be HubCatalog type" , catalogID )
228+ assert .Equal (t , catalog .Type , expectedType )
229+ }
165230 cmp .Equal (catalogs , tt .hubCatalogs )
166231 })
167232 }
@@ -170,34 +235,36 @@ func TestGetCatalogHub(t *testing.T) {
170235func TestGetHubCatalogTypeViaAPI (t * testing.T ) {
171236 tests := []struct {
172237 name string
173- serverStatus int
238+ serverStatus string
174239 expectedResult string
175240 }{
176241 {
177242 name : "returns ArtifactHubType on 200 OK" ,
178- serverStatus : http . StatusOK ,
243+ serverStatus : "200" ,
179244 expectedResult : hubtypes .ArtifactHubType ,
180245 },
181246 {
182247 name : "returns TektonHubType on 404 Not Found" ,
183- serverStatus : http . StatusNotFound ,
248+ serverStatus : "404" ,
184249 expectedResult : hubtypes .TektonHubType ,
185250 },
186251 {
187252 name : "returns TektonHubType on 500 Internal Server Error" ,
188- serverStatus : http . StatusInternalServerError ,
253+ serverStatus : "500" ,
189254 expectedResult : hubtypes .TektonHubType ,
190255 },
191256 }
192257
193258 for _ , tt := range tests {
194259 t .Run (tt .name , func (t * testing.T ) {
195- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
196- w .WriteHeader (tt .serverStatus )
197- }))
198- defer server .Close ()
260+ testURL := "https://test-hub.example.com"
261+ mockClient := testhttp .MakeHTTPTestClient (map [string ]map [string ]string {
262+ testURL + "/api/v1/stats" : {
263+ "code" : tt .serverStatus ,
264+ },
265+ })
199266
200- result := getHubCatalogTypeViaAPI (server . URL )
267+ result := getHubCatalogTypeViaAPI (testURL , mockClient )
201268 assert .Equal (t , result , tt .expectedResult )
202269 })
203270 }
0 commit comments