From 831f0695a99512e42e6cdf1fa1b4240680c8d440 Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Wed, 11 Apr 2018 13:43:57 +0100 Subject: [PATCH 1/7] Add vetting, formatting and vetting make commands and fix related errors --- .travis.yml | 1 + Makefile | 24 +++++++++++++++++++++++- cmd/runmqdevserver/keystore.go | 4 +++- internal/capabilities/capabilities.go | 2 +- internal/logger/logger.go | 11 +++++++++++ internal/mqini/mqini.go | 2 ++ test/docker/docker_api_test.go | 4 ++-- test/docker/docker_api_test_util.go | 15 ++++++--------- 8 files changed, 49 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index b75bd0f..4a761a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,5 +21,6 @@ install: script: - make deps + - make vet - make build-devserver - make test-devserver diff --git a/Makefile b/Makefile index eaa79d5..e57a013 100644 --- a/Makefile +++ b/Makefile @@ -100,7 +100,7 @@ all: build-devserver build-advancedserver test-all: test-devserver test-advancedserver .PHONY: precommit -precommit: all test-all +precommit: vet fmt all test-all lint .PHONY: devserver devserver: build-devserver test-devserver @@ -235,4 +235,26 @@ build-advancedserver-cover: docker-version build-explorer: downloads/$(MQ_ARCHIVE_DEV) $(call docker-build-mq,mq-explorer:latest-$(ARCH),incubating/mq-explorer/Dockerfile-mq-explorer,$(MQ_ARCHIVE_DEV),"98102d16795c4263ad9ca075190a2d4d","IBM MQ Advanced for Developers (Non-Warranted)",$(MQ_VERSION)) +# Select all go packages inside the test directory +GO_TEST_PKG = $(sort $(dir $(wildcard ./test/*/*.go))) +GO_TEST_FILES = $(addsuffix /$(wildcard *.go), $(GO_TEST_PKG)) + +# Select all go packages inside the internal directory +GO_INTERNAL_PKG = $(sort $(dir $(wildcard ./internal/*/*.go))) +GO_INTERNAL_FILES = $(addsuffix /$(wildcard *.go), $(GO_CMD_PKG)) + +# Select all go packages inside the cmd directory +GO_CMD_PKG = $(sort $(dir $(wildcard ./cmd/*/*.go))) +GO_CMD_FILES = $(addsuffix /$(wildcard *.go), $(GO_CMD_PKG)) + +fmt: $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) + go fmt $(GO_TEST_PKG) $(GO_CMD_PKG) $(GO_INTERNAL_PKG) + +vet: $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) + go vet $(GO_TEST_PKG) $(GO_CMD_PKG) $(GO_INTERNAL_PKG) + +lint: $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) + echo "\033[0;31m"; golint $(GO_TEST_PKG) $(GO_CMD_PKG) $(GO_INTERNAL_PKG) + @echo "\033[0m" + include formatting.mk diff --git a/cmd/runmqdevserver/keystore.go b/cmd/runmqdevserver/keystore.go index 1b49d68..3c9e7e7 100644 --- a/cmd/runmqdevserver/keystore.go +++ b/cmd/runmqdevserver/keystore.go @@ -25,6 +25,7 @@ import ( "github.com/ibm-messaging/mq-container/internal/command" ) +// KeyStore describes information about a keystore file type KeyStore struct { Filename string Password string @@ -72,7 +73,7 @@ func (ks *KeyStore) Create() error { return nil } -// Create a key stash, if it doesn't already exist +// CreateStash creates a key stash, if it doesn't already exist func (ks *KeyStore) CreateStash() error { extension := filepath.Ext(ks.Filename) stashFile := ks.Filename[0:len(ks.Filename)-len(extension)] + ".sth" @@ -96,6 +97,7 @@ func (ks *KeyStore) CreateStash() error { return nil } +// Import imports a certificate file in the keystore func (ks *KeyStore) Import(inputFile, password string) error { _, _, err := command.Run(ks.command, "-cert", "-import", "-file", inputFile, "-pw", password, "-target", ks.Filename, "-target_pw", ks.Password, "-target_type", ks.keyStoreType) if err != nil { diff --git a/internal/capabilities/capabilities.go b/internal/capabilities/capabilities.go index 2cdbc3a..be550fd 100644 --- a/internal/capabilities/capabilities.go +++ b/internal/capabilities/capabilities.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -// capabilities allows querying of information on Linux capabilities +// Package capabilities allows querying of information on Linux capabilities package capabilities import ( diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 619b43e..6e9b5aa 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Package logger provides utility functions for logging purposes package logger import ( @@ -108,46 +109,56 @@ func (l *Logger) log(level string, msg string) { l.mutex.Unlock() } +// LogDirect logs a message directly to stdout func (l *Logger) LogDirect(msg string) { fmt.Println(msg) } +// Debug logs a line as debug func (l *Logger) Debug(args ...interface{}) { if l.debug { l.log(debugLevel, fmt.Sprint(args...)) } } +// Debugf logs a line as debug using format specifiers func (l *Logger) Debugf(format string, args ...interface{}) { if l.debug { l.log(debugLevel, fmt.Sprintf(format, args...)) } } +// Print logs a message as info func (l *Logger) Print(args ...interface{}) { l.log(infoLevel, fmt.Sprint(args...)) } +// Println logs a message func (l *Logger) Println(args ...interface{}) { l.Print(args...) } +// Printf logs a message as info using format specifiers func (l *Logger) Printf(format string, args ...interface{}) { l.log(infoLevel, fmt.Sprintf(format, args...)) } +// PrintString logs a string as info func (l *Logger) PrintString(msg string) { l.log(infoLevel, msg) } +// Errorf logs a message as error func (l *Logger) Error(args ...interface{}) { l.log(errorLevel, fmt.Sprint(args...)) } +// Errorf logs a message as error using format specifiers func (l *Logger) Errorf(format string, args ...interface{}) { l.log(errorLevel, fmt.Sprintf(format, args...)) } +// Fatalf logs a message as fatal using format specifiers // TODO: Remove this func (l *Logger) Fatalf(format string, args ...interface{}) { l.log("FATAL", fmt.Sprintf(format, args...)) diff --git a/internal/mqini/mqini.go b/internal/mqini/mqini.go index d5ad25e..4bfd846 100644 --- a/internal/mqini/mqini.go +++ b/internal/mqini/mqini.go @@ -13,6 +13,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ + +// Package mqini provides information about queue managers package mqini import ( diff --git a/test/docker/docker_api_test.go b/test/docker/docker_api_test.go index 337b667..f06fc7d 100644 --- a/test/docker/docker_api_test.go +++ b/test/docker/docker_api_test.go @@ -296,7 +296,7 @@ func TestVolumeUnmount(t *testing.T) { t.Errorf("Expected chkmqhealthy to fail") _, df := execContainer(t, cli, ctr.ID, "mqm", []string{"df"}) t.Logf(df) - _, ps :=execContainer(t, cli, ctr.ID, "mqm", []string{"ps", "-ef"}) + _, ps := execContainer(t, cli, ctr.ID, "mqm", []string{"ps", "-ef"}) t.Logf(ps) } } @@ -404,7 +404,7 @@ func TestReadiness(t *testing.T) { t.Logf("readyRC=%v,queueCheckRC=%v\n", readyRC, queueCheckRC) if readyRC == 0 { - if (queueCheckRC != 0) { + if queueCheckRC != 0 { r := regexp.MustCompile("AMQ[0-9][0-9][0-9][0-9]E") t.Fatalf("Runmqsc returned %v with error %v. chkmqready returned %v when MQSC had not finished", queueCheckRC, r.FindString(queueCheckOut), readyRC) } else { diff --git a/test/docker/docker_api_test_util.go b/test/docker/docker_api_test_util.go index e891ba7..264fb9d 100644 --- a/test/docker/docker_api_test_util.go +++ b/test/docker/docker_api_test_util.go @@ -27,12 +27,12 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "strconv" "strings" "testing" "time" - "regexp" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" @@ -86,31 +86,28 @@ func coverageBind(t *testing.T) string { func isWSL(t *testing.T) bool { if runtime.GOOS == "linux" { uname, err := exec.Command("uname", "-r").Output() - if (err != nil) { + if err != nil { t.Fatal(err) } return strings.Contains(string(uname), "Microsoft") - } else { - return false } + return false } // getWindowsRoot get the path of the root directory on Windows, in UNIX or OS-specific style func getWindowsRoot(unixStylePath bool) string { if unixStylePath { return "/mnt/c/" - } else { - return "C:/" } + return "C:/" } // getTempDir get the path of the tmp directory, in UNIX or OS-specific style func getTempDir(t *testing.T, unixStylePath bool) string { if isWSL(t) { return getWindowsRoot(unixStylePath) + "Temp/" - } else { - return "/tmp/" } + return "/tmp/" } // terminationLogUnixPath returns the name of the file to use for the termination log message, with a UNIX path @@ -304,7 +301,7 @@ func waitForContainer(t *testing.T, cli *client.Client, ID string, timeout int64 // execContainer runs a command in a running container, and returns the exit code and output func execContainer(t *testing.T, cli *client.Client, ID string, user string, cmd []string) (int, string) { - rerun: +rerun: config := types.ExecConfig{ User: user, Privileged: false, From 3d569b3e8cac9582a1e7f734fac7c5d6c7a77c5a Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Wed, 11 Apr 2018 14:44:58 +0100 Subject: [PATCH 2/7] Fix make vet in Travis --- .travis.yml | 2 ++ Makefile | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4a761a4..8ecd990 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,8 @@ go: services: - docker +go_import_path: github.com/ibm-messagin/mq-container + before_install: - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" diff --git a/Makefile b/Makefile index e57a013..357f598 100644 --- a/Makefile +++ b/Makefile @@ -250,7 +250,7 @@ GO_CMD_FILES = $(addsuffix /$(wildcard *.go), $(GO_CMD_PKG)) fmt: $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) go fmt $(GO_TEST_PKG) $(GO_CMD_PKG) $(GO_INTERNAL_PKG) -vet: $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) +vet: test/docker/vendor $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) go vet $(GO_TEST_PKG) $(GO_CMD_PKG) $(GO_INTERNAL_PKG) lint: $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) From 460aec38632f63899b3ce8b1f69e8e771df1b08b Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Wed, 11 Apr 2018 15:00:43 +0100 Subject: [PATCH 3/7] Correct go_import_path in .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8ecd990..a61bac3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ go: services: - docker -go_import_path: github.com/ibm-messagin/mq-container +go_import_path: "github.com/ibm-messaging/mq-container" before_install: - curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - From c55d8f3956830496ca111c90b12f034a40f68b4f Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Wed, 11 Apr 2018 15:52:09 +0100 Subject: [PATCH 4/7] Use /... instead of listing packages --- Makefile | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 357f598..e82a7ec 100644 --- a/Makefile +++ b/Makefile @@ -100,7 +100,7 @@ all: build-devserver build-advancedserver test-all: test-devserver test-advancedserver .PHONY: precommit -precommit: vet fmt all test-all lint +precommit: vet fmt lint all test-all .PHONY: devserver devserver: build-devserver test-devserver @@ -144,7 +144,7 @@ test-unit: .PHONY: test-advancedserver test-advancedserver: test/docker/vendor $(info $(SPACER)$(shell printf $(TITLE)"Test $(MQ_IMAGE_ADVANCEDSERVER) on Docker"$(END))) - cd test/docker && TEST_IMAGE=$(MQ_IMAGE_ADVANCEDSERVER) go test -timeout 20m -parallel $(NUM_CPU) $(TEST_OPTS_DOCKER) + cd test/docker && TEST_IMAGE=$(MQ_IMAGE_ADVANCEDSERVER) go test -parallel $(NUM_CPU) $(TEST_OPTS_DOCKER) .PHONY: build-devjmstest build-devjmstest: @@ -235,26 +235,17 @@ build-advancedserver-cover: docker-version build-explorer: downloads/$(MQ_ARCHIVE_DEV) $(call docker-build-mq,mq-explorer:latest-$(ARCH),incubating/mq-explorer/Dockerfile-mq-explorer,$(MQ_ARCHIVE_DEV),"98102d16795c4263ad9ca075190a2d4d","IBM MQ Advanced for Developers (Non-Warranted)",$(MQ_VERSION)) -# Select all go packages inside the test directory -GO_TEST_PKG = $(sort $(dir $(wildcard ./test/*/*.go))) -GO_TEST_FILES = $(addsuffix /$(wildcard *.go), $(GO_TEST_PKG)) +GO_PKG_DIRS = ./cmd ./internal ./test -# Select all go packages inside the internal directory -GO_INTERNAL_PKG = $(sort $(dir $(wildcard ./internal/*/*.go))) -GO_INTERNAL_FILES = $(addsuffix /$(wildcard *.go), $(GO_CMD_PKG)) +fmt: $(addsuffix /$(wildcard *.go), $(GO_PKG_DIRS)) + go fmt $(addsuffix /..., $(GO_PKG_DIRS)) -# Select all go packages inside the cmd directory -GO_CMD_PKG = $(sort $(dir $(wildcard ./cmd/*/*.go))) -GO_CMD_FILES = $(addsuffix /$(wildcard *.go), $(GO_CMD_PKG)) +vet: $(addsuffix /$(wildcard *.go), $(GO_PKG_DIRS)) test/docker/vendor + go vet $(addsuffix /..., $(GO_PKG_DIRS)) -fmt: $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) - go fmt $(GO_TEST_PKG) $(GO_CMD_PKG) $(GO_INTERNAL_PKG) - -vet: test/docker/vendor $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) - go vet $(GO_TEST_PKG) $(GO_CMD_PKG) $(GO_INTERNAL_PKG) - -lint: $(GO_CMD_FILES) $(GO_TEST_FILES) $(GO_INTERNAL_FILES) - echo "\033[0;31m"; golint $(GO_TEST_PKG) $(GO_CMD_PKG) $(GO_INTERNAL_PKG) - @echo "\033[0m" +lint: $(addsuffix /$(wildcard *.go), $(GO_PKG_DIRS)) + @# This expression is necessary because /... includes the vendor directory in golint + @# As of 11/04/2018 there is an open issue to fix it: https://github.com/golang/lint/issues/320 + golint -set_exit_status $(sort $(dir $(wildcard $(addsuffix /*/*.go, $(GO_PKG_DIRS))))) include formatting.mk From cde2a5186e2ae2e716ab3cb9fa4c4e137e4bac49 Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Wed, 11 Apr 2018 16:05:37 +0100 Subject: [PATCH 5/7] Have Travis run golint after build --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index a61bac3..3d6c621 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,3 +26,6 @@ script: - make vet - make build-devserver - make test-devserver + +after_success: + - make lint From 1affe991cc3cffc522e9c52981512483eed65e72 Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Wed, 11 Apr 2018 16:46:52 +0100 Subject: [PATCH 6/7] Add go get golint to travis yaml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 3d6c621..d729bdf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,4 +28,5 @@ script: - make test-devserver after_success: + - go get golang.org/x/lint/golint - make lint From 49e90d2e54e60d9a70dc0f392002b1856e2e411e Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Thu, 12 Apr 2018 09:06:05 +0100 Subject: [PATCH 7/7] Update copyright statement in capabilities.go --- internal/capabilities/capabilities.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/capabilities/capabilities.go b/internal/capabilities/capabilities.go index be550fd..2c7beb8 100644 --- a/internal/capabilities/capabilities.go +++ b/internal/capabilities/capabilities.go @@ -1,5 +1,5 @@ /* -© Copyright IBM Corporation 2017 +© Copyright IBM Corporation 2017, 2018 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.