Skip to content

Commit

Permalink
Merge pull request philchia#43 from gocommon/master
Browse files Browse the repository at this point in the history
*auth create cache dir
  • Loading branch information
philchia authored Jun 27, 2019
2 parents d916611 + 92d21f2 commit bca9725
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
28 changes: 28 additions & 0 deletions agollo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agollo
import (
"log"
"os"
"path"
"testing"
"time"

Expand Down Expand Up @@ -33,28 +34,46 @@ func teardown() {
func TestAgolloStart(t *testing.T) {
if err := Start(); err == nil {
t.Errorf("Start with default app.properties should return err, got :%v", err)
return
}

if err := StartWithConfFile("fake.properties"); err == nil {
t.Errorf("Start with fake.properties should return err, got :%v", err)
return
}

if err := StartWithConfFile("./testdata/app.properties"); err != nil {
t.Errorf("Start with app.properties should return nil, got :%v", err)
return
}

f, err := os.Stat(path.Dir(defaultClient.getDumpFileName()))
if err != nil {
t.Errorf("dump file dir should exists, got err:%v", err)
return
}

if !f.IsDir() {
t.Errorf("dump file dir should be a dir, got file")
return
}

if err := Stop(); err != nil {
t.Errorf("Stop should return nil, got :%v", err)
return
}
os.Remove(defaultClient.getDumpFileName())

if err := StartWithConfFile("./testdata/app.properties"); err != nil {
t.Errorf("Start with app.properties should return nil, got :%v", err)
return
}
defer Stop()
defer os.Remove(defaultClient.getDumpFileName())

if err := defaultClient.loadLocal(defaultClient.getDumpFileName()); err != nil {
t.Errorf("loadLocal should return nil, got: %v", err)
return
}

mockserver.Set("application", "key", "value")
Expand All @@ -68,11 +87,13 @@ func TestAgolloStart(t *testing.T) {
val := GetStringValue("key", "defaultValue")
if val != "value" {
t.Errorf("GetStringValue of key should = value, got %v", val)
return
}

keys := GetAllKeys("application")
if len(keys) != 1 {
t.Errorf("GetAllKeys should return 1 key")
return
}

mockserver.Set("application", "key", "newvalue")
Expand All @@ -84,11 +105,13 @@ func TestAgolloStart(t *testing.T) {
val = defaultClient.GetStringValue("key", "defaultValue")
if val != "newvalue" {
t.Errorf("GetStringValue of key should = newvalue, got %v", val)
return
}

keys = GetAllKeys("application")
if len(keys) != 1 {
t.Errorf("GetAllKeys should return 1 key")
return
}

mockserver.Delete("application", "key")
Expand All @@ -100,11 +123,13 @@ func TestAgolloStart(t *testing.T) {
val = GetStringValue("key", "defaultValue")
if val != "defaultValue" {
t.Errorf("GetStringValue of key should = defaultValue, got %v", val)
return
}

keys = GetAllKeys("application")
if len(keys) != 0 {
t.Errorf("GetAllKeys should return 0 key")
return
}

mockserver.Set("client.json", "content", `{"name":"agollo"}`)
Expand All @@ -116,10 +141,12 @@ func TestAgolloStart(t *testing.T) {
val = GetNameSpaceContent("client.json", "{}")
if val != `{"name":"agollo"}` {
t.Errorf(`GetStringValue of client.json content should = {"name":"agollo"}, got %v`, val)
return
}

if err := SubscribeToNamespaces("new_namespace.json"); err != nil {
t.Error(err)
return
}

mockserver.Set("new_namespace.json", "key", "1")
Expand All @@ -131,5 +158,6 @@ func TestAgolloStart(t *testing.T) {
val = GetStringValueWithNameSpace("new_namespace.json", "key", "defaultValue")
if val != `1` {
t.Errorf(`GetStringValueWithNameSpace of new_namespace.json content should = 1, got %v`, val)
return
}
}
24 changes: 24 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
"os"
"path"
)

Expand Down Expand Up @@ -52,6 +53,11 @@ func NewClient(conf *Conf) *Client {
// Start sync config
func (c *Client) Start() error {

// check cache dir
if err := c.autoCreateCacheDir(); err != nil {
return err
}

// preload all config to local first
if err := c.preload(); err != nil {
return err
Expand Down Expand Up @@ -235,3 +241,21 @@ func (c *Client) GetReleaseKey(namespace string) string {
func (c *Client) setReleaseKey(namespace, releaseKey string) {
c.releaseKeyRepo.set(namespace, releaseKey)
}

// autoCreateCacheDir autoCreateCacheDir
func (c *Client) autoCreateCacheDir() error {
fs, err := os.Stat(c.conf.CacheDir)
if err != nil {
if os.IsNotExist(err) {
return os.MkdirAll(c.conf.CacheDir, os.ModePerm)
}

return err
}

if !fs.IsDir() {
return fmt.Errorf("conf.CacheDir is not a dir")
}

return nil
}
2 changes: 1 addition & 1 deletion testdata/app.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"appId":"SampleApp",
"cluster":"default",
"namespaceNames":["application", "notexist", "client.json"],
"cacheDir": "/tmp",
"cacheDir": "/tmp/agollo",
"ip":"localhost:8080"
}

0 comments on commit bca9725

Please sign in to comment.