From 8ca903742ed874ba53e146ccd01ff75c08013db1 Mon Sep 17 00:00:00 2001 From: Gabriel Corado Date: Fri, 30 Dec 2022 18:04:18 -0300 Subject: [PATCH] Fix Azure Managed SQL fetcher available definition (#19746) --- .../fetchers/db/azure_managed_sql.go | 6 +- .../fetchers/db/azure_managed_sql_test.go | 85 +++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 lib/srv/discovery/fetchers/db/azure_managed_sql_test.go diff --git a/lib/srv/discovery/fetchers/db/azure_managed_sql.go b/lib/srv/discovery/fetchers/db/azure_managed_sql.go index 238213fb75eae..140180e9a9acf 100644 --- a/lib/srv/discovery/fetchers/db/azure_managed_sql.go +++ b/lib/srv/discovery/fetchers/db/azure_managed_sql.go @@ -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 } @@ -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), diff --git a/lib/srv/discovery/fetchers/db/azure_managed_sql_test.go b/lib/srv/discovery/fetchers/db/azure_managed_sql_test.go new file mode 100644 index 0000000000000..84b9ad68b7552 --- /dev/null +++ b/lib/srv/discovery/fetchers/db/azure_managed_sql_test.go @@ -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), + }, + } +}