2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-07-28 16:26:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-01-06 18:40:00 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-06-30 20:58:55 +00:00
|
|
|
func TestAPI_StatusLeader(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2018-12-12 20:09:42 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
status := c.Status()
|
|
|
|
|
2020-07-29 16:09:15 +00:00
|
|
|
leader, err := status.Leader()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leader == "" {
|
|
|
|
t.Fatalf("Expected leader, found empty string")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_StatusLeaderWithQueryOptions(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
|
|
|
status := c.Status()
|
|
|
|
|
2020-07-28 16:26:50 +00:00
|
|
|
opts := QueryOptions{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
2020-07-29 16:09:15 +00:00
|
|
|
leader, err := status.LeaderWithQueryOptions(&opts)
|
2015-01-06 18:40:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leader == "" {
|
2020-07-29 16:09:15 +00:00
|
|
|
t.Fatalf("Expected leader, found empty string")
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 20:58:55 +00:00
|
|
|
func TestAPI_StatusPeers(t *testing.T) {
|
2015-05-08 17:27:24 +00:00
|
|
|
t.Parallel()
|
2015-01-06 23:26:50 +00:00
|
|
|
c, s := makeClient(t)
|
2015-03-03 02:18:38 +00:00
|
|
|
defer s.Stop()
|
2018-12-12 20:09:42 +00:00
|
|
|
s.WaitForSerfCheck(t)
|
2015-01-06 23:26:50 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
status := c.Status()
|
|
|
|
|
2020-07-29 16:09:15 +00:00
|
|
|
peers, err := status.Peers()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(peers) == 0 {
|
|
|
|
t.Fatalf("Expected peers, found %d", len(peers))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_StatusPeersWithQueryOptions(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
|
|
|
status := c.Status()
|
|
|
|
|
2020-07-28 16:26:50 +00:00
|
|
|
opts := QueryOptions{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2020-07-29 16:09:15 +00:00
|
|
|
|
|
|
|
peers, err := status.PeersWithQueryOptions(&opts)
|
2015-01-06 18:40:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(peers) == 0 {
|
2020-07-29 16:09:15 +00:00
|
|
|
t.Fatalf("Expected peers, found %d", len(peers))
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 16:26:50 +00:00
|
|
|
|
|
|
|
func TestAPI_StatusLeader_WrongDC(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
|
|
|
status := c.Status()
|
|
|
|
|
|
|
|
opts := QueryOptions{
|
|
|
|
Datacenter: "wrong_dc1",
|
|
|
|
}
|
2020-07-29 16:09:15 +00:00
|
|
|
|
|
|
|
_, err := status.LeaderWithQueryOptions(&opts)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "No path to datacenter")
|
2020-07-28 16:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPI_StatusPeers_WrongDC(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
s.WaitForSerfCheck(t)
|
|
|
|
|
|
|
|
status := c.Status()
|
|
|
|
|
|
|
|
opts := QueryOptions{
|
|
|
|
Datacenter: "wrong_dc1",
|
|
|
|
}
|
2020-07-29 16:09:15 +00:00
|
|
|
_, err := status.PeersWithQueryOptions(&opts)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "No path to datacenter")
|
2020-07-28 16:26:50 +00:00
|
|
|
}
|