2015-08-20 22:25:09 +00:00
|
|
|
package client
|
|
|
|
|
2015-08-20 23:12:28 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-09-12 18:47:44 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-08-20 23:12:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-02-08 05:22:48 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2015-08-25 23:21:29 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-06-11 20:33:18 +00:00
|
|
|
consulApi "github.com/hashicorp/nomad/client/consul"
|
2018-01-26 19:31:37 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver"
|
2016-06-08 06:02:37 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/consul"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2015-08-20 23:12:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad"
|
2015-08-29 21:22:24 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-08-21 00:49:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-04-06 03:50:35 +00:00
|
|
|
nconfig "github.com/hashicorp/nomad/nomad/structs/config"
|
2015-08-20 23:12:28 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2017-09-01 19:41:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-09-23 00:10:03 +00:00
|
|
|
|
2015-09-23 01:48:42 +00:00
|
|
|
ctestutil "github.com/hashicorp/nomad/client/testutil"
|
2015-08-20 23:12:28 +00:00
|
|
|
)
|
|
|
|
|
2017-08-22 00:31:32 +00:00
|
|
|
func testACLServer(t *testing.T, cb func(*nomad.Config)) (*nomad.Server, string, *structs.ACLToken) {
|
2018-01-12 01:00:30 +00:00
|
|
|
server, token := nomad.TestACLServer(t, cb)
|
|
|
|
return server, server.GetConfig().RPCAddr.String(), token
|
2017-08-22 00:31:32 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 23:12:28 +00:00
|
|
|
func testServer(t *testing.T, cb func(*nomad.Config)) (*nomad.Server, string) {
|
2018-01-12 01:00:30 +00:00
|
|
|
server := nomad.TestServer(t, cb)
|
|
|
|
return server, server.GetConfig().RPCAddr.String()
|
2015-08-20 22:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_StartStop(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2018-01-12 01:00:30 +00:00
|
|
|
client := TestClient(t, nil)
|
2015-08-20 22:25:09 +00:00
|
|
|
if err := client.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-08-20 23:12:28 +00:00
|
|
|
|
2017-09-26 22:26:33 +00:00
|
|
|
// Certain labels for metrics are dependant on client initial setup. This tests
|
2017-09-01 19:41:07 +00:00
|
|
|
// that the client has properly initialized before we assign values to labels
|
|
|
|
func TestClient_BaseLabels(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
client := TestClient(t, nil)
|
2017-09-01 19:41:07 +00:00
|
|
|
if err := client.Shutdown(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-09-05 14:27:28 +00:00
|
|
|
// directly invoke this function, as otherwise this will fail on a CI build
|
|
|
|
// due to a race condition
|
|
|
|
client.emitStats()
|
|
|
|
|
2017-09-04 02:56:47 +00:00
|
|
|
baseLabels := client.baseLabels
|
|
|
|
assert.NotEqual(0, len(baseLabels))
|
|
|
|
|
2017-09-01 19:41:07 +00:00
|
|
|
nodeID := client.Node().ID
|
2017-09-04 02:56:47 +00:00
|
|
|
for _, e := range baseLabels {
|
2017-09-01 19:41:07 +00:00
|
|
|
if e.Name == "node_id" {
|
|
|
|
assert.Equal(nodeID, e.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:12:28 +00:00
|
|
|
func TestClient_RPC(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2015-08-20 23:12:28 +00:00
|
|
|
s1, addr := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2015-08-20 23:12:28 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// RPC should succeed
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var out struct{}
|
|
|
|
err := c1.RPC("Status.Ping", struct{}{}, &out)
|
|
|
|
return err == nil, err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2015-08-20 23:13:05 +00:00
|
|
|
|
2018-04-04 01:05:28 +00:00
|
|
|
func TestClient_RPC_FireRetryWatchers(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
s1, addr := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
|
|
|
c.Servers = []string{addr}
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
watcher := c1.rpcRetryWatcher()
|
|
|
|
|
|
|
|
// RPC should succeed
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var out struct{}
|
|
|
|
err := c1.RPC("Status.Ping", struct{}{}, &out)
|
|
|
|
return err == nil, err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-watcher:
|
|
|
|
default:
|
|
|
|
t.Fatal("watcher should be fired")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:13:05 +00:00
|
|
|
func TestClient_RPC_Passthrough(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2015-08-20 23:13:05 +00:00
|
|
|
s1, _ := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2015-08-20 23:13:05 +00:00
|
|
|
c.RPCHandler = s1
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
// RPC should succeed
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var out struct{}
|
|
|
|
err := c1.RPC("Status.Ping", struct{}{}, &out)
|
|
|
|
return err == nil, err
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2015-08-20 23:41:29 +00:00
|
|
|
|
|
|
|
func TestClient_Fingerprint(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2018-03-27 22:51:54 +00:00
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c := TestClient(t, nil)
|
2015-08-20 23:41:29 +00:00
|
|
|
defer c.Shutdown()
|
|
|
|
|
2018-04-12 21:29:30 +00:00
|
|
|
// Ensure we are fingerprinting
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
node := c.Node()
|
|
|
|
if _, ok := node.Attributes["kernel.name"]; !ok {
|
|
|
|
return false, fmt.Errorf("Expected value for kernel.name")
|
|
|
|
}
|
|
|
|
if _, ok := node.Attributes["cpu.arch"]; !ok {
|
|
|
|
return false, fmt.Errorf("Expected value for cpu.arch")
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
2015-08-20 23:41:29 +00:00
|
|
|
}
|
2015-08-20 23:53:43 +00:00
|
|
|
|
2018-01-26 19:31:37 +00:00
|
|
|
func TestClient_Fingerprint_Periodic(t *testing.T) {
|
2018-10-10 03:01:20 +00:00
|
|
|
t.Skip("missing mock driver plugin implementation")
|
2018-01-26 19:31:37 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2018-01-26 19:31:37 +00:00
|
|
|
c.Options = map[string]string{
|
2018-02-23 20:01:57 +00:00
|
|
|
driver.ShutdownPeriodicAfter: "true",
|
2018-04-12 21:29:30 +00:00
|
|
|
driver.ShutdownPeriodicDuration: "1",
|
2018-01-26 19:31:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
node := c1.config.Node
|
2018-03-03 01:21:13 +00:00
|
|
|
{
|
|
|
|
// Ensure the mock driver is registered on the client
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
c1.configLock.Lock()
|
2018-03-14 17:10:41 +00:00
|
|
|
defer c1.configLock.Unlock()
|
2018-04-12 21:52:50 +00:00
|
|
|
|
|
|
|
// assert that the driver is set on the node attributes
|
|
|
|
mockDriverInfoAttr := node.Attributes["driver.mock_driver"]
|
|
|
|
if mockDriverInfoAttr == "" {
|
|
|
|
return false, fmt.Errorf("mock driver is empty when it should be set on the node attributes")
|
|
|
|
}
|
|
|
|
|
2018-03-03 01:21:13 +00:00
|
|
|
mockDriverInfo := node.Drivers["mock_driver"]
|
|
|
|
|
|
|
|
// assert that the Driver information for the node is also set correctly
|
|
|
|
if mockDriverInfo == nil {
|
|
|
|
return false, fmt.Errorf("mock driver is nil when it should be set on node Drivers")
|
|
|
|
}
|
|
|
|
if !mockDriverInfo.Detected {
|
2018-03-07 16:58:14 +00:00
|
|
|
return false, fmt.Errorf("mock driver should be set as detected")
|
2018-03-03 01:21:13 +00:00
|
|
|
}
|
|
|
|
if !mockDriverInfo.Healthy {
|
|
|
|
return false, fmt.Errorf("mock driver should be set as healthy")
|
|
|
|
}
|
|
|
|
if mockDriverInfo.HealthDescription == "" {
|
|
|
|
return false, fmt.Errorf("mock driver description should not be empty")
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2018-01-26 19:31:37 +00:00
|
|
|
|
2018-03-03 01:21:13 +00:00
|
|
|
{
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
c1.configLock.Lock()
|
2018-03-14 17:10:41 +00:00
|
|
|
defer c1.configLock.Unlock()
|
2018-03-07 16:58:14 +00:00
|
|
|
mockDriverInfo := node.Drivers["mock_driver"]
|
|
|
|
// assert that the Driver information for the node is also set correctly
|
|
|
|
if mockDriverInfo == nil {
|
|
|
|
return false, fmt.Errorf("mock driver is nil when it should be set on node Drivers")
|
|
|
|
}
|
2018-03-14 17:10:41 +00:00
|
|
|
if mockDriverInfo.Detected {
|
2018-03-07 16:58:14 +00:00
|
|
|
return false, fmt.Errorf("mock driver should be set as detected")
|
|
|
|
}
|
|
|
|
if mockDriverInfo.Healthy {
|
|
|
|
return false, fmt.Errorf("mock driver should be set as healthy")
|
|
|
|
}
|
|
|
|
if mockDriverInfo.HealthDescription == "" {
|
|
|
|
return false, fmt.Errorf("mock driver description should not be empty")
|
|
|
|
}
|
2018-03-03 01:21:13 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2018-01-26 19:31:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 03:50:35 +00:00
|
|
|
// TestClient_MixedTLS asserts that when a server is running with TLS enabled
|
|
|
|
// it will reject any RPC connections from clients that lack TLS. See #2525
|
|
|
|
func TestClient_MixedTLS(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2017-04-06 03:50:35 +00:00
|
|
|
const (
|
|
|
|
cafile = "../helper/tlsutil/testdata/ca.pem"
|
|
|
|
foocert = "../helper/tlsutil/testdata/nomad-foo.pem"
|
|
|
|
fookey = "../helper/tlsutil/testdata/nomad-foo-key.pem"
|
|
|
|
)
|
|
|
|
s1, addr := testServer(t, func(c *nomad.Config) {
|
|
|
|
c.TLSConfig = &nconfig.TLSConfig{
|
|
|
|
EnableHTTP: true,
|
|
|
|
EnableRPC: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CAFile: cafile,
|
|
|
|
CertFile: foocert,
|
|
|
|
KeyFile: fookey,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2017-04-06 03:50:35 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: c1.Node().ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
var out structs.SingleNodeResponse
|
2017-04-07 00:05:09 +00:00
|
|
|
testutil.AssertUntil(100*time.Millisecond,
|
|
|
|
func() (bool, error) {
|
|
|
|
err := c1.RPC("Node.GetNode", &req, &out)
|
|
|
|
if err == nil {
|
|
|
|
return false, fmt.Errorf("client RPC succeeded when it should have failed:\n%+v", out)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
},
|
|
|
|
)
|
2017-04-06 03:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestClient_BadTLS asserts that when a client and server are running with TLS
|
|
|
|
// enabled -- but their certificates are signed by different CAs -- they're
|
|
|
|
// unable to communicate.
|
|
|
|
func TestClient_BadTLS(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2017-04-06 03:50:35 +00:00
|
|
|
const (
|
|
|
|
cafile = "../helper/tlsutil/testdata/ca.pem"
|
|
|
|
foocert = "../helper/tlsutil/testdata/nomad-foo.pem"
|
|
|
|
fookey = "../helper/tlsutil/testdata/nomad-foo-key.pem"
|
|
|
|
badca = "../helper/tlsutil/testdata/ca-bad.pem"
|
|
|
|
badcert = "../helper/tlsutil/testdata/nomad-bad.pem"
|
|
|
|
badkey = "../helper/tlsutil/testdata/nomad-bad-key.pem"
|
|
|
|
)
|
|
|
|
s1, addr := testServer(t, func(c *nomad.Config) {
|
|
|
|
c.TLSConfig = &nconfig.TLSConfig{
|
|
|
|
EnableHTTP: true,
|
|
|
|
EnableRPC: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CAFile: cafile,
|
|
|
|
CertFile: foocert,
|
|
|
|
KeyFile: fookey,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2017-04-06 03:50:35 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
c.TLSConfig = &nconfig.TLSConfig{
|
|
|
|
EnableHTTP: true,
|
|
|
|
EnableRPC: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CAFile: badca,
|
|
|
|
CertFile: badcert,
|
|
|
|
KeyFile: badkey,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: c1.Node().ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
var out structs.SingleNodeResponse
|
2017-04-07 00:05:09 +00:00
|
|
|
testutil.AssertUntil(100*time.Millisecond,
|
|
|
|
func() (bool, error) {
|
|
|
|
err := c1.RPC("Node.GetNode", &req, &out)
|
|
|
|
if err == nil {
|
|
|
|
return false, fmt.Errorf("client RPC succeeded when it should have failed:\n%+v", out)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
},
|
|
|
|
)
|
2017-04-06 03:50:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 00:49:04 +00:00
|
|
|
func TestClient_Register(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2015-08-21 00:49:04 +00:00
|
|
|
s1, _ := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2015-08-21 00:49:04 +00:00
|
|
|
c.RPCHandler = s1
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: c1.Node().ID,
|
2015-09-14 01:18:40 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
2015-08-21 00:49:04 +00:00
|
|
|
}
|
|
|
|
var out structs.SingleNodeResponse
|
|
|
|
|
|
|
|
// Register should succeed
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-09-07 03:31:32 +00:00
|
|
|
err := s1.RPC("Node.GetNode", &req, &out)
|
2015-08-21 00:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if out.Node == nil {
|
|
|
|
return false, fmt.Errorf("missing reg")
|
|
|
|
}
|
|
|
|
return out.Node.ID == req.NodeID, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2015-08-29 21:15:34 +00:00
|
|
|
|
|
|
|
func TestClient_Heartbeat(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2015-08-29 21:15:34 +00:00
|
|
|
s1, _ := testServer(t, func(c *nomad.Config) {
|
|
|
|
c.MinHeartbeatTTL = 50 * time.Millisecond
|
|
|
|
})
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2015-08-29 21:15:34 +00:00
|
|
|
c.RPCHandler = s1
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: c1.Node().ID,
|
2015-09-14 01:18:40 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
2015-08-29 21:15:34 +00:00
|
|
|
}
|
|
|
|
var out structs.SingleNodeResponse
|
|
|
|
|
|
|
|
// Register should succeed
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2015-09-07 03:31:32 +00:00
|
|
|
err := s1.RPC("Node.GetNode", &req, &out)
|
2015-08-29 21:15:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if out.Node == nil {
|
|
|
|
return false, fmt.Errorf("missing reg")
|
|
|
|
}
|
|
|
|
return out.Node.Status == structs.NodeStatusReady, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2015-08-29 21:22:24 +00:00
|
|
|
|
|
|
|
func TestClient_UpdateAllocStatus(t *testing.T) {
|
2018-10-10 03:01:20 +00:00
|
|
|
t.Skip("missing exec driver plugin implementation")
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2015-08-29 21:22:24 +00:00
|
|
|
s1, _ := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2015-08-29 21:22:24 +00:00
|
|
|
c.RPCHandler = s1
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
2018-04-11 17:36:28 +00:00
|
|
|
// Wait until the node is ready
|
2016-08-16 06:11:57 +00:00
|
|
|
waitTilNodeReady(c1, t)
|
|
|
|
|
|
|
|
job := mock.Job()
|
2015-08-29 21:22:24 +00:00
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = c1.Node().ID
|
2016-08-16 06:11:57 +00:00
|
|
|
alloc.Job = job
|
|
|
|
alloc.JobID = job.ID
|
2016-02-22 03:20:50 +00:00
|
|
|
originalStatus := "foo"
|
|
|
|
alloc.ClientStatus = originalStatus
|
2015-08-29 21:22:24 +00:00
|
|
|
|
2016-08-16 06:11:57 +00:00
|
|
|
// Insert at zero so they are pulled
|
2015-08-29 21:22:24 +00:00
|
|
|
state := s1.State()
|
2016-08-16 06:11:57 +00:00
|
|
|
if err := state.UpsertJob(0, job); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := state.UpsertJobSummary(100, mock.JobSummary(alloc.JobID)); err != nil {
|
2016-07-21 21:43:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-16 06:11:57 +00:00
|
|
|
state.UpsertAllocs(101, []*structs.Allocation{alloc})
|
2015-08-29 21:22:24 +00:00
|
|
|
|
2016-02-22 03:20:50 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := state.AllocByID(ws, alloc.ID)
|
2016-02-22 03:20:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
return false, fmt.Errorf("no such alloc")
|
|
|
|
}
|
|
|
|
if out.ClientStatus == originalStatus {
|
|
|
|
return false, fmt.Errorf("Alloc client status not updated; got %v", out.ClientStatus)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
2015-08-29 21:22:24 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2016-02-22 03:20:50 +00:00
|
|
|
})
|
2015-08-29 21:22:24 +00:00
|
|
|
}
|
2015-08-29 21:33:30 +00:00
|
|
|
|
|
|
|
func TestClient_WatchAllocs(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2015-09-23 01:48:42 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
2015-08-29 21:33:30 +00:00
|
|
|
s1, _ := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2015-08-29 21:33:30 +00:00
|
|
|
c.RPCHandler = s1
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
2018-04-11 17:36:28 +00:00
|
|
|
// Wait until the node is ready
|
2016-08-16 06:11:57 +00:00
|
|
|
waitTilNodeReady(c1, t)
|
|
|
|
|
2015-08-29 21:33:30 +00:00
|
|
|
// Create mock allocations
|
2016-08-16 06:11:57 +00:00
|
|
|
job := mock.Job()
|
2015-08-29 21:33:30 +00:00
|
|
|
alloc1 := mock.Alloc()
|
2016-08-16 06:11:57 +00:00
|
|
|
alloc1.JobID = job.ID
|
|
|
|
alloc1.Job = job
|
2015-08-29 21:33:30 +00:00
|
|
|
alloc1.NodeID = c1.Node().ID
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.NodeID = c1.Node().ID
|
2016-08-16 06:11:57 +00:00
|
|
|
alloc2.JobID = job.ID
|
|
|
|
alloc2.Job = job
|
2015-08-29 21:33:30 +00:00
|
|
|
|
|
|
|
state := s1.State()
|
2016-08-16 06:11:57 +00:00
|
|
|
if err := state.UpsertJob(100, job); err != nil {
|
2016-07-21 21:43:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-16 06:11:57 +00:00
|
|
|
if err := state.UpsertJobSummary(101, mock.JobSummary(alloc1.JobID)); err != nil {
|
2016-07-21 21:43:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-16 06:11:57 +00:00
|
|
|
err := state.UpsertAllocs(102, []*structs.Allocation{alloc1, alloc2})
|
2015-08-29 21:33:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both allocations should get registered
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
c1.allocLock.RLock()
|
|
|
|
num := len(c1.allocs)
|
|
|
|
c1.allocLock.RUnlock()
|
|
|
|
return num == 2, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Delete one allocation
|
2017-10-19 00:06:46 +00:00
|
|
|
if err := state.DeleteEval(103, nil, []string{alloc1.ID}); err != nil {
|
2015-08-29 21:33:30 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-01 21:57:35 +00:00
|
|
|
// Update the other allocation. Have to make a copy because the allocs are
|
|
|
|
// shared in memory in the test and the modify index would be updated in the
|
|
|
|
// alloc runner.
|
2017-10-19 00:06:46 +00:00
|
|
|
alloc2_2 := alloc2.Copy()
|
2016-02-01 21:57:35 +00:00
|
|
|
alloc2_2.DesiredStatus = structs.AllocDesiredStatusStop
|
2017-10-19 00:06:46 +00:00
|
|
|
if err := state.UpsertAllocs(104, []*structs.Allocation{alloc2_2}); err != nil {
|
|
|
|
t.Fatalf("err upserting stopped alloc: %v", err)
|
2015-08-29 21:33:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 00:06:46 +00:00
|
|
|
// One allocation should get GC'd and removed
|
2015-08-29 21:33:30 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
c1.allocLock.RLock()
|
|
|
|
num := len(c1.allocs)
|
|
|
|
c1.allocLock.RUnlock()
|
|
|
|
return num == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// One allocations should get updated
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
c1.allocLock.RLock()
|
|
|
|
ar := c1.allocs[alloc2.ID]
|
|
|
|
c1.allocLock.RUnlock()
|
|
|
|
return ar.Alloc().DesiredStatus == structs.AllocDesiredStatusStop, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
2015-08-31 00:19:20 +00:00
|
|
|
|
2016-08-16 06:11:57 +00:00
|
|
|
func waitTilNodeReady(client *Client, t *testing.T) {
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
n := client.Node()
|
|
|
|
if n.Status != structs.NodeStatusReady {
|
|
|
|
return false, fmt.Errorf("node not registered")
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-31 00:19:20 +00:00
|
|
|
func TestClient_SaveRestoreState(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2015-09-23 01:48:42 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
2015-08-31 00:19:20 +00:00
|
|
|
s1, _ := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2015-11-11 00:03:18 +00:00
|
|
|
c.DevMode = false
|
2015-08-31 00:19:20 +00:00
|
|
|
c.RPCHandler = s1
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
2018-04-11 17:36:28 +00:00
|
|
|
// Wait until the node is ready
|
2016-08-16 06:11:57 +00:00
|
|
|
waitTilNodeReady(c1, t)
|
|
|
|
|
2015-08-31 00:19:20 +00:00
|
|
|
// Create mock allocations
|
2016-08-16 06:11:57 +00:00
|
|
|
job := mock.Job()
|
2015-08-31 00:19:20 +00:00
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.NodeID = c1.Node().ID
|
2016-08-16 06:11:57 +00:00
|
|
|
alloc1.Job = job
|
|
|
|
alloc1.JobID = job.ID
|
2016-09-05 02:09:08 +00:00
|
|
|
alloc1.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver"
|
2015-08-31 00:19:20 +00:00
|
|
|
task := alloc1.Job.TaskGroups[0].Tasks[0]
|
2016-09-05 02:09:08 +00:00
|
|
|
task.Config["run_for"] = "10s"
|
2015-08-31 00:19:20 +00:00
|
|
|
|
|
|
|
state := s1.State()
|
2016-08-16 06:11:57 +00:00
|
|
|
if err := state.UpsertJob(100, job); err != nil {
|
2016-07-21 21:43:21 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-16 06:11:57 +00:00
|
|
|
if err := state.UpsertJobSummary(101, mock.JobSummary(alloc1.JobID)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := state.UpsertAllocs(102, []*structs.Allocation{alloc1}); err != nil {
|
2015-08-31 00:19:20 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocations should get registered
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
c1.allocLock.RLock()
|
2015-11-11 00:03:18 +00:00
|
|
|
ar := c1.allocs[alloc1.ID]
|
2015-08-31 00:19:20 +00:00
|
|
|
c1.allocLock.RUnlock()
|
2016-08-16 06:11:57 +00:00
|
|
|
if ar == nil {
|
|
|
|
return false, fmt.Errorf("nil alloc runner")
|
|
|
|
}
|
|
|
|
if ar.Alloc().ClientStatus != structs.AllocClientStatusRunning {
|
|
|
|
return false, fmt.Errorf("client status: got %v; want %v", ar.Alloc().ClientStatus, structs.AllocClientStatusRunning)
|
|
|
|
}
|
|
|
|
return true, nil
|
2015-08-31 00:19:20 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Shutdown the client, saves state
|
2016-07-21 21:43:21 +00:00
|
|
|
if err := c1.Shutdown(); err != nil {
|
2015-08-31 00:19:20 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new client
|
2018-09-13 17:43:40 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
c1.config.Logger = logger
|
2017-02-01 00:43:57 +00:00
|
|
|
catalog := consul.NewMockCatalog(logger)
|
2018-09-13 17:43:40 +00:00
|
|
|
mockService := consulApi.NewMockConsulServiceClient(t, logger)
|
|
|
|
c2, err := NewClient(c1.config, catalog, mockService)
|
2015-08-31 00:19:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer c2.Shutdown()
|
|
|
|
|
|
|
|
// Ensure the allocation is running
|
2016-02-22 05:12:58 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
c2.allocLock.RLock()
|
|
|
|
ar := c2.allocs[alloc1.ID]
|
|
|
|
c2.allocLock.RUnlock()
|
|
|
|
status := ar.Alloc().ClientStatus
|
2017-02-28 00:00:19 +00:00
|
|
|
alive := status == structs.AllocClientStatusRunning || status == structs.AllocClientStatusPending
|
2016-02-22 05:12:58 +00:00
|
|
|
if !alive {
|
|
|
|
return false, fmt.Errorf("incorrect client status: %#v", ar.Alloc())
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
2016-09-02 19:44:05 +00:00
|
|
|
|
|
|
|
// Destroy all the allocations
|
2017-01-05 21:19:01 +00:00
|
|
|
for _, ar := range c2.getAllocRunners() {
|
2016-09-02 19:44:05 +00:00
|
|
|
ar.Destroy()
|
|
|
|
}
|
2017-01-05 20:32:44 +00:00
|
|
|
|
2017-01-05 21:19:01 +00:00
|
|
|
for _, ar := range c2.getAllocRunners() {
|
2017-01-05 20:32:44 +00:00
|
|
|
<-ar.WaitCh()
|
|
|
|
}
|
2015-08-31 00:19:20 +00:00
|
|
|
}
|
2015-09-12 18:47:44 +00:00
|
|
|
|
|
|
|
func TestClient_Init(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2015-09-12 18:47:44 +00:00
|
|
|
dir, err := ioutil.TempDir("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
allocDir := filepath.Join(dir, "alloc")
|
|
|
|
|
|
|
|
client := &Client{
|
|
|
|
config: &config.Config{
|
|
|
|
AllocDir: allocDir,
|
|
|
|
},
|
2018-09-16 00:48:59 +00:00
|
|
|
logger: testlog.HCLogger(t),
|
2015-09-12 18:47:44 +00:00
|
|
|
}
|
|
|
|
if err := client.init(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(allocDir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 16:34:24 +00:00
|
|
|
|
|
|
|
func TestClient_BlockedAllocations(t *testing.T) {
|
2018-10-10 03:01:20 +00:00
|
|
|
t.Skip("missing mock driver plugin implementation")
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
2016-08-22 16:34:24 +00:00
|
|
|
s1, _ := testServer(t, nil)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2016-08-22 16:34:24 +00:00
|
|
|
c.RPCHandler = s1
|
|
|
|
})
|
2016-09-02 19:44:05 +00:00
|
|
|
defer c1.Shutdown()
|
2016-08-22 16:34:24 +00:00
|
|
|
|
|
|
|
// Wait for the node to be ready
|
|
|
|
state := s1.State()
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := state.NodeByID(ws, c1.Node().ID)
|
2016-08-22 16:34:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if out == nil || out.Status != structs.NodeStatusReady {
|
|
|
|
return false, fmt.Errorf("bad node: %#v", out)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Add an allocation
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = c1.Node().ID
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0].Driver = "mock_driver"
|
|
|
|
alloc.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"kill_after": "1s",
|
|
|
|
"run_for": "100s",
|
|
|
|
"exit_code": 0,
|
|
|
|
"exit_signal": 0,
|
|
|
|
"exit_err": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
state.UpsertJobSummary(99, mock.JobSummary(alloc.JobID))
|
|
|
|
state.UpsertAllocs(100, []*structs.Allocation{alloc})
|
|
|
|
|
|
|
|
// Wait until the client downloads and starts the allocation
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := state.AllocByID(ws, alloc.ID)
|
2016-08-22 16:34:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if out == nil || out.ClientStatus != structs.AllocClientStatusRunning {
|
|
|
|
return false, fmt.Errorf("bad alloc: %#v", out)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Add a new chained alloc
|
|
|
|
alloc2 := alloc.Copy()
|
2017-09-29 16:58:48 +00:00
|
|
|
alloc2.ID = uuid.Generate()
|
2016-08-22 16:34:24 +00:00
|
|
|
alloc2.Job = alloc.Job
|
|
|
|
alloc2.JobID = alloc.JobID
|
|
|
|
alloc2.PreviousAllocation = alloc.ID
|
|
|
|
if err := state.UpsertAllocs(200, []*structs.Allocation{alloc2}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enusre that the chained allocation is being tracked as blocked
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2017-08-11 17:27:21 +00:00
|
|
|
ar := c1.getAllocRunners()[alloc2.ID]
|
|
|
|
if ar == nil {
|
|
|
|
return false, fmt.Errorf("alloc 2's alloc runner does not exist")
|
|
|
|
}
|
2017-08-14 23:02:28 +00:00
|
|
|
if !ar.IsWaiting() {
|
2017-08-11 17:27:21 +00:00
|
|
|
return false, fmt.Errorf("alloc 2 is not blocked")
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
2017-08-11 17:27:21 +00:00
|
|
|
return true, nil
|
2016-08-22 16:34:24 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Change the desired state of the parent alloc to stop
|
|
|
|
alloc1 := alloc.Copy()
|
|
|
|
alloc1.DesiredStatus = structs.AllocDesiredStatusStop
|
|
|
|
if err := state.UpsertAllocs(300, []*structs.Allocation{alloc1}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that there are no blocked allocations
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2017-08-11 17:27:21 +00:00
|
|
|
for id, ar := range c1.getAllocRunners() {
|
2017-08-14 23:02:28 +00:00
|
|
|
if ar.IsWaiting() {
|
2017-08-11 17:27:21 +00:00
|
|
|
return false, fmt.Errorf("%q still blocked", id)
|
|
|
|
}
|
2017-08-14 23:02:28 +00:00
|
|
|
if ar.IsMigrating() {
|
2017-08-11 17:27:21 +00:00
|
|
|
return false, fmt.Errorf("%q still migrating", id)
|
|
|
|
}
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
2016-09-02 19:44:05 +00:00
|
|
|
|
|
|
|
// Destroy all the allocations
|
2017-01-05 21:19:01 +00:00
|
|
|
for _, ar := range c1.getAllocRunners() {
|
2016-09-02 19:44:05 +00:00
|
|
|
ar.Destroy()
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:19:01 +00:00
|
|
|
for _, ar := range c1.getAllocRunners() {
|
2017-01-05 21:15:08 +00:00
|
|
|
<-ar.WaitCh()
|
|
|
|
}
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
2017-10-04 18:46:47 +00:00
|
|
|
|
|
|
|
func TestClient_ValidateMigrateToken_ValidToken(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c := TestClient(t, func(c *config.Config) {
|
2017-10-04 18:46:47 +00:00
|
|
|
c.ACLEnabled = true
|
|
|
|
})
|
|
|
|
defer c.Shutdown()
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2018-01-12 21:58:44 +00:00
|
|
|
validToken, err := structs.GenerateMigrateToken(alloc.ID, c.secretNodeID())
|
2017-10-04 18:46:47 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
|
|
|
|
assert.Equal(c.ValidateMigrateToken(alloc.ID, validToken), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_ValidateMigrateToken_InvalidToken(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c := TestClient(t, func(c *config.Config) {
|
2017-10-04 18:46:47 +00:00
|
|
|
c.ACLEnabled = true
|
|
|
|
})
|
|
|
|
defer c.Shutdown()
|
|
|
|
|
|
|
|
assert.Equal(c.ValidateMigrateToken("", ""), false)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
assert.Equal(c.ValidateMigrateToken(alloc.ID, alloc.ID), false)
|
|
|
|
assert.Equal(c.ValidateMigrateToken(alloc.ID, ""), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_ValidateMigrateToken_ACLDisabled(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c := TestClient(t, func(c *config.Config) {})
|
2017-10-04 18:46:47 +00:00
|
|
|
defer c.Shutdown()
|
|
|
|
|
|
|
|
assert.Equal(c.ValidateMigrateToken("", ""), true)
|
|
|
|
}
|
2017-11-20 15:38:46 +00:00
|
|
|
|
|
|
|
func TestClient_ReloadTLS_UpgradePlaintextToTLS(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
s1, addr := testServer(t, func(c *nomad.Config) {
|
2018-01-16 12:34:39 +00:00
|
|
|
c.Region = "regionFoo"
|
2017-11-20 15:38:46 +00:00
|
|
|
})
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
const (
|
|
|
|
cafile = "../helper/tlsutil/testdata/ca.pem"
|
|
|
|
foocert = "../helper/tlsutil/testdata/nomad-foo.pem"
|
|
|
|
fookey = "../helper/tlsutil/testdata/nomad-foo-key.pem"
|
|
|
|
)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2017-11-20 15:38:46 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
2018-01-16 12:34:39 +00:00
|
|
|
// Registering a node over plaintext should succeed
|
|
|
|
{
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: c1.Node().ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "regionFoo"},
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var out structs.SingleNodeResponse
|
|
|
|
err := c1.RPC("Node.GetNode", &req, &out)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("client RPC failed when it should have succeeded:\n%+v", err)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
func(err error) {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-11-20 15:38:46 +00:00
|
|
|
newConfig := &nconfig.TLSConfig{
|
|
|
|
EnableHTTP: true,
|
|
|
|
EnableRPC: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CAFile: cafile,
|
|
|
|
CertFile: foocert,
|
|
|
|
KeyFile: fookey,
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:29:43 +00:00
|
|
|
err := c1.reloadTLSConnections(newConfig)
|
2017-11-20 15:38:46 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
|
2018-01-16 12:34:39 +00:00
|
|
|
// Registering a node over plaintext should fail after the node has upgraded
|
|
|
|
// to TLS
|
|
|
|
{
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: c1.Node().ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "regionFoo"},
|
|
|
|
}
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var out structs.SingleNodeResponse
|
2017-11-20 15:38:46 +00:00
|
|
|
err := c1.RPC("Node.GetNode", &req, &out)
|
|
|
|
if err == nil {
|
|
|
|
return false, fmt.Errorf("client RPC succeeded when it should have failed:\n%+v", err)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
},
|
2018-01-16 12:34:39 +00:00
|
|
|
func(err error) {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2017-11-20 15:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_ReloadTLS_DowngradeTLSToPlaintext(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
s1, addr := testServer(t, func(c *nomad.Config) {
|
2018-01-16 12:34:39 +00:00
|
|
|
c.Region = "regionFoo"
|
2017-11-20 15:38:46 +00:00
|
|
|
})
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
const (
|
|
|
|
cafile = "../helper/tlsutil/testdata/ca.pem"
|
|
|
|
foocert = "../helper/tlsutil/testdata/nomad-foo.pem"
|
|
|
|
fookey = "../helper/tlsutil/testdata/nomad-foo-key.pem"
|
|
|
|
)
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
c1 := TestClient(t, func(c *config.Config) {
|
2017-11-20 15:38:46 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
c.TLSConfig = &nconfig.TLSConfig{
|
|
|
|
EnableHTTP: true,
|
|
|
|
EnableRPC: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CAFile: cafile,
|
|
|
|
CertFile: foocert,
|
|
|
|
KeyFile: fookey,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
2018-01-16 12:34:39 +00:00
|
|
|
// assert that when one node is running in encrypted mode, a RPC request to a
|
|
|
|
// node running in plaintext mode should fail
|
|
|
|
{
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: c1.Node().ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "regionFoo"},
|
|
|
|
}
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var out structs.SingleNodeResponse
|
|
|
|
err := c1.RPC("Node.GetNode", &req, &out)
|
|
|
|
if err == nil {
|
|
|
|
return false, fmt.Errorf("client RPC succeeded when it should have failed :\n%+v", err)
|
|
|
|
}
|
|
|
|
return true, nil
|
2018-02-16 00:04:53 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf(err.Error())
|
2018-01-16 12:34:39 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-11-20 15:38:46 +00:00
|
|
|
newConfig := &nconfig.TLSConfig{}
|
|
|
|
|
2017-12-05 00:29:43 +00:00
|
|
|
err := c1.reloadTLSConnections(newConfig)
|
2017-11-20 15:38:46 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
|
2018-01-16 12:34:39 +00:00
|
|
|
// assert that when both nodes are in plaintext mode, a RPC request should
|
|
|
|
// succeed
|
|
|
|
{
|
|
|
|
req := structs.NodeSpecificRequest{
|
|
|
|
NodeID: c1.Node().ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "regionFoo"},
|
|
|
|
}
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
var out structs.SingleNodeResponse
|
2017-11-20 15:38:46 +00:00
|
|
|
err := c1.RPC("Node.GetNode", &req, &out)
|
|
|
|
if err != nil {
|
2017-11-21 18:21:29 +00:00
|
|
|
return false, fmt.Errorf("client RPC failed when it should have succeeded:\n%+v", err)
|
2017-11-20 15:38:46 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
2018-02-16 00:04:53 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf(err.Error())
|
2017-11-20 15:38:46 +00:00
|
|
|
},
|
2018-01-16 12:34:39 +00:00
|
|
|
)
|
|
|
|
}
|
2017-11-20 15:38:46 +00:00
|
|
|
}
|
2018-01-09 23:26:53 +00:00
|
|
|
|
|
|
|
// TestClient_ServerList tests client methods that interact with the internal
|
|
|
|
// nomad server list.
|
|
|
|
func TestClient_ServerList(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
client := TestClient(t, func(c *config.Config) {})
|
|
|
|
|
|
|
|
if s := client.GetServers(); len(s) != 0 {
|
|
|
|
t.Fatalf("expected server lit to be empty but found: %+q", s)
|
|
|
|
}
|
2018-05-11 19:52:05 +00:00
|
|
|
if _, err := client.SetServers(nil); err != noServersErr {
|
2018-01-09 23:26:53 +00:00
|
|
|
t.Fatalf("expected setting an empty list to return a 'no servers' error but received %v", err)
|
|
|
|
}
|
2018-05-11 19:52:05 +00:00
|
|
|
if _, err := client.SetServers([]string{"123.456.13123.123.13:80"}); err == nil {
|
2018-01-09 23:26:53 +00:00
|
|
|
t.Fatalf("expected setting a bad server to return an error")
|
|
|
|
}
|
2018-05-11 19:52:05 +00:00
|
|
|
if _, err := client.SetServers([]string{"123.456.13123.123.13:80", "127.0.0.1:1234", "127.0.0.1"}); err == nil {
|
2018-01-09 23:26:53 +00:00
|
|
|
t.Fatalf("expected setting at least one good server to succeed but received: %v", err)
|
|
|
|
}
|
|
|
|
s := client.GetServers()
|
2018-01-26 01:56:47 +00:00
|
|
|
if len(s) != 0 {
|
2018-01-09 23:26:53 +00:00
|
|
|
t.Fatalf("expected 2 servers but received: %+q", s)
|
|
|
|
}
|
|
|
|
}
|