Skip to content

Commit 53524f9

Browse files
Merge pull request #847 from joshbranham/bug/fix-ocm-client-calls
Remove real API calls to OCM in tests
2 parents be91a76 + 6397a99 commit 53524f9

File tree

2 files changed

+79
-28
lines changed

2 files changed

+79
-28
lines changed

pkg/utils/ocm.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,27 @@ func CreateConnection() (*sdk.Connection, error) {
284284
return connBuilder.Build()
285285
}
286286

287+
// ValidateAndResolveOcmUrl validates an OCM URL or alias and resolves it to a full URL.
288+
// Returns the resolved URL and an error if the input is invalid.
289+
func ValidateAndResolveOcmUrl(ocmUrl string) (string, error) {
290+
if len(ocmUrl) <= 0 {
291+
return "", fmt.Errorf("empty OCM URL")
292+
}
293+
// Validate URL in the case where it may be an alias
294+
resolvedUrl, ok := urlAliases[ocmUrl]
295+
if !ok {
296+
return "", fmt.Errorf("invalid OCM_URL found: %s\nValid URL aliases are: 'production', 'staging', 'integration'", ocmUrl)
297+
}
298+
return resolvedUrl, nil
299+
}
300+
287301
// Creates a connection to OCM
288302
func CreateConnectionWithUrl(OcmUrl string) (*sdk.Connection, error) {
289-
if len(OcmUrl) <= 0 {
290-
return nil, fmt.Errorf("CreateConnectionWithUrl provided empty OCM URL")
291-
}
292-
// First we need to validate URL in the case where it may be an alias
293-
ocmApiUrl, ok := urlAliases[OcmUrl]
294-
if !ok {
295-
return nil, fmt.Errorf("invalid OCM_URL found: %s\nValid URL aliases are: 'production', 'staging', 'integration'", OcmUrl)
303+
ocmApiUrl, err := ValidateAndResolveOcmUrl(OcmUrl)
304+
if err != nil {
305+
return nil, err
296306
}
307+
297308
config, err := ocmConfig.Load()
298309
if err != nil {
299310
return nil, fmt.Errorf("unable to load OCM config. %w", err)

pkg/utils/ocm_test.go

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -531,15 +531,13 @@ func TestGetOCMConfigFromEnv(t *testing.T) {
531531
}
532532
}
533533

534-
// TestCreateConnectionWithUrl tests the CreateConnectionWithUrl function which creates
535-
// an OCM SDK connection with a specified URL. It validates URL alias handling for
536-
// 'production', 'staging', and 'integration' environments.
537-
// Note: Successful connection creation requires valid OCM credentials and is tested
538-
// in integration tests or the hive-login test command.
539-
func TestCreateConnectionWithUrl(t *testing.T) {
534+
// TestValidateAndResolveOcmUrl tests the ValidateAndResolveOcmUrl function which validates
535+
// and resolves OCM URL aliases without making any network calls. This is a pure validation test.
536+
func TestValidateAndResolveOcmUrl(t *testing.T) {
540537
tests := []struct {
541538
name string
542539
ocmUrl string
540+
wantUrl string
543541
wantErr bool
544542
errContains string
545543
}{
@@ -558,43 +556,85 @@ func TestCreateConnectionWithUrl(t *testing.T) {
558556
errContains: "invalid OCM_URL found",
559557
},
560558
{
561-
// Test that 'production' alias doesn't fail with "invalid alias" error
562-
// Will fail with credentials error if not logged in, which is expected
559+
// Test that 'production' alias is correctly resolved
563560
name: "production alias recognized",
564561
ocmUrl: "production",
565-
wantErr: true, // Will fail without credentials, but not with "invalid alias" error
562+
wantUrl: "https://api.openshift.com",
563+
wantErr: false,
564+
},
565+
{
566+
// Test that 'prod' alias is correctly resolved
567+
name: "prod alias recognized",
568+
ocmUrl: "prod",
569+
wantUrl: "https://api.openshift.com",
570+
wantErr: false,
566571
},
567572
{
568-
// Test that 'staging' alias doesn't fail with "invalid alias" error
569-
// Will fail with credentials error if not logged in, which is expected
573+
// Test that 'staging' alias is correctly resolved
570574
name: "staging alias recognized",
571575
ocmUrl: "staging",
572-
wantErr: true, // Will fail without credentials, but not with "invalid alias" error
576+
wantUrl: "https://api.stage.openshift.com",
577+
wantErr: false,
578+
},
579+
{
580+
// Test that 'stage' alias is correctly resolved
581+
name: "stage alias recognized",
582+
ocmUrl: "stage",
583+
wantUrl: "https://api.stage.openshift.com",
584+
wantErr: false,
573585
},
574586
{
575-
// Test that 'integration' alias doesn't fail with "invalid alias" error
576-
// Will fail with credentials error if not logged in, which is expected
587+
// Test that 'integration' alias is correctly resolved
577588
name: "integration alias recognized",
578589
ocmUrl: "integration",
579-
wantErr: true, // Will fail without credentials, but not with "invalid alias" error
590+
wantUrl: "https://api.integration.openshift.com",
591+
wantErr: false,
592+
},
593+
{
594+
// Test that 'int' alias is correctly resolved
595+
name: "int alias recognized",
596+
ocmUrl: "int",
597+
wantUrl: "https://api.integration.openshift.com",
598+
wantErr: false,
599+
},
600+
{
601+
// Test that full production URL is accepted
602+
name: "full production URL",
603+
ocmUrl: "https://api.openshift.com",
604+
wantUrl: "https://api.openshift.com",
605+
wantErr: false,
606+
},
607+
{
608+
// Test that full staging URL is accepted
609+
name: "full staging URL",
610+
ocmUrl: "https://api.stage.openshift.com",
611+
wantUrl: "https://api.stage.openshift.com",
612+
wantErr: false,
613+
},
614+
{
615+
// Test that gov cloud aliases are recognized
616+
name: "production gov alias recognized",
617+
ocmUrl: "productiongov",
618+
wantUrl: "https://api-admin.openshiftusgov.com",
619+
wantErr: false,
580620
},
581621
}
582622

583623
for _, tt := range tests {
584624
t.Run(tt.name, func(t *testing.T) {
585-
conn, err := CreateConnectionWithUrl(tt.ocmUrl)
625+
resolvedUrl, err := ValidateAndResolveOcmUrl(tt.ocmUrl)
586626
if tt.wantErr {
587627
if err == nil {
588-
t.Errorf("CreateConnectionWithUrl() expected error but got none")
628+
t.Errorf("ValidateAndResolveOcmUrl() expected error but got none")
589629
} else if tt.errContains != "" && !contains(err.Error(), tt.errContains) {
590-
t.Errorf("CreateConnectionWithUrl() error = %v, want error containing %v", err, tt.errContains)
630+
t.Errorf("ValidateAndResolveOcmUrl() error = %v, want error containing %v", err, tt.errContains)
591631
}
592632
} else {
593633
if err != nil {
594-
t.Errorf("CreateConnectionWithUrl() unexpected error = %v", err)
634+
t.Errorf("ValidateAndResolveOcmUrl() unexpected error = %v", err)
595635
}
596-
if conn != nil {
597-
defer conn.Close()
636+
if resolvedUrl != tt.wantUrl {
637+
t.Errorf("ValidateAndResolveOcmUrl() = %v, want %v", resolvedUrl, tt.wantUrl)
598638
}
599639
}
600640
})

0 commit comments

Comments
 (0)