Skip to content

Commit

Permalink
Fix Azure Managed SQL fetcher available definition (#19746)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcorado authored Dec 30, 2022
1 parent ecef346 commit 8ca9037
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/srv/discovery/fetchers/db/azure_managed_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (f *azureManagedSQLServerFetcher) NewDatabaseFromServer(server *armsql.Mana

database, err := services.NewDatabaseFromAzureManagedSQLServer(server)
if err != nil {
log.Warnf("Could not convert Azure SQL server %q to database resource: %v.", azure.StringVal(server.Name), err)
log.Warnf("Could not convert Azure Managed SQL server %q to database resource: %v.", azure.StringVal(server.Name), err)
return nil
}

Expand All @@ -76,12 +76,12 @@ func (f *azureManagedSQLServerFetcher) isAvailable(server *armsql.ManagedInstanc
armsql.ManagedInstancePropertiesProvisioningStateRegistering,
armsql.ManagedInstancePropertiesProvisioningStateUnknown,
armsql.ManagedInstancePropertiesProvisioningStateUnrecognized:
return true
return false
case armsql.ManagedInstancePropertiesProvisioningStateCreated,
armsql.ManagedInstancePropertiesProvisioningStateRunning,
armsql.ManagedInstancePropertiesProvisioningStateSucceeded,
armsql.ManagedInstancePropertiesProvisioningStateUpdating:
return false
return true
default:
logrus.Warnf("Unknown status type: %q. Assuming Managed SQL Server %q is available.",
azure.StringVal(server.Properties.ProvisioningState),
Expand Down
85 changes: 85 additions & 0 deletions lib/srv/discovery/fetchers/db/azure_managed_sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2022 Gravitational, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package db

import (
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/sql/armsql"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"

"github.com/gravitational/teleport/lib/utils"
)

func TestSQLManagedServerFetcher(t *testing.T) {
logger := utils.NewLoggerForTests()
fetcher := &azureManagedSQLServerFetcher{}

t.Run("NewDatabaseFromServer", func(t *testing.T) {
// List of provisioning states that are used to identify a database is
// available and can be discovered.
availableStates := []armsql.ManagedInstancePropertiesProvisioningState{
armsql.ManagedInstancePropertiesProvisioningStateCreated,
armsql.ManagedInstancePropertiesProvisioningStateRunning,
armsql.ManagedInstancePropertiesProvisioningStateSucceeded,
armsql.ManagedInstancePropertiesProvisioningStateUpdating,
}

// For available states, it should return a parsed database.
for _, state := range availableStates {
t.Run(string(state), func(t *testing.T) {
require.NotNil(t, fetcher.NewDatabaseFromServer(makeManagedSQLInstance(state), logger), "expected to have a database, but got nil")
})
}

// The remaining possible states should not return a database.
for _, state := range armsql.PossibleManagedInstancePropertiesProvisioningStateValues() {
// Skip if the state was already tested.
if slices.Contains(availableStates, state) {
continue
}

t.Run(string(state), func(t *testing.T) {
require.Nil(t, fetcher.NewDatabaseFromServer(makeManagedSQLInstance(state), logger), "expected to have nil, but got a database")
})
}

t.Run("RandomState", func(t *testing.T) {
require.NotNil(t,
fetcher.NewDatabaseFromServer(
makeManagedSQLInstance("RandomState"),
logger,
),
"expected to have a database, but got nil",
)
})
})
}

// makeManagedSQLInstances returns a ManagedInstance struct with the provided
// provisioning state.
func makeManagedSQLInstance(state armsql.ManagedInstancePropertiesProvisioningState) *armsql.ManagedInstance {
return &armsql.ManagedInstance{
ID: to.Ptr("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-groupd/providers/Microsoft.Sql/servers/sqlserver"),
Name: to.Ptr("sqlserver"),
Location: to.Ptr("westus"),
Properties: &armsql.ManagedInstanceProperties{
FullyQualifiedDomainName: to.Ptr("sqlserver.database.windows.net"),
ProvisioningState: to.Ptr(state),
},
}
}

0 comments on commit 8ca9037

Please sign in to comment.