2016-10-26 02:20:24 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-08-23 14:52:48 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2019-02-20 17:36:14 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2016-10-26 02:20:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSnapshot(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-10-26 02:20:24 +00:00
|
|
|
var snap io.Reader
|
2019-02-20 17:36:14 +00:00
|
|
|
t.Run("create snapshot", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2019-02-20 17:36:14 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/snapshot?token=root", body)
|
2016-10-26 02:20:24 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
if _, err := a.srv.Snapshot(resp, req); err != nil {
|
2016-10-26 02:20:24 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
snap = resp.Body
|
|
|
|
|
|
|
|
header := resp.Header().Get("X-Consul-Index")
|
|
|
|
if header == "" {
|
|
|
|
t.Fatalf("bad: %v", header)
|
|
|
|
}
|
|
|
|
header = resp.Header().Get("X-Consul-KnownLeader")
|
|
|
|
if header != "true" {
|
|
|
|
t.Fatalf("bad: %v", header)
|
|
|
|
}
|
|
|
|
header = resp.Header().Get("X-Consul-LastContact")
|
|
|
|
if header != "0" {
|
|
|
|
t.Fatalf("bad: %v", header)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-02-20 17:36:14 +00:00
|
|
|
t.Run("restore snapshot", func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
2019-02-20 17:36:14 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-21 18:31:20 +00:00
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/snapshot?token=root", snap)
|
2016-10-26 02:20:24 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
if _, err := a.srv.Snapshot(resp, req); err != nil {
|
2016-10-26 02:20:24 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnapshot_Options(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-10-26 02:20:24 +00:00
|
|
|
for _, method := range []string{"GET", "PUT"} {
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run(method, func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, TestACLConfig())
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest(method, "/v1/snapshot?token=anonymous", body)
|
2016-10-26 02:20:24 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
_, err := a.srv.Snapshot(resp, req)
|
2017-08-23 14:52:48 +00:00
|
|
|
if !acl.IsErrPermissionDenied(err) {
|
2016-10-26 02:20:24 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run(method, func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, TestACLConfig())
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest(method, "/v1/snapshot?dc=nope", body)
|
2016-10-26 02:20:24 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
_, err := a.srv.Snapshot(resp, req)
|
2016-10-26 02:20:24 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "No path to datacenter") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-05-21 18:31:20 +00:00
|
|
|
t.Run(method, func(t *testing.T) {
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, TestACLConfig())
|
2017-05-21 18:31:20 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
body := bytes.NewBuffer(nil)
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest(method, "/v1/snapshot?token=root&stale", body)
|
2016-10-26 02:20:24 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 18:31:20 +00:00
|
|
|
_, err := a.srv.Snapshot(resp, req)
|
2016-10-26 02:20:24 +00:00
|
|
|
if method == "GET" {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "stale not allowed") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|