From 0d0d33aec90ca82eca542ee85e53586e68c1da8d Mon Sep 17 00:00:00 2001 From: Zenara Daley Date: Tue, 24 Apr 2018 12:10:57 -0400 Subject: [PATCH] add balancer unit tests --- internal/file/bindata.go | 4 +- rootfs/etc/nginx/lua/balancer.lua | 7 + rootfs/etc/nginx/lua/test/balancer_test.lua | 295 +++++++++++++++++++- 3 files changed, 303 insertions(+), 3 deletions(-) diff --git a/internal/file/bindata.go b/internal/file/bindata.go index fee9ece4a..2311ed953 100644 --- a/internal/file/bindata.go +++ b/internal/file/bindata.go @@ -98,7 +98,7 @@ func etcNginxLuaBalancerEwmaLua() (*asset, error) { return a, nil } -var _etcNginxLuaBalancerLua = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x58\x5f\x6f\x1b\xb9\x11\x7f\xd7\xa7\x18\xec\x93\x16\x95\x16\x71\xd1\x27\xa3\x2a\xe0\x24\xbe\x22\x88\x63\x17\x4a\xae\x87\xc3\x21\x20\xe8\xdd\x91\xc4\x13\x45\x6e\x49\xae\x65\xf7\x70\xf9\xec\x87\xe1\x92\xdc\xbf\xb6\x93\x17\x5b\x22\x87\xf3\x7f\x7e\x33\x23\xa9\x4b\x2e\x41\xed\x1f\xd9\x3d\x97\x5c\x95\x68\x60\x03\x06\xff\xd7\x08\x83\xcb\x4c\xed\x1f\x8b\x78\x9e\xe5\x8b\x96\xf8\x77\xab\x55\x9f\xa8\xa4\x83\x74\x5b\x6a\xb5\x13\xfb\xc6\x70\x27\x46\x64\xfd\x8b\x44\xde\x38\x21\xfb\x54\xf4\x3d\x5d\x4a\xd3\x94\xbc\x3c\x60\x9f\xc0\xa0\x75\x4f\x45\xbc\x49\xa4\xfe\x98\x49\x5d\x1e\x67\x88\x75\x79\x4c\x84\x78\x3e\xf1\x3e\x49\x34\xaf\xa0\x8b\x44\x65\x9d\x28\x8f\x4f\x7d\xba\xf6\x24\xcb\x17\x8b\xf5\x1a\x4e\xc8\x6d\x63\xb0\x02\xa1\xc0\x62\xa9\x55\x65\xe9\x78\xa7\x0d\x70\x05\xb7\x7b\xa1\x1e\xe1\xac\xcd\x11\x0d\x38\x0d\xb5\x28\x8f\xd0\xd4\xe0\x0e\x08\x0a\xcf\x20\x85\x75\xa0\x77\xd0\xd4\xd6\x19\xe4\x27\xa8\x11\x8d\x67\x20\x1c\x9c\x85\x94\xe0\xf8\x11\xe1\x9f\x44\x5f\xa1\xe4\x4f\xd0\x28\x72\x53\xa9\x95\x33\x5a\x4a\x34\xf0\x9f\xbb\xcf\x5f\xb0\xf2\x1c\xef\x79\x79\x44\x55\x81\xbe\xff\x1d\x4b\x47\xf2\xe8\xb4\xd5\x01\x55\x55\x6b\xa1\xdc\xbf\xe0\x6f\xf0\xf6\xea\xdd\xc7\xeb\xdb\xf7\x9f\xd9\xe7\x5f\x6f\xdf\xb1\x0f\xb7\x5f\xae\xb7\xff\xbd\xba\x09\xf6\xce\x5f\xc2\x06\x2e\x16\x81\x62\x7b\xf7\xf3\xed\x7b\xb6\xbd\x7b\xfb\xe1\x96\xdd\xdc\xbd\xfb\xc8\x3e\x5e\xff\x0a\x1b\xc8\x8c\x6e\x54\xc5\x8c\xbe\x17\x2a\x0b\xa4\xef\xaf\x7f\xba\xfa\xf9\xe6\x0b\xbb\x79\xcb\xae\x6e\xfe\x3d\x21\x8a\x11\xeb\xce\x98\x75\xdc\x51\x94\x29\xdf\xec\x81\x1b\xac\x8a\xc9\x75\x7c\xc7\x3e\xc1\x06\xfe\xf8\x73\x8e\x4d\x0a\x7f\xcc\x85\x4b\x85\xe7\x65\x46\x9f\x6c\xb6\x82\x3f\x9c\x38\xa1\x6e\x1c\x6c\xe0\xcd\x0a\xf0\xb1\xa6\xef\xf4\xa5\xb8\xf8\x33\x8f\xfc\x82\x3b\xed\x0a\xd0\x50\x2d\xc4\x4c\x2b\x88\xd5\xc5\x9b\xbf\xff\x23\x5f\x88\x1d\x28\xed\x12\x25\xf9\x5b\x2d\x00\x0c\xba\xc6\x28\x7a\xa6\xcd\x32\xdb\x71\x21\x29\x42\x1a\x4a\x83\x64\x1d\x45\xa5\xcd\x66\x4a\x93\xf8\xf8\x12\x32\x28\x0a\x58\x92\x30\x6d\x20\x6b\xd4\x51\xe9\xb3\xca\xf2\x7c\x81\xaa\x8a\x4a\xed\x1a\x55\xfa\x7a\xda\xa3\x63\x65\x63\x0c\x2a\xc7\x02\x8b\x65\xbe\x00\x18\xe8\xce\x14\x3f\x45\x6f\x3e\x70\x53\xd4\x46\x3f\x3e\xb1\x98\x6d\xfe\x76\xfc\x04\x36\x9d\x46\x7b\x74\xcb\x3e\xa7\x7c\xb1\x00\x18\xda\x1c\x4d\x06\x2f\x43\xea\xfd\x92\xfe\xff\x72\xb5\xbd\x5d\x41\xa6\x74\x22\x1b\x82\xc1\x8e\x42\xe5\xad\xf7\x36\x3b\x6d\x9d\x11\x6a\x3f\x14\x46\xd6\x78\xcb\x93\x43\xc3\xf5\xab\xfe\x90\xf7\x8c\xcb\xfd\xd4\x1d\xb0\x79\xd6\x6d\xcf\x59\x15\x24\x2b\x21\x9f\xd3\xe6\xb7\x4c\x6a\x5e\xad\x03\x7a\x64\x5f\x29\x78\xc3\xbc\x9f\xd5\x37\xd0\xff\x90\x92\x01\x0b\xbd\x75\x23\xb2\x89\xc9\xc2\xb2\x04\x5d\xed\x87\x22\x1d\x45\x3f\xc7\x78\x76\xb4\xc9\xee\x00\x91\x01\x35\x3a\x1e\x24\x33\x9e\x76\x6c\xe8\x85\xd8\x75\xe4\xdf\x36\xe4\xb1\x8e\x5b\x57\x12\x81\xa2\xe0\x55\x65\xd0\x52\x6d\xc5\x93\x5a\x1b\xe7\x89\xc9\x5b\x5e\x85\x68\xe7\x10\x34\x52\x18\xc4\x2e\x91\x6c\x20\x13\x35\x3b\x70\x7b\xc8\x3a\xa1\xeb\x35\x7c\xb9\x7b\x7f\xb7\x44\xf9\x20\x14\xee\x50\x55\x22\x07\x71\xaa\x25\x9e\x50\x39\xf0\xa9\x3f\x0e\x66\x11\xd5\xb1\xbf\xbd\xf9\xda\x29\x39\x7b\x1b\x14\x46\x69\x71\xa8\x8a\xef\x1f\x2f\xb8\x92\xee\x63\x33\x5d\x4e\x58\xe7\x8b\x1f\xf2\xd7\xc4\x19\xdf\x36\x63\xd8\x7d\xa1\x44\x53\xe5\xb5\x8f\x73\xaa\xc6\x0c\x84\xf5\xc5\x60\x9b\x9a\x84\x60\xb5\x82\x1d\x97\x52\xa8\xbd\x77\x04\x21\x99\x2f\xdb\xa1\x98\x5e\xb9\xae\xd7\xb0\xa5\x90\xad\xb7\x14\x32\xaa\x98\x11\x34\x5f\xd2\x9f\x64\xba\x07\xa9\xa2\x98\x6d\x2c\xbd\xb4\xe7\xd6\x31\xa1\x2a\x7c\x24\x60\x1f\xf7\x84\x3e\x56\x15\x2d\x56\xa5\x4a\xa0\x37\xab\x7e\x00\x14\x3e\xba\xa9\xe3\x57\x3d\x11\x3d\x48\x68\x45\x26\x1f\x46\x0d\x2e\x62\xae\x46\xa6\xd3\x1c\xf1\xa4\x5f\x17\x31\xa3\xc3\x3c\xd1\x94\xa5\x0f\xe4\x4e\x9b\x52\xdc\x4b\xca\xc2\x74\x86\xc6\x74\x17\xb3\x66\xda\x91\x99\x2b\x18\xeb\x1b\x98\xbd\x08\xcc\xb3\x7c\x21\x34\x2a\x1f\x5b\x34\x26\x4f\x9a\x8b\x5d\xa7\xd4\x8f\xb3\x7d\xe0\x52\x54\x20\x1c\x9e\x6c\x64\xf3\x04\xfa\x01\xcd\xd9\x08\xe7\x90\x26\xc0\x9e\xa0\x19\x2c\x0a\xd0\x63\xfb\xd0\x13\x3f\xa4\xda\xec\x98\x4c\x92\xad\x51\x3f\x90\x6e\x8b\xef\xae\xbd\x39\x54\xb7\x4f\xaa\x4c\x90\xdd\xd3\x2c\xb5\xd4\x69\xfc\xfa\x58\xbc\x5e\x03\x97\x56\xd3\xdc\x82\xce\xcf\x09\x06\x6d\x8d\xa5\x13\x0f\x08\x69\x2c\x6f\x67\x24\x2b\x54\xd9\xcd\x7c\x07\x6e\xa1\x3c\x70\xb5\xc7\xb1\x0b\xda\x40\x54\x28\xd1\xe1\xa8\x42\x16\x09\x24\x2f\x61\xeb\x45\xb6\xac\xf5\x0e\xae\x7f\xf9\x74\x05\x35\xa6\xf1\x64\xda\x7d\xbe\xbf\xfd\x8d\xa1\x7a\x84\x8f\xbd\x51\x2f\x9a\xc8\x88\xe4\x72\x27\x1b\x7b\x60\x5c\xca\x65\xfe\x22\x21\xf3\x75\xeb\x74\x53\x1e\xb0\x62\xdc\x8d\x1e\x06\x50\xea\xe7\xeb\x87\xdb\x9f\xee\x56\x90\x51\xb4\x8c\x56\xe2\xff\xed\x54\x52\x6a\x6a\x0e\x0e\xfd\x64\x12\xc6\xb1\xa1\xc3\x5e\x0b\xb9\x9d\xb6\x73\xcb\x2a\xee\x68\xcf\x18\x4c\x40\xbe\x8f\x0e\x28\x66\xa6\x90\xf0\x74\x34\x8b\x74\x16\xb5\x72\xf4\x71\x45\xbb\x44\xe2\x06\x1b\xa8\x4b\xb2\x9d\x96\xb1\xa2\xc2\x52\x57\x5d\x96\xb5\x2c\x7b\xa2\xf4\x71\xbe\xa2\xaf\xb7\xdb\x15\x40\x56\xea\x46\x56\x9e\xb0\xe6\xc6\x62\x37\xe9\x12\x9b\xcb\xd1\xf4\xd6\xd7\x22\xcf\x67\x55\xa6\x91\x8f\x0d\xf4\xa5\xc5\xa9\xe6\xc2\xd8\xe1\x73\xa8\x74\xaf\x79\x3e\x33\x9b\xf6\x5e\x24\xcc\x1f\x4f\xc0\xa1\x28\x60\x03\xce\x34\xb8\x88\xa3\xca\x64\xd0\x83\x99\x17\x64\x37\xad\xa1\x45\x85\x58\x33\x4a\x0f\x6e\x52\x0d\x0d\xac\xc8\xd3\xe4\x32\x12\x90\xb8\xf5\x04\x0d\x40\x62\x96\x49\xfb\x77\x2e\xdb\xf8\xce\xa1\x61\xd3\xe1\xf1\xb5\xa1\xf0\xc5\x0a\xf4\x03\xc9\x94\x73\xd2\x21\x49\x67\x9f\x0a\xa1\x84\x63\xed\x46\xeb\x89\x58\x5c\x8f\x28\x6b\x68\x8d\x32\x05\x3e\xa0\x79\x5a\xce\x2f\x92\xab\x61\xbd\x04\xd5\x88\xc3\xf3\x69\x98\xf9\x45\x0a\xce\x07\xa4\x1d\xdb\x39\x1a\x44\x68\x89\xee\x84\xf9\xbc\x1a\x30\x1e\xe7\x26\xf5\xb3\x67\x6d\x2a\x23\x54\xb4\xbe\xac\x0f\xdc\xc6\xad\x89\xdc\xe9\xbf\x47\x2f\x86\xcb\x0d\x64\x52\xef\x7b\x3e\x9c\xba\x6f\x94\xfd\xbd\xd7\xdf\x36\x90\x7e\x6d\xc8\x26\xbb\x46\x58\x1b\x4f\x8d\x75\x70\x4f\x9b\xa2\x94\xed\xef\x0b\xa9\x01\x68\x03\x52\xef\x57\x70\xdf\x38\x38\x13\xf0\x47\x92\x60\xb5\x97\x32\x84\xbe\xf4\xa3\x8e\x6f\xa3\x27\x6d\x90\x39\x23\xd0\x2e\x2f\xf2\x0e\x4a\x0e\xda\xba\x15\x50\x73\xf3\x95\x16\x4d\xe9\x41\xcd\x02\x3c\xde\xa4\x88\x0f\xd9\xc6\xbc\xab\x11\xcd\xb2\x63\x16\x3c\x37\x07\x35\xa1\x22\x12\x26\x87\xef\x59\xe0\xe4\x7f\x0f\xa1\x71\xd4\xb7\xc3\x30\x76\x12\x63\x3f\xa7\x5e\xb6\xc6\x92\xba\x7e\x6c\x6d\x2c\x65\x46\x48\xf3\x61\xf8\xe7\xaa\xa2\x8d\x51\x1e\x46\xf8\x57\x52\x4f\x48\x4c\xb9\x17\x95\x1b\xfc\x6a\x03\x4e\xbf\x9a\x73\x21\xc0\xec\xd3\xe2\xaf\x00\x00\x00\xff\xff\xf7\x27\x98\x07\x6d\x13\x00\x00") +var _etcNginxLuaBalancerLua = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x58\x5f\x6f\x1b\xb9\x11\x7f\xd7\xa7\x18\xec\x93\x16\x95\x16\x71\xd1\x27\xa3\x2a\xe0\x24\xba\xd6\x88\x23\x1f\x14\x27\x87\xc3\x21\x20\xe8\xdd\x91\xc4\x13\x45\x6e\x49\xae\x65\xf7\x70\xf9\xec\xc5\x70\x97\xdc\xbf\xb6\x13\xe0\x5e\x6c\x89\x1c\xce\xff\xf9\xcd\x8c\xa4\xce\xb9\x04\xb5\x7f\x64\xf7\x5c\x72\x95\xa3\x81\x15\x18\xfc\x6f\x25\x0c\xce\x13\xb5\x7f\xcc\xc2\x79\x92\xce\x6a\xe2\xdf\xad\x56\x5d\xa2\x9c\x0e\xe2\x6d\xae\xd5\x4e\xec\x2b\xc3\x9d\x18\x90\x75\x2f\x22\x79\xe5\x84\xec\x52\xd1\xf7\x78\x29\x4d\x95\xf3\xfc\x80\x5d\x02\x83\xd6\x3d\x65\xe1\x26\x92\xfa\x63\x26\x75\x7e\x9c\x20\xd6\xf9\x31\x12\xe2\xf9\xc4\xbb\x24\xc1\xbc\x8c\x2e\x22\x95\x75\x22\x3f\x3e\x75\xe9\xea\x93\x24\x9d\xcd\x96\x4b\x38\x21\xb7\x95\xc1\x02\x84\x02\x8b\xb9\x56\x85\xa5\xe3\x9d\x36\xc0\x15\x6c\xf6\x42\x3d\xc2\x59\x9b\x23\x1a\x70\x1a\x4a\x91\x1f\xa1\x2a\xc1\x1d\x10\x14\x9e\x41\x0a\xeb\x40\xef\xa0\x2a\xad\x33\xc8\x4f\x50\x22\x1a\xcf\x40\x38\x38\x0b\x29\xc1\xf1\x23\xc2\x3f\x89\xbe\x40\xc9\x9f\xa0\x52\xe4\xa6\x5c\x2b\x67\xb4\x94\x68\xe0\xe7\xdb\x4f\x77\x58\x78\x8e\xf7\x3c\x3f\xa2\x2a\x40\xdf\xff\x8e\xb9\x23\x79\x74\x5a\xeb\x80\xaa\x28\xb5\x50\xee\x5f\xf0\x37\x78\x7b\xf5\xee\xc3\x7a\xf3\xfe\x13\xfb\xf4\xeb\xe6\x1d\xbb\xde\xdc\xad\xb7\x5f\xae\x6e\x1a\x7b\xa7\x2f\x61\x05\x17\xb3\x86\x62\x7b\xfb\x79\xf3\x9e\x6d\x6f\xdf\x5e\x6f\xd8\xcd\xed\xbb\x0f\xec\xc3\xfa\x57\x58\x41\x62\x74\xa5\x0a\x66\xf4\xbd\x50\x49\x43\xfa\x7e\xfd\xd3\xd5\xe7\x9b\x3b\x76\xf3\x96\x5d\xdd\xfc\x7b\x44\x14\x22\xd6\x9e\x31\xeb\xb8\xa3\x28\x53\xbe\xd9\x03\x37\x58\x64\xa3\xeb\xf0\x8e\x7d\x84\x15\xfc\xf1\xe7\x14\x9b\x18\xfe\x90\x0b\x97\x0a\xcf\xf3\x84\x3e\xd9\x64\x01\x7f\x38\x71\x42\x5d\x39\x58\xc1\x9b\x05\xe0\x63\x49\xdf\xe9\x4b\x76\xf1\x67\x1a\xf8\x35\xee\xb4\x0b\x40\x43\xb5\x10\x32\x2d\x23\x56\x17\x6f\xfe\xfe\x8f\x74\x26\x76\xa0\xb4\x8b\x94\xe4\x6f\x35\x03\x30\xe8\x2a\xa3\xe8\x99\x36\xf3\x64\xc7\x85\xa4\x08\x69\xc8\x0d\x92\x75\x14\x95\x3a\x9b\x29\x4d\xc2\xe3\x4b\x48\x20\xcb\x60\x4e\xc2\xb4\x81\xa4\x52\x47\xa5\xcf\x2a\x49\xd3\x19\xaa\x22\x28\xb5\xab\x54\xee\xeb\x69\x8f\x8e\xe5\x95\x31\xa8\x1c\x6b\x58\xcc\xd3\x19\x40\x4f\x77\xa6\xf8\x29\x78\xf3\x81\x9b\xac\x34\xfa\xf1\x89\x85\x6c\xf3\xb7\xc3\x27\xb0\x6a\x35\xda\xa3\x9b\x77\x39\xa5\xb3\x19\x40\xdf\xe6\x60\x32\x78\x19\x52\xef\xe7\xf4\xff\x97\xab\xed\x66\x01\x89\xd2\x91\xac\x0f\x06\x3b\x0a\x95\xb7\xde\xdb\xec\xb4\x75\x46\xa8\x7d\x5f\x18\x59\xe3\x2d\x8f\x0e\x6d\xae\x5f\xf5\x87\xbc\x67\x5c\xee\xc7\xee\x80\xd5\xb3\x6e\x7b\xce\xaa\x46\xb2\x12\xf2\x39\x6d\x7e\x4b\xa4\xe6\xc5\xb2\x41\x8f\xe4\x2b\x05\xaf\x9f\xf7\x93\xfa\x36\xf4\x7f\xb1\x92\x8b\x8e\xa6\x81\x6f\xed\x8d\x01\xdb\x91\x8b\x84\x65\x11\xea\xea\x0f\x59\x3c\x0a\x71\x09\xf1\x6f\x69\xa3\x0a\x0d\xa4\x36\x28\xd3\xf2\x20\x99\xe1\xb4\x65\x43\x2f\xc4\xae\x25\xff\xb6\x22\xbd\x5b\x6e\x6d\x09\x35\x14\x19\x2f\x0a\x83\x96\x6a\x31\x9c\x94\xda\x38\x4f\x5c\xdb\x0a\xad\x9d\x7d\x90\x89\x61\x13\xbb\x48\xb2\x82\x44\x94\xec\xc0\xed\x21\x69\x85\x2e\x97\x70\x77\xfb\xfe\x76\x8e\xf2\x41\x28\xdc\xa1\x2a\x44\x0a\xe2\x54\x4a\x3c\xa1\x72\xe0\x4b\x65\x18\xfc\x2c\xa8\x63\x7f\x7b\xf3\xb5\x55\x72\xf2\xb6\x51\x18\xa5\xc5\xbe\x2a\xbe\xdf\xbc\xe0\x4a\xba\x0f\xcd\x77\x3e\x62\x9d\xce\x7e\xc8\x5f\x23\x67\x7c\x5b\x0d\x61\xfa\x85\x92\x8e\x95\x5a\x3f\x4e\xa9\x7a\x13\x10\xd6\xe7\xa5\xad\x4a\x12\x82\xc5\x02\x76\x5c\x4a\xa1\xf6\xde\x11\x84\x7c\xbe\xcc\xfb\x62\x3a\xe5\xbd\x5c\xc2\x96\x42\xb6\xdc\x52\xc8\xa8\xc2\x06\x50\x7e\x49\x7f\xa2\xe9\x1e\xd4\xb2\x6c\xb2\x11\xb5\x09\x2d\xb9\x75\x4c\xa8\x02\x1f\xa9\x11\x0c\x7b\x48\x17\xdb\xb2\x1a\xdb\x62\x25\xd0\x9b\x45\x37\x00\x0a\x1f\xdd\xd8\xf1\x8b\x8e\x88\x4e\x75\xd6\x22\xa3\x0f\x83\x06\x17\x21\x57\x03\xd3\x71\x8e\x78\xd2\xaf\x83\xea\xb5\x55\x9e\xfb\x40\xee\xb4\xc9\xc5\xbd\xa4\x2c\x8c\x67\x68\x4c\x7b\x31\x69\xa6\x1d\x98\xb9\x80\xa1\xbe\x0d\xb3\x17\x81\x7c\x92\x2f\x34\x8d\xcd\xc7\x16\x8d\x49\xa3\xe6\x62\xd7\x2a\xf5\xe3\x6c\x1f\xb8\x14\x05\x08\x87\x27\x1b\xd8\x3c\x81\x7e\x40\x73\x36\xc2\x39\xa4\x89\xb1\x23\x68\x02\x8b\x1a\xe8\xb1\x5d\xe8\x09\x1f\x62\x6d\xb6\x4c\x46\xc9\x56\xa9\x1f\x48\xb7\xd9\x77\xd7\xde\x54\x17\xb0\x4f\x2a\x8f\x10\xdf\xd1\x2c\xb6\xe0\x71\xfc\xba\x58\xbc\x5c\x02\x97\x56\xd3\x9c\x83\xce\xcf\x15\x06\x6d\x89\xb9\x13\x0f\x08\x71\x8c\xaf\x67\x2a\x2b\x54\xde\xce\x88\x07\x6e\x21\x3f\x70\xb5\xc7\xa1\x0b\xea\x40\x14\x28\xd1\xe1\xa0\x42\x66\x11\x24\x2f\x61\xeb\x45\xd6\xac\xf5\x0e\xd6\xbf\x7c\xbc\x82\x12\xe3\x38\x33\xee\x3e\xdf\xdf\x2e\x87\x50\x3d\xc0\xc7\xce\x68\x18\x4c\x64\x44\x72\xb9\x93\x95\x3d\x30\x2e\xe5\x3c\x7d\x91\x90\xf9\xba\x75\xba\xca\x0f\x58\x30\xee\x06\x0f\x1b\x50\xea\xe6\xeb\xf5\xe6\xa7\xdb\x05\x24\x14\x2d\xa3\x95\xf8\x5f\x3d\xc5\xe4\x9a\x9a\x83\x43\x3f\xc9\x34\xe3\x5b\xdf\x61\xaf\x85\xdc\x8e\xdb\xbf\x65\x05\x77\xb4\x97\xf4\x26\x26\xdf\x47\x7b\x14\x13\x03\x41\xf3\x74\x30\x16\xb4\x16\xd5\x72\xf4\x71\x41\xbb\x47\xe4\x06\x2b\x28\x73\xb2\x9d\x96\xb7\xac\xc0\x5c\x17\x6d\x96\xd5\x2c\x3b\xa2\xf4\x71\xba\xa2\xd7\xdb\xed\x02\x20\xc9\x75\x25\x0b\x4f\x58\x72\x63\xb1\x9d\x8c\x89\xcd\xe5\x60\xda\xeb\x6a\x91\xa6\x93\x2a\xd3\x88\xc8\x7a\xfa\xd2\xa2\x55\x72\x61\x6c\xff\x39\x14\xba\xd3\x3c\x9f\x99\x65\x3b\x2f\x22\xe6\x0f\x27\xe6\xa6\x28\x60\x05\xce\x54\x38\x0b\xa3\xca\x68\xe6\x82\x89\x17\x64\x37\xad\xad\x59\x81\x58\x32\x4a\x0f\x6e\x62\x0d\xf5\xac\x48\xe3\xe4\x32\x10\x10\xb9\x75\x04\xf5\x40\x62\x92\x49\xfd\x77\x2a\xdb\xf8\xce\xa1\x61\xe3\x61\xf3\xb5\xa1\xf0\xc5\x0a\xf4\x03\xc9\x98\x73\xd4\x21\x4a\x67\x1f\x33\xa1\x84\x63\xf5\x06\xec\x89\x58\x58\xa7\x28\x6b\x68\xed\x32\x19\x3e\xa0\x79\x9a\x4f\x2f\x9e\x8b\x7e\xbd\x34\xaa\x11\x87\xe7\xd3\x30\xf1\x8b\x17\x9c\x0f\x48\x3b\xb9\x73\x34\x88\xd0\xd2\xdd\x0a\xf3\x79\xd5\x63\x3c\xcc\x4d\xea\x67\xcf\xda\x94\x07\xa8\xa8\x7d\x59\x1e\xb8\x0d\x5b\x16\xb9\xd3\x7f\x0f\x5e\x6c\x2e\x57\x90\x48\xbd\xef\xf8\x70\xec\xbe\x41\xf6\x77\x5e\x7f\x5b\x41\xfc\x75\x22\x19\x8d\xfd\xcd\x9a\x79\xaa\xac\x83\x7b\xda\x2c\xa5\xac\x7f\x8f\x88\x0d\x40\x1b\x90\x7a\xbf\x80\xfb\xca\xc1\x99\x80\x3f\x90\x34\x56\x7b\x29\x7d\xe8\x8b\x3f\x02\xf9\x36\x7a\xd2\x06\x99\x33\x02\xed\xfc\x22\x6d\xa1\xe4\xa0\xad\x5b\x00\x35\x37\x5f\x69\xad\x29\x0d\x58\xd0\xfd\x00\xb7\x1d\x77\x95\x6d\x7c\xf5\x9f\xbb\xbb\x9f\xd9\xa7\xf5\xf6\xcb\xf5\xbb\x35\xfb\xbc\xb9\xfa\x72\x75\x7d\x73\xf5\xf6\x66\xfd\x22\x78\xcd\xc0\x23\x58\xcc\xa1\xbe\xa2\x21\x93\x4b\x44\x33\x6f\xd5\x6b\x74\x9a\x02\xaf\xa6\xc6\x22\xca\x37\xdf\x93\x86\x93\xff\x45\x86\x06\x5c\xdf\x60\x9b\x41\xd6\xdb\x45\x93\xef\x65\xed\x3e\x72\x80\x1f\x84\x2b\x4b\xb9\xd6\x14\x4e\x3f\xa1\xa6\xea\xac\x8e\x7a\xda\x2c\x05\xaf\x24\xb3\x90\x18\xb3\x39\x28\xd7\xfb\xdd\x08\x9c\x7e\x35\x8b\x9b\x94\x61\x1f\x67\xff\x0f\x00\x00\xff\xff\x4e\x72\xa7\xc6\xef\x13\x00\x00") func etcNginxLuaBalancerLuaBytes() ([]byte, error) { return bindataRead( @@ -158,7 +158,7 @@ func etcNginxLuaStickyLua() (*asset, error) { return a, nil } -var _etcNginxLuaTestBalancer_testLua = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x4a\x49\x2d\x4e\x2e\xca\x4c\x4a\xd5\x50\x8a\x4e\x4a\xcc\x49\xcc\x4b\x4e\x2d\x8a\x2f\x49\x2d\x2e\x89\x55\xd2\x51\x48\x2b\xcd\x4b\x2e\xc9\xcc\xcf\xd3\xd0\xe4\x52\x50\xd0\xd5\x55\x08\xf1\x77\xf1\xb7\x52\x28\x2f\xca\x2c\x49\x55\x00\xa9\x29\x56\xe0\x4a\xcd\x4b\xd1\xe4\x02\x04\x00\x00\xff\xff\xa5\x22\x75\xe9\x44\x00\x00\x00") +var _etcNginxLuaTestBalancer_testLua = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x5a\xdd\x6f\xe4\xb6\x11\x7f\xdf\xbf\x62\xa0\x97\x48\x85\x2a\xdb\x39\x04\x08\x5c\x2c\x8a\x6b\x71\x6d\x0f\x08\xae\xc5\x35\x4d\x1f\x0e\x81\xc0\x95\x66\x77\xd9\xa5\x49\x85\xa4\xbc\x36\x8c\xfd\xdf\x0b\x92\x12\x45\x4a\xda\x0f\xa3\x71\xd2\xbb\x38\xc0\xc5\x2b\x6a\xbe\x38\x1f\xbf\xa1\x46\x62\xa2\x22\x0c\xaa\x7d\x0d\x4b\xa0\xa2\x68\x44\x83\x3c\x4d\x9a\x7d\x9d\x64\xb7\x12\x49\x9d\x7e\xf5\x3b\xf6\x55\xb6\x68\x48\xb5\x23\x1b\x2c\x1a\xa2\xb7\xb0\xb4\xf4\x45\x01\xc9\x95\x14\x42\xaf\xd5\x15\xea\xea\x8a\x6f\x28\x7f\xb8\x62\x2d\xb9\xfa\x63\xc1\x5a\xf2\x87\xc4\x50\x84\x7c\x8b\x85\x53\xb6\x22\x8c\xf0\x0a\x65\x0e\x77\xa2\xda\x95\xd5\x7f\x94\xe0\xfd\x6f\xc1\xd7\x74\xd3\x5d\x28\x4d\xab\xdd\x63\x77\xb1\x22\xd5\x0e\x79\xad\xba\x4b\x26\xdb\x8a\x54\x5b\xcc\x81\x89\x6a\xd7\x2f\x9a\x9f\x0b\x70\x17\x7c\xf3\x50\x8e\x34\xe1\xfe\x8e\xf4\x46\xac\x5b\x5e\x69\x2a\x38\xd4\xb4\xd2\xe5\x06\x39\x4a\xa2\x85\x4c\xef\x09\x53\xd9\x02\xc0\x51\x95\xe6\x2e\x2c\xe1\x09\xca\x92\xf2\x1a\x1f\xcc\xef\x05\xd8\xff\x36\x68\xee\xf4\x72\x52\x85\x6c\x9d\xc3\x0e\x1f\xb3\xee\x3e\x80\x44\xdd\x4a\x0e\xe6\x4e\x51\x1a\xc1\x9f\x76\xf8\xf8\x63\x77\x1b\x79\x9d\x77\x3f\xd5\x11\x49\x39\xdc\x13\x36\x88\x83\xb1\x24\x58\x1a\x82\xe0\x7e\xa7\x50\xcb\x16\x73\xe0\x94\xe5\xb0\x26\x4c\xe1\x54\x63\x8d\x0c\x35\x5e\x6e\xfe\xad\x42\x9d\x5a\x8b\x38\xf5\x16\x05\xf2\xd6\xac\x55\xdb\x92\x30\x36\x16\x39\x96\x36\x65\xb5\xdb\x71\x3b\x51\x76\xe9\xb0\x70\xff\xbc\x7a\x7d\x87\x9a\x68\xb2\x62\x98\x3e\x85\xd4\x87\xdc\xc5\x27\x5b\x20\xaf\x27\x71\xed\x12\x26\x08\x2d\x27\x77\x98\x1b\xd5\x8d\xa0\x5c\xab\x1c\xd8\xaa\x24\x6c\x93\x0d\xba\x5c\x68\x0d\x1d\x2c\xed\x1f\x67\xa4\x67\x81\x65\xc0\x6e\x6f\x7d\x4a\x98\x20\xf5\xef\xbb\x44\x4b\x4c\x48\x9c\xd4\xdc\xee\x22\x30\xac\xc6\x35\x69\x99\x2e\x43\x61\x46\xdf\x13\xa9\x6b\x89\xca\x5c\x26\xd7\xd7\xd7\x45\xf7\x2f\xc9\xa1\x11\xd2\xe4\x45\xf2\xed\xf5\xb7\xd7\xc9\x21\x3f\x4a\x7b\x13\xd3\xde\x18\xda\xc3\x58\x6d\x5f\x3f\x9d\x56\x5b\x0f\x52\xf6\xcb\xb0\x9c\xf1\x57\x32\x22\x4a\xf2\xe9\x26\x72\x48\xa4\x68\x79\x5d\x4a\xb1\xa2\x3c\xc9\x7c\xf9\x99\x5a\xbb\x40\x7a\x48\x76\x44\xbe\x21\x31\x82\x0f\x93\x10\x53\x4e\x75\x9a\xf5\x1a\x2d\x8e\x98\xed\x1d\xfc\x8a\x45\x93\x68\xc9\x61\x4a\xb4\x14\x02\x45\x74\xc3\x28\x8e\x16\x02\x1f\x8e\x50\x63\xec\x65\x6f\x54\x8f\x53\x1e\x37\x38\xee\x83\x1a\x81\x34\xeb\x73\x2f\xd6\x60\x12\xc7\x95\x81\x01\x35\xcf\xdd\x5d\xf8\x12\xf3\xdc\xbe\xa0\x5a\x7e\x8a\xa6\x93\xe9\xd1\xf2\xbc\x59\x96\x6a\xe0\x2c\xff\x5a\xf0\xcd\x80\x82\x6a\x4b\x24\xd6\x01\x28\x06\xc9\x50\x2a\x4d\x2c\xc6\x8c\x7c\xf5\x74\xc8\xfa\xda\xef\xdd\xde\xbb\xfa\x42\xca\x92\x11\xa5\x4b\x2d\xda\x6a\x8b\x75\x49\xf4\x09\xce\x83\xfb\x73\x4f\x5c\x68\xf3\xce\x8b\x9b\xd3\x4e\xfc\xf7\xdb\x8f\x1f\x4c\x35\xed\x89\xe4\x89\x5b\x7a\xff\xe1\x2f\x7f\x37\x4b\x94\xaf\x45\xb7\xf4\xee\xe3\x47\xb3\x82\x52\x76\x0b\x7f\xfb\xfe\xfb\x7f\x94\xff\x7c\xf7\xf1\x87\xf7\x7f\x7e\x57\xfe\xeb\xc3\xdb\x1f\xde\xbe\xff\xee\xed\x9f\xbe\x7b\x07\x4b\xf8\xe6\xfa\x4d\xe7\xc1\xbe\x23\x1a\xdc\xc0\xfa\x53\xc2\x37\x0f\x45\xbf\x3b\x8b\x1f\x93\xa4\x9c\x61\x92\xa8\xf4\x63\xd1\x27\xd7\xc0\xd6\xaf\xcc\xb0\xd8\xf2\x18\x28\xed\xe5\x71\xc9\xa2\xda\x05\x52\x45\xb5\x9b\x21\xed\xed\x2b\x6c\x85\x7a\x6a\xdb\x65\x67\xf4\xdb\x62\x6c\x25\x31\x3e\x0f\xec\xb0\xcb\x33\xf4\xae\x52\x07\x42\x77\xbd\x18\x52\x01\x96\x20\xf1\xa7\x96\x4a\x4c\xbd\x29\x49\xd7\x0b\x6a\x54\x95\xa4\x2b\x4c\x93\x4f\x3e\x73\x34\x2a\xfd\x63\x92\x07\x71\x5f\xd8\xd6\xdb\x36\x69\xb4\x04\x03\xae\x20\xaf\xb3\xc5\x02\x40\x23\x91\xb5\xd8\xf3\x31\xa1\xc3\xa3\xce\x72\x0b\xad\x71\x38\x0d\x3a\xc6\x91\xca\xbb\x38\x0c\x77\x8c\xa7\x73\x18\x39\x33\x1f\xb9\x2b\x87\xde\x1f\x07\xab\x78\x2d\x24\xd0\xbc\xd7\x5c\xda\x8e\x45\x39\xd0\x86\x50\xa9\xd2\xde\xa0\x0c\x6a\xd1\xd5\xcf\xc8\xbb\x21\xa3\xf1\x30\xa7\xac\xef\x74\xc1\xb6\x07\x2f\x7a\xeb\x2a\xc2\x58\x9a\xdd\x8e\xdd\x78\xc4\x91\x33\x08\x5b\x28\xd4\xe5\x9d\x90\x58\x6a\x49\xad\xcf\x66\x40\xc7\x99\x71\x9c\xbf\x6a\xa5\x44\xae\xcb\x06\x6d\x1a\x9c\x97\x60\x9c\x5a\x90\xb5\x46\xd9\x0b\x3a\xcd\xd6\x7b\x00\x60\x85\x6b\x63\x2c\x92\x6a\x3b\xdd\x9d\x43\xc3\x62\x83\xba\x6c\xb6\x44\x61\xe0\x49\x7f\xd3\x41\x64\x31\x01\xc6\xa2\x3f\xca\x3c\x1d\x62\x86\x1e\xab\x42\xfb\xfb\xbe\xe0\x99\xc6\x0d\x27\x24\x76\x99\x52\x50\x35\x74\x3b\x6f\xf9\xca\x6f\xd5\x9e\x0c\x67\x36\x3c\x04\xdd\x6e\x69\xc9\xc4\x66\x1a\xed\x33\x7e\x99\xf5\xcc\x14\x6f\x13\x23\x3b\x08\xd5\x60\x04\x00\xd5\x69\xe2\x4e\x52\x4b\x13\xbc\x1c\x2c\xf0\xc7\x21\xdc\x13\x05\x26\x21\xb1\x9e\xb3\x30\x74\x68\xd1\x48\xf1\xf0\x58\xb6\x8d\xd2\x12\xc9\x5d\xd9\x1d\xf1\x66\xce\x20\x0b\xcf\xed\x8a\x7b\xaa\xb6\x54\x8d\x71\xa9\x6a\x1e\x0b\xc1\x53\x2f\x21\x87\x24\x22\x4b\xb2\x41\x14\x51\x0a\xa5\x2e\xb6\x44\x95\x5c\x94\x28\xa5\x90\x2a\x8d\x6a\x2a\x1b\xd3\xaa\xe6\x31\x9d\xd7\x9d\x15\x7b\xa2\x4a\xb7\xef\x34\x3b\xed\xba\x20\xeb\x8e\x7a\x90\x0b\xfd\x73\x78\x31\x38\x27\x7e\x2e\x3e\xe4\x42\xcf\xfa\xf1\x74\x41\x04\xe8\xfe\x32\x55\xe1\x15\x9c\x2f\x8d\x28\xbe\x16\x0d\x4d\x44\x55\x5b\x55\xa8\xd4\xba\x65\xec\xd1\x00\xf3\x4b\xc5\x75\x38\xd4\xeb\x99\x80\x0e\x4f\xec\xc9\x06\x75\x92\x8d\x98\xe3\x3e\x30\xc3\x1f\x3f\xc0\x27\x31\xfd\xac\xb8\xb0\x2d\x5c\x26\x30\xe4\xf8\x19\x92\x6d\xe4\x90\xb0\x52\xcb\x3d\xd5\xdb\xf4\x8e\xe8\x6a\x6b\x90\xd9\x3d\xce\x66\xf9\xd4\xc9\xb3\x82\xa7\xce\x9a\xca\xbe\x39\xca\x39\xf6\xcb\x94\x77\xf4\xe4\xe9\x1e\x39\x03\x7f\xc4\x4d\x68\x83\xfa\xb6\x62\x48\x64\x90\x4d\xe7\x1a\xfd\x85\x0c\xa1\xa9\x9e\xe5\x35\x2c\xee\x21\xdf\x3d\xdd\x9f\x81\x7c\x87\xa3\x2f\x83\x05\x47\x3a\xe5\x70\xc4\x9a\x1e\xae\x82\x13\xc7\xdc\xf4\xe2\xe6\x66\x34\xbd\x78\x93\x1c\x2c\xec\xbd\x42\xcd\x0b\xe5\x74\x14\xc3\x5f\x37\xab\x5d\xf0\x5d\xd4\x8f\x65\xb5\x3b\xc2\x2e\xdd\x48\xd3\x25\x92\x02\xa5\x85\xc4\x3a\x98\xcb\x11\x5e\xff\x92\xed\xef\x19\xc7\x6c\x63\x78\xd0\xc7\x63\x5e\x13\xbc\x7e\x13\xb3\x67\x81\xd9\x81\xdf\xb8\x64\xbe\x7e\x2d\x99\xdf\x4a\x1b\xf0\x05\xf3\xf5\x65\x05\x53\x0b\x74\x07\xfc\x7e\x98\x3e\x2a\x1c\x3f\x71\x55\xa0\x85\x1b\xde\x81\x9b\xe4\x7e\xbe\x35\xc3\x29\x3b\xca\xac\x8e\x30\xaf\xa2\x09\xc0\x6b\x21\x7d\xf1\x85\x14\x1d\x73\xe7\x0b\x69\x4d\x28\x53\xb0\xdf\x22\x07\x2e\xfa\x24\x00\x7c\xa0\x4a\xab\x17\x2a\x8f\xc9\x98\xe7\xe9\xf0\x9a\x8c\x27\x92\x91\x98\x67\xed\x9f\x5a\xc2\x52\x3b\x69\xd3\x44\xb7\x2a\x87\x6f\xae\xdf\x7c\xfe\x69\xfb\xdc\xd9\x84\x01\x79\xca\xc1\x8d\x15\x98\xd8\x80\x90\x70\x6a\x52\x61\x32\xbc\x3f\x4e\x39\x5f\x9f\xcc\xe9\x33\x23\x0b\x2e\x1a\x4c\x22\xd4\x0d\xa2\x69\xc5\xc7\x91\x34\x9e\x6d\x95\x86\x15\x76\xa3\x27\x63\xbb\x1f\xec\x0b\x69\x76\x90\xc3\xaa\xd5\xc1\x8c\x0f\x28\xbf\x05\xab\x68\xde\x21\x27\xe7\xd6\x94\x53\x5d\xee\x85\xdc\xa1\x7c\xce\xf8\xba\xdb\xbd\xa6\x77\xee\xdd\xa0\xdf\x1e\xde\xa3\x8c\xba\x17\xe5\x1a\xe5\x3d\x61\x4e\xf2\x30\x65\x6d\x79\x95\x66\x81\x67\xa2\xb1\xae\x7d\x17\x50\xd4\x58\x89\x3a\x72\xec\x83\xe7\x7f\x78\xee\x48\xfa\x19\xe3\xe2\xb9\x34\x52\x8f\xbc\xf2\x54\xb3\x9e\xea\x4e\x19\xe6\x8c\x8d\xf5\x80\x8f\x86\x11\xa8\xea\x5f\xc9\x1c\x99\x25\x06\xaf\x7c\x6c\x4a\xf5\x9a\xca\x9a\x68\x32\x9b\x5a\x93\xf7\xd7\x27\x1a\xb4\x3a\x87\x89\xca\x62\xe2\x85\x88\x13\xa4\xcc\x49\x38\x51\x21\x9c\x1c\x2b\xdb\x33\xa3\xbb\xb6\xa9\x89\x46\xe5\xbb\x8c\xf5\xea\x4b\xb9\xf4\x69\xe2\xd4\x62\xfc\x39\xc0\x61\x7a\x82\x9a\xeb\x4d\x23\x90\x38\xfb\xaa\xe3\x9c\xde\xff\x31\xb0\x31\xb3\x94\xa5\xfb\xcc\x25\x66\x3d\x63\x6b\x0e\x89\xe3\x9a\xc8\xb3\x0f\xce\xee\x43\x97\x13\x02\xa3\x17\xd5\x39\x24\xfe\xcb\x98\x79\x79\x4c\x3f\x57\xe6\xf8\xe5\xf7\x48\xc7\x0b\x25\xb7\xa7\x01\xb8\xa0\x6d\x9e\x8b\x73\xe1\x3e\xc5\x19\x33\xce\x5a\x12\x85\xf1\x92\x9e\x7d\x91\xf2\xe3\xef\x0a\x7c\x34\x8e\x16\xf4\x94\x27\x8a\xe2\x73\x81\xc0\x25\xca\xff\x05\x02\x44\x1f\xed\xfc\xa2\x18\x10\x6a\x7e\x45\x81\x2f\x19\x05\xc2\x48\xff\x0a\x38\x30\x51\x7f\x31\x12\x9c\xd2\xf6\x2c\x68\xb8\x44\xd0\xec\xd1\xd6\xfe\xef\xbf\x01\x00\x00\xff\xff\xa3\x22\xb2\x79\xb1\x2b\x00\x00") func etcNginxLuaTestBalancer_testLuaBytes() ([]byte, error) { return bindataRead( diff --git a/rootfs/etc/nginx/lua/balancer.lua b/rootfs/etc/nginx/lua/balancer.lua index a66573acc..4651f5d45 100644 --- a/rootfs/etc/nginx/lua/balancer.lua +++ b/rootfs/etc/nginx/lua/balancer.lua @@ -48,6 +48,9 @@ end local function balance() local backend = get_current_backend() + if not backend then + return nil, nil + end local lb_alg = get_current_lb_alg() local is_sticky = sticky.is_sticky(backend) @@ -164,6 +167,10 @@ function _M.call() ngx_balancer.set_more_tries(1) local host, port = balance() + if not host then + ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE + return + end local ok ok, err = ngx_balancer.set_current_peer(host, port) diff --git a/rootfs/etc/nginx/lua/test/balancer_test.lua b/rootfs/etc/nginx/lua/test/balancer_test.lua index a3257249f..0e6b7b58e 100644 --- a/rootfs/etc/nginx/lua/test/balancer_test.lua +++ b/rootfs/etc/nginx/lua/test/balancer_test.lua @@ -1,3 +1,296 @@ +local cwd = io.popen("pwd"):read('*l') +package.path = cwd .. "/rootfs/etc/nginx/lua/?.lua;" .. package.path + +local balancer, mock_cjson, mock_config, mock_sticky, mock_backends, mock_lrucache, lock, mock_lock, + mock_ngx_balancer, mock_ewma + +local function dict_generator(vals) + local _dict = { __index = { + get = function(self, key) + return self._vals[key] + end, + set = function(self, key, val) + self._vals[key] = val + return true, nil, false + end, + delete = function(self, key) + return self:set(key, nil) + end, + flush_all = function(self) + return + end, + _vals = vals + } + } + return setmetatable({_vals = vals}, _dict) +end + +local function backend_generator(name, endpoints, lb_alg) + return { + name = name, + endpoints = endpoints, + ["load-balance"] = lb_alg, + } +end + +local default_endpoints = { + {address = "000.000.000", port = "8080"}, + {address = "000.000.001", port = "8081"}, +} + +local default_backends = { + mock_rr_backend = backend_generator("mock_rr_backend", default_endpoints, "round_robin"), + mock_ewma_backend = backend_generator("mock_ewma_backend", default_endpoints, "ewma"), +} + +local function init() + mock_cjson = {} + mock_config = {} + mock_sticky = {} + mock_ngx_balancer = {} + mock_ewma = {} + mock_backends = dict_generator(default_backends) + mock_lrucache = { + new = function () return mock_backends end + } + lock = { + lock = function() return end, + unlock = function() return end + } + mock_lock = { + new = function () return lock end + } + _G.ngx = { + shared = { + round_robin_state = dict_generator({}), + balancer_ewma = dict_generator({}), + balancer_ewma_last_touched_at = dict_generator({}), + }, + var = {}, + log = function() return end, + WARN = "warn", + INFO = "info", + ERR = "err", + HTTP_SERVICE_UNAVAILABLE = 503 + } + package.loaded["ngx.balancer"] = mock_ngx_balancer + package.loaded["resty.lrucache"] = mock_lrucache + package.loaded["cjson"] = mock_cjson + package.loaded["resty.lock"] = mock_lock + package.loaded["balancer.ewma"] = mock_ewma + package.loaded["configuration"] = mock_config + package.loaded["sticky"] = mock_sticky + balancer = require("balancer") +end + describe("[balancer_test]", function() - -- TODO: write tests + setup(function() + init() + end) + + teardown(function() + local packages = {"ngx.balancer", "resty.lrucache","cjson", "resty.lock", "balancer.ewma","configuration", "sticky"} + for i, package_name in ipairs(packages) do + package.loaded[package_name] = nil + end + end) + + describe("balancer.call():", function() + setup(function() + mock_ngx_balancer.set_more_tries = function () return end + mock_ngx_balancer.set_current_peer = function () return end + mock_ewma.after_balance = function () return end + end) + + before_each(function() + _G.ngx.get_phase = nil + _G.ngx.shared.round_robin_state._vals = {} + _G.ngx.var = {} + mock_backends._vals = default_backends + mock_sticky.is_sticky = function(b) return false end + end) + + describe("phase=log", function() + before_each(function() + _G.ngx.get_phase = function() return "log" end + end) + + it("lb_alg=ewma, ewma_after_balance was called", function() + _G.ngx.var.proxy_upstream_name = "mock_ewma_backend" + + local ewma_after_balance_spy = spy.on(mock_ewma, "after_balance") + + assert.has_no_errors(balancer.call) + assert.spy(ewma_after_balance_spy).was_called() + end) + + it("lb_alg=round_robin, ewma_after_balance was not called", function() + _G.ngx.var.proxy_upstream_name = "mock_rr_backend" + + local ewma_after_balance_spy = spy.on(mock_ewma, "after_balance") + + assert.has_no_errors(balancer.call) + assert.spy(ewma_after_balance_spy).was_not_called() + end) + end) + + describe("phase=balancer", function() + before_each(function() + _G.ngx.get_phase = function() return "balancer" end + end) + + it("lb_alg=round_robin, peer was successfully set", function() + _G.ngx.var.proxy_upstream_name = "mock_rr_backend" + + local backend_get_spy = spy.on(mock_backends, "get") + local set_more_tries_spy = spy.on(mock_ngx_balancer, "set_more_tries") + local set_current_peer_spy = spy.on(mock_ngx_balancer, "set_current_peer") + + assert.has_no_errors(balancer.call) + assert.spy(backend_get_spy).was_called_with(match.is_table(), "mock_rr_backend") + assert.spy(set_more_tries_spy).was_called_with(1) + assert.spy(set_current_peer_spy).was_called_with("000.000.000", "8080") + + mock_backends.get:clear() + mock_ngx_balancer.set_more_tries:clear() + mock_ngx_balancer.set_current_peer:clear() + + assert.has_no_errors(balancer.call) + assert.spy(backend_get_spy).was_called_with(match.is_table(), "mock_rr_backend") + assert.spy(set_more_tries_spy).was_called_with(1) + assert.spy(set_current_peer_spy).was_called_with("000.000.001", "8081") + end) + + it("lb_alg=ewma, peer was successfully set", function() + _G.ngx.var.proxy_upstream_name = "mock_ewma_backend" + + mock_ewma.balance = function(b) return {address = "000.000.111", port = "8083"} end + + local backend_get_spy = spy.on(mock_backends, "get") + local set_more_tries_spy = spy.on(mock_ngx_balancer, "set_more_tries") + local set_current_peer_spy = spy.on(mock_ngx_balancer, "set_current_peer") + + assert.has_no_errors(balancer.call) + assert.spy(backend_get_spy).was_called_with(match.is_table(), "mock_ewma_backend") + assert.spy(set_more_tries_spy).was_called_with(1) + assert.spy(set_current_peer_spy).was_called_with("000.000.111", "8083") + end) + + it("sticky=true, returns stored endpoints and peer was successfully set", function() + _G.ngx.var.proxy_upstream_name = "mock_rr_backend" + + mock_sticky.is_sticky = function(b) return true end + mock_sticky.get_endpoint = function() return {address = "000.000.011", port = "8082"} end + + local backend_get_spy = spy.on(mock_backends, "get") + local set_more_tries_spy = spy.on(mock_ngx_balancer, "set_more_tries") + local set_current_peer_spy = spy.on(mock_ngx_balancer, "set_current_peer") + + assert.has_no_errors(balancer.call) + assert.spy(backend_get_spy).was_called_with(match.is_table(), "mock_rr_backend") + assert.spy(set_more_tries_spy).was_called_with(1) + assert.spy(set_current_peer_spy).was_called_with("000.000.011", "8082") + end) + + it("sticky=true, does not return stored endpoints, defaults to round robin", function() + _G.ngx.var.proxy_upstream_name = "mock_rr_backend" + + mock_sticky.is_sticky = function(b) return true end + mock_sticky.get_endpoint = function() return nil end + mock_sticky.set_endpoint = function(b) return end + + local backend_get_spy = spy.on(mock_backends, "get") + local set_more_tries_spy = spy.on(mock_ngx_balancer, "set_more_tries") + local set_current_peer_spy = spy.on(mock_ngx_balancer, "set_current_peer") + + assert.has_no_errors(balancer.call) + assert.spy(backend_get_spy).was_called_with(match.is_table(), "mock_rr_backend") + assert.spy(set_more_tries_spy).was_called_with(1) + assert.spy(set_current_peer_spy).was_called_with("000.000.000", "8080") + end) + + it("fails when no backend exists", function() + _G.ngx.var.proxy_upstream_name = "mock_rr_backend" + + mock_backends._vals = {} + + local backend_get_spy = spy.on(mock_backends, "get") + local set_more_tries_spy = spy.on(mock_ngx_balancer, "set_more_tries") + local set_current_peer_spy = spy.on(mock_ngx_balancer, "set_current_peer") + + assert.has_no_errors(balancer.call) + assert.are_equal(ngx.status, 503) + assert.spy(backend_get_spy).was_called_with(match.is_table(), "mock_rr_backend") + assert.spy(set_more_tries_spy).was_called_with(1) + assert.spy(set_current_peer_spy).was_not_called() + end) + end) + + describe("not in phase log or balancer", function() + it("returns errors", function() + _G.ngx.get_phase = function() return "nope" end + assert.has_error(balancer.call, "must be called in balancer or log, but was called in: nope") + end) + end) + end) + + describe("balancer.init_worker():", function() + setup(function() + _G.ngx.timer = { + every = function(interval, func) return func() end + } + mock_cjson.decode = function(x) return x end + end) + + before_each(function() + mock_backends._vals = default_backends + end) + + describe("sync_backends():", function() + it("succeeds when no sync is required", function() + mock_config.get_backends_data = function() return default_backends end + + local backend_set_spy = spy.on(mock_backends, "set") + + assert.has_no_errors(balancer.init_worker) + assert.spy(backend_set_spy).was_not_called() + end) + + it("lb_alg=round_robin, updates backend when sync is required", function() + mock_config.get_backends_data = function() return { default_backends.mock_rr_backend } end + mock_backends._vals = {} + _G.ngx.shared.round_robin_state._vals = default_backends.mock_rr_backend + + local backend_set_spy = spy.on(mock_backends, "set") + local rr_delete_spy = spy.on(_G.ngx.shared.round_robin_state, "delete") + local ewma_flush_spy = spy.on(_G.ngx.shared.balancer_ewma, "flush_all") + local ewma_lta_flush_spy = spy.on(_G.ngx.shared.balancer_ewma_last_touched_at, "flush_all") + + assert.has_no_errors(balancer.init_worker) + assert.spy(backend_set_spy) + .was_called_with(match.is_table(), default_backends.mock_rr_backend.name, match.is_table()) + assert.spy(rr_delete_spy).was_called_with(match.is_table(), default_backends.mock_rr_backend.name) + assert.spy(ewma_flush_spy).was_not_called() + assert.spy(ewma_lta_flush_spy).was_not_called() + end) + + it("lb_alg=ewma, updates backend when sync is required", function() + mock_config.get_backends_data = function() return { default_backends.mock_ewma_backend } end + mock_backends._vals = {} + _G.ngx.shared.round_robin_state._vals = default_backends.mock_ewma_backend + + local backend_set_spy = spy.on(mock_backends, "set") + local rr_delete_spy = spy.on(_G.ngx.shared.round_robin_state, "delete") + local ewma_flush_spy = spy.on(_G.ngx.shared.balancer_ewma, "flush_all") + local ewma_lta_flush_spy = spy.on(_G.ngx.shared.balancer_ewma_last_touched_at, "flush_all") + + assert.has_no_errors(balancer.init_worker) + assert.spy(backend_set_spy) + .was_called_with(match.is_table(), default_backends.mock_ewma_backend.name, match.is_table()) + assert.spy(rr_delete_spy).was_called_with(match.is_table(), default_backends.mock_ewma_backend.name) + assert.spy(ewma_flush_spy).was_called_with(match.is_table()) + assert.spy(ewma_lta_flush_spy).was_called_with(match.is_table()) + end) + end) + end) end)