From 831f0695a99512e42e6cdf1fa1b4240680c8d440 Mon Sep 17 00:00:00 2001 From: Riccardo Biraghi Date: Wed, 11 Apr 2018 13:43:57 +0100 Subject: [PATCH] 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,