From e1eb1f6bab539be9a7583a8e189468000cea188f Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Tue, 12 Nov 2019 09:32:13 -0500 Subject: [PATCH 1/4] api: avoid depending on nomad in test files --- GNUmakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GNUmakefile b/GNUmakefile index 849f7c138..37e3b1c93 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -196,7 +196,7 @@ check: ## Lint the source code @if (git status | grep -q .pb.go); then echo the following proto files are out of sync; git status |grep .pb.go; exit 1; fi @echo "==> Check API package is isolated from rest" - @! go list -f '{{ join .Deps "\n" }}' ./api | grep github.com/hashicorp/nomad/ | grep -v -e /vendor/ -e /nomad/api/ + @! go list --test -f '{{ join .Deps "\n" }}' ./api | grep github.com/hashicorp/nomad/ | grep -v -e /vendor/ -e /nomad/api/ .PHONY: checkscripts checkscripts: ## Lint shell scripts From d4514c7b73db226f0c80a2ae4c776cdfed74cc67 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Tue, 12 Nov 2019 09:47:18 -0500 Subject: [PATCH 2/4] api: avoid depending on helper internal package --- api/allocations_test.go | 13 ++++++------- api/api_test.go | 13 ++----------- api/util_test.go | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/api/allocations_test.go b/api/allocations_test.go index 4f9ab4501..d9543ccf2 100644 --- a/api/allocations_test.go +++ b/api/allocations_test.go @@ -10,7 +10,6 @@ import ( "time" - "github.com/hashicorp/nomad/helper/uuid" "github.com/stretchr/testify/require" ) @@ -150,11 +149,11 @@ func TestAllocations_RescheduleInfo(t *testing.T) { job.Canonicalize() alloc := &Allocation{ - ID: uuid.Generate(), + ID: generateUUID(), Namespace: DefaultNamespace, - EvalID: uuid.Generate(), + EvalID: generateUUID(), Name: "foo-bar[1]", - NodeID: uuid.Generate(), + NodeID: generateUUID(), TaskGroup: *job.TaskGroups[0].Name, JobID: *job.ID, Job: job, @@ -266,14 +265,14 @@ func TestAllocations_ExecErrors(t *testing.T) { } job.Canonicalize() - allocID := uuid.Generate() + allocID := generateUUID() alloc := &Allocation{ ID: allocID, Namespace: DefaultNamespace, - EvalID: uuid.Generate(), + EvalID: generateUUID(), Name: "foo-bar[1]", - NodeID: uuid.Generate(), + NodeID: generateUUID(), TaskGroup: *job.TaskGroups[0].Name, JobID: *job.ID, Job: job, diff --git a/api/api_test.go b/api/api_test.go index 18fc214d3..e15897da4 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -11,7 +11,6 @@ import ( "testing" "time" - "github.com/hashicorp/go-uuid" "github.com/hashicorp/nomad/api/internal/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -318,24 +317,16 @@ func TestQueryString(t *testing.T) { func TestClient_NodeClient(t *testing.T) { http := "testdomain:4646" tlsNode := func(string, *QueryOptions) (*Node, *QueryMeta, error) { - uu, err := uuid.GenerateUUID() - if err != nil { - t.Fatal(err) - } return &Node{ - ID: uu, + ID: generateUUID(), Status: "ready", HTTPAddr: http, TLSEnabled: true, }, nil, nil } noTlsNode := func(string, *QueryOptions) (*Node, *QueryMeta, error) { - uu, err := uuid.GenerateUUID() - if err != nil { - t.Fatal(err) - } return &Node{ - ID: uu, + ID: generateUUID(), Status: "ready", HTTPAddr: http, TLSEnabled: false, diff --git a/api/util_test.go b/api/util_test.go index 9b2358db2..2ebf502d7 100644 --- a/api/util_test.go +++ b/api/util_test.go @@ -1,6 +1,8 @@ package api import ( + crand "crypto/rand" + "fmt" "testing" ) @@ -90,3 +92,18 @@ func int64ToPtr(i int64) *int64 { func float64ToPtr(f float64) *float64 { return &f } + +// generateUUID generates a uuid useful for testing only +func generateUUID() string { + buf := make([]byte, 16) + if _, err := crand.Read(buf); err != nil { + panic(fmt.Errorf("failed to read random bytes: %v", err)) + } + + return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x", + buf[0:4], + buf[4:6], + buf[6:8], + buf[8:10], + buf[10:16]) +} From 90d81fcd559906d7e7fad9c8a08e8be65a502baa Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Tue, 12 Nov 2019 09:48:04 -0500 Subject: [PATCH 3/4] api: go-uuid is no longer needed --- api/go.mod | 1 - api/go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/api/go.mod b/api/go.mod index 6068ea2f8..8e89f7f58 100644 --- a/api/go.mod +++ b/api/go.mod @@ -8,7 +8,6 @@ require ( github.com/gorilla/websocket v1.4.1 github.com/hashicorp/go-cleanhttp v0.5.1 github.com/hashicorp/go-rootcerts v1.0.0 - github.com/hashicorp/go-uuid v1.0.1 github.com/kr/pretty v0.1.0 github.com/mitchellh/go-testing-interface v1.0.0 github.com/stretchr/testify v1.3.0 diff --git a/api/go.sum b/api/go.sum index c6b86de6c..bf994bbd7 100644 --- a/api/go.sum +++ b/api/go.sum @@ -10,8 +10,6 @@ github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVo github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-rootcerts v1.0.0 h1:Rqb66Oo1X/eSV1x66xbDccZjhJigjg0+e82kpwzSwCI= github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU= -github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE= -github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= From 9e211ce16f06456278436116f22f962c8a6594da Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Tue, 12 Nov 2019 11:33:20 -0500 Subject: [PATCH 4/4] tests appear as github.com/hashicorp/nomad/api.test --- GNUmakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GNUmakefile b/GNUmakefile index 37e3b1c93..3bac30389 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -196,7 +196,7 @@ check: ## Lint the source code @if (git status | grep -q .pb.go); then echo the following proto files are out of sync; git status |grep .pb.go; exit 1; fi @echo "==> Check API package is isolated from rest" - @! go list --test -f '{{ join .Deps "\n" }}' ./api | grep github.com/hashicorp/nomad/ | grep -v -e /vendor/ -e /nomad/api/ + @! go list --test -f '{{ join .Deps "\n" }}' ./api | grep github.com/hashicorp/nomad/ | grep -v -e /vendor/ -e /nomad/api/ -e nomad/api.test .PHONY: checkscripts checkscripts: ## Lint shell scripts