2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-02-06 21:03:09 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-02-06 21:03:09 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-02-15 23:22:57 +00:00
|
|
|
sconfig "github.com/hashicorp/nomad/nomad/structs/config"
|
2018-02-06 21:03:09 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRpc_streamingRpcConn_badEndpoint(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 21:03:09 +00:00
|
|
|
require := require.New(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := nomad.TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
2018-02-06 21:03:09 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
2018-02-06 21:03:09 +00:00
|
|
|
c.Servers = []string{s1.GetConfig().RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 21:03:09 +00:00
|
|
|
|
|
|
|
// Wait for the client to connect
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
node, err := s1.State().NodeByID(nil, c.NodeID())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if node == nil {
|
|
|
|
return false, errors.New("no node")
|
|
|
|
}
|
|
|
|
|
|
|
|
return node.Status == structs.NodeStatusReady, errors.New("wrong status")
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Get the server
|
|
|
|
server := c.servers.FindServer()
|
|
|
|
require.NotNil(server)
|
|
|
|
|
|
|
|
conn, err := c.streamingRpcConn(server, "Bogus")
|
|
|
|
require.Nil(conn)
|
|
|
|
require.NotNil(err)
|
2018-02-15 23:22:57 +00:00
|
|
|
require.Contains(err.Error(), "Unknown rpc method: \"Bogus\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRpc_streamingRpcConn_badEndpoint_TLS(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-15 23:22:57 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
const (
|
2023-05-22 13:31:56 +00:00
|
|
|
cafile = "../helper/tlsutil/testdata/nomad-agent-ca.pem"
|
|
|
|
fooservercert = "../helper/tlsutil/testdata/regionFoo-server-nomad.pem"
|
|
|
|
fooserverkey = "../helper/tlsutil/testdata/regionFoo-server-nomad-key.pem"
|
2018-02-15 23:22:57 +00:00
|
|
|
)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
s1, cleanupS1 := nomad.TestServer(t, func(c *nomad.Config) {
|
2018-02-15 23:22:57 +00:00
|
|
|
c.Region = "regionFoo"
|
|
|
|
c.BootstrapExpect = 1
|
|
|
|
c.TLSConfig = &sconfig.TLSConfig{
|
|
|
|
EnableHTTP: true,
|
|
|
|
EnableRPC: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CAFile: cafile,
|
2023-05-22 13:31:56 +00:00
|
|
|
CertFile: fooservercert,
|
|
|
|
KeyFile: fooserverkey,
|
2018-02-15 23:22:57 +00:00
|
|
|
}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
2018-02-15 23:22:57 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := TestClient(t, func(c *config.Config) {
|
2018-02-15 23:22:57 +00:00
|
|
|
c.Region = "regionFoo"
|
|
|
|
c.Servers = []string{s1.GetConfig().RPCAddr.String()}
|
|
|
|
c.TLSConfig = &sconfig.TLSConfig{
|
|
|
|
EnableHTTP: true,
|
|
|
|
EnableRPC: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CAFile: cafile,
|
2023-05-22 13:31:56 +00:00
|
|
|
CertFile: fooservercert,
|
|
|
|
KeyFile: fooserverkey,
|
2018-02-15 23:22:57 +00:00
|
|
|
}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-15 23:22:57 +00:00
|
|
|
|
|
|
|
// Wait for the client to connect
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
node, err := s1.State().NodeByID(nil, c.NodeID())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if node == nil {
|
|
|
|
return false, errors.New("no node")
|
|
|
|
}
|
|
|
|
|
|
|
|
return node.Status == structs.NodeStatusReady, errors.New("wrong status")
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Get the server
|
|
|
|
server := c.servers.FindServer()
|
|
|
|
require.NotNil(server)
|
|
|
|
|
|
|
|
conn, err := c.streamingRpcConn(server, "Bogus")
|
|
|
|
require.Nil(conn)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "Unknown rpc method: \"Bogus\"")
|
2018-02-06 21:03:09 +00:00
|
|
|
}
|