github.com/thanos-io/thanos@v0.32.5/pkg/discovery/memcache/resolver_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package memcache 5 6 import ( 7 "bufio" 8 "strings" 9 "testing" 10 11 "github.com/pkg/errors" 12 13 "github.com/efficientgo/core/testutil" 14 ) 15 16 func TestGoodClusterConfigs(t *testing.T) { 17 resolver := memcachedAutoDiscovery{} 18 testCases := []struct { 19 content string 20 config clusterConfig 21 }{ 22 {"CONFIG cluster 0 23\r\n100\r\ndns-1|ip-1|11211\r\nEND\r\n", 23 clusterConfig{nodes: []node{{dns: "dns-1", ip: "ip-1", port: 11211}}, version: 100}, 24 }, 25 {"CONFIG cluster 0 37\r\n0\r\ndns-1|ip-1|11211 dns-2|ip-2|8080\r\nEND\r\n", 26 clusterConfig{nodes: []node{{dns: "dns-1", ip: "ip-1", port: 11211}, {dns: "dns-2", ip: "ip-2", port: 8080}}, version: 0}, 27 }, 28 } 29 30 for _, testCase := range testCases { 31 reader := bufio.NewReader(strings.NewReader(testCase.content)) 32 33 config, err := resolver.parseConfig(reader) 34 35 testutil.Ok(t, err) 36 testutil.Equals(t, testCase.config, *config) 37 } 38 } 39 40 func TestBadClusterConfigs(t *testing.T) { 41 resolver := memcachedAutoDiscovery{} 42 testCases := []struct { 43 content string 44 expectedErr error 45 }{ 46 {"", 47 errors.New("failed to read config metadata: EOF"), 48 }, 49 {"CONFIG cluster\r\n", 50 errors.New("expected 4 components in config metadata, and received 2, meta: CONFIG cluster"), 51 }, 52 {"CONFIG cluster 0 configSize\r\n", 53 errors.New("failed to parse config size from metadata: CONFIG cluster 0 configSize, error: strconv.Atoi: parsing \"configSize\": invalid syntax"), 54 }, 55 {"CONFIG cluster 0 100\r\n", 56 errors.New("failed to find config version: EOF"), 57 }, 58 {"CONFIG cluster 0 100\r\nconfigVersion\r\n", 59 errors.New("failed to parser config version: strconv.Atoi: parsing \"configVersion\": invalid syntax"), 60 }, 61 {"CONFIG cluster 0 100\r\n0\r\n", 62 errors.New("failed to read nodes: EOF"), 63 }, 64 {"CONFIG cluster 0 0\r\n100\r\ndns-1|ip-1|11211\r\nEND\r\n", 65 errors.New("expected 0 in config payload, but got 23 instead."), 66 }, 67 {"CONFIG cluster 0 17\r\n100\r\ndns-1|ip-1\r\nEND\r\n", 68 errors.New("node not in expected format: [dns-1 ip-1]"), 69 }, 70 {"CONFIG cluster 0 22\r\n100\r\ndns-1|ip-1|port\r\nEND\r\n", 71 errors.New("failed to parse port: [dns-1 ip-1 port], err: strconv.Atoi: parsing \"port\": invalid syntax"), 72 }, 73 } 74 75 for _, testCase := range testCases { 76 reader := bufio.NewReader(strings.NewReader(testCase.content)) 77 78 _, err := resolver.parseConfig(reader) 79 80 testutil.Assert(t, testCase.expectedErr.Error() == err.Error(), "expected error '%v', but got '%v'", testCase.expectedErr.Error(), err.Error()) 81 } 82 83 }