2021-05-12 14:11:13 +03:00
package frontend
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"net/http"
"strings"
"testing"
"github.com/Azure/ARO-RP/pkg/api"
2021-07-20 11:20:00 +03:00
"github.com/Azure/ARO-RP/pkg/api/v20210901preview"
2021-05-12 14:11:13 +03:00
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/metrics/noop"
testdatabase "github.com/Azure/ARO-RP/test/database"
)
func TestPostOpenShiftClusterKubeConfigCredentials ( t * testing . T ) {
ctx := context . Background ( )
apis := map [ string ] * api . Version {
2021-07-20 11:20:00 +03:00
"2021-09-01-preview" : api . APIs [ "2021-09-01-preview" ] ,
2021-05-12 14:11:13 +03:00
"no-credentials" : {
2021-07-20 11:20:00 +03:00
OpenShiftClusterConverter : api . APIs [ "2021-09-01-preview" ] . OpenShiftClusterConverter ,
OpenShiftClusterStaticValidator : api . APIs [ "2021-09-01-preview" ] . OpenShiftClusterStaticValidator ,
2021-05-12 14:11:13 +03:00
} ,
}
mockSubID := "00000000-0000-0000-0000-000000000000"
2021-06-10 10:14:34 +03:00
resourceID := fmt . Sprintf ( "/subscriptions/%s/resourcegroups/resourceGroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/resourceName" , mockSubID )
2021-05-12 14:11:13 +03:00
type test struct {
name string
resourceID string
apiVersion string
fixture func ( * testdatabase . Fixture )
dbError error
wantStatusCode int
2021-07-20 11:20:00 +03:00
wantResponse func ( * test ) * v20210901preview . OpenShiftClusterAdminKubeconfig
2021-05-12 14:11:13 +03:00
wantError string
}
for _ , tt := range [ ] * test {
{
name : "cluster exists in db" ,
2021-06-10 10:14:34 +03:00
resourceID : resourceID ,
2021-05-12 14:11:13 +03:00
fixture : func ( f * testdatabase . Fixture ) {
f . AddOpenShiftClusterDocuments ( & api . OpenShiftClusterDocument {
Key : strings . ToLower ( testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ) ,
OpenShiftCluster : & api . OpenShiftCluster {
ID : testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ,
Name : "resourceName" ,
Type : "Microsoft.RedHatOpenShift/openshiftClusters" ,
Properties : api . OpenShiftClusterProperties {
2024-04-22 18:34:09 +03:00
ProvisioningState : api . ProvisioningStateSucceeded ,
UserAdminKubeconfig : api . SecureBytes ( "{kubeconfig}" ) ,
ServicePrincipalProfile : & api . ServicePrincipalProfile { } ,
2021-05-12 14:11:13 +03:00
} ,
} ,
} )
f . AddSubscriptionDocuments ( & api . SubscriptionDocument {
ID : mockSubID ,
Subscription : & api . Subscription {
State : api . SubscriptionStateRegistered ,
Properties : & api . SubscriptionProperties {
TenantID : "11111111-1111-1111-1111-111111111111" ,
} ,
} ,
} )
} ,
wantStatusCode : http . StatusOK ,
2021-07-20 11:20:00 +03:00
wantResponse : func ( tt * test ) * v20210901preview . OpenShiftClusterAdminKubeconfig {
return & v20210901preview . OpenShiftClusterAdminKubeconfig {
2021-06-10 10:14:34 +03:00
Kubeconfig : [ ] byte ( "{kubeconfig}" ) ,
2021-05-12 14:11:13 +03:00
}
} ,
} ,
{
name : "credentials request is not allowed in the API version" ,
2021-06-10 10:14:34 +03:00
resourceID : resourceID ,
2021-05-12 14:11:13 +03:00
apiVersion : "no-credentials" ,
wantStatusCode : http . StatusBadRequest ,
wantError : ` 400: InvalidResourceType: : The resource type 'openshiftclusters' could not be found in the namespace 'microsoft.redhatopenshift' for api version 'no-credentials'. ` ,
} ,
{
name : "cluster exists in db in creating state" ,
2021-06-10 10:14:34 +03:00
resourceID : resourceID ,
2021-05-12 14:11:13 +03:00
fixture : func ( f * testdatabase . Fixture ) {
f . AddOpenShiftClusterDocuments ( & api . OpenShiftClusterDocument {
Key : strings . ToLower ( testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ) ,
OpenShiftCluster : & api . OpenShiftCluster {
ID : testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ,
Name : "resourceName" ,
Type : "Microsoft.RedHatOpenShift/openshiftClusters" ,
Properties : api . OpenShiftClusterProperties {
ProvisioningState : api . ProvisioningStateCreating ,
2021-06-10 10:14:34 +03:00
UserAdminKubeconfig : api . SecureBytes ( "{kubeconfig}" ) ,
2021-05-12 14:11:13 +03:00
} ,
} ,
} )
f . AddSubscriptionDocuments ( & api . SubscriptionDocument {
ID : mockSubID ,
Subscription : & api . Subscription {
State : api . SubscriptionStateRegistered ,
Properties : & api . SubscriptionProperties {
TenantID : "11111111-1111-1111-1111-111111111111" ,
} ,
} ,
} )
} ,
wantStatusCode : http . StatusBadRequest ,
wantError : ` 400: RequestNotAllowed: : Request is not allowed in provisioningState 'Creating'. ` ,
} ,
{
name : "cluster exists in db in deleting state" ,
2021-06-10 10:14:34 +03:00
resourceID : resourceID ,
2021-05-12 14:11:13 +03:00
fixture : func ( f * testdatabase . Fixture ) {
f . AddOpenShiftClusterDocuments ( & api . OpenShiftClusterDocument {
Key : strings . ToLower ( testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ) ,
OpenShiftCluster : & api . OpenShiftCluster {
ID : testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ,
Name : "resourceName" ,
Type : "Microsoft.RedHatOpenShift/openshiftClusters" ,
Properties : api . OpenShiftClusterProperties {
ProvisioningState : api . ProvisioningStateDeleting ,
2021-06-10 10:14:34 +03:00
UserAdminKubeconfig : api . SecureBytes ( "{kubeconfig}" ) ,
2021-05-12 14:11:13 +03:00
} ,
} ,
} )
f . AddSubscriptionDocuments ( & api . SubscriptionDocument {
ID : mockSubID ,
Subscription : & api . Subscription {
State : api . SubscriptionStateRegistered ,
Properties : & api . SubscriptionProperties {
TenantID : "11111111-1111-1111-1111-111111111111" ,
} ,
} ,
} )
} ,
wantStatusCode : http . StatusBadRequest ,
wantError : ` 400: RequestNotAllowed: : Request is not allowed in provisioningState 'Deleting'. ` ,
} ,
{
name : "cluster failed to create" ,
2021-06-10 10:14:34 +03:00
resourceID : resourceID ,
2021-05-12 14:11:13 +03:00
fixture : func ( f * testdatabase . Fixture ) {
f . AddOpenShiftClusterDocuments ( & api . OpenShiftClusterDocument {
Key : strings . ToLower ( testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ) ,
OpenShiftCluster : & api . OpenShiftCluster {
ID : testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ,
Name : "resourceName" ,
Type : "Microsoft.RedHatOpenShift/openshiftClusters" ,
Properties : api . OpenShiftClusterProperties {
ProvisioningState : api . ProvisioningStateFailed ,
UserAdminKubeconfig : api . SecureBytes ( "{}" ) ,
FailedProvisioningState : api . ProvisioningStateCreating ,
} ,
} ,
} )
f . AddSubscriptionDocuments ( & api . SubscriptionDocument {
ID : mockSubID ,
Subscription : & api . Subscription {
State : api . SubscriptionStateRegistered ,
Properties : & api . SubscriptionProperties {
TenantID : "11111111-1111-1111-1111-111111111111" ,
} ,
} ,
} )
} ,
wantStatusCode : http . StatusBadRequest ,
wantError : ` 400: RequestNotAllowed: : Request is not allowed in provisioningState 'Failed'. ` ,
} ,
{
name : "cluster failed to delete" ,
2021-06-10 10:14:34 +03:00
resourceID : resourceID ,
2021-05-12 14:11:13 +03:00
fixture : func ( f * testdatabase . Fixture ) {
f . AddOpenShiftClusterDocuments ( & api . OpenShiftClusterDocument {
Key : strings . ToLower ( testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ) ,
OpenShiftCluster : & api . OpenShiftCluster {
ID : testdatabase . GetResourcePath ( mockSubID , "resourceName" ) ,
Name : "resourceName" ,
Type : "Microsoft.RedHatOpenShift/openshiftClusters" ,
Properties : api . OpenShiftClusterProperties {
ProvisioningState : api . ProvisioningStateFailed ,
FailedProvisioningState : api . ProvisioningStateDeleting ,
2021-06-10 10:14:34 +03:00
UserAdminKubeconfig : api . SecureBytes ( "{kubeconfig}" ) ,
2021-05-12 14:11:13 +03:00
} ,
} ,
} )
f . AddSubscriptionDocuments ( & api . SubscriptionDocument {
ID : mockSubID ,
Subscription : & api . Subscription {
State : api . SubscriptionStateRegistered ,
Properties : & api . SubscriptionProperties {
2022-10-07 23:15:39 +03:00
TenantID : "11111111-1111-1111-1111-111111111111" ,
RegisteredFeatures : [ ] api . RegisteredFeatureProfile { } ,
2021-05-12 14:11:13 +03:00
} ,
} ,
} )
} ,
wantStatusCode : http . StatusBadRequest ,
wantError : ` 400: RequestNotAllowed: : Request is not allowed in provisioningState 'Failed'. ` ,
} ,
{
name : "cluster not found in db" ,
2021-06-10 10:14:34 +03:00
resourceID : resourceID ,
2021-05-12 14:11:13 +03:00
fixture : func ( f * testdatabase . Fixture ) {
f . AddSubscriptionDocuments ( & api . SubscriptionDocument {
ID : mockSubID ,
Subscription : & api . Subscription {
State : api . SubscriptionStateRegistered ,
Properties : & api . SubscriptionProperties {
TenantID : "11111111-1111-1111-1111-111111111111" ,
} ,
} ,
} )
} ,
wantStatusCode : http . StatusNotFound ,
wantError : ` 404: ResourceNotFound: : The Resource 'openshiftclusters/resourcename' under resource group 'resourcegroup' was not found. ` ,
} ,
{
name : "internal error" ,
2021-06-10 10:14:34 +03:00
resourceID : resourceID ,
2021-05-12 14:11:13 +03:00
dbError : & cosmosdb . Error { Code : "500" , Message : "oh no!" } ,
wantStatusCode : http . StatusInternalServerError ,
wantError : ` 500: InternalServerError: : Internal server error. ` ,
} ,
} {
t . Run ( tt . name , func ( t * testing . T ) {
ti := newTestInfra ( t ) . WithOpenShiftClusters ( ) . WithSubscriptions ( )
defer ti . done ( )
if tt . dbError != nil {
ti . subscriptionsClient . SetError ( tt . dbError )
ti . openShiftClustersClient . SetError ( tt . dbError )
}
err := ti . buildFixtures ( tt . fixture )
if err != nil {
t . Fatal ( err )
}
2024-07-18 01:25:32 +03:00
f , err := NewFrontend ( ctx , ti . audit , ti . log , ti . env , ti . dbGroup , apis , & noop . Noop { } , & noop . Noop { } , nil , nil , nil , nil , nil , nil )
2021-05-12 14:11:13 +03:00
if err != nil {
t . Fatal ( err )
}
go f . Run ( ctx , nil , nil )
2021-07-20 11:20:00 +03:00
reqAPIVersion := "2021-09-01-preview"
2021-05-12 14:11:13 +03:00
if tt . apiVersion != "" {
reqAPIVersion = tt . apiVersion
}
resp , b , err := ti . request ( http . MethodPost ,
fmt . Sprintf ( "https://server%s/listAdminCredentials?api-version=%s" , tt . resourceID , reqAPIVersion ) ,
nil , nil )
if err != nil {
t . Error ( err )
}
var wantResponse interface { }
if tt . wantResponse != nil {
wantResponse = tt . wantResponse ( tt )
}
err = validateResponse ( resp , b , tt . wantStatusCode , tt . wantError , wantResponse )
if err != nil {
t . Error ( err )
}
} )
}
}