diff --git a/.github/workflows/integration-tests-v2.yml b/.github/workflows/integration-tests-v2.yml index cb8a64d..c0d8772 100644 --- a/.github/workflows/integration-tests-v2.yml +++ b/.github/workflows/integration-tests-v2.yml @@ -27,15 +27,14 @@ jobs: firebolt-client-id: ${{ secrets.FIREBOLT_CLIENT_ID_STG_NEW_IDN }} firebolt-client-secret: ${{ secrets.FIREBOLT_CLIENT_SECRET_STG_NEW_IDN}} api-endpoint: "api.staging.firebolt.io" - account: ${{ vars.FIREBOLT_ACCOUNT_V1 }} + account: ${{ vars.FIREBOLT_ACCOUNT }} - name: Run integration tests env: DATABASE_NAME: ${{ steps.setup.outputs.database_name }} ENGINE_NAME: ${{ steps.setup.outputs.engine_name }} FIREBOLT_ENDPOINT: "api.staging.firebolt.io" - ACCOUNT_NAME_V1: ${{ vars.FIREBOLT_ACCOUNT_V1 }} - ACCOUNT_NAME_V2: ${{ vars.FIREBOLT_ACCOUNT_V2 }} + ACCOUNT_NAME: ${{ vars.FIREBOLT_ACCOUNT }} CLIENT_ID: ${{ secrets.FIREBOLT_CLIENT_ID_STG_NEW_IDN }} CLIENT_SECRET: ${{ secrets.FIREBOLT_CLIENT_SECRET_STG_NEW_IDN }} run: | diff --git a/client.go b/client.go index 1449781..5c3986a 100644 --- a/client.go +++ b/client.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "strings" "github.com/astaxie/beego/cache" ) @@ -21,11 +20,6 @@ type ClientImpl struct { BaseClient } -const engineStatusRunning = "Running" -const engineInfoSQL = ` -SELECT url, status, attached_to FROM information_schema.engines -WHERE engine_name='%s' -` const accountError = `account '%s' does not exist in this organization or is not authorized. Please verify the account name and make sure your service account has the correct RBAC permissions and is linked to a user` @@ -69,44 +63,6 @@ func MakeClient(settings *fireboltSettings, apiEndpoint string) (*ClientImpl, er } return client, nil } -func (c *ClientImpl) getEngineUrlStatusDBByName(ctx context.Context, engineName string, systemEngineUrl string) (string, string, string, error) { - infolog.Printf("Get info for engine '%s'", engineName) - engineSQL := fmt.Sprintf(engineInfoSQL, engineName) - queryRes, err := c.Query(ctx, systemEngineUrl, engineSQL, make(map[string]string), connectionControl{}) - if err != nil { - return "", "", "", ConstructNestedError("error executing engine info sql query", err) - } - - if len(queryRes.Data) == 0 { - return "", "", "", fmt.Errorf("engine with name %s doesn't exist", engineName) - } - - engineUrl, status, dbName, err := parseEngineInfoResponse(queryRes.Data) - if err != nil { - return "", "", "", ConstructNestedError("error parsing server response for engine info SQL query", err) - } - return engineUrl, status, dbName, nil -} - -func parseEngineInfoResponse(resp [][]interface{}) (string, string, string, error) { - if len(resp) != 1 || len(resp[0]) != 3 { - return "", "", "", fmt.Errorf("invalid response shape: %v", resp) - } - engineUrl, ok := resp[0][0].(string) - if !ok { - return "", "", "", fmt.Errorf("expected string for engine URL, got %v", resp[0][0]) - } - status, ok := resp[0][1].(string) - if !ok { - return "", "", "", fmt.Errorf("expected string for engine status, got %v", resp[0][1]) - } - dbName, ok := resp[0][2].(string) - // NULL is also acceptable for database name - if !ok && resp[0][2] != nil { - return "", "", "", fmt.Errorf("expected string for engine status, got %v", resp[0][1]) - } - return engineUrl, status, dbName, nil -} func constructParameters(databaseName string, queryParams map[string][]string) map[string]string { parameters := make(map[string]string) @@ -217,15 +173,6 @@ func (c *ClientImpl) getQueryParams(setStatements map[string]string) (map[string for setKey, setValue := range setStatements { params[setKey] = setValue } - // Account id is only used when querying system engine for infra v1 - if c.ConnectedToSystemEngine && c.AccountVersion == 1 { - if len(c.AccountID) == 0 { - return nil, fmt.Errorf("Trying to run a query against system engine without account id defined") - } - if _, ok := params["account_id"]; !ok { - params["account_id"] = c.AccountID - } - } return params, nil } @@ -233,15 +180,16 @@ func (c *ClientImpl) getAccessToken() (string, error) { return getAccessTokenServiceAccount(c.ClientID, c.ClientSecret, c.ApiEndpoint, c.UserAgent) } -func (c *ClientImpl) getConnectionParametersV2( - ctx context.Context, - engineName, - databaseName, - systemEngineURL string, - systemEngineParameters map[string]string, -) (string, map[string]string, error) { - engineURL := systemEngineURL - parameters := systemEngineParameters +// GetConnectionParameters returns engine URL and parameters based on engineName and databaseName +func (c *ClientImpl) GetConnectionParameters(ctx context.Context, engineName, databaseName string) (string, map[string]string, error) { + // Assume we are connected to a system engine in the beginning + + engineURL, parameters, err := c.getSystemEngineURLAndParameters(context.Background(), c.AccountName, databaseName) + if err != nil { + return "", nil, ConstructNestedError("error during getting system engine url", err) + } + c.ConnectedToSystemEngine = true + control := connectionControl{ updateParameters: func(key, value string) { parameters[key] = value @@ -265,50 +213,3 @@ func (c *ClientImpl) getConnectionParametersV2( } return engineURL, parameters, nil } - -func (c *ClientImpl) getConnectionParametersV1( - ctx context.Context, - engineName, - databaseName, - systemEngineURL string, -) (string, map[string]string, error) { - // If engine name is empty, assume system engine - if len(engineName) == 0 { - return systemEngineURL, map[string]string{"database": databaseName}, nil - } - - engineUrl, status, dbName, err := c.getEngineUrlStatusDBByName(ctx, engineName, systemEngineURL) - params := map[string]string{"database": dbName} - if err != nil { - return "", params, ConstructNestedError("error during getting engine info", err) - } - // Case-insensitive comparison - if !strings.EqualFold(status, engineStatusRunning) { - return "", params, fmt.Errorf("engine %s is not running", engineName) - } - if len(dbName) == 0 { - return "", params, fmt.Errorf("engine %s not attached to any DB or you don't have permission to access its database", engineName) - } - if len(databaseName) != 0 && databaseName != dbName { - return "", params, fmt.Errorf("engine %s is not attached to database %s", engineName, databaseName) - } - c.ConnectedToSystemEngine = false - - return engineUrl, params, nil -} - -// GetConnectionParameters returns engine URL and parameters based on engineName and databaseName -func (c *ClientImpl) GetConnectionParameters(ctx context.Context, engineName, databaseName string) (string, map[string]string, error) { - // Assume we are connected to a system engine in the beginning - - systemEngineURL, systemEngineParameters, err := c.getSystemEngineURLAndParameters(context.Background(), c.AccountName, databaseName) - if err != nil { - return "", nil, ConstructNestedError("error during getting system engine url", err) - } - - c.ConnectedToSystemEngine = true - if c.AccountVersion == 2 { - return c.getConnectionParametersV2(ctx, engineName, databaseName, systemEngineURL, systemEngineParameters) - } - return c.getConnectionParametersV1(ctx, engineName, databaseName, systemEngineURL) -} diff --git a/client_integration_test.go b/client_integration_test.go index 7d82825..3b119e3 100644 --- a/client_integration_test.go +++ b/client_integration_test.go @@ -5,30 +5,18 @@ package fireboltgosdk import ( "context" - "strings" "testing" "time" ) // TestGetEnginePropsByName test getting system engine url, as well as engine url, status and database by name func TestGetEnginePropsByName(t *testing.T) { - systemEngineURL, _, err := clientMockWithAccount.getSystemEngineURLAndParameters(context.TODO(), accountNameV1Mock, "") + systemEngineURL, _, err := clientMockWithAccount.getSystemEngineURLAndParameters(context.TODO(), accountName, "") if err != nil { t.Errorf("Error returned by getSystemEngineURL: %s", err) } if len(systemEngineURL) == 0 { - t.Errorf("Empty system engine url returned by getSystemEngineURL for account: %s", accountNameV1Mock) - } - - engineURL, status, _, err := clientMockWithAccount.getEngineUrlStatusDBByName(context.TODO(), engineNameMock, systemEngineURL) - if err != nil { - t.Errorf("Error returned by getEngineUrlStatusDBByName: %s", err) - } - if engineURL == "" { - t.Errorf("Empty engine url returned by getEngineUrlStatusDBByName") - } - if !strings.EqualFold(status, "Running") { - t.Errorf("Invalid status returned by getEngineUrlStatusDBByName. Got: %s, should be Running", status) + t.Errorf("Empty system engine url returned by getSystemEngineURL for account: %s", accountName) } } diff --git a/connection_integration_test.go b/connection_integration_test.go index b64b9c6..09b2c2a 100644 --- a/connection_integration_test.go +++ b/connection_integration_test.go @@ -10,42 +10,6 @@ import ( "testing" ) -func setupEngineAndDatabase(t *testing.T) { - conn, err := sql.Open("firebolt", dsnSystemEngineV2Mock) - if err != nil { - t.Errorf("opening a connection failed unexpectedly: %v", err) - t.FailNow() - } - if _, err = conn.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS \"%s\"", databaseMock)); err != nil { - t.Errorf("creating a database failed unexpectedly: %v", err) - t.FailNow() - } - if _, err = conn.Exec(fmt.Sprintf("CREATE ENGINE IF NOT EXISTS \"%s\"", engineNameMock)); err != nil { - t.Errorf("creating an engine failed unexpectedly: %v", err) - t.FailNow() - } -} - -func cleanupEngineAndDatabase(t *testing.T) { - conn, err := sql.Open("firebolt", dsnSystemEngineV2Mock) - if err != nil { - t.Errorf("opening a connection failed unexpectedly: %v", err) - t.FailNow() - } - if _, err = conn.Exec(fmt.Sprintf("STOP ENGINE \"%s\"", engineNameMock)); err != nil { - t.Errorf("stopping an engine failed unexpectedly: %v", err) - t.FailNow() - } - if _, err = conn.Exec(fmt.Sprintf("DROP ENGINE IF EXISTS \"%s\"", engineNameMock)); err != nil { - t.Errorf("dropping an engine failed unexpectedly: %v", err) - t.FailNow() - } - if _, err = conn.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS \"%s\"", databaseMock)); err != nil { - t.Errorf("dropping a database failed unexpectedly: %v", err) - t.FailNow() - } -} - func TestConnectionUseDatabase(t *testing.T) { tableName := "test_use_database" createTableSQL := "CREATE TABLE IF NOT EXISTS " + tableName + " (id INT)" @@ -107,32 +71,13 @@ func TestConnectionUseDatabase(t *testing.T) { } } -func TestConnectionV2(t *testing.T) { - setupEngineAndDatabase(t) - defer cleanupEngineAndDatabase(t) - - conn, err := sql.Open("firebolt", dsnV2Mock) - if err != nil { - t.Errorf("opening a connection failed unexpectedly") - t.FailNow() - } - - _, err = conn.Exec("SELECT 1") - if err != nil { - t.Errorf("query failed with %v", err) - t.FailNow() - } -} - -func TestConnectionV2UseDatabaseEngine(t *testing.T) { - setupEngineAndDatabase(t) - defer cleanupEngineAndDatabase(t) +func TestConnectionUseDatabaseEngine(t *testing.T) { const createTableSQL = "CREATE TABLE IF NOT EXISTS test_use (id INT)" const insertSQL = "INSERT INTO test_use VALUES (1)" const insertSQL2 = "INSERT INTO test_use VALUES (2)" - conn, err := sql.Open("firebolt", dsnSystemEngineV2Mock) + conn, err := sql.Open("firebolt", dsnSystemEngineMock) if err != nil { t.Errorf("opening a connection failed unexpectedly") t.FailNow() @@ -188,7 +133,7 @@ func TestConnectionV2UseDatabaseEngine(t *testing.T) { } func TestConnectionUppercaseNames(t *testing.T) { - systemConnection, err := sql.Open("firebolt", dsnSystemEngineV2Mock) + systemConnection, err := sql.Open("firebolt", dsnSystemEngineMock) if err != nil { t.Errorf("opening a system connection failed unexpectedly %v", err) t.FailNow() @@ -212,7 +157,7 @@ func TestConnectionUppercaseNames(t *testing.T) { dsnUppercase := fmt.Sprintf( "firebolt:///%s?account_name=%s&engine=%s&client_id=%s&client_secret=%s", - databaseName, accountNameV2Mock, engineName, clientIdMock, clientSecretMock, + databaseName, accountName, engineName, clientIdMock, clientSecretMock, ) conn, err := sql.Open("firebolt", dsnUppercase) diff --git a/driver_integration_test.go b/driver_integration_test.go index 271f737..97246c6 100644 --- a/driver_integration_test.go +++ b/driver_integration_test.go @@ -14,8 +14,6 @@ import ( "strings" "testing" "time" - - "github.com/google/uuid" ) var ( @@ -23,15 +21,12 @@ var ( dsnNoDatabaseMock string dsnSystemEngineWithDatabaseMock string dsnSystemEngineMock string - dsnV2Mock string - dsnSystemEngineV2Mock string clientIdMock string clientSecretMock string databaseMock string engineNameMock string engineUrlMock string - accountNameV1Mock string - accountNameV2Mock string + accountName string serviceAccountNoUserName string clientMock *ClientImpl clientMockWithAccount *ClientImpl @@ -45,22 +40,19 @@ func init() { clientSecretMock = os.Getenv("CLIENT_SECRET") databaseMock = os.Getenv("DATABASE_NAME") engineNameMock = os.Getenv("ENGINE_NAME") - accountNameV1Mock = os.Getenv("ACCOUNT_NAME_V1") - accountNameV2Mock = os.Getenv("ACCOUNT_NAME_V2") + accountName = os.Getenv("ACCOUNT_NAME") - dsnMock = fmt.Sprintf("firebolt:///%s?account_name=%s&engine=%s&client_id=%s&client_secret=%s", databaseMock, accountNameV1Mock, engineNameMock, clientIdMock, clientSecretMock) - dsnSystemEngineMock = fmt.Sprintf("firebolt://?account_name=%s&client_id=%s&client_secret=%s", accountNameV1Mock, clientIdMock, clientSecretMock) - dsnNoDatabaseMock = fmt.Sprintf("firebolt://?account_name=%s&engine=%s&client_id=%s&client_secret=%s", accountNameV1Mock, engineNameMock, clientIdMock, clientSecretMock) - dsnSystemEngineWithDatabaseMock = fmt.Sprintf("firebolt:///%s?account_name=%s&client_id=%s&client_secret=%s", databaseMock, accountNameV1Mock, clientIdMock, clientSecretMock) + dsnMock = fmt.Sprintf("firebolt:///%s?account_name=%s&engine=%s&client_id=%s&client_secret=%s", databaseMock, accountName, engineNameMock, clientIdMock, clientSecretMock) + dsnNoDatabaseMock = fmt.Sprintf("firebolt://?account_name=%s&engine=%s&client_id=%s&client_secret=%s", accountName, engineNameMock, clientIdMock, clientSecretMock) + dsnSystemEngineWithDatabaseMock = fmt.Sprintf("firebolt:///%s?account_name=%s&client_id=%s&client_secret=%s", databaseMock, accountName, clientIdMock, clientSecretMock) - dsnV2Mock = fmt.Sprintf("firebolt:///%s?account_name=%s&engine=%s&client_id=%s&client_secret=%s", databaseMock, accountNameV2Mock, engineNameMock, clientIdMock, clientSecretMock) - dsnSystemEngineV2Mock = fmt.Sprintf("firebolt://?account_name=%s&client_id=%s&client_secret=%s", accountNameV2Mock, clientIdMock, clientSecretMock) + dsnSystemEngineMock = fmt.Sprintf("firebolt://?account_name=%s&client_id=%s&client_secret=%s", accountName, clientIdMock, clientSecretMock) var err error client, err := Authenticate(&fireboltSettings{ clientID: clientIdMock, clientSecret: clientSecretMock, - accountName: accountNameV1Mock, + accountName: accountName, engineName: engineNameMock, database: databaseMock, newVersion: true, @@ -72,35 +64,22 @@ func init() { clientWithAccount, err := Authenticate(&fireboltSettings{ clientID: clientIdMock, clientSecret: clientSecretMock, - accountName: accountNameV1Mock, + accountName: accountName, database: databaseMock, newVersion: true, }, GetHostNameURL()) if err != nil { panic(fmt.Sprintf("Authentication error: %v", err)) } + engineUrlMock, _, err = clientMock.GetConnectionParameters(context.TODO(), engineNameMock, databaseMock) + if err != nil { + panic(fmt.Errorf("Error getting connection parameters: %v", err)) + } clientMockWithAccount = clientWithAccount.(*ClientImpl) clientMockWithAccount.ConnectedToSystemEngine = true - engineUrlMock = getEngineURL() serviceAccountNoUserName = databaseMock + "_sa_no_user" } -func getEngineURL() string { - systemEngineURL, _, err := clientMockWithAccount.getSystemEngineURLAndParameters(context.TODO(), accountNameV1Mock, "") - if err != nil { - panic(fmt.Sprintf("Error returned by getSystemEngineURL: %s", err)) - } - if len(systemEngineURL) == 0 { - panic(fmt.Sprintf("Empty system engine url returned by getSystemEngineURL for account: %s", accountNameV1Mock)) - } - - engineURL, _, _, err := clientMockWithAccount.getEngineUrlStatusDBByName(context.TODO(), engineNameMock, systemEngineURL) - if err != nil { - panic(fmt.Sprintf("Error returned by getEngineUrlStatusDBByName: %s", err)) - } - return engineURL -} - // TestDriverQueryResult tests query happy path, as user would do it func TestDriverQueryResult(t *testing.T) { loc, _ := time.LoadLocation("UTC") @@ -229,94 +208,6 @@ func TestDriverSystemEngineDbContext(t *testing.T) { } } -// TestDriverSystemEngine checks system engine queries are executed without error -func TestDriverSystemEngine(t *testing.T) { - suffix := strings.ReplaceAll(uuid.New().String(), "-", "") - databaseName := fmt.Sprintf("gosdk_system_engine_test_%s", suffix) - engineName := fmt.Sprintf("gosdk_system_engine_test_e_%s", suffix) - engineNewName := fmt.Sprintf("gosdk_system_engine_test_e_2_%s", suffix) - - db, err := sql.Open("firebolt", dsnSystemEngineMock) - if err != nil { - t.Errorf("failed unexpectedly with %v", err) - } - ddlStatements := []string{ - fmt.Sprintf("CREATE DATABASE \"%s\"", databaseName), - fmt.Sprintf("CREATE ENGINE \"%s\" WITH TYPE = S NODES = 1 AUTO_START = false", engineName), - fmt.Sprintf("ALTER DATABASE \"%s\" SET DESCRIPTION = 'GO SDK Integration test'", databaseName), - fmt.Sprintf("ALTER ENGINE \"%s\" RENAME TO %s", engineName, engineNewName), - fmt.Sprintf("START ENGINE \"%s\"", engineNewName), - fmt.Sprintf("STOP ENGINE \"%s\"", engineNewName), - } - - // Cleanup - defer func() { - stopEngineQuery := fmt.Sprintf("STOP ENGINE \"%s\"", engineName) - stopNewEngineQuery := fmt.Sprintf("STOP ENGINE \"%s\"", engineNewName) - dropEngineQuery := fmt.Sprintf("DROP ENGINE IF EXISTS \"%s\"", engineName) - dropNewEngineQuery := fmt.Sprintf("DROP ENGINE IF EXISTS \"%s\"", engineNewName) - for _, query := range []string{stopEngineQuery, stopNewEngineQuery, dropEngineQuery, dropNewEngineQuery} { - db.Query(query) - } - dropDbQuery := fmt.Sprintf("DROP DATABASE \"%s\"", databaseName) - _, err = db.Query(dropDbQuery) - if err != nil { - t.Errorf("The cleanup query %s returned an error: %v", dropDbQuery, err) - } - }() - - for _, query := range ddlStatements { - _, err := db.Query(query) - if err != nil { - t.Errorf("The query %s returned an error: %v", query, err) - } - } - rows, err := db.Query(fmt.Sprintf("SELECT database_name FROM information_schema.databases WHERE database_name='%s'", databaseName)) - defer rows.Close() - if err != nil { - t.Errorf("Failed to query information_schema.databases : %v", err) - } - - if !rows.Next() { - t.Errorf("Could not find database with name %s", databaseName) - } - rows, err = db.Query(fmt.Sprintf("SELECT engine_name FROM information_schema.engines WHERE engine_name='%s'", engineNewName)) - defer rows.Close() - if err != nil { - t.Errorf("Failed to query information_schema.engines : %v", err) - } - if !rows.Next() { - t.Errorf("Could not find engine with name %s", engineNewName) - } -} - -func containsDatabase(rows *sql.Rows, databaseToFind string) (bool, error) { - var databaseName, region, attachedEngines, createdOn, createdBy, errors string - for rows.Next() { - if err := rows.Scan(&databaseName, ®ion, &attachedEngines, &createdOn, &createdBy, &errors); err != nil { - return false, err - } - if databaseToFind == databaseName { - return true, nil - } - } - return false, nil -} - -func containsEngine(rows *sql.Rows, engineToFind string) (bool, error) { - var engineName, region, spec, scale, status, attachedTo, version string - defer rows.Close() - for rows.Next() { - if err := rows.Scan(&engineName, ®ion, &spec, &scale, &status, &attachedTo, &version); err != nil { - return false, err - } - if engineName == engineToFind { - return true, nil - } - } - return false, nil -} - func TestIncorrectAccount(t *testing.T) { _, err := Authenticate(&fireboltSettings{ clientID: clientIdMock, @@ -392,10 +283,13 @@ func TestServiceAccountAuthentication(t *testing.T) { serviceAccountID, serviceAccountSecret := createServiceAccountNoUser(t, serviceAccountNoUserName) defer deleteServiceAccount(t, serviceAccountNoUserName) // Delete service account after the test + // Clear the cache to ensure that the new service account is used + AccountCache.ClearAll() + _, err := Authenticate(&fireboltSettings{ clientID: serviceAccountID, clientSecret: serviceAccountSecret, - accountName: accountNameV1Mock, + accountName: accountName, engineName: engineNameMock, database: databaseMock, newVersion: true, @@ -403,7 +297,7 @@ func TestServiceAccountAuthentication(t *testing.T) { if err == nil { t.Errorf("Authentication didn't return an error, although it should") } - if !strings.HasPrefix(err.Error(), fmt.Sprintf("error during getting account id: account '%s' does not exist", accountNameV1Mock)) { + if !strings.HasPrefix(err.Error(), fmt.Sprintf("error during getting account id: account '%s' does not exist", accountName)) { t.Errorf("Authentication didn't return an error with correct message, got: %s", err.Error()) } } diff --git a/driver_integration_v0_test.go b/driver_integration_v0_test.go index 8994ea4..233f1ef 100644 --- a/driver_integration_v0_test.go +++ b/driver_integration_v0_test.go @@ -11,11 +11,8 @@ import ( "os" "reflect" "runtime/debug" - "strings" "testing" "time" - - "github.com/google/uuid" ) var ( @@ -180,110 +177,3 @@ func TestDriverOpenDefaultEngine(t *testing.T) { func TestDriverExecStatement(t *testing.T) { runTestDriverExecStatement(t, dsnMock) } - -// TestDriverSystemEngine checks system engine queries are executed without error -func TestDriverSystemEngine(t *testing.T) { - suffix := strings.ReplaceAll(uuid.New().String(), "-", "") - databaseName := fmt.Sprintf("gosdk_system_engine_test_%s", suffix) - engineName := fmt.Sprintf("gosdk_system_engine_test_e_%s", suffix) - engineNewName := fmt.Sprintf("gosdk_system_engine_test_e_2_%s", suffix) - - db, err := sql.Open("firebolt", dsnSystemEngineMock) - if err != nil { - t.Errorf("failed unexpectedly with %v", err) - } - ddlStatements := []string{ - fmt.Sprintf("CREATE DATABASE \"%s\"", databaseName), - fmt.Sprintf("CREATE ENGINE \"%s\" WITH SPEC = 'C1' SCALE = 1", engineName), - fmt.Sprintf("ATTACH ENGINE \"%s\" TO \"%s\"", engineName, databaseName), - fmt.Sprintf("ALTER DATABASE \"%s\" SET DESCRIPTION = 'GO SDK Integration test'", databaseName), - fmt.Sprintf("ALTER ENGINE \"%s\" RENAME TO \"%s\"", engineName, engineNewName), - fmt.Sprintf("START ENGINE \"%s\"", engineNewName), - fmt.Sprintf("STOP ENGINE \"%s\"", engineNewName), - } - - // Cleanup - defer func() { - stopEngineQuery := fmt.Sprintf("STOP ENGINE \"%s\"", engineName) - stopNewEngineQuery := fmt.Sprintf("STOP ENGINE \"%s\"", engineNewName) - dropEngineQuery := fmt.Sprintf("DROP ENGINE IF EXISTS \"%s\"", engineName) - dropNewEngineQuery := fmt.Sprintf("DROP ENGINE IF EXISTS \"%s\"", engineNewName) - for _, query := range []string{stopEngineQuery, stopNewEngineQuery, dropEngineQuery, dropNewEngineQuery} { - db.Query(query) - } - dropDbQuery := fmt.Sprintf("DROP DATABASE \"%s\"", databaseName) - _, err = db.Query(dropDbQuery) - if err != nil { - t.Errorf("The cleanup query %s returned an error: %v", dropDbQuery, err) - } - }() - - for _, query := range ddlStatements { - _, err := db.Query(query) - if err != nil { - t.Errorf("The query %s returned an error: %v", query, err) - } - } - rows, err := db.Query("SHOW DATABASES") - defer rows.Close() - if err != nil { - t.Errorf("Failed to execute query 'SHOW DATABASES' : %v", err) - } - containsDatabase, err := containsDatabase(rows, databaseName) - if err != nil { - t.Errorf("Failed to read response for query 'SHOW DATABASES' : %v", err) - } - - if !containsDatabase { - t.Errorf("Could not find database with name %s", databaseName) - } - // Uncomment once https://packboard.atlassian.net/browse/FIR-17301 is done - //rows, err = db.Query("SHOW ENGINES") - //defer rows.Close() - //if err != nil { - // t.Errorf("Failed to execute query 'SHOW ENGINES' : %v", err) - //} - //containsEngine, err := containsEngine(rows, databaseName) - //if err != nil { - // t.Errorf("Failed to read response for query 'SHOW ENGINES' : %v", err) - //} - //if !containsEngine { - // t.Errorf("Could not find engine with name %s", engineName) - //} -} - -func containsDatabase(rows *sql.Rows, databaseToFind string) (bool, error) { - var databaseName, compressed_size, uncompressed_size, description, createdOn, createdBy, region, attachedEngines, errors string - for rows.Next() { - if err := rows.Scan( - &databaseName, - &compressed_size, - &uncompressed_size, - &description, - &createdOn, - &createdBy, - ®ion, - &attachedEngines, - &errors); err != nil { - return false, err - } - if databaseToFind == databaseName { - return true, nil - } - } - return false, nil -} - -func containsEngine(rows *sql.Rows, engineToFind string) (bool, error) { - var engineName, region, spec, scale, status, attachedTo, version string - defer rows.Close() - for rows.Next() { - if err := rows.Scan(&engineName, ®ion, &spec, &scale, &status, &attachedTo, &version); err != nil { - return false, err - } - if engineName == engineToFind { - return true, nil - } - } - return false, nil -} diff --git a/driver_options_integration_test.go b/driver_options_integration_test.go index 8256689..8722dc8 100644 --- a/driver_options_integration_test.go +++ b/driver_options_integration_test.go @@ -8,51 +8,31 @@ import ( "testing" ) -func testFireboltConnectorWithOptions(t *testing.T, engineUrl, databaseName, accountID, token, userAgent string) { - conn := FireboltConnectorWithOptions( - WithEngineUrl(engineUrl), - WithDatabaseName(databaseMock), - WithClientParams(accountID, token, userAgent), - ) - - resp, err := conn.client.Query(context.Background(), conn.engineUrl, "SELECT 1", nil, connectionControl{}) - if err != nil { - t.Errorf("failed unexpectedly with: %v", err) - } - assert(len(resp.Data), 1, t, "result data length is not 1") - assert(len(resp.Data[0]), 1, t, "result value is invalid") - assert(resp.Data[0][0].(float64), float64(1), t, "result is not 1") -} - -func TestFireboltConnectorWithOptionsAccountV1(t *testing.T) { - accountID := clientMockWithAccount.AccountID +func TestFireboltConnectorWithOptions(t *testing.T) { userAgent := "test user agent" token, err := getAccessTokenServiceAccount(clientIdMock, clientSecretMock, GetHostNameURL(), userAgent) if err != nil { t.Errorf("failed to get access token: %v", err) } - engineUrl, _, err := clientMockWithAccount.getSystemEngineURLAndParameters(context.TODO(), accountNameV1Mock, "") + engineUrl, engineParameters, err := clientMockWithAccount.getSystemEngineURLAndParameters(context.TODO(), accountName, "") if err != nil { t.Errorf("failed to get system engine url: %v", err) } - testFireboltConnectorWithOptions(t, engineUrl, databaseMock, accountID, token, userAgent) -} + accountID, _ := engineParameters["account_id"] -func TestFireboltConnectorWithOptionsAccountV2(t *testing.T) { - userAgent := "test user agent" - token, err := getAccessTokenServiceAccount(clientIdMock, clientSecretMock, GetHostNameURL(), userAgent) - if err != nil { - t.Errorf("failed to get access token: %v", err) - } + conn := FireboltConnectorWithOptions( + WithEngineUrl(engineUrl), + WithDatabaseName(databaseMock), + WithClientParams(accountID, token, userAgent), + ) - engineUrl, engineParameters, err := clientMockWithAccount.getSystemEngineURLAndParameters(context.TODO(), accountNameV2Mock, "") + resp, err := conn.client.Query(context.Background(), conn.engineUrl, "SELECT 1", nil, connectionControl{}) if err != nil { - t.Errorf("failed to get system engine url: %v", err) + t.Errorf("failed unexpectedly with: %v", err) } - - accountID, _ := engineParameters["account_id"] - - testFireboltConnectorWithOptions(t, engineUrl, databaseMock, accountID, token, userAgent) + assert(len(resp.Data), 1, t, "result data length is not 1") + assert(len(resp.Data[0]), 1, t, "result value is invalid") + assert(resp.Data[0][0].(float64), float64(1), t, "result is not 1") } diff --git a/go.mod b/go.mod index 8f8e89b..8e41bac 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,3 @@ require github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 require github.com/matishsiao/goInfo v0.0.0-20210923090445-da2e3fa8d45f require github.com/astaxie/beego v1.12.3 - -require github.com/google/uuid v1.3.0 diff --git a/go.sum b/go.sum index ce39a16..2cf2296 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,6 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=