// newClientSet creates a new client set from the given config. func newClientSet(config *Config) (*dep.ClientSet, error) { clients := dep.NewClientSet() if err := clients.CreateConsulClient(&dep.CreateConsulClientInput{ Address: config.Consul, Token: config.Token, AuthEnabled: config.Auth.Enabled, AuthUsername: config.Auth.Username, AuthPassword: config.Auth.Password, SSLEnabled: config.SSL.Enabled, SSLVerify: config.SSL.Verify, SSLCert: config.SSL.Cert, SSLKey: config.SSL.Key, SSLCACert: config.SSL.CaCert, }); err != nil { return nil, fmt.Errorf("runner: %s", err) } if err := clients.CreateVaultClient(&dep.CreateVaultClientInput{ Address: config.Vault.Address, Token: config.Vault.Token, SSLEnabled: config.Vault.SSL.Enabled, SSLVerify: config.Vault.SSL.Verify, SSLCert: config.Vault.SSL.Cert, SSLKey: config.Vault.SSL.Key, SSLCACert: config.Vault.SSL.CaCert, }); err != nil { return nil, fmt.Errorf("runner: %s", err) } return clients, nil }
// newWatcher creates a new watcher. func newWatcher(config *Config, client *api.Client, once bool) (*watch.Watcher, error) { log.Printf("[INFO] (runner) creating Watcher") clients := dep.NewClientSet() if err := clients.CreateConsulClient(&dep.CreateConsulClientInput{ Address: config.Consul, Token: config.Token, AuthEnabled: config.Auth.Enabled, AuthUsername: config.Auth.Username, AuthPassword: config.Auth.Password, SSLEnabled: config.SSL.Enabled, SSLVerify: config.SSL.Verify, }); err != nil { return nil, err } watcher, err := watch.NewWatcher(&watch.WatcherConfig{ Clients: clients, Once: once, MaxStale: config.MaxStale, RetryFunc: func(current time.Duration) time.Duration { return config.Retry }, }) if err != nil { return nil, err } return watcher, err }
func TestNewWatcher_renewVault(t *testing.T) { clients := dep.NewClientSet() w, err := NewWatcher(&WatcherConfig{ Clients: clients, Once: true, RenewVault: true, }) if err != nil { t.Fatal(err) } defer w.Stop() if !w.Watching(&dep.VaultToken{}) { t.Errorf("expected watcher to be renewing vault token") } }
// newWatcher creates a new watcher. func newWatcher(config *JsonExport, client *api.Client, once bool) (*watch.Watcher, error) { log.Println("[INFO] (runner) creating Watcher") clientSet := dep.NewClientSet() if err := clientSet.Add(client); err != nil { return nil, err } watcher, err := watch.NewWatcher(&watch.WatcherConfig{ Clients: clientSet, Once: once, }) if err != nil { return nil, err } return watcher, err }
func TestNewWatcher_values(t *testing.T) { clients := dep.NewClientSet() w, err := NewWatcher(&WatcherConfig{ Clients: clients, Once: true, }) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(w.config.Clients, clients) { t.Errorf("expected %#v to be %#v", w.config.Clients, clients) } if w.config.Once != true { t.Errorf("expected w.config.Once to be true") } }
// newClientSet creates a new client set from the given config. func newClientSet(config *Config) (*dep.ClientSet, error) { clients := dep.NewClientSet() consul, err := newConsulClient(config) if err != nil { return nil, err } if err := clients.Add(consul); err != nil { return nil, err } vault, err := newVaultClient(config) if err != nil { return nil, err } if err := clients.Add(vault); err != nil { return nil, err } return clients, nil }
// newWatcher creates a new watcher. func newWatcher(config *Config, client *api.Client, once bool) (*watch.Watcher, error) { log.Printf("[INFO] (runner) creating Watcher") clientSet := dep.NewClientSet() if err := clientSet.Add(client); err != nil { return nil, err } watcher, err := watch.NewWatcher(&watch.WatcherConfig{ Clients: clientSet, Once: once, MaxStale: config.MaxStale, RetryFunc: func(current time.Duration) time.Duration { return config.Retry }, }) if err != nil { return nil, err } return watcher, err }
// newClientSet creates a new client set from the given config. func newClientSet(c *config.Config) (*dep.ClientSet, error) { clients := dep.NewClientSet() if err := clients.CreateConsulClient(&dep.CreateConsulClientInput{ Address: config.StringVal(c.Consul.Address), Token: config.StringVal(c.Consul.Token), AuthEnabled: config.BoolVal(c.Consul.Auth.Enabled), AuthUsername: config.StringVal(c.Consul.Auth.Username), AuthPassword: config.StringVal(c.Consul.Auth.Password), SSLEnabled: config.BoolVal(c.Consul.SSL.Enabled), SSLVerify: config.BoolVal(c.Consul.SSL.Verify), SSLCert: config.StringVal(c.Consul.SSL.Cert), SSLKey: config.StringVal(c.Consul.SSL.Key), SSLCACert: config.StringVal(c.Consul.SSL.CaCert), SSLCAPath: config.StringVal(c.Consul.SSL.CaPath), ServerName: config.StringVal(c.Consul.SSL.ServerName), }); err != nil { return nil, fmt.Errorf("runner: %s", err) } if err := clients.CreateVaultClient(&dep.CreateVaultClientInput{ Address: config.StringVal(c.Vault.Address), Token: config.StringVal(c.Vault.Token), UnwrapToken: config.BoolVal(c.Vault.UnwrapToken), SSLEnabled: config.BoolVal(c.Vault.SSL.Enabled), SSLVerify: config.BoolVal(c.Vault.SSL.Verify), SSLCert: config.StringVal(c.Vault.SSL.Cert), SSLKey: config.StringVal(c.Vault.SSL.Key), SSLCACert: config.StringVal(c.Vault.SSL.CaCert), SSLCAPath: config.StringVal(c.Vault.SSL.CaPath), ServerName: config.StringVal(c.Vault.SSL.ServerName), }); err != nil { return nil, fmt.Errorf("runner: %s", err) } return clients, nil }
package watch import ( "fmt" "reflect" "strings" "testing" "time" dep "github.com/hashicorp/consul-template/dependency" ) var defaultWatcherConfig = &WatcherConfig{ Clients: dep.NewClientSet(), Once: true, RetryFunc: func(time.Duration) time.Duration { return 0 }, } func TestNewWatcher_noConfig(t *testing.T) { _, err := NewWatcher(nil) if err == nil { t.Fatal("expected error, but nothing was returned") } expected := "watcher: missing config" if !strings.Contains(err.Error(), expected) { t.Errorf("expected %q to contain %q", err.Error(), expected) } } func TestNewWatcher_defaultValues(t *testing.T) {