github.imxd.top/hashicorp/consul@v1.4.5/agent/proxycfg/state_test.go (about) 1 package proxycfg 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 8 "github.com/hashicorp/consul/agent/structs" 9 ) 10 11 func TestStateChanged(t *testing.T) { 12 tests := []struct { 13 name string 14 ns *structs.NodeService 15 token string 16 mutate func(ns structs.NodeService, token string) (*structs.NodeService, string) 17 want bool 18 }{ 19 { 20 name: "nil node service", 21 ns: structs.TestNodeServiceProxy(t), 22 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 23 return nil, token 24 }, 25 want: true, 26 }, 27 { 28 name: "same service", 29 ns: structs.TestNodeServiceProxy(t), 30 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 31 return &ns, token 32 }, want: false, 33 }, 34 { 35 name: "same service, different token", 36 ns: structs.TestNodeServiceProxy(t), 37 token: "foo", 38 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 39 return &ns, "bar" 40 }, 41 want: true, 42 }, 43 { 44 name: "different service ID", 45 ns: structs.TestNodeServiceProxy(t), 46 token: "foo", 47 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 48 ns.ID = "badger" 49 return &ns, token 50 }, 51 want: true, 52 }, 53 { 54 name: "different address", 55 ns: structs.TestNodeServiceProxy(t), 56 token: "foo", 57 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 58 ns.Address = "10.10.10.10" 59 return &ns, token 60 }, 61 want: true, 62 }, 63 { 64 name: "different port", 65 ns: structs.TestNodeServiceProxy(t), 66 token: "foo", 67 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 68 ns.Port = 12345 69 return &ns, token 70 }, 71 want: true, 72 }, 73 { 74 name: "different service kind", 75 ns: structs.TestNodeServiceProxy(t), 76 token: "foo", 77 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 78 ns.Kind = "" 79 return &ns, token 80 }, 81 want: true, 82 }, 83 { 84 name: "different proxy target", 85 ns: structs.TestNodeServiceProxy(t), 86 token: "foo", 87 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 88 ns.Proxy.DestinationServiceName = "badger" 89 return &ns, token 90 }, 91 want: true, 92 }, 93 { 94 name: "different proxy upstreams", 95 ns: structs.TestNodeServiceProxy(t), 96 token: "foo", 97 mutate: func(ns structs.NodeService, token string) (*structs.NodeService, string) { 98 ns.Proxy.Upstreams = nil 99 return &ns, token 100 }, 101 want: true, 102 }, 103 } 104 105 for _, tt := range tests { 106 t.Run(tt.name, func(t *testing.T) { 107 require := require.New(t) 108 state, err := newState(tt.ns, tt.token) 109 require.NoError(err) 110 otherNS, otherToken := tt.mutate(*tt.ns, tt.token) 111 require.Equal(tt.want, state.Changed(otherNS, otherToken)) 112 }) 113 } 114 }