2018-01-12 01:00:30 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2018-12-15 20:26:29 +00:00
|
|
|
"fmt"
|
2018-12-08 23:24:42 +00:00
|
|
|
"time"
|
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2020-09-04 17:50:11 +00:00
|
|
|
consulapi "github.com/hashicorp/nomad/client/consul"
|
2018-01-12 01:00:30 +00:00
|
|
|
"github.com/hashicorp/nomad/client/fingerprint"
|
2020-09-04 17:50:11 +00:00
|
|
|
agentconsul "github.com/hashicorp/nomad/command/agent/consul"
|
2019-01-22 22:42:49 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/catalog"
|
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/singleton"
|
2019-01-23 14:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-12-20 01:08:24 +00:00
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
2018-01-12 01:00:30 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 03:07:16 +00:00
|
|
|
// TestClient creates an in-memory client for testing purposes and returns a
|
|
|
|
// cleanup func to shutdown the client and remove the alloc and state dirs.
|
|
|
|
//
|
|
|
|
// There is no need to override the AllocDir or StateDir as they are randomized
|
|
|
|
// and removed in the returned cleanup function. If they are overridden in the
|
|
|
|
// callback then the caller still must run the returned cleanup func.
|
2018-12-15 20:26:29 +00:00
|
|
|
func TestClient(t testing.T, cb func(c *config.Config)) (*Client, func() error) {
|
2018-10-16 03:07:16 +00:00
|
|
|
conf, cleanup := config.TestClientConfig(t)
|
2018-03-01 00:25:56 +00:00
|
|
|
|
2018-09-27 00:29:54 +00:00
|
|
|
// Tighten the fingerprinter timeouts (must be done in client package
|
|
|
|
// to avoid circular dependencies)
|
2018-01-12 01:00:30 +00:00
|
|
|
if conf.Options == nil {
|
|
|
|
conf.Options = make(map[string]string)
|
|
|
|
}
|
|
|
|
conf.Options[fingerprint.TightenNetworkTimeoutsConfig] = "true"
|
|
|
|
|
2018-09-28 23:54:41 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2018-10-04 23:32:14 +00:00
|
|
|
conf.Logger = logger
|
2018-09-28 23:54:41 +00:00
|
|
|
|
2018-01-12 01:00:30 +00:00
|
|
|
if cb != nil {
|
|
|
|
cb(conf)
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
// Set the plugin loaders
|
|
|
|
if conf.PluginLoader == nil {
|
|
|
|
conf.PluginLoader = catalog.TestPluginLoaderWithOptions(t, "", conf.Options, nil)
|
2018-12-20 01:08:24 +00:00
|
|
|
}
|
|
|
|
if conf.PluginSingletonLoader == nil {
|
2018-10-10 03:01:20 +00:00
|
|
|
conf.PluginSingletonLoader = singleton.NewSingletonLoader(logger, conf.PluginLoader)
|
|
|
|
}
|
2020-09-04 17:50:11 +00:00
|
|
|
mockCatalog := agentconsul.NewMockCatalog(logger)
|
|
|
|
mockService := consulapi.NewMockConsulServiceClient(t, logger)
|
|
|
|
client, err := NewClient(conf, mockCatalog, nil, mockService)
|
2018-01-12 01:00:30 +00:00
|
|
|
if err != nil {
|
2018-10-16 03:07:16 +00:00
|
|
|
cleanup()
|
2018-01-12 01:00:30 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-12-15 20:26:29 +00:00
|
|
|
return client, func() error {
|
2018-12-11 11:49:20 +00:00
|
|
|
ch := make(chan error)
|
2018-10-16 03:07:16 +00:00
|
|
|
|
2018-12-08 23:24:42 +00:00
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
// Shutdown client
|
|
|
|
err := client.Shutdown()
|
|
|
|
if err != nil {
|
2018-12-15 20:26:29 +00:00
|
|
|
ch <- fmt.Errorf("failed to shutdown client: %v", err)
|
2018-12-08 23:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call TestClientConfig cleanup
|
|
|
|
cleanup()
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2018-12-15 20:26:29 +00:00
|
|
|
case e := <-ch:
|
|
|
|
return e
|
2018-12-08 23:24:42 +00:00
|
|
|
case <-time.After(1 * time.Minute):
|
2018-12-15 20:26:29 +00:00
|
|
|
return fmt.Errorf("timed out while shutting down client")
|
2018-12-08 23:24:42 +00:00
|
|
|
}
|
2018-10-16 03:07:16 +00:00
|
|
|
}
|
2018-01-12 01:00:30 +00:00
|
|
|
}
|