2017-02-10 01:58:20 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-21 14:05:04 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2018-01-16 21:35:32 +00:00
|
|
|
"fmt"
|
2020-05-21 14:05:04 +00:00
|
|
|
"io"
|
2017-02-10 01:58:20 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-05-21 14:05:04 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-06-04 21:14:43 +00:00
|
|
|
"path/filepath"
|
2017-02-10 01:58:20 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-12-18 21:16:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-06-04 21:14:43 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2017-02-10 01:58:20 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2023-01-02 14:06:20 +00:00
|
|
|
"github.com/shoenig/test/must"
|
|
|
|
"github.com/shoenig/test/wait"
|
2018-01-16 21:35:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-28 04:27:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-02-10 01:58:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTP_OperatorRaftConfiguration(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2017-02-10 01:58:20 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
req, err := http.NewRequest("GET", "/v1/operator/raft/configuration", body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := s.Server.OperatorRaftConfiguration(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("bad code: %d", resp.Code)
|
|
|
|
}
|
|
|
|
out, ok := obj.(structs.RaftConfigurationResponse)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected: %T", obj)
|
|
|
|
}
|
|
|
|
if len(out.Servers) != 1 ||
|
|
|
|
!out.Servers[0].Leader ||
|
|
|
|
!out.Servers[0].Voter {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP_OperatorRaftPeer(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-16 21:35:32 +00:00
|
|
|
assert := assert.New(t)
|
2017-07-20 05:14:36 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2017-02-10 01:58:20 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/operator/raft/peer?address=nope", body)
|
2018-01-16 21:35:32 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
// If we get this error, it proves we sent the address all the
|
|
|
|
// way through.
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.OperatorRaftPeer(resp, req)
|
|
|
|
if err == nil || !strings.Contains(err.Error(),
|
|
|
|
"address \"nope\" was not found in the Raft configuration") {
|
2017-02-10 01:58:20 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-16 21:35:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/operator/raft/peer?id=nope", body)
|
|
|
|
assert.Nil(err)
|
2017-02-10 01:58:20 +00:00
|
|
|
|
|
|
|
// If we get this error, it proves we sent the address all the
|
|
|
|
// way through.
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.OperatorRaftPeer(resp, req)
|
|
|
|
if err == nil || !strings.Contains(err.Error(),
|
2018-01-16 21:35:32 +00:00
|
|
|
"id \"nope\" was not found in the Raft configuration") {
|
2017-02-10 01:58:20 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
func TestOperator_AutopilotGetConfiguration(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-12-18 21:16:23 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/configuration", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := s.Server.OperatorAutopilotConfiguration(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("bad code: %d", resp.Code)
|
|
|
|
}
|
|
|
|
out, ok := obj.(api.AutopilotConfiguration)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected: %T", obj)
|
|
|
|
}
|
|
|
|
if !out.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_AutopilotSetConfiguration(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-12-18 21:16:23 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
body := bytes.NewBuffer([]byte(`{"CleanupDeadServers": false}`))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/operator/autopilot/configuration", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := s.Server.OperatorAutopilotConfiguration(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
2018-01-30 03:53:34 +00:00
|
|
|
t.Fatalf("bad code: %d, %q", resp.Code, resp.Body.String())
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.GenericRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: s.Config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
var reply structs.AutopilotConfig
|
2017-12-18 21:16:23 +00:00
|
|
|
if err := s.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if reply.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", reply)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_AutopilotCASConfiguration(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-12-18 21:16:23 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
body := bytes.NewBuffer([]byte(`{"CleanupDeadServers": false}`))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/operator/autopilot/configuration", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
if _, err := s.Server.OperatorAutopilotConfiguration(resp, req); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf("bad code: %d", resp.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.GenericRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: s.Config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
var reply structs.AutopilotConfig
|
2017-12-18 21:16:23 +00:00
|
|
|
if err := s.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reply.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a CAS request, bad index
|
|
|
|
{
|
|
|
|
buf := bytes.NewBuffer([]byte(`{"CleanupDeadServers": true}`))
|
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/operator/autopilot/configuration?cas=%d", reply.ModifyIndex-1), buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := s.Server.OperatorAutopilotConfiguration(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); res {
|
|
|
|
t.Fatalf("should NOT work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a CAS request, good index
|
|
|
|
{
|
|
|
|
buf := bytes.NewBuffer([]byte(`{"CleanupDeadServers": true}`))
|
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/operator/autopilot/configuration?cas=%d", reply.ModifyIndex), buf)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := s.Server.OperatorAutopilotConfiguration(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res := obj.(bool); !res {
|
|
|
|
t.Fatalf("should work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the update
|
|
|
|
if err := s.RPC("Operator.AutopilotGetConfiguration", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !reply.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %#v", reply)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_ServerHealth(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.RaftProtocol = 3
|
|
|
|
}, func(s *TestAgent) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/health", body)
|
2023-01-02 14:06:20 +00:00
|
|
|
f := func() error {
|
2017-12-18 21:16:23 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := s.Server.OperatorServerHealth(resp, req)
|
|
|
|
if err != nil {
|
2023-01-02 14:06:20 +00:00
|
|
|
return fmt.Errorf("failed to get operator server health: %w", err)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2023-01-02 14:06:20 +00:00
|
|
|
if code := resp.Code; code != 200 {
|
|
|
|
return fmt.Errorf("response code not 200, got: %d", code)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2023-01-02 14:06:20 +00:00
|
|
|
out := obj.(*api.OperatorHealthReply)
|
|
|
|
if n := len(out.Servers); n != 1 {
|
|
|
|
return fmt.Errorf("expected 1 server, got: %d", n)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2023-01-02 14:06:20 +00:00
|
|
|
s1, s2 := out.Servers[0].Name, s.server.LocalMember().Name
|
|
|
|
if s1 != s2 {
|
|
|
|
return fmt.Errorf("expected server names to match, got %s and %s", s1, s2)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2023-01-02 14:06:20 +00:00
|
|
|
if out.Servers[0].SerfStatus != "alive" {
|
|
|
|
return fmt.Errorf("expected serf status to be alive, got: %s", out.Servers[0].SerfStatus)
|
|
|
|
}
|
|
|
|
if out.FailureTolerance != 0 {
|
|
|
|
return fmt.Errorf("expected failure tolerance of 0, got: %d", out.FailureTolerance)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
must.Wait(t, wait.InitialSuccess(
|
|
|
|
wait.ErrorFunc(f),
|
|
|
|
wait.Timeout(10*time.Second),
|
|
|
|
wait.Gap(1*time.Second),
|
|
|
|
))
|
2017-12-18 21:16:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_ServerHealth_Unhealthy(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-12-18 21:16:23 +00:00
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.RaftProtocol = 3
|
|
|
|
c.Autopilot.LastContactThreshold = -1 * time.Second
|
|
|
|
}, func(s *TestAgent) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/autopilot/health", body)
|
2023-01-02 14:06:20 +00:00
|
|
|
f := func() error {
|
2017-12-18 21:16:23 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := s.Server.OperatorServerHealth(resp, req)
|
|
|
|
if err != nil {
|
2023-01-02 14:06:20 +00:00
|
|
|
return fmt.Errorf("failed to get operator server health: %w", err)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2023-01-02 14:06:20 +00:00
|
|
|
if code := resp.Code; code != 429 {
|
|
|
|
return fmt.Errorf("expected code 429, got: %d", code)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2023-01-02 14:06:20 +00:00
|
|
|
out := obj.(*api.OperatorHealthReply)
|
|
|
|
if n := len(out.Servers); n != 1 {
|
|
|
|
return fmt.Errorf("expected 1 server, got: %d", n)
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2023-01-02 14:06:20 +00:00
|
|
|
if out.Healthy {
|
|
|
|
return fmt.Errorf("expected server to be unhealthy")
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2023-01-02 14:06:20 +00:00
|
|
|
s1, s2 := out.Servers[0].Name, s.server.LocalMember().Name
|
|
|
|
if s1 != s2 {
|
|
|
|
return fmt.Errorf("expected server names to match, got %s and %s", s1, s2)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
must.Wait(t, wait.InitialSuccess(
|
|
|
|
wait.ErrorFunc(f),
|
|
|
|
wait.Timeout(10*time.Second),
|
|
|
|
wait.Gap(1*time.Second),
|
|
|
|
))
|
2017-12-18 21:16:23 +00:00
|
|
|
})
|
|
|
|
}
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
func TestOperator_SchedulerGetConfiguration(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-28 04:27:38 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
body := bytes.NewBuffer(nil)
|
2018-11-12 21:57:45 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/scheduler/configuration", body)
|
2018-09-28 04:27:38 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := s.Server.OperatorSchedulerConfiguration(resp, req)
|
2022-07-06 14:13:48 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code)
|
2021-11-02 21:42:52 +00:00
|
|
|
out, ok := obj.(structs.SchedulerConfigurationResponse)
|
2022-07-06 14:13:48 +00:00
|
|
|
require.True(t, ok)
|
2019-06-04 22:41:40 +00:00
|
|
|
|
|
|
|
// Only system jobs can preempt other jobs by default.
|
2022-07-06 14:13:48 +00:00
|
|
|
require.True(t, out.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled)
|
|
|
|
require.False(t, out.SchedulerConfig.PreemptionConfig.SysBatchSchedulerEnabled)
|
|
|
|
require.False(t, out.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled)
|
|
|
|
require.False(t, out.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled)
|
|
|
|
require.False(t, out.SchedulerConfig.MemoryOversubscriptionEnabled)
|
|
|
|
require.False(t, out.SchedulerConfig.PauseEvalBroker)
|
2018-09-28 04:27:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_SchedulerSetConfiguration(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-28 04:27:38 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
2021-05-03 02:53:53 +00:00
|
|
|
body := bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"MemoryOversubscriptionEnabled": true,
|
2022-07-06 14:13:48 +00:00
|
|
|
"PauseEvalBroker": true,
|
2021-05-03 02:53:53 +00:00
|
|
|
"PreemptionConfig": {
|
|
|
|
"SystemSchedulerEnabled": true,
|
|
|
|
"ServiceSchedulerEnabled": true
|
|
|
|
}
|
|
|
|
}`))
|
2018-11-12 21:57:45 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/operator/scheduler/configuration", body)
|
2018-09-28 04:27:38 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2018-11-10 16:31:10 +00:00
|
|
|
setResp, err := s.Server.OperatorSchedulerConfiguration(resp, req)
|
2022-07-06 14:13:48 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code)
|
2018-11-10 16:31:10 +00:00
|
|
|
schedSetResp, ok := setResp.(structs.SchedulerSetConfigurationResponse)
|
2022-07-06 14:13:48 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
require.NotZero(t, schedSetResp.Index)
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
args := structs.GenericRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: s.Config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:10:46 +00:00
|
|
|
var reply structs.SchedulerConfigurationResponse
|
2018-09-28 04:27:38 +00:00
|
|
|
err = s.RPC("Operator.SchedulerGetConfiguration", &args, &reply)
|
2022-07-06 14:13:48 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.True(t, reply.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled)
|
|
|
|
require.False(t, reply.SchedulerConfig.PreemptionConfig.SysBatchSchedulerEnabled)
|
|
|
|
require.False(t, reply.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled)
|
|
|
|
require.True(t, reply.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled)
|
|
|
|
require.True(t, reply.SchedulerConfig.MemoryOversubscriptionEnabled)
|
|
|
|
require.True(t, reply.SchedulerConfig.PauseEvalBroker)
|
2018-09-28 04:27:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_SchedulerCASConfiguration(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-28 04:27:38 +00:00
|
|
|
httpTest(t, nil, func(s *TestAgent) {
|
|
|
|
require := require.New(t)
|
2018-10-01 14:26:52 +00:00
|
|
|
body := bytes.NewBuffer([]byte(`{"PreemptionConfig": {
|
2019-04-29 23:48:07 +00:00
|
|
|
"SystemSchedulerEnabled": true,
|
2020-10-09 21:31:38 +00:00
|
|
|
"SysBatchSchedulerEnabled":true,
|
2019-05-03 19:06:12 +00:00
|
|
|
"BatchSchedulerEnabled":true
|
2018-10-01 14:26:52 +00:00
|
|
|
}}`))
|
2018-11-12 21:57:45 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/operator/scheduler/configuration", body)
|
2018-09-28 04:27:38 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2018-11-10 16:31:10 +00:00
|
|
|
setResp, err := s.Server.OperatorSchedulerConfiguration(resp, req)
|
2018-09-28 04:27:38 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(200, resp.Code)
|
2018-11-10 16:31:10 +00:00
|
|
|
schedSetResp, ok := setResp.(structs.SchedulerSetConfigurationResponse)
|
|
|
|
require.True(ok)
|
|
|
|
require.NotZero(schedSetResp.Index)
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
args := structs.GenericRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: s.Config.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:10:46 +00:00
|
|
|
var reply structs.SchedulerConfigurationResponse
|
2018-09-28 04:27:38 +00:00
|
|
|
if err := s.RPC("Operator.SchedulerGetConfiguration", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-10-30 16:10:46 +00:00
|
|
|
require.True(reply.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled)
|
2020-10-09 21:31:38 +00:00
|
|
|
require.True(reply.SchedulerConfig.PreemptionConfig.SysBatchSchedulerEnabled)
|
2019-05-03 19:06:12 +00:00
|
|
|
require.True(reply.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled)
|
2020-10-09 21:31:38 +00:00
|
|
|
require.False(reply.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled)
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
// Create a CAS request, bad index
|
|
|
|
{
|
2018-10-01 14:26:52 +00:00
|
|
|
buf := bytes.NewBuffer([]byte(`{"PreemptionConfig": {
|
2019-04-29 23:48:07 +00:00
|
|
|
"SystemSchedulerEnabled": false,
|
2019-05-03 19:06:12 +00:00
|
|
|
"BatchSchedulerEnabled":true
|
2018-10-01 14:26:52 +00:00
|
|
|
}}`))
|
2018-11-12 21:57:45 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/operator/scheduler/configuration?cas=%d", reply.QueryMeta.Index-1), buf)
|
2018-09-28 04:27:38 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2018-11-10 16:31:10 +00:00
|
|
|
setResp, err := s.Server.OperatorSchedulerConfiguration(resp, req)
|
2018-09-28 04:27:38 +00:00
|
|
|
require.Nil(err)
|
2018-11-10 16:31:10 +00:00
|
|
|
// Verify that the response has Updated=false
|
|
|
|
schedSetResp, ok := setResp.(structs.SchedulerSetConfigurationResponse)
|
|
|
|
require.True(ok)
|
|
|
|
require.NotZero(schedSetResp.Index)
|
|
|
|
require.False(schedSetResp.Updated)
|
2018-09-28 04:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a CAS request, good index
|
|
|
|
{
|
2018-10-01 14:26:52 +00:00
|
|
|
buf := bytes.NewBuffer([]byte(`{"PreemptionConfig": {
|
2019-04-29 23:48:07 +00:00
|
|
|
"SystemSchedulerEnabled": false,
|
2019-05-03 19:06:12 +00:00
|
|
|
"BatchSchedulerEnabled":false
|
2018-10-01 14:26:52 +00:00
|
|
|
}}`))
|
2018-11-12 21:57:45 +00:00
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/operator/scheduler/configuration?cas=%d", reply.QueryMeta.Index), buf)
|
2018-09-28 04:27:38 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2018-11-10 16:31:10 +00:00
|
|
|
setResp, err := s.Server.OperatorSchedulerConfiguration(resp, req)
|
2018-09-28 04:27:38 +00:00
|
|
|
require.Nil(err)
|
2018-11-10 16:31:10 +00:00
|
|
|
// Verify that the response has Updated=true
|
|
|
|
schedSetResp, ok := setResp.(structs.SchedulerSetConfigurationResponse)
|
|
|
|
require.True(ok)
|
|
|
|
require.NotZero(schedSetResp.Index)
|
|
|
|
require.True(schedSetResp.Updated)
|
2018-09-28 04:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the update
|
|
|
|
if err := s.RPC("Operator.SchedulerGetConfiguration", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-10-30 16:10:46 +00:00
|
|
|
require.False(reply.SchedulerConfig.PreemptionConfig.SystemSchedulerEnabled)
|
2020-10-09 21:31:38 +00:00
|
|
|
require.False(reply.SchedulerConfig.PreemptionConfig.SysBatchSchedulerEnabled)
|
2019-05-03 19:06:12 +00:00
|
|
|
require.False(reply.SchedulerConfig.PreemptionConfig.BatchSchedulerEnabled)
|
2020-10-09 21:31:38 +00:00
|
|
|
require.False(reply.SchedulerConfig.PreemptionConfig.ServiceSchedulerEnabled)
|
2018-09-28 04:27:38 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-21 14:05:04 +00:00
|
|
|
|
2020-06-04 21:14:43 +00:00
|
|
|
func TestOperator_SnapshotRequests(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-05-21 14:05:04 +00:00
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
dir := t.TempDir()
|
2020-05-21 14:05:04 +00:00
|
|
|
|
2020-06-04 21:14:43 +00:00
|
|
|
snapshotPath := filepath.Join(dir, "snapshot.bin")
|
|
|
|
job := mock.Job()
|
|
|
|
|
|
|
|
// test snapshot generation
|
2020-05-21 14:05:04 +00:00
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.BootstrapExpect = 1
|
|
|
|
c.DevMode = false
|
|
|
|
c.DataDir = path.Join(dir, "server")
|
|
|
|
c.AdvertiseAddrs.HTTP = "127.0.0.1"
|
|
|
|
c.AdvertiseAddrs.RPC = "127.0.0.1"
|
|
|
|
c.AdvertiseAddrs.Serf = "127.0.0.1"
|
2020-06-04 21:14:43 +00:00
|
|
|
|
|
|
|
// don't actually run the job
|
|
|
|
c.Client.Enabled = false
|
2020-05-21 14:05:04 +00:00
|
|
|
}, func(s *TestAgent) {
|
2020-06-04 21:14:43 +00:00
|
|
|
// make a simple update
|
|
|
|
jargs := structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var jresp structs.JobRegisterResponse
|
|
|
|
err := s.Agent.RPC("Job.Register", &jargs, &jresp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// now actually snapshot
|
2020-05-21 14:05:04 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/operator/snapshot", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2020-06-04 21:14:43 +00:00
|
|
|
_, err = s.Server.SnapshotRequest(resp, req)
|
2020-05-21 14:05:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code)
|
|
|
|
|
|
|
|
digest := resp.Header().Get("Digest")
|
|
|
|
require.NotEmpty(t, digest)
|
|
|
|
require.Contains(t, digest, "sha-256=")
|
|
|
|
|
|
|
|
hash := sha256.New()
|
2020-06-04 21:14:43 +00:00
|
|
|
f, err := os.Create(snapshotPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(io.MultiWriter(f, hash), resp.Body)
|
2020-05-21 14:05:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedChecksum := "sha-256=" + base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
|
|
|
require.Equal(t, digest, expectedChecksum)
|
|
|
|
})
|
|
|
|
|
2020-06-04 21:14:43 +00:00
|
|
|
// test snapshot restoration
|
|
|
|
httpTest(t, func(c *Config) {
|
|
|
|
c.Server.BootstrapExpect = 1
|
|
|
|
c.DevMode = false
|
|
|
|
c.DataDir = path.Join(dir, "server2")
|
|
|
|
c.AdvertiseAddrs.HTTP = "127.0.0.1"
|
|
|
|
c.AdvertiseAddrs.RPC = "127.0.0.1"
|
|
|
|
c.AdvertiseAddrs.Serf = "127.0.0.1"
|
|
|
|
|
|
|
|
// don't actually run the job
|
|
|
|
c.Client.Enabled = false
|
|
|
|
}, func(s *TestAgent) {
|
|
|
|
jobExists := func() bool {
|
|
|
|
// check job isn't present
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/job/"+job.ID, nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
j, _ := s.Server.jobCRUD(resp, req, job.ID)
|
|
|
|
return j != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// job doesn't get initially
|
|
|
|
require.False(t, jobExists())
|
|
|
|
|
|
|
|
// restrore and check if job exists after
|
|
|
|
f, err := os.Open(snapshotPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/operator/snapshot", f)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = s.Server.SnapshotRequest(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code)
|
|
|
|
|
|
|
|
require.True(t, jobExists())
|
|
|
|
})
|
2020-05-21 14:05:04 +00:00
|
|
|
}
|