2019-03-19 22:56:17 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-04-07 06:38:08 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2019-03-27 23:52:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-19 22:56:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_ConfigEntry(t *testing.T) {
|
2019-03-27 23:52:38 +00:00
|
|
|
require := require.New(t)
|
2019-03-19 22:56:17 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
expected := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
2019-03-27 23:52:38 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"DestinationServiceName": "foo",
|
2019-03-19 22:56:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(0, expected))
|
2019-03-19 22:56:17 +00:00
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
idx, config, err := s.ConfigEntry(nil, structs.ProxyDefaults, "global")
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(0), idx)
|
|
|
|
require.Equal(expected, config)
|
2019-03-19 22:56:17 +00:00
|
|
|
|
|
|
|
// Update
|
|
|
|
updated := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
2019-03-27 23:52:38 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"DestinationServiceName": "bar",
|
2019-03-19 22:56:17 +00:00
|
|
|
},
|
|
|
|
}
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(1, updated))
|
2019-03-19 22:56:17 +00:00
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
idx, config, err = s.ConfigEntry(nil, structs.ProxyDefaults, "global")
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(1), idx)
|
|
|
|
require.Equal(updated, config)
|
2019-03-19 22:56:17 +00:00
|
|
|
|
|
|
|
// Delete
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(s.DeleteConfigEntry(2, structs.ProxyDefaults, "global"))
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
idx, config, err = s.ConfigEntry(nil, structs.ProxyDefaults, "global")
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(2), idx)
|
|
|
|
require.Nil(config)
|
2019-04-07 06:38:08 +00:00
|
|
|
|
|
|
|
// Set up a watch.
|
|
|
|
serviceConf := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "foo",
|
|
|
|
}
|
|
|
|
require.NoError(s.EnsureConfigEntry(3, serviceConf))
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
_, _, err = s.ConfigEntry(ws, structs.ServiceDefaults, "foo")
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Make an unrelated modification and make sure the watch doesn't fire.
|
|
|
|
require.NoError(s.EnsureConfigEntry(4, updated))
|
|
|
|
require.False(watchFired(ws))
|
|
|
|
|
|
|
|
// Update the watched config and make sure it fires.
|
|
|
|
serviceConf.Protocol = "http"
|
|
|
|
require.NoError(s.EnsureConfigEntry(5, serviceConf))
|
|
|
|
require.True(watchFired(ws))
|
2019-03-27 23:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_ConfigEntryCAS(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
expected := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"DestinationServiceName": "foo",
|
|
|
|
},
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 23:52:38 +00:00
|
|
|
// Create
|
|
|
|
require.NoError(s.EnsureConfigEntry(1, expected))
|
|
|
|
|
2019-04-07 06:38:08 +00:00
|
|
|
idx, config, err := s.ConfigEntry(nil, structs.ProxyDefaults, "global")
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(1), idx)
|
|
|
|
require.Equal(expected, config)
|
|
|
|
|
|
|
|
// Update with invalid index
|
|
|
|
updated := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"DestinationServiceName": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ok, err := s.EnsureConfigEntryCAS(2, 99, updated)
|
|
|
|
require.False(ok)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Entry should not be changed
|
2019-04-07 06:38:08 +00:00
|
|
|
idx, config, err = s.ConfigEntry(nil, structs.ProxyDefaults, "global")
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(1), idx)
|
|
|
|
require.Equal(expected, config)
|
|
|
|
|
|
|
|
// Update with a valid index
|
|
|
|
ok, err = s.EnsureConfigEntryCAS(2, 1, updated)
|
|
|
|
require.True(ok)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Entry should be updated
|
2019-04-07 06:38:08 +00:00
|
|
|
idx, config, err = s.ConfigEntry(nil, structs.ProxyDefaults, "global")
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(uint64(2), idx)
|
|
|
|
require.Equal(updated, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_ConfigEntries(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
entry1 := &structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "test1",
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
2019-03-27 23:52:38 +00:00
|
|
|
entry2 := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "test2",
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|
2019-04-07 06:38:08 +00:00
|
|
|
entry3 := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "test3",
|
|
|
|
}
|
2019-03-27 23:52:38 +00:00
|
|
|
|
|
|
|
require.NoError(s.EnsureConfigEntry(0, entry1))
|
|
|
|
require.NoError(s.EnsureConfigEntry(1, entry2))
|
2019-04-07 06:38:08 +00:00
|
|
|
require.NoError(s.EnsureConfigEntry(2, entry3))
|
2019-03-27 23:52:38 +00:00
|
|
|
|
|
|
|
// Get all entries
|
2019-04-07 06:38:08 +00:00
|
|
|
idx, entries, err := s.ConfigEntries(nil)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
2019-04-07 06:38:08 +00:00
|
|
|
require.Equal(uint64(2), idx)
|
|
|
|
require.Equal([]structs.ConfigEntry{entry1, entry2, entry3}, entries)
|
2019-03-27 23:52:38 +00:00
|
|
|
|
|
|
|
// Get all proxy entries
|
2019-04-07 06:38:08 +00:00
|
|
|
idx, entries, err = s.ConfigEntriesByKind(nil, structs.ProxyDefaults)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
2019-04-07 06:38:08 +00:00
|
|
|
require.Equal(uint64(2), idx)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.Equal([]structs.ConfigEntry{entry1}, entries)
|
|
|
|
|
|
|
|
// Get all service entries
|
2019-04-07 06:38:08 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
idx, entries, err = s.ConfigEntriesByKind(ws, structs.ServiceDefaults)
|
2019-03-27 23:52:38 +00:00
|
|
|
require.NoError(err)
|
2019-04-07 06:38:08 +00:00
|
|
|
require.Equal(uint64(2), idx)
|
|
|
|
require.Equal([]structs.ConfigEntry{entry2, entry3}, entries)
|
|
|
|
|
|
|
|
// Watch should not have fired
|
|
|
|
require.False(watchFired(ws))
|
|
|
|
|
|
|
|
// Now make an update and make sure the watch fires.
|
|
|
|
require.NoError(s.EnsureConfigEntry(3, &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "test2",
|
|
|
|
Protocol: "tcp",
|
|
|
|
}))
|
|
|
|
require.True(watchFired(ws))
|
2019-03-19 22:56:17 +00:00
|
|
|
}
|