github.com/vicanso/pike@v1.0.1-0.20210630235453-9099e041f6ec/config/config_test.go (about) 1 // MIT License 2 3 // Copyright (c) 2020 Tree Xie 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package config 24 25 import ( 26 "math/rand" 27 "os" 28 "strconv" 29 "testing" 30 31 "github.com/stretchr/testify/assert" 32 ) 33 34 func TestValidate(t *testing.T) { 35 assert := assert.New(t) 36 37 // location配置的upstream不存在 38 c := &PikeConfig{ 39 Locations: []LocationConfig{ 40 { 41 Name: "location-test", 42 Upstream: "upstream-test", 43 }, 44 }, 45 } 46 err := c.Validate() 47 assert.Equal(ErrUpstreamNotFound, err) 48 49 c = &PikeConfig{ 50 Servers: []ServerConfig{ 51 { 52 Addr: ":3015", 53 Locations: []string{ 54 "location-test", 55 }, 56 Cache: "cache-test", 57 }, 58 }, 59 } 60 err = c.Validate() 61 assert.Equal(ErrLocationNotFound, err) 62 63 c = &PikeConfig{ 64 Upstreams: []UpstreamConfig{ 65 { 66 Name: "upstream-test", 67 Servers: []UpstreamServerConfig{ 68 { 69 Addr: "http://127.0.0.1:3015", 70 }, 71 }, 72 }, 73 }, 74 Locations: []LocationConfig{ 75 { 76 Name: "location-test", 77 Upstream: "upstream-test", 78 }, 79 }, 80 Servers: []ServerConfig{ 81 { 82 Addr: ":3015", 83 Locations: []string{ 84 "location-test", 85 }, 86 Cache: "cache-test", 87 }, 88 }, 89 } 90 err = c.Validate() 91 assert.Equal(ErrCacheNotFound, err) 92 93 c = &PikeConfig{ 94 Caches: []CacheConfig{ 95 { 96 Name: "cache-test", 97 Size: 100, 98 HitForPass: "1m", 99 }, 100 }, 101 Upstreams: []UpstreamConfig{ 102 { 103 Name: "upstream-test", 104 Servers: []UpstreamServerConfig{ 105 { 106 Addr: "http://127.0.0.1:3015", 107 }, 108 }, 109 }, 110 }, 111 Locations: []LocationConfig{ 112 { 113 Name: "location-test", 114 Upstream: "upstream-test", 115 }, 116 }, 117 Servers: []ServerConfig{ 118 { 119 Addr: ":3015", 120 Locations: []string{ 121 "location-test", 122 }, 123 Cache: "cache-test", 124 Compress: "compress-test", 125 }, 126 }, 127 } 128 err = c.Validate() 129 assert.Equal(ErrCompressNotFound, err) 130 131 c = &PikeConfig{ 132 Caches: []CacheConfig{ 133 { 134 Name: "cache-test", 135 Size: 100, 136 HitForPass: "1m", 137 }, 138 }, 139 Compresses: []CompressConfig{ 140 { 141 Name: "compress-test", 142 }, 143 }, 144 Upstreams: []UpstreamConfig{ 145 { 146 Name: "upstream-test", 147 HealthCheck: "/ping", 148 Policy: "first", 149 Servers: []UpstreamServerConfig{ 150 { 151 Addr: "http://127.0.0.1:3015", 152 }, 153 }, 154 }, 155 }, 156 Locations: []LocationConfig{ 157 { 158 Name: "location-test", 159 Upstream: "upstream-test", 160 Prefixes: []string{ 161 "/api", 162 }, 163 Rewrites: []string{ 164 "/api/:/$1", 165 }, 166 QueryStrings: []string{ 167 "id:1", 168 }, 169 Hosts: []string{ 170 "test.com", 171 }, 172 RespHeaders: []string{ 173 "X-Resp-Id:1", 174 }, 175 ReqHeaders: []string{ 176 "X-Req-Id:2", 177 }, 178 }, 179 }, 180 Servers: []ServerConfig{ 181 { 182 Addr: ":3015", 183 Locations: []string{ 184 "location-test", 185 }, 186 Cache: "cache-test", 187 Compress: "compress-test", 188 CompressMinLength: "1kb", 189 CompressContentTypeFilter: "text|json", 190 }, 191 }, 192 } 193 err = c.Validate() 194 assert.Nil(err) 195 } 196 197 func TestInitDefaultClient(t *testing.T) { 198 assert := assert.New(t) 199 200 file := strconv.Itoa(int(rand.Int31())) 201 202 err := InitDefaultClient("etcd://127.0.0.1:2379/" + file) 203 assert.Nil(err) 204 205 err = InitDefaultClient(os.TempDir() + "/" + file) 206 assert.Nil(err) 207 err = Close() 208 assert.Nil(err) 209 } 210 211 func TestReadWriteConfig(t *testing.T) { 212 assert := assert.New(t) 213 214 file := strconv.Itoa(int(rand.Int31())) 215 err := InitDefaultClient(os.TempDir() + "/" + file) 216 assert.Nil(err) 217 218 c := &PikeConfig{ 219 Compresses: []CompressConfig{ 220 { 221 Name: "compress-test", 222 }, 223 }, 224 } 225 err = Write(c) 226 assert.Nil(err) 227 228 currentConfig, err := Read() 229 assert.Nil(err) 230 assert.Equal(c.Compresses[0].Name, currentConfig.Compresses[0].Name) 231 }