1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

Add test case for load settings

This commit is contained in:
Timothy 2016-05-26 13:02:27 +08:00
parent 78f33ffed7
commit 24d26c94be
3 changed files with 34 additions and 8 deletions

View File

@ -2,7 +2,9 @@ package main
import ( import (
"flag" "flag"
"fmt"
"log" "log"
"os"
"runtime/debug" "runtime/debug"
"strings" "strings"
"time" "time"
@ -32,7 +34,15 @@ func main() {
return return
} }
Configuration = LoadSettings(*optConf) var err error
Configuration, err = LoadSettings(*optConf)
if err != nil {
fmt.Println(err.Error())
log.Println(err.Error())
os.Exit(1)
}
dns_loop() dns_loop()
} }

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
) )
type Settings struct { type Settings struct {
@ -21,19 +20,19 @@ type Settings struct {
Group int Group int
} }
func LoadSettings(config_path string) Settings { func LoadSettings(config_path string) (Settings, error) {
setting := Settings{}
file, err := ioutil.ReadFile(config_path) file, err := ioutil.ReadFile(config_path)
if err != nil { if err != nil {
fmt.Println("Error occurs while reading config file, please make sure config file exists!") fmt.Println("Error occurs while reading config file, please make sure config file exists!")
os.Exit(1) return setting, err
} }
var setting Settings
err = json.Unmarshal(file, &setting) err = json.Unmarshal(file, &setting)
if err != nil { if err != nil {
fmt.Println("Error occurs while unmarshal config file, please make sure config file correct!") fmt.Println("Error occurs while unmarshal config file, please make sure config file correct!")
os.Exit(1) return setting, err
} }
return setting return setting, nil
} }

17
settings_test.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"testing"
)
func TestLoadSetting(t *testing.T) {
settings, err := LoadSettings("./config_sample.json")
if err != nil {
t.Error(err.Error())
}
if settings.IP_Url == "" {
t.Error("Cannot load ip_url from config file")
}
}