本文整理匯總了Golang中github.com/hashicorp/consul/testutil.NewTestServerConfig函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewTestServerConfig函數的具體用法?Golang NewTestServerConfig怎麽用?Golang NewTestServerConfig使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewTestServerConfig函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestStart_runsCommandOnChange
func TestStart_runsCommandOnChange(t *testing.T) {
t.Parallel()
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
consul.SetKV("foo/bar", []byte("one"))
defer consul.Stop()
config := testConfig(fmt.Sprintf(`
consul = "%s"
upcase = true
prefix {
path = "foo"
}
`, consul.HTTPAddr), t)
f := test.CreateTempfile(nil, t)
defer os.Remove(f.Name())
os.Remove(f.Name())
runner, err := NewRunner(config, []string{"sh", "-c", "echo $BAR > " + f.Name()}, true)
if err != nil {
t.Fatal(err)
}
runner.outStream, runner.errStream = ioutil.Discard, ioutil.Discard
go runner.Start()
defer runner.Stop()
test.WaitForFileContents(f.Name(), []byte("one\n"), t)
}
示例2: testDedupManager
func testDedupManager(t *testing.T, templ []*Template) (*testutil.TestServer, *DedupManager) {
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
// Setup the configuration
config := DefaultConfig()
config.Consul = consul.HTTPAddr
// Create the clientset
clients, err := newClientSet(config)
if err != nil {
t.Fatalf("runner: %s", err)
}
// Setup a brain
brain := NewBrain()
// Create the dedup manager
dedup, err := NewDedupManager(config, clients, brain, templ)
if err != nil {
t.Fatalf("err: %v", err)
}
return consul, dedup
}
示例3: NewTestServer
func NewTestServer(t *testing.T) *testutil.TestServer {
// Create a server
srv := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
if !testing.Verbose() {
c.LogLevel = "err"
}
})
return srv
}
示例4: TestRun_merges
func TestRun_merges(t *testing.T) {
t.Parallel()
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
defer consul.Stop()
consul.SetKV("config/global/address", []byte("1.2.3.4"))
consul.SetKV("config/global/port", []byte("5598"))
consul.SetKV("config/redis/port", []byte("8000"))
config := testConfig(fmt.Sprintf(`
consul = "%s"
upcase = true
prefix {
path = "config/global"
}
prefix {
path = "config/redis"
}
`, consul.HTTPAddr), t)
runner, err := NewRunner(config, []string{"env"}, true)
if err != nil {
t.Fatal(err)
}
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
runner.outStream, runner.errStream = outStream, errStream
go runner.Start()
defer runner.Stop()
select {
case err := <-runner.ErrCh:
t.Fatal(err)
case <-runner.ExitCh:
expected := "ADDRESS=1.2.3.4"
if !strings.Contains(outStream.String(), expected) {
t.Fatalf("expected %q to include %q", outStream.String(), expected)
}
expected = "PORT=8000"
if !strings.Contains(outStream.String(), expected) {
t.Fatalf("expected %q to include %q", outStream.String(), expected)
}
}
}
示例5: TestRunner_quiescence
// Warning: this is a super fragile and time-dependent test. If it's failing,
// check the demo Consul cluster and your own sanity before you assume your
// code broke something...
func TestRunner_quiescence(t *testing.T) {
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
defer consul.Stop()
in := test.CreateTempfile([]byte(`
{{ range service "consul" "any" }}{{.Node}}{{ end }}
`), t)
defer test.DeleteTempfile(in, t)
out := test.CreateTempfile(nil, t)
test.DeleteTempfile(out, t)
config := testConfig(fmt.Sprintf(`
consul = "%s"
wait = "50ms:200ms"
template {
source = "%s"
destination = "%s"
}
`, consul.HTTPAddr, in.Name(), out.Name()), t)
runner, err := NewRunner(config, false, false)
if err != nil {
t.Fatal(err)
}
go runner.Start()
defer runner.Stop()
min := time.After(10 * time.Millisecond)
max := time.After(500 * time.Millisecond)
for {
select {
case <-min:
if _, err = os.Stat(out.Name()); !os.IsNotExist(err) {
t.Errorf("expected quiescence timer to not fire for yet")
}
continue
case <-max:
if _, err = os.Stat(out.Name()); os.IsNotExist(err) {
t.Errorf("expected template to be rendered by now")
}
return
}
}
}
示例6: makeClientWithConfig
func makeClientWithConfig(t *testing.T) (*consulapi.Client, *testutil.TestServer) {
// Make client config
conf := consulapi.DefaultConfig()
// Create server
server := testutil.NewTestServerConfig(t, nil)
conf.Address = server.HTTPAddr
// Create client
client, err := consulapi.NewClient(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
return client, server
}
示例7: TestRun_stdin
func TestRun_stdin(t *testing.T) {
t.Parallel()
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
defer consul.Stop()
consul.SetKV("foo/bar/bar", []byte("baz"))
config := testConfig(fmt.Sprintf(`
consul = "%s"
prefix {
path = "foo/bar"
}
`, consul.HTTPAddr), t)
runner, err := NewRunner(config, []string{"cat"}, true)
if err != nil {
t.Fatal(err)
}
outStream, errStream := gatedio.NewByteBuffer(), gatedio.NewByteBuffer()
inStream := gatedio.NewByteBuffer()
runner.outStream, runner.errStream = outStream, errStream
runner.inStream = inStream
go runner.Start()
defer runner.Stop()
if _, err := inStream.WriteString("foo"); err != nil {
t.Fatal(err)
}
select {
case err := <-runner.ErrCh:
t.Fatal(err)
case <-runner.ExitCh:
expected := "foo"
if !strings.Contains(outStream.String(), expected) {
t.Fatalf("expected %q to include %q", outStream.String(), expected)
}
}
}
示例8: TestRun_pristine
func TestRun_pristine(t *testing.T) {
t.Parallel()
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
defer consul.Stop()
consul.SetKV("foo/bar/bar", []byte("baz"))
config := testConfig(fmt.Sprintf(`
consul = "%s"
pristine = true
prefix {
path = "foo/bar"
}
`, consul.HTTPAddr), t)
runner, err := NewRunner(config, []string{"env"}, true)
if err != nil {
t.Fatal(err)
}
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
runner.outStream, runner.errStream = outStream, errStream
go runner.Start()
defer runner.Stop()
select {
case err := <-runner.ErrCh:
t.Fatal(err)
case <-runner.ExitCh:
expected := "bar=baz"
if !strings.Contains(outStream.String(), expected) {
t.Fatalf("expected %q to include %q", outStream.String(), expected)
}
notExpected := "HOME="
if strings.Contains(outStream.String(), notExpected) {
t.Fatalf("did not expect %q to include %q", outStream.String(), notExpected)
}
}
}
示例9: testConsulServer
// testConsulServer is a helper for creating a Consul server and returning the
// appropriate configuration to connect to it.
func testConsulServer(t *testing.T) (*ClientSet, *testutil.TestServer) {
t.Parallel()
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
clients := NewClientSet()
if err := clients.CreateConsulClient(&CreateConsulClientInput{
Address: consul.HTTPAddr,
}); err != nil {
consul.Stop()
t.Fatalf("clientset: %s", err)
}
return clients, consul
}
示例10: TestRun_overwrites
func TestRun_overwrites(t *testing.T) {
t.Parallel()
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
defer consul.Stop()
consul.SetKV("config/global/address", []byte("1.2.3.4"))
config := testConfig(fmt.Sprintf(`
consul = "%s"
prefix {
path = "config/global"
}
`, consul.HTTPAddr), t)
runner, err := NewRunner(config, []string{"env"}, true)
if err != nil {
t.Fatal(err)
}
outStream, errStream := gatedio.NewByteBuffer(), gatedio.NewByteBuffer()
runner.outStream, runner.errStream = outStream, errStream
// Set the env to ensure it overwrites
os.Setenv("address", "should_be_overwritten")
defer os.Unsetenv("address")
go runner.Start()
defer runner.Stop()
select {
case err := <-runner.ErrCh:
t.Fatal(err)
case <-runner.ExitCh:
expected := "address=1.2.3.4"
if !strings.Contains(outStream.String(), expected) {
t.Fatalf("expected %q to include %q", outStream.String(), expected)
}
}
}
示例11: testConsul
// testConsul returns a Syncer configured with an embedded Consul server.
//
// Callers must defer Syncer.Shutdown() and TestServer.Stop()
//
func testConsul(t *testing.T) (*Syncer, *testutil.TestServer) {
// Create an embedded Consul server
testconsul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
// If -v wasn't specified squelch consul logging
if !testing.Verbose() {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
}
})
// Configure Syncer to talk to the test server
cconf := config.DefaultConsulConfig()
cconf.Addr = testconsul.HTTPAddr
cs, err := NewSyncer(cconf, nil, logger)
if err != nil {
t.Fatalf("Error creating Syncer: %v", err)
}
return cs, testconsul
}
示例12: TestRun_onceFlag
func TestRun_onceFlag(t *testing.T) {
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
defer consul.Stop()
consul.SetKV("foo", []byte("bar"))
template := test.CreateTempfile([]byte(`
{{key "foo"}}
`), t)
defer test.DeleteTempfile(template, t)
out := test.CreateTempfile(nil, t)
defer test.DeleteTempfile(out, t)
outStream := gatedio.NewByteBuffer()
cli := NewCLI(outStream, outStream)
command := fmt.Sprintf("consul-template -consul %s -template %s:%s -once -log-level debug",
consul.HTTPAddr, template.Name(), out.Name())
args := strings.Split(command, " ")
ch := make(chan int, 1)
go func() {
ch <- cli.Run(args)
}()
select {
case status := <-ch:
if status != ExitCodeOK {
t.Errorf("expected %d to eq %d", status, ExitCodeOK)
t.Errorf("out: %s", outStream.String())
}
case <-time.After(500 * time.Millisecond):
t.Errorf("expected exit, did not exit after 500ms")
t.Errorf("out: %s", outStream.String())
}
}
示例13: makeClientWithConfig
func makeClientWithConfig(
t *testing.T,
cb1 configCallback,
cb2 testutil.ServerConfigCallback) (*Client, *testutil.TestServer) {
// Make client config
conf := DefaultConfig()
if cb1 != nil {
cb1(conf)
}
// Create server
server := testutil.NewTestServerConfig(t, cb2)
conf.Address = server.HTTPAddr
// Create client
client, err := NewClient(conf)
if err != nil {
t.Fatalf("err: %v", err)
}
return client, server
}
示例14: testConsulServer
// testConsulServer is a helper for creating a Consul server and returning the
// appropriate configuration to connect to it.
func testConsulServer(t *testing.T) (*ClientSet, *testutil.TestServer) {
t.Parallel()
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
config := consulapi.DefaultConfig()
config.Address = consul.HTTPAddr
client, err := consulapi.NewClient(config)
if err != nil {
consul.Stop()
t.Fatalf("consul api client err: %s", err)
}
clients := NewClientSet()
if err := clients.Add(client); err != nil {
consul.Stop()
t.Fatalf("clientset err: %s", err)
}
return clients, consul
}
示例15: TestRun_vaultPrecedenceOverConsul
func TestRun_vaultPrecedenceOverConsul(t *testing.T) {
t.Parallel()
consul := testutil.NewTestServerConfig(t, func(c *testutil.TestServerConfig) {
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
defer consul.Stop()
consul.SetKV("secret/foo/secret_foo_zip", []byte("baz"))
vaultCore, _, token := vault.TestCoreUnsealed(t)
ln, vaultAddr := http.TestServer(t, vaultCore)
defer ln.Close()
req := &logical.Request{
Operation: logical.WriteOperation,
Path: "secret/foo",
Data: map[string]interface{}{
"zip": "zap",
"ding": "dong",
},
ClientToken: token,
}
if _, err := vaultCore.HandleRequest(req); err != nil {
t.Fatal(err)
}
vaultconfig := vaultapi.DefaultConfig()
vaultconfig.Address = vaultAddr
client, err := vaultapi.NewClient(vaultconfig)
if err != nil {
t.Fatal(err)
}
client.SetToken(token)
// Create a new token - the core token is a root token and is therefore
// not renewable
secret, err := client.Auth().Token().Create(&vaultapi.TokenCreateRequest{
Lease: "1h",
})
if err != nil {
t.Fatal(err)
}
config := testConfig(fmt.Sprintf(`
consul = "%s"
vault {
address = "%s"
token = "%s"
ssl {
enabled = false
}
}
secret {
path = "secret/foo"
}
prefix {
path = "secret/foo"
}
`, consul.HTTPAddr, vaultAddr, secret.Auth.ClientToken), t)
runner, err := NewRunner(config, []string{"env"}, true)
if err != nil {
t.Fatal(err)
}
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
runner.outStream, runner.errStream = outStream, errStream
go runner.Start()
defer runner.Stop()
select {
case err := <-runner.ErrCh:
t.Fatal(err)
case <-runner.ExitCh:
case <-time.After(250 * time.Millisecond):
}
expected := "secret_foo_zip=zap"
if !strings.Contains(outStream.String(), expected) {
t.Errorf("expected %q to include %q", outStream.String(), expected)
}
expected = "secret_foo_ding=dong"
if !strings.Contains(outStream.String(), expected) {
t.Errorf("expected %q to include %q", outStream.String(), expected)
}
}