go-micro.dev/v5@v5.12.0/config/default_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "runtime" 8 "strings" 9 "testing" 10 "time" 11 12 "go-micro.dev/v5/config/source" 13 "go-micro.dev/v5/config/source/env" 14 "go-micro.dev/v5/config/source/file" 15 "go-micro.dev/v5/config/source/memory" 16 ) 17 18 func createFileForIssue18(t *testing.T, content string) *os.File { 19 data := []byte(content) 20 path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano())) 21 fh, err := os.Create(path) 22 if err != nil { 23 t.Error(err) 24 } 25 _, err = fh.Write(data) 26 if err != nil { 27 t.Error(err) 28 } 29 30 return fh 31 } 32 33 func createFileForTest(t *testing.T) *os.File { 34 data := []byte(`{"foo": "bar"}`) 35 path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano())) 36 fh, err := os.Create(path) 37 if err != nil { 38 t.Error(err) 39 } 40 _, err = fh.Write(data) 41 if err != nil { 42 t.Error(err) 43 } 44 45 return fh 46 } 47 48 func TestConfigLoadWithGoodFile(t *testing.T) { 49 fh := createFileForTest(t) 50 path := fh.Name() 51 defer func() { 52 fh.Close() 53 os.Remove(path) 54 }() 55 56 // Create new config 57 conf, err := NewConfig() 58 if err != nil { 59 t.Fatalf("Expected no error but got %v", err) 60 } 61 // Load file source 62 if err := conf.Load(file.NewSource( 63 file.WithPath(path), 64 )); err != nil { 65 t.Fatalf("Expected no error but got %v", err) 66 } 67 } 68 69 func TestConfigLoadWithInvalidFile(t *testing.T) { 70 fh := createFileForTest(t) 71 path := fh.Name() 72 defer func() { 73 fh.Close() 74 os.Remove(path) 75 }() 76 77 // Create new config 78 conf, err := NewConfig() 79 if err != nil { 80 t.Fatalf("Expected no error but got %v", err) 81 } 82 // Load file source 83 err = conf.Load(file.NewSource( 84 file.WithPath(path), 85 file.WithPath("/i/do/not/exists.json"), 86 )) 87 88 if err == nil { 89 t.Fatal("Expected error but none !") 90 } 91 if !strings.Contains(fmt.Sprintf("%v", err), "/i/do/not/exists.json") { 92 t.Fatalf("Expected error to contain the unexisting file but got %v", err) 93 } 94 } 95 96 func TestConfigMerge(t *testing.T) { 97 fh := createFileForIssue18(t, `{ 98 "amqp": { 99 "host": "rabbit.platform", 100 "port": 80 101 }, 102 "handler": { 103 "exchange": "springCloudBus" 104 } 105 }`) 106 path := fh.Name() 107 defer func() { 108 fh.Close() 109 os.Remove(path) 110 }() 111 os.Setenv("AMQP_HOST", "rabbit.testing.com") 112 113 conf, err := NewConfig() 114 if err != nil { 115 t.Fatalf("Expected no error but got %v", err) 116 } 117 if err := conf.Load( 118 file.NewSource( 119 file.WithPath(path), 120 ), 121 env.NewSource(), 122 ); err != nil { 123 t.Fatalf("Expected no error but got %v", err) 124 } 125 126 actualHost, err := conf.Get("amqp", "host") 127 if err != nil { 128 t.Fatal(err) 129 } 130 host := actualHost.String("backup") 131 if host != "rabbit.testing.com" { 132 t.Fatalf("Expected %v but got %v", 133 "rabbit.testing.com", 134 host) 135 } 136 } 137 138 func equalS(t *testing.T, actual, expect string) { 139 if actual != expect { 140 t.Errorf("Expected %s but got %s", actual, expect) 141 } 142 } 143 144 func TestConfigWatcherDirtyOverrite(t *testing.T) { 145 n := runtime.GOMAXPROCS(0) 146 defer runtime.GOMAXPROCS(n) 147 148 runtime.GOMAXPROCS(1) 149 150 l := 100 151 152 ss := make([]source.Source, l, l) 153 154 for i := 0; i < l; i++ { 155 ss[i] = memory.NewSource(memory.WithJSON([]byte(fmt.Sprintf(`{"key%d": "val%d"}`, i, i)))) 156 } 157 158 conf, _ := NewConfig() 159 160 for _, s := range ss { 161 _ = conf.Load(s) 162 } 163 runtime.Gosched() 164 165 for i := range ss { 166 k := fmt.Sprintf("key%d", i) 167 v := fmt.Sprintf("val%d", i) 168 cc, err := conf.Get(k) 169 if err != nil { 170 t.Fatal(err) 171 } 172 equalS(t, cc.String(""), v) 173 } 174 }