github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/xds/endpoints_test.go (about) 1 package xds 2 3 import ( 4 "testing" 5 6 "github.com/mitchellh/copystructure" 7 8 "github.com/stretchr/testify/require" 9 10 envoy "github.com/envoyproxy/go-control-plane/envoy/api/v2" 11 "github.com/envoyproxy/go-control-plane/envoy/api/v2/core" 12 envoyendpoint "github.com/envoyproxy/go-control-plane/envoy/api/v2/endpoint" 13 "github.com/hashicorp/consul/agent/structs" 14 ) 15 16 func Test_makeLoadAssignment(t *testing.T) { 17 18 testCheckServiceNodes := structs.CheckServiceNodes{ 19 structs.CheckServiceNode{ 20 Node: &structs.Node{ 21 ID: "node1-id", 22 Node: "node1", 23 Address: "10.10.10.10", 24 Datacenter: "dc1", 25 }, 26 Service: &structs.NodeService{ 27 Service: "web", 28 Port: 1234, 29 }, 30 Checks: structs.HealthChecks{ 31 &structs.HealthCheck{ 32 Node: "node1", 33 CheckID: "serfHealth", 34 Status: "passing", 35 }, 36 &structs.HealthCheck{ 37 Node: "node1", 38 ServiceID: "web", 39 CheckID: "web:check", 40 Status: "passing", 41 }, 42 }, 43 }, 44 structs.CheckServiceNode{ 45 Node: &structs.Node{ 46 ID: "node2-id", 47 Node: "node2", 48 Address: "10.10.10.20", 49 Datacenter: "dc1", 50 }, 51 Service: &structs.NodeService{ 52 Service: "web", 53 Port: 1234, 54 }, 55 Checks: structs.HealthChecks{ 56 &structs.HealthCheck{ 57 Node: "node2", 58 CheckID: "serfHealth", 59 Status: "passing", 60 }, 61 &structs.HealthCheck{ 62 Node: "node2", 63 ServiceID: "web", 64 CheckID: "web:check", 65 Status: "passing", 66 }, 67 }, 68 }, 69 } 70 71 testWeightedCheckServiceNodesRaw, err := copystructure.Copy(testCheckServiceNodes) 72 require.NoError(t, err) 73 testWeightedCheckServiceNodes := testWeightedCheckServiceNodesRaw.(structs.CheckServiceNodes) 74 75 testWeightedCheckServiceNodes[0].Service.Weights = &structs.Weights{ 76 Passing: 10, 77 Warning: 1, 78 } 79 testWeightedCheckServiceNodes[1].Service.Weights = &structs.Weights{ 80 Passing: 5, 81 Warning: 0, 82 } 83 84 testWarningCheckServiceNodesRaw, err := copystructure.Copy(testWeightedCheckServiceNodes) 85 require.NoError(t, err) 86 testWarningCheckServiceNodes := testWarningCheckServiceNodesRaw.(structs.CheckServiceNodes) 87 88 testWarningCheckServiceNodes[0].Checks[0].Status = "warning" 89 testWarningCheckServiceNodes[1].Checks[0].Status = "warning" 90 91 tests := []struct { 92 name string 93 clusterName string 94 endpoints structs.CheckServiceNodes 95 want *envoy.ClusterLoadAssignment 96 }{ 97 { 98 name: "no instances", 99 clusterName: "service:test", 100 endpoints: structs.CheckServiceNodes{}, 101 want: &envoy.ClusterLoadAssignment{ 102 ClusterName: "service:test", 103 Endpoints: []envoyendpoint.LocalityLbEndpoints{{ 104 LbEndpoints: []envoyendpoint.LbEndpoint{}, 105 }}, 106 }, 107 }, 108 { 109 name: "instances, no weights", 110 clusterName: "service:test", 111 endpoints: testCheckServiceNodes, 112 want: &envoy.ClusterLoadAssignment{ 113 ClusterName: "service:test", 114 Endpoints: []envoyendpoint.LocalityLbEndpoints{{ 115 LbEndpoints: []envoyendpoint.LbEndpoint{ 116 envoyendpoint.LbEndpoint{ 117 Endpoint: &envoyendpoint.Endpoint{ 118 Address: makeAddressPtr("10.10.10.10", 1234), 119 }, 120 HealthStatus: core.HealthStatus_HEALTHY, 121 LoadBalancingWeight: makeUint32Value(1), 122 }, 123 envoyendpoint.LbEndpoint{ 124 Endpoint: &envoyendpoint.Endpoint{ 125 Address: makeAddressPtr("10.10.10.20", 1234), 126 }, 127 HealthStatus: core.HealthStatus_HEALTHY, 128 LoadBalancingWeight: makeUint32Value(1), 129 }, 130 }, 131 }}, 132 }, 133 }, 134 { 135 name: "instances, healthy weights", 136 clusterName: "service:test", 137 endpoints: testWeightedCheckServiceNodes, 138 want: &envoy.ClusterLoadAssignment{ 139 ClusterName: "service:test", 140 Endpoints: []envoyendpoint.LocalityLbEndpoints{{ 141 LbEndpoints: []envoyendpoint.LbEndpoint{ 142 envoyendpoint.LbEndpoint{ 143 Endpoint: &envoyendpoint.Endpoint{ 144 Address: makeAddressPtr("10.10.10.10", 1234), 145 }, 146 HealthStatus: core.HealthStatus_HEALTHY, 147 LoadBalancingWeight: makeUint32Value(10), 148 }, 149 envoyendpoint.LbEndpoint{ 150 Endpoint: &envoyendpoint.Endpoint{ 151 Address: makeAddressPtr("10.10.10.20", 1234), 152 }, 153 HealthStatus: core.HealthStatus_HEALTHY, 154 LoadBalancingWeight: makeUint32Value(5), 155 }, 156 }, 157 }}, 158 }, 159 }, 160 { 161 name: "instances, warning weights", 162 clusterName: "service:test", 163 endpoints: testWarningCheckServiceNodes, 164 want: &envoy.ClusterLoadAssignment{ 165 ClusterName: "service:test", 166 Endpoints: []envoyendpoint.LocalityLbEndpoints{{ 167 LbEndpoints: []envoyendpoint.LbEndpoint{ 168 envoyendpoint.LbEndpoint{ 169 Endpoint: &envoyendpoint.Endpoint{ 170 Address: makeAddressPtr("10.10.10.10", 1234), 171 }, 172 HealthStatus: core.HealthStatus_HEALTHY, 173 LoadBalancingWeight: makeUint32Value(1), 174 }, 175 envoyendpoint.LbEndpoint{ 176 Endpoint: &envoyendpoint.Endpoint{ 177 Address: makeAddressPtr("10.10.10.20", 1234), 178 }, 179 HealthStatus: core.HealthStatus_UNHEALTHY, 180 LoadBalancingWeight: makeUint32Value(1), 181 }, 182 }, 183 }}, 184 }, 185 }, 186 } 187 for _, tt := range tests { 188 t.Run(tt.name, func(t *testing.T) { 189 got := makeLoadAssignment(tt.clusterName, tt.endpoints) 190 require.Equal(t, tt.want, got) 191 }) 192 } 193 }