-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add GetConfiguration using yaml w/ test
- Loading branch information
Showing
15 changed files
with
315 additions
and
53 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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package common | ||
|
||
import ( | ||
"os/user" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func RelativeToAbsolutePath(rpath string) (string, error) { | ||
if rpath == "" { | ||
return rpath, nil | ||
} | ||
|
||
absolutePath := "" | ||
|
||
// case ./ ../ | ||
if strings.Contains(rpath, "./") { | ||
abs, err := filepath.Abs(rpath) | ||
if err != nil { | ||
return rpath, err | ||
} | ||
return abs, nil | ||
} | ||
|
||
// case ~/ | ||
if strings.Contains(rpath, "~") { | ||
i := strings.Index(rpath, "~") // 처음 나온 ~만 반환 | ||
|
||
if i > -1 { | ||
pathRemain := rpath[i+1:] | ||
usr, err := user.Current() | ||
if err != nil { | ||
return rpath, err | ||
} | ||
return path.Join(usr.HomeDir, pathRemain), nil | ||
|
||
} else { | ||
return rpath, nil | ||
} | ||
} | ||
|
||
if string(rpath[0]) == "/" { | ||
return rpath, nil | ||
} | ||
|
||
if string(rpath[0]) != "." && string(rpath[0]) != "/" { | ||
currentPath, err := filepath.Abs(".") | ||
if err != nil { | ||
return rpath, err | ||
} | ||
|
||
return path.Join(currentPath, rpath), nil | ||
} | ||
|
||
return absolutePath, nil | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package common_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"os/user" | ||
"path" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/DE-labtory/swim/cmd/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRelativeToAbsolutePath(t *testing.T) { | ||
|
||
testfile1 := "./util.go" | ||
testabsresult1, err := filepath.Abs(testfile1) | ||
assert.NoError(t, err) | ||
testabs1, err := common.RelativeToAbsolutePath(testfile1) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, testabs1, testabsresult1) | ||
|
||
testfile2 := "../README.md" | ||
testabsresult2, err := filepath.Abs(testfile2) | ||
assert.NoError(t, err) | ||
|
||
testabs2, err := common.RelativeToAbsolutePath(testfile2) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, testabs2, testabsresult2) | ||
|
||
// 남의 홈패스에 뭐가있는지 알길이 없으니 하나 만들었다 지움 | ||
usr, err := user.Current() | ||
assert.NoError(t, err) | ||
|
||
testfile3 := usr.HomeDir + "/test.txt" | ||
|
||
_, err = os.Stat(usr.HomeDir) | ||
if os.IsNotExist(err) { | ||
file, err := os.Create(testfile3) | ||
assert.NoError(t, err) | ||
defer file.Close() | ||
} | ||
|
||
err = ioutil.WriteFile(testfile3, []byte("test"), os.ModePerm) | ||
assert.NoError(t, err) | ||
|
||
testfile4 := "~/test.txt" | ||
|
||
testabs3, err := common.RelativeToAbsolutePath(testfile4) | ||
assert.NoError(t, err) | ||
assert.Equal(t, testfile3, testabs3) | ||
|
||
err = os.Remove(testfile3) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestRelativeToAbsolutePath_WhenGivenPathIsAbsolute(t *testing.T) { | ||
sshPath := "/iAmRoot" | ||
|
||
absPath, err := common.RelativeToAbsolutePath(sshPath) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, sshPath, absPath) | ||
} | ||
|
||
func TestRelativeToAbsolutePath_WhenGivenPathWithOnlyName(t *testing.T) { | ||
sshPath := "test-dir" | ||
|
||
absPath, err := common.RelativeToAbsolutePath(sshPath) | ||
currentPath, _ := filepath.Abs(".") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, path.Join(currentPath, sshPath), absPath) | ||
} | ||
|
||
func TestRelativeToAbsolutePath_WhenGivenPathIsEmpty(t *testing.T) { | ||
sshPath := "" | ||
|
||
absPath, err := common.RelativeToAbsolutePath(sshPath) | ||
|
||
assert.Equal(t, nil, err) | ||
assert.Equal(t, "", absPath) | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package conf | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sync" | ||
|
||
"github.com/DE-labtory/swim" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
var confPath = os.Getenv("GOPATH") + "/src/github.com/DE-labtory/swim/conf/config.yaml" | ||
|
||
type Configuration struct { | ||
SWIMConfig swim.Config | ||
SuspicionConfig swim.SuspicionConfig | ||
MessageEndpointConfig swim.MessageEndpointConfig | ||
Member swim.Member | ||
} | ||
|
||
// config is instance of SWIM configuration | ||
var config = &Configuration{} | ||
|
||
var once = sync.Once{} | ||
|
||
func SetConfigPath(abspath string) { | ||
confPath = abspath | ||
} | ||
|
||
func GetConfiguration() *Configuration { | ||
once.Do(func() { | ||
viper.SetConfigFile(confPath) | ||
if err := viper.ReadInConfig(); err != nil { | ||
panic(fmt.Sprintf("cannot read config from %s", confPath)) | ||
} | ||
err := viper.Unmarshal(&config) | ||
if err != nil { | ||
panic("error in read config") | ||
} | ||
}) | ||
|
||
return config | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package conf_test | ||
|
||
import ( | ||
"github.com/DE-labtory/swim" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/DE-labtory/swim/conf" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetConfiguration(t *testing.T) { | ||
path := os.Getenv("GOPATH") + "/src/github.com/DE-labtory/swim/conf" | ||
confFileName := "/config-test.yaml" | ||
defer os.Remove(path + confFileName) | ||
|
||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
os.Mkdir(path, os.ModePerm) | ||
} | ||
|
||
// please leave this whitespace as space not tab | ||
ConfJson := []byte(` | ||
swimconfig: | ||
maxlocalcount: 2 | ||
maxnsacounter: 8 | ||
t: 3000 | ||
acktimeout: 3000 | ||
k: 2 | ||
bindaddress: 127.0.0.1 | ||
bindport: 50000 | ||
suspicionconfig: | ||
k: 2 | ||
min: 3000 | ||
max: 10000 | ||
messageendpointconfig: | ||
encryptionenabled: false | ||
sendtimeout: 5000 | ||
callbackcollectinterval: 7000 | ||
member: | ||
id: | ||
id: "hello_id" | ||
`) | ||
|
||
err := ioutil.WriteFile(path+confFileName, ConfJson, os.ModePerm) | ||
assert.NoError(t, err) | ||
|
||
conf.SetConfigPath(path + confFileName) | ||
config := conf.GetConfiguration() | ||
assert.Equal(t, config.SWIMConfig.MaxlocalCount, 2) | ||
assert.Equal(t, config.SWIMConfig.MaxNsaCounter, 8) | ||
assert.Equal(t, config.SWIMConfig.T, 3000) | ||
assert.Equal(t, config.SWIMConfig.AckTimeOut, 3000) | ||
assert.Equal(t, config.SWIMConfig.K, 2) | ||
assert.Equal(t, config.SWIMConfig.BindAddress, "127.0.0.1") | ||
assert.Equal(t, config.SWIMConfig.BindPort, 50000) | ||
|
||
assert.Equal(t, config.SuspicionConfig.K, 2) | ||
assert.Equal(t, config.SuspicionConfig.Min.Nanoseconds(), int64(3000)) | ||
assert.Equal(t, config.SuspicionConfig.Max.Nanoseconds(), int64(10000)) | ||
|
||
assert.Equal(t, config.MessageEndpointConfig.EncryptionEnabled, false) | ||
assert.Equal(t, config.MessageEndpointConfig.SendTimeout.Nanoseconds(), int64(5000)) | ||
assert.Equal(t, config.MessageEndpointConfig.CallbackCollectInterval.Nanoseconds(), int64(7000)) | ||
|
||
assert.Equal(t, config.Member.ID, swim.MemberID{ID: "hello_id"}) | ||
} |
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
Oops, something went wrong.