From 7822c2c3bc79a6a95f76d3066461dc404c9d149b Mon Sep 17 00:00:00 2001 From: Rob Parker Date: Thu, 5 Jul 2018 11:44:16 +0100 Subject: [PATCH] Add basic versioning into main golang file (#151) * Add basic versioning into main golang file * Add versioning set test * address Arthur's comments --- Dockerfile-server | 5 +- Makefile | 9 +- cmd/runmqserver/main.go | 3 + cmd/runmqserver/version.go | 68 ++++++++++++ incubating/mqadvanced-server-dev/Dockerfile | 5 +- test/docker/docker_api_test.go | 116 ++++++++++++++++++++ 6 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 cmd/runmqserver/version.go diff --git a/Dockerfile-server b/Dockerfile-server index 3edebf4..aa46d76 100644 --- a/Dockerfile-server +++ b/Dockerfile-server @@ -20,10 +20,13 @@ ARG BUILDER_IMAGE=mq-golang-sdk:9.0.5.0-x86_64-ubuntu-16.04 ############################################################################### FROM $BUILDER_IMAGE as builder WORKDIR /go/src/github.com/ibm-messaging/mq-container/ +ARG IMAGE_REVISION="Not specified" +ARG IMAGE_CREATED="Not specified" +ARG IMAGE_SOURCE="Not specified" COPY cmd/ ./cmd COPY internal/ ./internal COPY vendor/ ./vendor -RUN go build ./cmd/runmqserver/ +RUN go build -ldflags "-X \"main.ImageCreated=$IMAGE_CREATED\" -X \"main.ImageRevision=$IMAGE_REVISION\" -X \"main.ImageSource=$IMAGE_SOURCE\"" ./cmd/runmqserver/ RUN go build ./cmd/chkmqready/ RUN go build ./cmd/chkmqhealthy/ # Run all unit tests diff --git a/Makefile b/Makefile index 17b8584..ce12826 100644 --- a/Makefile +++ b/Makefile @@ -59,6 +59,10 @@ BASE_IMAGE_TAG=$(subst /,-,$(subst :,-,$(BASE_IMAGE))) MQ_IMAGE_DEVSERVER_BASE=mqadvanced-server-dev-base:$(MQ_VERSION)-$(ARCH)-$(BASE_IMAGE_TAG) # Docker image name to use for JMS tests DEV_JMS_IMAGE=mq-dev-jms-test +# Variables for versioning +IMAGE_REVISION=$(shell git rev-parse HEAD) +IMAGE_SOURCE=$(shell git remote get-url origin) +IMAGE_CREATED=$(shell date -u +%Y-%m-%dT%H:%M:%S%:z) ifneq (,$(findstring Microsoft,$(shell uname -r))) @@ -218,6 +222,9 @@ define docker-build-mq --build-arg MQ_URL=http://build:80/$3 \ --build-arg BASE_IMAGE=$(BASE_IMAGE) \ --build-arg BUILDER_IMAGE=$(MQ_IMAGE_GOLANG_SDK) \ + --build-arg IMAGE_REVISION="$(IMAGE_REVISION)" \ + --build-arg IMAGE_CREATED="$(IMAGE_CREATED)" \ + --build-arg IMAGE_SOURCE="$(IMAGE_SOURCE)" \ --label IBM_PRODUCT_ID=$4 \ --label IBM_PRODUCT_NAME=$5 \ --label IBM_PRODUCT_VERSION=$6 \ @@ -248,7 +255,7 @@ endif build-devserver: downloads/$(MQ_ARCHIVE_DEV) docker-version build-golang-sdk $(info $(shell printf $(TITLE)"Build $(MQ_IMAGE_DEVSERVER_BASE)"$(END))) $(call docker-build-mq,$(MQ_IMAGE_DEVSERVER_BASE),Dockerfile-server,$(MQ_ARCHIVE_DEV),"98102d16795c4263ad9ca075190a2d4d","IBM MQ Advanced for Developers (Non-Warranted)",$(MQ_VERSION)) - $(DOCKER) build --tag $(MQ_IMAGE_DEVSERVER) --build-arg BASE_IMAGE=$(MQ_IMAGE_DEVSERVER_BASE) --build-arg BUILDER_IMAGE=$(MQ_IMAGE_GOLANG_SDK) --file incubating/mqadvanced-server-dev/Dockerfile . + $(DOCKER) build --tag $(MQ_IMAGE_DEVSERVER) --build-arg IMAGE_SOURCE="$(IMAGE_SOURCE)" --build-arg IMAGE_REVISION="$(IMAGE_REVISION)" --build-arg IMAGE_CREATED="$(IMAGE_CREATED)" --build-arg BASE_IMAGE=$(MQ_IMAGE_DEVSERVER_BASE) --build-arg BUILDER_IMAGE=$(MQ_IMAGE_GOLANG_SDK) --file incubating/mqadvanced-server-dev/Dockerfile . .PHONY: build-advancedserver-cover build-advancedserver-cover: docker-version diff --git a/cmd/runmqserver/main.go b/cmd/runmqserver/main.go index cee73de..3c100a2 100644 --- a/cmd/runmqserver/main.go +++ b/cmd/runmqserver/main.go @@ -75,6 +75,9 @@ func doMain() error { return err } + // Print out versioning information + logVersionInfo() + err = postInit(name) if err != nil { logTermination(err) diff --git a/cmd/runmqserver/version.go b/cmd/runmqserver/version.go new file mode 100644 index 0000000..bab2fc5 --- /dev/null +++ b/cmd/runmqserver/version.go @@ -0,0 +1,68 @@ +/* +© Copyright IBM Corporation 2018 + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +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 main + +import ( + "strings" + + "github.com/ibm-messaging/mq-container/internal/command" +) + +var ( + ImageCreated = "Not specified" + ImageRevision = "Not specified" + ImageSource = "Not specified" +) + +func logDateStamp() { + log.Printf("Image created: %v", ImageCreated) +} + +func logGitRepo() { + log.Printf("Image revision: %v", ImageRevision) +} + +func logGitCommit() { + log.Printf("Image source: %v", ImageSource) +} + +func logMQVersion() { + mqVersion, _, err := command.Run("dspmqver", "-b", "-f", "2") + if err != nil { + log.Printf("Error Getting MQ version: %v", strings.TrimSuffix(string(mqVersion), "\n")) + } + + mqBuild, _, err := command.Run("dspmqver", "-b", "-f", "4") + if err != nil { + log.Printf("Error Getting MQ build: %v", strings.TrimSuffix(string(mqBuild), "\n")) + } + mqLicense, _, err := command.Run("dspmqver", "-b", "-f", "8192") + if err != nil { + log.Printf("Error Getting MQ license: %v", strings.TrimSuffix(string(mqLicense), "\n")) + } + + log.Printf("MQ version: %v", strings.TrimSuffix(mqVersion, "\n")) + log.Printf("MQ level: %v", strings.TrimSuffix(mqBuild, "\n")) + log.Printf("MQ license: %v", strings.TrimSuffix(mqLicense, "\n")) +} + +func logVersionInfo() { + logDateStamp() + logGitRepo() + logGitCommit() + logMQVersion() +} diff --git a/incubating/mqadvanced-server-dev/Dockerfile b/incubating/mqadvanced-server-dev/Dockerfile index 3d850d5..208ee52 100644 --- a/incubating/mqadvanced-server-dev/Dockerfile +++ b/incubating/mqadvanced-server-dev/Dockerfile @@ -19,12 +19,15 @@ ARG BUILDER_IMAGE=mq-golang-sdk:9.0.5.0-x86_64-ubuntu-16.04 # Build stage to build Go code ############################################################################### FROM $BUILDER_IMAGE as builder +ARG IMAGE_REVISION="Not specified" +ARG IMAGE_CREATED="Not specified" +ARG IMAGE_SOURCE="Not specified" WORKDIR /go/src/github.com/ibm-messaging/mq-container/ COPY cmd/ ./cmd COPY internal/ ./internal COPY vendor/ ./vendor # Re-build runmqserver, with code tagged with 'mqdev' enabled -RUN go build --tags 'mqdev' ./cmd/runmqserver +RUN go build -ldflags "-X \"main.ImageCreated=$IMAGE_CREATED\" -X \"main.ImageRevision=$IMAGE_REVISION\" -X \"main.ImageSource=$IMAGE_SOURCE\"" --tags 'mqdev' ./cmd/runmqserver RUN go build ./cmd/runmqdevserver/ # Run all unit tests RUN go test -v ./cmd/runmqdevserver/... diff --git a/test/docker/docker_api_test.go b/test/docker/docker_api_test.go index 0299556..b5f05a5 100644 --- a/test/docker/docker_api_test.go +++ b/test/docker/docker_api_test.go @@ -648,3 +648,119 @@ func TestCorrectLicense(t *testing.T) { t.Errorf("Expected license to be '%s' but was '%s", expectedLicense, license) } } + +func TestVersioning(t *testing.T) { + t.Parallel() + + cli, err := client.NewEnvClient() + if err != nil { + t.Fatal(err) + } + + containerConfig := container.Config{ + Env: []string{"LICENSE=accept"}, + } + id := runContainer(t, cli, &containerConfig) + defer cleanContainer(t, cli, id) + waitForReady(t, cli, id) + + // Get whole logs and check versioning system + l := inspectLogs(t, cli, id) + scanner := bufio.NewScanner(strings.NewReader(l)) + + total := 6 + foundCreated := false + foundRevision := false + foundSource := false + foundMQVersion := false + foundMQLevel := false + foundMQLicense := false + + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, "Image created:") && !foundCreated { + total-- + foundCreated = true + dataAr := strings.Split(line, " ") + data := dataAr[len(dataAr)-1] + + // Verify created + _, err := time.Parse(time.RFC3339, data) + if err != nil { + t.Errorf("Failed to validate Image created (%v) - %v", data, err) + } + } + + if strings.Contains(line, "Image revision:") && !foundRevision { + total-- + foundRevision = true + dataAr := strings.Split(line, " ") + data := dataAr[len(dataAr)-1] + + // Verify revision + pattern := regexp.MustCompile("^[a-fA-F0-9]{40}$") + if !pattern.MatchString(data) { + t.Errorf("Failed to validate revision (%v)", data) + } + } + + if strings.Contains(line, "Image source:") && !foundSource { + total-- + foundSource = true + dataAr := strings.Split(line, " ") + data := dataAr[len(dataAr)-1] + + // Verify source + if !strings.Contains(data, "github") { + t.Errorf("Failed to validate source (%v)", data) + } + } + + if strings.Contains(line, "MQ version:") && !foundMQVersion { + total-- + foundMQVersion = true + dataAr := strings.Split(line, " ") + data := dataAr[len(dataAr)-1] + + // Verify MQ version + pattern := regexp.MustCompile("^\\d+\\.\\d+\\.\\d+\\.\\d+$") + if !pattern.MatchString(data) { + t.Errorf("Failed to validate mq version (%v)", data) + } + } + + if strings.Contains(line, "MQ level:") && !foundMQLevel { + total-- + foundMQLevel = true + dataAr := strings.Split(line, " ") + data := dataAr[len(dataAr)-1] + + // Verify MQ version + pattern := regexp.MustCompile("^p\\d{3}-.+$") + if !pattern.MatchString(data) { + t.Errorf("Failed to validate mq level (%v)", data) + } + } + + if strings.Contains(line, "MQ license:") && !foundMQLicense { + total-- + foundMQLicense = true + dataAr := strings.Split(line, " ") + data := dataAr[len(dataAr)-1] + + // Verify MQ version + if data != "Developer" && data != "Production" { + t.Errorf("Failed to validate mq license (%v)", data) + } + } + + // end loop early + if total == 0 { + break + } + } + + if !foundCreated || !foundRevision || !foundSource || !foundMQVersion || !foundMQLevel || !foundMQLicense { + t.Errorf("Failed to find one or more version strings: created(%v) revision(%v) source(%v) mqversion(%v) mqlevel(%v) mqlicense(%v)", foundCreated, foundRevision, foundSource, foundMQVersion, foundMQLevel, foundMQLicense) + } +}