github.com/xmidt-org/webpa-common@v1.11.9/service/consul/instancer_test.go (about) 1 package consul 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/hashicorp/consul/api" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 // newServiceEntry creates a consul ServiceEntry with a service address 12 func newServiceEntry(serviceAddress string, port int, tags ...string) *api.ServiceEntry { 13 return &api.ServiceEntry{ 14 Node: &api.Node{}, 15 Service: &api.AgentService{ 16 Address: serviceAddress, 17 Port: port, 18 Tags: tags, 19 }, 20 } 21 } 22 23 // newServiceEntryNode creates a consul service entry with a node address 24 func newServiceEntryNode(nodeAddress string, port int, tags ...string) *api.ServiceEntry { 25 return &api.ServiceEntry{ 26 Node: &api.Node{ 27 Address: nodeAddress, 28 }, 29 Service: &api.AgentService{ 30 Port: port, 31 Tags: tags, 32 }, 33 } 34 } 35 36 func testFilterEntriesNil(t *testing.T) { 37 var ( 38 assert = assert.New(t) 39 entries []*api.ServiceEntry 40 filtered = filterEntries(entries, nil) 41 ) 42 43 assert.Len(filtered, 0) 44 } 45 46 func testFilterEntries(t *testing.T, unfiltered []*api.ServiceEntry, tags []string, expected []*api.ServiceEntry) { 47 var ( 48 assert = assert.New(t) 49 filtered = filterEntries(unfiltered, tags) 50 ) 51 52 assert.Equal(expected, filtered) 53 } 54 55 func TestFilterEntries(t *testing.T) { 56 t.Run("Nil", testFilterEntriesNil) 57 58 testData := []struct { 59 unfiltered []*api.ServiceEntry 60 tags []string 61 expected []*api.ServiceEntry 62 }{ 63 { 64 []*api.ServiceEntry{ 65 newServiceEntry("service1.com", 8080, "foo", "bar"), 66 newServiceEntry("service2.com", 1234), 67 newServiceEntryNode("node1.com", 9090, "bar"), 68 }, 69 []string{"foo"}, 70 []*api.ServiceEntry{ 71 newServiceEntry("service1.com", 8080, "foo", "bar"), 72 }, 73 }, 74 { 75 []*api.ServiceEntry{ 76 newServiceEntry("service1.com", 9567, "foo", "bar"), 77 newServiceEntry("service2.com", 1111), 78 newServiceEntryNode("node1.com", 2222, "bar"), 79 newServiceEntryNode("node2.com", 671, "bar", "foo"), 80 newServiceEntryNode("node3.com", 772, "bar", "foo", "moo"), 81 newServiceEntry("service3.com", 12560, "moo", "foo", "bar"), 82 }, 83 []string{"foo", "bar"}, 84 []*api.ServiceEntry{ 85 newServiceEntry("service1.com", 9567, "foo", "bar"), 86 newServiceEntryNode("node2.com", 671, "bar", "foo"), 87 newServiceEntryNode("node3.com", 772, "bar", "foo", "moo"), 88 newServiceEntry("service3.com", 12560, "moo", "foo", "bar"), 89 }, 90 }, 91 } 92 93 for i, record := range testData { 94 t.Run(strconv.Itoa(i), func(t *testing.T) { 95 testFilterEntries(t, record.unfiltered, record.tags, record.expected) 96 }) 97 } 98 } 99 100 func TestMakeInstances(t *testing.T) { 101 testData := []struct { 102 entries []*api.ServiceEntry 103 expected []string 104 }{ 105 { 106 []*api.ServiceEntry{ 107 newServiceEntry("service1.com", 4343, "foo", "bar"), 108 newServiceEntry("service2.com", 1717), 109 newServiceEntryNode("node1.com", 901, "bar"), 110 }, 111 []string{"service1.com:4343", "service2.com:1717", "node1.com:901"}, 112 }, 113 { 114 []*api.ServiceEntry{ 115 newServiceEntry("service1.com", 8080, "foo", "bar"), 116 newServiceEntry("service2.com", 9090), 117 newServiceEntryNode("node1.com", 16721, "bar"), 118 newServiceEntryNode("node2.com", 6, "bar", "foo"), 119 newServiceEntryNode("node3.com", 916, "bar", "foo", "moo"), 120 newServiceEntry("service3.com", 99, "moo", "foo", "bar"), 121 }, 122 []string{"service1.com:8080", "service2.com:9090", "node1.com:16721", "node2.com:6", "node3.com:916", "service3.com:99"}, 123 }, 124 } 125 126 for i, record := range testData { 127 t.Run(strconv.Itoa(i), func(t *testing.T) { 128 assert := assert.New(t) 129 assert.Equal(record.expected, makeInstances(record.entries)) 130 }) 131 } 132 }