2017-12-18 21:16:23 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2018-02-08 00:47:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-12-18 21:16:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPI_OperatorAutopilotGetSetConfiguration(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-02-08 00:47:44 +00:00
|
|
|
require := require.New(t)
|
2017-12-18 21:16:23 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
operator := c.Operator()
|
2018-01-30 03:53:34 +00:00
|
|
|
var config *AutopilotConfiguration
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
var err error
|
2018-02-08 00:47:44 +00:00
|
|
|
config, _, err = operator.AutopilotGetConfiguration(nil)
|
2018-01-30 03:53:34 +00:00
|
|
|
r.Check(err)
|
|
|
|
})
|
2018-02-08 00:47:44 +00:00
|
|
|
require.True(config.CleanupDeadServers)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// Change a config setting
|
|
|
|
newConf := &AutopilotConfiguration{CleanupDeadServers: false}
|
2018-02-08 00:47:44 +00:00
|
|
|
_, err := operator.AutopilotSetConfiguration(newConf, nil)
|
|
|
|
require.Nil(err)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2018-02-08 00:47:44 +00:00
|
|
|
config, _, err = operator.AutopilotGetConfiguration(nil)
|
|
|
|
require.Nil(err)
|
|
|
|
require.False(config.CleanupDeadServers)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_OperatorAutopilotCASConfiguration(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-02-08 00:47:44 +00:00
|
|
|
require := require.New(t)
|
2017-12-18 21:16:23 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
operator := c.Operator()
|
2018-02-08 00:47:44 +00:00
|
|
|
var config *AutopilotConfiguration
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
var err error
|
|
|
|
config, _, err = operator.AutopilotGetConfiguration(nil)
|
|
|
|
r.Check(err)
|
|
|
|
})
|
|
|
|
require.True(config.CleanupDeadServers)
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// Pass an invalid ModifyIndex
|
|
|
|
{
|
|
|
|
newConf := &AutopilotConfiguration{
|
|
|
|
CleanupDeadServers: false,
|
|
|
|
ModifyIndex: config.ModifyIndex - 1,
|
|
|
|
}
|
2018-02-08 00:47:44 +00:00
|
|
|
resp, _, err := operator.AutopilotCASConfiguration(newConf, nil)
|
|
|
|
require.Nil(err)
|
|
|
|
require.False(resp)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pass a valid ModifyIndex
|
|
|
|
{
|
|
|
|
newConf := &AutopilotConfiguration{
|
|
|
|
CleanupDeadServers: false,
|
|
|
|
ModifyIndex: config.ModifyIndex,
|
|
|
|
}
|
2018-02-08 00:47:44 +00:00
|
|
|
resp, _, err := operator.AutopilotCASConfiguration(newConf, nil)
|
|
|
|
require.Nil(err)
|
|
|
|
require.True(resp)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_OperatorAutopilotServerHealth(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.AdvertiseAddrs.RPC = "127.0.0.1"
|
|
|
|
c.Server.RaftProtocol = 3
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
operator := c.Operator()
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
2018-02-08 00:47:44 +00:00
|
|
|
out, _, err := operator.AutopilotServerHealth(nil)
|
2017-12-18 21:16:23 +00:00
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Servers) != 1 ||
|
|
|
|
!out.Servers[0].Healthy ||
|
|
|
|
out.Servers[0].Name != fmt.Sprintf("%s.global", s.Config.NodeName) {
|
|
|
|
r.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|