-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: test cases of new config management
- Loading branch information
1 parent
38d3e5a
commit cd1bfc6
Showing
3 changed files
with
370 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,98 @@ | ||
package commands | ||
|
||
import "testing" | ||
|
||
// import ( | ||
// "os" | ||
// "path/filepath" | ||
// "testing" | ||
|
||
// "github.com/dicedb/dice/config" | ||
// ) | ||
|
||
// // scenario 1: Create a config file if the directory is provided (-o flag) | ||
// func TestSetupConfig_CreateAndLoadDefault(t *testing.T) { | ||
// config.ResetConfig() | ||
// tempDir := t.TempDir() | ||
|
||
// // Simulate the flag: -o=<dir_path> | ||
// config.CustomConfigFilePath = tempDir | ||
// config.SetupConfig() | ||
|
||
// if config.DiceConfig.AsyncServer.Addr != config.DefaultHost { | ||
// t.Fatalf("Expected server addr to be '%s', got '%s'", config.DefaultHost, config.DiceConfig.AsyncServer.Addr) | ||
// } | ||
// if config.DiceConfig.AsyncServer.Port != config.DefaultPort { | ||
// t.Fatalf("Expected server port to be %d, got %d", config.DefaultPort, config.DiceConfig.AsyncServer.Port) | ||
// } | ||
// } | ||
|
||
// // scenario 2: Load default config if no config file or directory is provided | ||
// func TestSetupConfig_DefaultConfig(t *testing.T) { | ||
// // Simulate no flags being set (default config scenario) | ||
// config.ResetConfig() | ||
// config.CustomConfigFilePath = "" | ||
// config.FileLocation = filepath.Join(config.DefaultConfigFilePath, config.DefaultConfigName) | ||
|
||
// // Verify that the configuration was loaded from the default values | ||
// if config.DiceConfig.AsyncServer.Addr != config.DefaultHost { | ||
// t.Fatalf("Expected server addr to be '%s', got '%s'", config.DefaultHost, config.DiceConfig.AsyncServer.Addr) // 127.0.0.1 | ||
// } | ||
// if config.DiceConfig.AsyncServer.Port != config.DefaultPort { | ||
// t.Fatalf("Expected server port to be %d, got %d", 8739, config.DiceConfig.AsyncServer.Port) | ||
// } | ||
// } | ||
|
||
// // scenario 3: Config file is present but not well-structured (Malformed) | ||
// func TestSetupConfig_InvalidConfigFile(t *testing.T) { | ||
// config.DiceConfig = nil | ||
// tempDir := t.TempDir() | ||
// configFilePath := filepath.Join(tempDir, "dice.toml") | ||
|
||
// content := ` | ||
// [asyncserver] | ||
// addr = 127.0.0.1 // Missing quotes around string value | ||
// port = abc // Invalid integer | ||
// ` | ||
// if err := os.WriteFile(configFilePath, []byte(content), 0666); err != nil { | ||
// t.Fatalf("Failed to create invalid test config file: %v", err) | ||
// } | ||
|
||
// // Simulate the flag: -c=<configfile_path> | ||
// config.CustomConfigFilePath = "" | ||
// config.FileLocation = configFilePath | ||
|
||
// config.SetupConfig() | ||
|
||
// if config.DiceConfig.AsyncServer.Addr != config.DefaultHost { | ||
// t.Fatalf("Expected server addr to be '%s' after unmarshal error, got '%s'", config.DefaultHost, config.DiceConfig.AsyncServer.Addr) | ||
// } | ||
// if config.DiceConfig.AsyncServer.Port != config.DefaultPort { | ||
// t.Fatalf("Expected server port to be %d after unmarshal error, got %d", config.DefaultPort, config.DiceConfig.AsyncServer.Port) | ||
// } | ||
// } | ||
|
||
// // scenario 4: Config file is present with partial content | ||
// func TestSetupConfig_PartialConfigFile(t *testing.T) { | ||
// tempDir := t.TempDir() | ||
// configFilePath := filepath.Join(tempDir, "dice.toml") | ||
|
||
// content := ` | ||
// [asyncserver] | ||
// addr = "127.0.0.1" | ||
// ` | ||
// if err := os.WriteFile(configFilePath, []byte(content), 0666); err != nil { | ||
// t.Fatalf("Failed to create partial test config file: %v", err) | ||
// } | ||
|
||
// // Simulate the flag: -c=<configfile_path> | ||
// config.CustomConfigFilePath = "" | ||
// config.FileLocation = configFilePath | ||
|
||
// config.SetupConfig() | ||
|
||
// t.Log(config.DiceConfig.AsyncServer.Port) | ||
|
||
// if config.DiceConfig.AsyncServer.Addr != "127.0.0.1" { | ||
// t.Fatalf("Expected server addr to be '127.0.0.1', got '%s'", config.DiceConfig.AsyncServer.Addr) | ||
// } | ||
// if config.DiceConfig.AsyncServer.Port != config.DefaultPort { | ||
// t.Fatalf("Expected server port to be %d (default), got %d", config.DefaultPort, config.DiceConfig.AsyncServer.Port) | ||
// } | ||
// } | ||
|
||
// // scenario 5: Load config from the provided file path | ||
// func TestSetupConfig_LoadFromFile(t *testing.T) { | ||
// config.ResetConfig() | ||
// tempDir := t.TempDir() | ||
// configFilePath := filepath.Join(tempDir, "dice.toml") | ||
|
||
// content := ` | ||
// [asyncserver] | ||
// addr = "127.0.0.1" | ||
// port = 8739 | ||
// ` | ||
// if err := os.WriteFile(configFilePath, []byte(content), 0666); err != nil { | ||
// t.Fatalf("Failed to write test config file: %v", err) | ||
// } | ||
|
||
// // Simulate the flag: -c=<configfile_path> | ||
// config.CustomConfigFilePath = "" | ||
// config.FileLocation = configFilePath | ||
|
||
// config.SetupConfig() | ||
|
||
// if config.DiceConfig.AsyncServer.Addr != "127.0.0.1" { | ||
// t.Fatalf("Expected server addr to be '127.0.0.1', got '%s'", config.DiceConfig.AsyncServer.Addr) | ||
// } | ||
// if config.DiceConfig.AsyncServer.Port != 8739 { | ||
// t.Fatalf("Expected server port to be 8374, got %d", config.DiceConfig.AsyncServer.Port) | ||
// } | ||
|
||
// } | ||
|
||
func Test_demo(t *testing.T) { | ||
t.Log("todo implement new tests") | ||
package config_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/dicedb/dice/config" | ||
) | ||
|
||
const configFileName = "dicedb.conf" | ||
|
||
// TestCreateConfigFile_FileExists tests the scenario when config file already exists | ||
func TestCreateConfigFile_FileExists(t *testing.T) { | ||
tempDir := t.TempDir() | ||
configPath := filepath.Join(tempDir, configFileName) | ||
|
||
if err := os.WriteFile(configPath, []byte("test config"), 0644); err != nil { | ||
t.Fatalf("Failed to create test config file: %v", err) | ||
} | ||
|
||
config.CreateConfigFile(configPath) | ||
|
||
content, err := os.ReadFile(configPath) | ||
if err != nil { | ||
t.Fatalf("Failed to read config file: %v", err) | ||
} | ||
|
||
if string(content) != "test config" { | ||
t.Error("Config file content was modified when it should have been preserved") | ||
} | ||
} | ||
|
||
// TestCreateConfigFile_NewFile tests creating a new config file | ||
func TestCreateConfigFile_NewFile(t *testing.T) { | ||
tempDir := t.TempDir() | ||
configPath := filepath.Join(tempDir, configFileName) | ||
config.CreateConfigFile(configPath) | ||
|
||
if _, err := os.Stat(configPath); os.IsNotExist(err) { | ||
t.Error("Config file was not created") | ||
} | ||
|
||
content, err := os.ReadFile(configPath) | ||
if err != nil { | ||
t.Fatalf("Failed to read created config file: %v", err) | ||
} | ||
|
||
if len(content) == 0 { | ||
t.Error("Created config file is empty") | ||
} | ||
} | ||
|
||
// TestCreateConfigFile_InvalidPath tests creation with an invalid file path | ||
func TestCreateConfigFile_InvalidPath(t *testing.T) { | ||
configPath := "/nonexistent/directory/dicedb.conf" | ||
config.CreateConfigFile(configPath) | ||
|
||
if _, err := os.Stat(configPath); !os.IsNotExist(err) { | ||
t.Error("Config file should not have been created at invalid path") | ||
} | ||
} | ||
|
||
func TestCreateConfigFile_NoPermission(t *testing.T) { | ||
if os.Getuid() == 0 { | ||
t.Skip("Skipping test when running as root") | ||
} | ||
|
||
tempDir := t.TempDir() | ||
err := os.Chmod(tempDir, 0555) // read + execute only | ||
if err != nil { | ||
t.Fatalf("Failed to change directory permissions: %v", err) | ||
} | ||
defer os.Chmod(tempDir, 0755) // restore permissions | ||
|
||
configPath := filepath.Join(tempDir, configFileName) | ||
config.CreateConfigFile(configPath) | ||
|
||
if _, err := os.Stat(configPath); !os.IsNotExist(err) { | ||
t.Error("Config file should not have been created without permissions") | ||
} | ||
} | ||
|
||
// TestCreateConfigFile_ExistingDirectory tests creation in existing directory | ||
func TestCreateConfigFile_ExistingDirectory(t *testing.T) { | ||
tempDir := t.TempDir() | ||
configDir := filepath.Join(tempDir, "config") | ||
if err := os.MkdirAll(configDir, 0755); err != nil { | ||
t.Fatalf("Failed to create config directory: %v", err) | ||
} | ||
|
||
configPath := filepath.Join(configDir, configFileName) | ||
config.CreateConfigFile(configPath) | ||
|
||
if _, err := os.Stat(configPath); os.IsNotExist(err) { | ||
t.Error("Config file was not created in existing directory") | ||
} | ||
} |
Oops, something went wrong.