Skip to content

Commit

Permalink
add: server config testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Sep 13, 2023
1 parent ef96406 commit 04d5ae5
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 3 deletions.
2 changes: 1 addition & 1 deletion clog/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func SetPath(path string) {
// 如果已经存在了就直接追加,不存在就创建
file, err := os.OpenFile(path, caw, permissions)
if err != nil {
Error(err)
Failed(err)
}
// 正常模式的日志记录需要输出到控制台和日志文件中
NewColorLogger(io.MultiWriter(os.Stdout, file), "["+processName+":C] ", log.Ldate|log.Ltime)
Expand Down
13 changes: 12 additions & 1 deletion conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conf
import (
"encoding/json"
"io/fs"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -125,8 +126,18 @@ func (opt *ServerConfig) Saved() error {
return err
}

path := filepath.Join(opt.Path, Dirs[0], defaultFileName+"."+cfSuffix)

// 创建 config.yaml 文件
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, opt.Permissions)
if err != nil {
return err
}

defer file.Close()

// 将配置对象写入 YAML 文件
return v.WriteConfigAs(filepath.Join(opt.Path, Dirs[0], defaultFileName+"."+cfSuffix))
return v.WriteConfigAs(path)
}

func (opt *ServerConfig) Unmarshal(data []byte) error {
Expand Down
134 changes: 133 additions & 1 deletion conf/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,139 @@
package conf

import "testing"
import (
"os"
"path/filepath"
"reflect"
"testing"
)

func TestConfigLoad(t *testing.T) {
// 创建一个临时目录用于测试
tmpDir := t.TempDir()

// 设置 Settings.Path 为临时目录
Settings.Path = tmpDir

// 创建一个配置文件并写入测试数据
configFile := filepath.Join(tmpDir, "test-config.yaml")
testConfigData := []byte(`
vasedb:
port: 8080
path: "/test/path"
debug: true
`)

err := os.WriteFile(configFile, testConfigData, 0644)
if err != nil {
t.Fatalf("Error writing test config file: %v", err)
}

// 调用 Load 函数
loadedConfig := new(ServerConfig)
err = Load(configFile, loadedConfig)
if err != nil {
t.Fatalf("Error loading config: %v", err)
}

// 检查加载的配置是否正确
expectedConfig := &ServerConfig{
VaseDB: VaseDB{
Port: 8080,
Path: "/test/path",
Debug: true,
},
}

// 检查比较是否一致
if !reflect.DeepEqual(loadedConfig, expectedConfig) {
t.Errorf("Loaded config is not as expected.\nGot: %+v\nExpected: %+v", loadedConfig, expectedConfig)
}
}

func TestReloadConfig(t *testing.T) {

// 创建一个临时目录用于测试
tmpDir := t.TempDir()

// 设置 Settings.Path 为临时目录
Settings.Path = tmpDir

// 创建一个配置文件并写入测试数据
configFile := filepath.Join(tmpDir, "etc", "config.yaml")
// 模拟文件中数据
configData := []byte(`
{
"vasedb": {
"port": 8080,
"path": "/test/path",
"debug": true
}
}
`)

// 设置文件系统权限
perm := os.FileMode(0755)

err := os.MkdirAll(filepath.Dir(configFile), perm)
if err != nil {
t.Fatalf("Error creating test directory: %v", err)
}
err = os.WriteFile(configFile, configData, perm)
if err != nil {
t.Fatalf("Error writing test config file: %v", err)
}

// 调用 ReloadConfig 函数
reloadedConfig, err := ReloadConfig()
if err != nil {
t.Fatalf("Error reloading config: %v", err)
}

// 检查重新加载的配置是否正确
expectedConfig := &ServerConfig{
VaseDB: VaseDB{
Port: 8080,
Path: "/test/path",
Debug: true,
},
}

// 采用深度比较是否一致
if !reflect.DeepEqual(reloadedConfig, expectedConfig) {
t.Errorf("Reloaded config is not as expected.\nGot: %+v\nExpected: %+v", reloadedConfig, expectedConfig)
}
}

func TestSavedConfig(t *testing.T) {

// 创建一个 ServerConfig 实例
configToSave := &ServerConfig{
VaseDB: VaseDB{
Port: 8080,
Path: "./",
Debug: true,
},
}

// 调用 Saved 函数
err := configToSave.Saved()
if err != nil {
t.Fatalf("Error saving config: %v", err)
}
}

func TestMain(m *testing.M) {
// 执行一些初始化操作
dir := "./etc"

os.Mkdir(dir, 0755)

// 运行测试,并获取返回的退出代码
exitCode := m.Run()

// 执行一些清理操作
os.RemoveAll(dir)

// 退出测试程序
os.Exit(exitCode)
}

0 comments on commit 04d5ae5

Please sign in to comment.