From 1eb3d0e0d49b7c18afd5851210b4456fc5ea4312 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Wed, 25 Oct 2017 19:21:38 +0200 Subject: [PATCH] replace custom unique id with a UUID --- agent/checks/check_test.go | 38 +++++++++++++++++++++++--------------- agent/testagent.go | 3 +-- agent/unique/id.go | 19 ------------------- 3 files changed, 24 insertions(+), 36 deletions(-) delete mode 100644 agent/unique/id.go diff --git a/agent/checks/check_test.go b/agent/checks/check_test.go index c5a046848..a3f89efe9 100644 --- a/agent/checks/check_test.go +++ b/agent/checks/check_test.go @@ -15,12 +15,20 @@ import ( "time" "github.com/hashicorp/consul/agent/mock" - "github.com/hashicorp/consul/agent/unique" "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/testutil/retry" "github.com/hashicorp/consul/types" + uuid "github.com/hashicorp/go-uuid" ) +func uniqueID() string { + id, err := uuid.GenerateUUID() + if err != nil { + panic(err) + } + return id +} + func TestCheckMonitor_Script(t *testing.T) { tests := []struct { script, status string @@ -39,7 +47,7 @@ func TestCheckMonitor_Script(t *testing.T) { CheckID: types.CheckID("foo"), Script: tt.script, Interval: 25 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() defer check.Stop() @@ -74,7 +82,7 @@ func TestCheckMonitor_Args(t *testing.T) { CheckID: types.CheckID("foo"), ScriptArgs: tt.args, Interval: 25 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() defer check.Stop() @@ -99,7 +107,7 @@ func TestCheckMonitor_Timeout(t *testing.T) { ScriptArgs: []string{"sh", "-c", "sleep 1 && exit 0"}, Interval: 50 * time.Millisecond, Timeout: 25 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() defer check.Stop() @@ -123,7 +131,7 @@ func TestCheckMonitor_RandomStagger(t *testing.T) { CheckID: types.CheckID("foo"), ScriptArgs: []string{"sh", "-c", "exit 0"}, Interval: 25 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() defer check.Stop() @@ -148,7 +156,7 @@ func TestCheckMonitor_LimitOutput(t *testing.T) { CheckID: types.CheckID("foo"), ScriptArgs: []string{"od", "-N", "81920", "/dev/urandom"}, Interval: 25 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() defer check.Stop() @@ -168,7 +176,7 @@ func TestCheckTTL(t *testing.T) { Notify: notif, CheckID: types.CheckID("foo"), TTL: 200 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() defer check.Stop() @@ -292,7 +300,7 @@ func TestCheckHTTP(t *testing.T) { Method: tt.method, Header: tt.header, Interval: 10 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() defer check.Stop() @@ -328,7 +336,7 @@ func TestCheckHTTPTimeout(t *testing.T) { HTTP: server.URL, Timeout: timeout, Interval: 10 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() @@ -349,7 +357,7 @@ func TestCheckHTTP_disablesKeepAlives(t *testing.T) { CheckID: types.CheckID("foo"), HTTP: "http://foo.bar/baz", Interval: 10 * time.Second, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() @@ -366,7 +374,7 @@ func TestCheckHTTP_TLSSkipVerify_defaultFalse(t *testing.T) { CheckID: "foo", HTTP: "https://foo.bar/baz", Interval: 10 * time.Second, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() @@ -398,7 +406,7 @@ func TestCheckHTTP_TLSSkipVerify_true_pass(t *testing.T) { CheckID: types.CheckID("skipverify_true"), HTTP: server.URL, Interval: 25 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), TLSSkipVerify: true, } @@ -430,7 +438,7 @@ func TestCheckHTTP_TLSSkipVerify_true_fail(t *testing.T) { CheckID: types.CheckID("skipverify_true"), HTTP: server.URL, Interval: 5 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), TLSSkipVerify: true, } check.Start() @@ -458,7 +466,7 @@ func TestCheckHTTP_TLSSkipVerify_false(t *testing.T) { CheckID: types.CheckID("skipverify_false"), HTTP: server.URL, Interval: 100 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), TLSSkipVerify: false, } @@ -505,7 +513,7 @@ func expectTCPStatus(t *testing.T, tcp string, status string) { CheckID: types.CheckID("foo"), TCP: tcp, Interval: 10 * time.Millisecond, - Logger: log.New(ioutil.Discard, unique.ID(), log.LstdFlags), + Logger: log.New(ioutil.Discard, uniqueID(), log.LstdFlags), } check.Start() defer check.Stop() diff --git a/agent/testagent.go b/agent/testagent.go index 33a6f4945..788d90571 100644 --- a/agent/testagent.go +++ b/agent/testagent.go @@ -19,7 +19,6 @@ import ( "github.com/hashicorp/consul/agent/config" "github.com/hashicorp/consul/agent/consul" "github.com/hashicorp/consul/agent/structs" - "github.com/hashicorp/consul/agent/unique" "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/lib/freeport" "github.com/hashicorp/consul/logger" @@ -112,7 +111,7 @@ func (a *TestAgent) Start() *TestAgent { } hclDataDir = `data_dir = "` + d + `"` } - id := unique.ID() + id := NodeID() for i := 10; i >= 0; i-- { a.Config = TestConfig( diff --git a/agent/unique/id.go b/agent/unique/id.go deleted file mode 100644 index d4ba76dfc..000000000 --- a/agent/unique/id.go +++ /dev/null @@ -1,19 +0,0 @@ -package unique - -import ( - "math/rand" - "strconv" - "time" -) - -func init() { - rand.Seed(time.Now().UnixNano()) // seed random number generator -} - -func ID() string { - id := strconv.FormatUint(rand.Uint64(), 36) - for len(id) < 16 { - id += " " - } - return id -}