From 6326fa94ded72ddaf0c1b265caf8f887d039d558 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Thu, 19 Oct 2023 13:38:37 +0200 Subject: [PATCH] move client_configuration_finder to client_configuration --- client_configuration.go | 63 +++++++++++ client_configuration_finder.go | 71 ------------ client_configuration_finder_test.go | 165 ---------------------------- client_configuration_test.go | 156 ++++++++++++++++++++++++++ 4 files changed, 219 insertions(+), 236 deletions(-) delete mode 100644 client_configuration_finder.go delete mode 100644 client_configuration_finder_test.go diff --git a/client_configuration.go b/client_configuration.go index 381ed8d86..27f8ceaf7 100644 --- a/client_configuration.go +++ b/client_configuration.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "os" + "path" "strings" ) @@ -20,6 +21,68 @@ const ( levelTrace string = "TRACE" // trace log level ) +const ( + defaultConfigName = "sf_client_config.json" + clientConfEnvName = "SF_CLIENT_CONFIG_FILE" +) + +func getClientConfig(filePathFromConnectionString string) (*ClientConfig, error) { + configPredefinedFilePaths, err := clientConfigPredefinedDirs() + var filePath string + filePath, err = findClientConfigFilePath(filePathFromConnectionString, configPredefinedFilePaths) + if err != nil { + return nil, err + } + if filePath == "" { // we did not find a config file + return nil, nil + } + return parseClientConfiguration(filePath) +} + +func findClientConfigFilePath(filePathFromConnectionString string, configPredefinedDirs []string) (string, error) { + if filePathFromConnectionString != "" { + return filePathFromConnectionString, nil + } + envConfigFilePath := os.Getenv(clientConfEnvName) + if envConfigFilePath != "" { + return envConfigFilePath, nil + } + return searchForConfigFile(configPredefinedDirs) +} + +func searchForConfigFile(directories []string) (string, error) { + for _, dir := range directories { + filePath := path.Join(dir, defaultConfigName) + exists, err := existsFile(filePath) + if err != nil { + return "", err + } + if exists { + return filePath, nil + } + } + return "", nil +} + +func existsFile(filePath string) (bool, error) { + _, err := os.Stat(filePath) + if err == nil { + return true, nil + } + if errors.Is(err, os.ErrNotExist) { + return false, nil + } + return false, err +} + +func clientConfigPredefinedDirs() ([]string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return nil, err + } + return []string{".", homeDir, os.TempDir()}, nil +} + // ClientConfig config root type ClientConfig struct { Common *ClientConfigCommonProps `json:"common"` diff --git a/client_configuration_finder.go b/client_configuration_finder.go deleted file mode 100644 index 9a6baf5ee..000000000 --- a/client_configuration_finder.go +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved. - -package gosnowflake - -import ( - "errors" - "os" - "path" -) - -const ( - defaultConfigName = "sf_client_config.json" - clientConfEnvName = "SF_CLIENT_CONFIG_FILE" -) - -func getClientConfig(filePathFromConnectionString string) (*ClientConfig, error) { - configPredefinedFilePaths, err := clientConfigPredefinedDirs() - var filePath string - filePath, err = findClientConfigFilePath(filePathFromConnectionString, configPredefinedFilePaths) - if err != nil { - return nil, err - } - if filePath == "" { // we did not find a config file - return nil, nil - } - return parseClientConfiguration(filePath) -} - -func findClientConfigFilePath(filePathFromConnectionString string, configPredefinedDirs []string) (string, error) { - if filePathFromConnectionString != "" { - return filePathFromConnectionString, nil - } - envConfigFilePath := os.Getenv(clientConfEnvName) - if envConfigFilePath != "" { - return envConfigFilePath, nil - } - return searchForConfigFile(configPredefinedDirs) -} - -func searchForConfigFile(directories []string) (string, error) { - for _, dir := range directories { - filePath := path.Join(dir, defaultConfigName) - exists, err := existsFile(filePath) - if err != nil { - return "", err - } - if exists { - return filePath, nil - } - } - return "", nil -} - -func existsFile(filePath string) (bool, error) { - _, err := os.Stat(filePath) - if err == nil { - return true, nil - } - if errors.Is(err, os.ErrNotExist) { - return false, nil - } - return false, err -} - -func clientConfigPredefinedDirs() ([]string, error) { - homeDir, err := os.UserHomeDir() - if err != nil { - return nil, err - } - return []string{".", homeDir, os.TempDir()}, nil -} diff --git a/client_configuration_finder_test.go b/client_configuration_finder_test.go deleted file mode 100644 index 554e22d83..000000000 --- a/client_configuration_finder_test.go +++ /dev/null @@ -1,165 +0,0 @@ -// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved. - -package gosnowflake - -import ( - "os" - "path" - "testing" -) - -func TestFindConfigFileFromConnectionParameters(t *testing.T) { - dirs := createTestDirectories(t) - connParameterConfigPath := createFile(t, "conn_parameters_config.json", "random content", dirs.dir) - envConfigPath := createFile(t, "env_var_config.json", "random content", dirs.dir) - t.Setenv(clientConfEnvName, envConfigPath) - createFile(t, defaultConfigName, "random content", dirs.driverDir) - createFile(t, defaultConfigName, "random content", dirs.homeDir) - createFile(t, defaultConfigName, "random content", dirs.tempDir) - - clientConfigFilePath, err := findClientConfigFilePath(connParameterConfigPath, predefinedTestDirs(dirs)) - - assertNilF(t, err, "get client config error") - assertEqualE(t, clientConfigFilePath, connParameterConfigPath, "config file path") -} - -func TestFindConfigFileFromEnvVariable(t *testing.T) { - dirs := createTestDirectories(t) - envConfigPath := createFile(t, "env_var_config.json", "random content", dirs.dir) - t.Setenv(clientConfEnvName, envConfigPath) - createFile(t, defaultConfigName, "random content", dirs.driverDir) - createFile(t, defaultConfigName, "random content", dirs.homeDir) - createFile(t, defaultConfigName, "random content", dirs.tempDir) - - clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) - - assertNilF(t, err, "get client config error") - assertEqualE(t, clientConfigFilePath, envConfigPath, "config file path") -} - -func TestFindConfigFileFromDriverDirectory(t *testing.T) { - dirs := createTestDirectories(t) - driverConfigPath := createFile(t, defaultConfigName, "random content", dirs.driverDir) - createFile(t, defaultConfigName, "random content", dirs.homeDir) - createFile(t, defaultConfigName, "random content", dirs.tempDir) - - clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) - - assertNilF(t, err, "get client config error") - assertEqualE(t, clientConfigFilePath, driverConfigPath, "config file path") -} - -func TestFindConfigFileFromHomeDirectory(t *testing.T) { - dirs := createTestDirectories(t) - createFile(t, "wrong_file_name.json", "random content", dirs.driverDir) - homeConfigPath := createFile(t, defaultConfigName, "random content", dirs.homeDir) - createFile(t, defaultConfigName, "random content", dirs.tempDir) - - clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) - - assertNilF(t, err, "get client config error") - assertEqualE(t, clientConfigFilePath, homeConfigPath, "config file path") -} - -func TestFindConfigFileFromTempDirectory(t *testing.T) { - dirs := createTestDirectories(t) - createFile(t, "wrong_file_name.json", "random content", dirs.driverDir) - createFile(t, "wrong_file_name.json", "random content", dirs.homeDir) - tempConfigPath := createFile(t, defaultConfigName, "random content", dirs.tempDir) - - clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) - - assertNilF(t, err, "get client config error") - assertEqualE(t, clientConfigFilePath, tempConfigPath, "config file path") -} - -func TestNotFindConfigFileWhenNotDefined(t *testing.T) { - dirs := createTestDirectories(t) - createFile(t, "wrong_file_name.json", "random content", dirs.driverDir) - createFile(t, "wrong_file_name.json", "random content", dirs.homeDir) - createFile(t, "wrong_file_name.json", "random content", dirs.tempDir) - - clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) - - assertNilF(t, err, "get client config error") - assertEqualE(t, clientConfigFilePath, "", "config file path") -} - -func TestCreatePredefinedDirs(t *testing.T) { - homeDir, err := os.UserHomeDir() - assertNilF(t, err, "get home dir error") - var locations []string - - locations, err = clientConfigPredefinedDirs() - - assertNilF(t, err, "error") - assertEqualF(t, len(locations), 3, "size") - assertEqualE(t, locations[0], ".", "driver directory") - assertEqualE(t, locations[1], homeDir, "home directory") - assertEqualE(t, locations[2], os.TempDir(), "temp directory") -} - -func TestGetClientConfig(t *testing.T) { - dir := t.TempDir() - fileName := "config.json" - configContents := `{ - "common": { - "log_level" : "INFO", - "log_path" : "/some-path/some-directory" - } - }` - createFile(t, fileName, configContents, dir) - filePath := path.Join(dir, fileName) - - clientConfigFilePath, err := getClientConfig(filePath) - - assertNilF(t, err) - assertNotNilF(t, clientConfigFilePath) - assertEqualE(t, clientConfigFilePath.Common.LogLevel, "INFO", "log level") - assertEqualE(t, clientConfigFilePath.Common.LogPath, "/some-path/some-directory", "log path") -} - -func TestNoResultForGetClientConfigWhenNoFileFound(t *testing.T) { - clientConfigFilePath, err := getClientConfig("") - - assertNilF(t, err) - assertNilF(t, clientConfigFilePath) -} - -func createTestDirectories(t *testing.T) struct { - dir string - driverDir string - homeDir string - tempDir string -} { - dir := t.TempDir() - driverDir := path.Join(dir, "driver") // we pretend "." to be a folder inside t.TempDir() not to interfere with user's real directories - err := os.Mkdir(driverDir, 0755) - assertNilF(t, err, "make driver dir error") - homeDir := path.Join(dir, "home") // we pretend home directory to be a folder inside t.TempDir() not to interfere with user's real directories - err = os.Mkdir(homeDir, 0755) - assertNilF(t, err, "make home dir error") - tempDir := path.Join(dir, "temp") // we pretend temp directory to be a folder inside t.TempDir() not to interfere with user's real directories - err = os.Mkdir(tempDir, 0755) - assertNilF(t, err, "make temp dir error") - return struct { - dir string - driverDir string - homeDir string - tempDir string - }{ - dir: dir, - driverDir: driverDir, - homeDir: homeDir, - tempDir: tempDir, - } -} - -func predefinedTestDirs(dirs struct { - dir string - driverDir string - homeDir string - tempDir string -}) []string { - return []string{dirs.driverDir, dirs.homeDir, dirs.tempDir} -} diff --git a/client_configuration_test.go b/client_configuration_test.go index 8da3be3ec..9453d0da8 100644 --- a/client_configuration_test.go +++ b/client_configuration_test.go @@ -9,6 +9,124 @@ import ( "testing" ) +func TestFindConfigFileFromConnectionParameters(t *testing.T) { + dirs := createTestDirectories(t) + connParameterConfigPath := createFile(t, "conn_parameters_config.json", "random content", dirs.dir) + envConfigPath := createFile(t, "env_var_config.json", "random content", dirs.dir) + t.Setenv(clientConfEnvName, envConfigPath) + createFile(t, defaultConfigName, "random content", dirs.driverDir) + createFile(t, defaultConfigName, "random content", dirs.homeDir) + createFile(t, defaultConfigName, "random content", dirs.tempDir) + + clientConfigFilePath, err := findClientConfigFilePath(connParameterConfigPath, predefinedTestDirs(dirs)) + + assertNilF(t, err, "get client config error") + assertEqualE(t, clientConfigFilePath, connParameterConfigPath, "config file path") +} + +func TestFindConfigFileFromEnvVariable(t *testing.T) { + dirs := createTestDirectories(t) + envConfigPath := createFile(t, "env_var_config.json", "random content", dirs.dir) + t.Setenv(clientConfEnvName, envConfigPath) + createFile(t, defaultConfigName, "random content", dirs.driverDir) + createFile(t, defaultConfigName, "random content", dirs.homeDir) + createFile(t, defaultConfigName, "random content", dirs.tempDir) + + clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) + + assertNilF(t, err, "get client config error") + assertEqualE(t, clientConfigFilePath, envConfigPath, "config file path") +} + +func TestFindConfigFileFromDriverDirectory(t *testing.T) { + dirs := createTestDirectories(t) + driverConfigPath := createFile(t, defaultConfigName, "random content", dirs.driverDir) + createFile(t, defaultConfigName, "random content", dirs.homeDir) + createFile(t, defaultConfigName, "random content", dirs.tempDir) + + clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) + + assertNilF(t, err, "get client config error") + assertEqualE(t, clientConfigFilePath, driverConfigPath, "config file path") +} + +func TestFindConfigFileFromHomeDirectory(t *testing.T) { + dirs := createTestDirectories(t) + createFile(t, "wrong_file_name.json", "random content", dirs.driverDir) + homeConfigPath := createFile(t, defaultConfigName, "random content", dirs.homeDir) + createFile(t, defaultConfigName, "random content", dirs.tempDir) + + clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) + + assertNilF(t, err, "get client config error") + assertEqualE(t, clientConfigFilePath, homeConfigPath, "config file path") +} + +func TestFindConfigFileFromTempDirectory(t *testing.T) { + dirs := createTestDirectories(t) + createFile(t, "wrong_file_name.json", "random content", dirs.driverDir) + createFile(t, "wrong_file_name.json", "random content", dirs.homeDir) + tempConfigPath := createFile(t, defaultConfigName, "random content", dirs.tempDir) + + clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) + + assertNilF(t, err, "get client config error") + assertEqualE(t, clientConfigFilePath, tempConfigPath, "config file path") +} + +func TestNotFindConfigFileWhenNotDefined(t *testing.T) { + dirs := createTestDirectories(t) + createFile(t, "wrong_file_name.json", "random content", dirs.driverDir) + createFile(t, "wrong_file_name.json", "random content", dirs.homeDir) + createFile(t, "wrong_file_name.json", "random content", dirs.tempDir) + + clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs)) + + assertNilF(t, err, "get client config error") + assertEqualE(t, clientConfigFilePath, "", "config file path") +} + +func TestCreatePredefinedDirs(t *testing.T) { + homeDir, err := os.UserHomeDir() + assertNilF(t, err, "get home dir error") + var locations []string + + locations, err = clientConfigPredefinedDirs() + + assertNilF(t, err, "error") + assertEqualF(t, len(locations), 3, "size") + assertEqualE(t, locations[0], ".", "driver directory") + assertEqualE(t, locations[1], homeDir, "home directory") + assertEqualE(t, locations[2], os.TempDir(), "temp directory") +} + +func TestGetClientConfig(t *testing.T) { + dir := t.TempDir() + fileName := "config.json" + configContents := `{ + "common": { + "log_level" : "INFO", + "log_path" : "/some-path/some-directory" + } + }` + createFile(t, fileName, configContents, dir) + filePath := path.Join(dir, fileName) + + clientConfigFilePath, err := getClientConfig(filePath) + + assertNilF(t, err) + assertNotNilF(t, clientConfigFilePath) + assertEqualE(t, clientConfigFilePath.Common.LogLevel, "INFO", "log level") + assertEqualE(t, clientConfigFilePath.Common.LogPath, "/some-path/some-directory", "log path") +} + +func TestNoResultForGetClientConfigWhenNoFileFound(t *testing.T) { + clientConfigFilePath, err := getClientConfig("") + + assertNilF(t, err) + assertNilF(t, clientConfigFilePath) +} + func TestParseConfiguration(t *testing.T) { dir := t.TempDir() testCases := []struct { @@ -154,3 +272,41 @@ func createFile(t *testing.T, fileName string, fileContents string, directory st assertNilF(t, err, "create file error") return fullFileName } + +func createTestDirectories(t *testing.T) struct { + dir string + driverDir string + homeDir string + tempDir string +} { + dir := t.TempDir() + driverDir := path.Join(dir, "driver") // we pretend "." to be a folder inside t.TempDir() not to interfere with user's real directories + err := os.Mkdir(driverDir, 0755) + assertNilF(t, err, "make driver dir error") + homeDir := path.Join(dir, "home") // we pretend home directory to be a folder inside t.TempDir() not to interfere with user's real directories + err = os.Mkdir(homeDir, 0755) + assertNilF(t, err, "make home dir error") + tempDir := path.Join(dir, "temp") // we pretend temp directory to be a folder inside t.TempDir() not to interfere with user's real directories + err = os.Mkdir(tempDir, 0755) + assertNilF(t, err, "make temp dir error") + return struct { + dir string + driverDir string + homeDir string + tempDir string + }{ + dir: dir, + driverDir: driverDir, + homeDir: homeDir, + tempDir: tempDir, + } +} + +func predefinedTestDirs(dirs struct { + dir string + driverDir string + homeDir string + tempDir string +}) []string { + return []string{dirs.driverDir, dirs.homeDir, dirs.tempDir} +}