Skip to content

Commit 7822c2c

Browse files
parrobearthurbarr
authored andcommitted
Add basic versioning into main golang file (#151)
* Add basic versioning into main golang file * Add versioning set test * address Arthur's comments
1 parent f0e681e commit 7822c2c

File tree

6 files changed

+203
-3
lines changed

6 files changed

+203
-3
lines changed

Dockerfile-server

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ ARG BUILDER_IMAGE=mq-golang-sdk:9.0.5.0-x86_64-ubuntu-16.04
2020
###############################################################################
2121
FROM $BUILDER_IMAGE as builder
2222
WORKDIR /go/src/github.com/ibm-messaging/mq-container/
23+
ARG IMAGE_REVISION="Not specified"
24+
ARG IMAGE_CREATED="Not specified"
25+
ARG IMAGE_SOURCE="Not specified"
2326
COPY cmd/ ./cmd
2427
COPY internal/ ./internal
2528
COPY vendor/ ./vendor
26-
RUN go build ./cmd/runmqserver/
29+
RUN go build -ldflags "-X \"main.ImageCreated=$IMAGE_CREATED\" -X \"main.ImageRevision=$IMAGE_REVISION\" -X \"main.ImageSource=$IMAGE_SOURCE\"" ./cmd/runmqserver/
2730
RUN go build ./cmd/chkmqready/
2831
RUN go build ./cmd/chkmqhealthy/
2932
# Run all unit tests

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ BASE_IMAGE_TAG=$(subst /,-,$(subst :,-,$(BASE_IMAGE)))
5959
MQ_IMAGE_DEVSERVER_BASE=mqadvanced-server-dev-base:$(MQ_VERSION)-$(ARCH)-$(BASE_IMAGE_TAG)
6060
# Docker image name to use for JMS tests
6161
DEV_JMS_IMAGE=mq-dev-jms-test
62+
# Variables for versioning
63+
IMAGE_REVISION=$(shell git rev-parse HEAD)
64+
IMAGE_SOURCE=$(shell git remote get-url origin)
65+
IMAGE_CREATED=$(shell date -u +%Y-%m-%dT%H:%M:%S%:z)
6266

6367

6468
ifneq (,$(findstring Microsoft,$(shell uname -r)))
@@ -218,6 +222,9 @@ define docker-build-mq
218222
--build-arg MQ_URL=http://build:80/$3 \
219223
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
220224
--build-arg BUILDER_IMAGE=$(MQ_IMAGE_GOLANG_SDK) \
225+
--build-arg IMAGE_REVISION="$(IMAGE_REVISION)" \
226+
--build-arg IMAGE_CREATED="$(IMAGE_CREATED)" \
227+
--build-arg IMAGE_SOURCE="$(IMAGE_SOURCE)" \
221228
--label IBM_PRODUCT_ID=$4 \
222229
--label IBM_PRODUCT_NAME=$5 \
223230
--label IBM_PRODUCT_VERSION=$6 \
@@ -248,7 +255,7 @@ endif
248255
build-devserver: downloads/$(MQ_ARCHIVE_DEV) docker-version build-golang-sdk
249256
$(info $(shell printf $(TITLE)"Build $(MQ_IMAGE_DEVSERVER_BASE)"$(END)))
250257
$(call docker-build-mq,$(MQ_IMAGE_DEVSERVER_BASE),Dockerfile-server,$(MQ_ARCHIVE_DEV),"98102d16795c4263ad9ca075190a2d4d","IBM MQ Advanced for Developers (Non-Warranted)",$(MQ_VERSION))
251-
$(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 .
258+
$(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 .
252259

253260
.PHONY: build-advancedserver-cover
254261
build-advancedserver-cover: docker-version

cmd/runmqserver/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func doMain() error {
7575
return err
7676
}
7777

78+
// Print out versioning information
79+
logVersionInfo()
80+
7881
err = postInit(name)
7982
if err != nil {
8083
logTermination(err)

cmd/runmqserver/version.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
© Copyright IBM Corporation 2018
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"strings"
21+
22+
"github.com/ibm-messaging/mq-container/internal/command"
23+
)
24+
25+
var (
26+
ImageCreated = "Not specified"
27+
ImageRevision = "Not specified"
28+
ImageSource = "Not specified"
29+
)
30+
31+
func logDateStamp() {
32+
log.Printf("Image created: %v", ImageCreated)
33+
}
34+
35+
func logGitRepo() {
36+
log.Printf("Image revision: %v", ImageRevision)
37+
}
38+
39+
func logGitCommit() {
40+
log.Printf("Image source: %v", ImageSource)
41+
}
42+
43+
func logMQVersion() {
44+
mqVersion, _, err := command.Run("dspmqver", "-b", "-f", "2")
45+
if err != nil {
46+
log.Printf("Error Getting MQ version: %v", strings.TrimSuffix(string(mqVersion), "\n"))
47+
}
48+
49+
mqBuild, _, err := command.Run("dspmqver", "-b", "-f", "4")
50+
if err != nil {
51+
log.Printf("Error Getting MQ build: %v", strings.TrimSuffix(string(mqBuild), "\n"))
52+
}
53+
mqLicense, _, err := command.Run("dspmqver", "-b", "-f", "8192")
54+
if err != nil {
55+
log.Printf("Error Getting MQ license: %v", strings.TrimSuffix(string(mqLicense), "\n"))
56+
}
57+
58+
log.Printf("MQ version: %v", strings.TrimSuffix(mqVersion, "\n"))
59+
log.Printf("MQ level: %v", strings.TrimSuffix(mqBuild, "\n"))
60+
log.Printf("MQ license: %v", strings.TrimSuffix(mqLicense, "\n"))
61+
}
62+
63+
func logVersionInfo() {
64+
logDateStamp()
65+
logGitRepo()
66+
logGitCommit()
67+
logMQVersion()
68+
}

incubating/mqadvanced-server-dev/Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ ARG BUILDER_IMAGE=mq-golang-sdk:9.0.5.0-x86_64-ubuntu-16.04
1919
# Build stage to build Go code
2020
###############################################################################
2121
FROM $BUILDER_IMAGE as builder
22+
ARG IMAGE_REVISION="Not specified"
23+
ARG IMAGE_CREATED="Not specified"
24+
ARG IMAGE_SOURCE="Not specified"
2225
WORKDIR /go/src/github.com/ibm-messaging/mq-container/
2326
COPY cmd/ ./cmd
2427
COPY internal/ ./internal
2528
COPY vendor/ ./vendor
2629
# Re-build runmqserver, with code tagged with 'mqdev' enabled
27-
RUN go build --tags 'mqdev' ./cmd/runmqserver
30+
RUN go build -ldflags "-X \"main.ImageCreated=$IMAGE_CREATED\" -X \"main.ImageRevision=$IMAGE_REVISION\" -X \"main.ImageSource=$IMAGE_SOURCE\"" --tags 'mqdev' ./cmd/runmqserver
2831
RUN go build ./cmd/runmqdevserver/
2932
# Run all unit tests
3033
RUN go test -v ./cmd/runmqdevserver/...

test/docker/docker_api_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,119 @@ func TestCorrectLicense(t *testing.T) {
648648
t.Errorf("Expected license to be '%s' but was '%s", expectedLicense, license)
649649
}
650650
}
651+
652+
func TestVersioning(t *testing.T) {
653+
t.Parallel()
654+
655+
cli, err := client.NewEnvClient()
656+
if err != nil {
657+
t.Fatal(err)
658+
}
659+
660+
containerConfig := container.Config{
661+
Env: []string{"LICENSE=accept"},
662+
}
663+
id := runContainer(t, cli, &containerConfig)
664+
defer cleanContainer(t, cli, id)
665+
waitForReady(t, cli, id)
666+
667+
// Get whole logs and check versioning system
668+
l := inspectLogs(t, cli, id)
669+
scanner := bufio.NewScanner(strings.NewReader(l))
670+
671+
total := 6
672+
foundCreated := false
673+
foundRevision := false
674+
foundSource := false
675+
foundMQVersion := false
676+
foundMQLevel := false
677+
foundMQLicense := false
678+
679+
for scanner.Scan() {
680+
line := scanner.Text()
681+
if strings.Contains(line, "Image created:") && !foundCreated {
682+
total--
683+
foundCreated = true
684+
dataAr := strings.Split(line, " ")
685+
data := dataAr[len(dataAr)-1]
686+
687+
// Verify created
688+
_, err := time.Parse(time.RFC3339, data)
689+
if err != nil {
690+
t.Errorf("Failed to validate Image created (%v) - %v", data, err)
691+
}
692+
}
693+
694+
if strings.Contains(line, "Image revision:") && !foundRevision {
695+
total--
696+
foundRevision = true
697+
dataAr := strings.Split(line, " ")
698+
data := dataAr[len(dataAr)-1]
699+
700+
// Verify revision
701+
pattern := regexp.MustCompile("^[a-fA-F0-9]{40}$")
702+
if !pattern.MatchString(data) {
703+
t.Errorf("Failed to validate revision (%v)", data)
704+
}
705+
}
706+
707+
if strings.Contains(line, "Image source:") && !foundSource {
708+
total--
709+
foundSource = true
710+
dataAr := strings.Split(line, " ")
711+
data := dataAr[len(dataAr)-1]
712+
713+
// Verify source
714+
if !strings.Contains(data, "github") {
715+
t.Errorf("Failed to validate source (%v)", data)
716+
}
717+
}
718+
719+
if strings.Contains(line, "MQ version:") && !foundMQVersion {
720+
total--
721+
foundMQVersion = true
722+
dataAr := strings.Split(line, " ")
723+
data := dataAr[len(dataAr)-1]
724+
725+
// Verify MQ version
726+
pattern := regexp.MustCompile("^\\d+\\.\\d+\\.\\d+\\.\\d+$")
727+
if !pattern.MatchString(data) {
728+
t.Errorf("Failed to validate mq version (%v)", data)
729+
}
730+
}
731+
732+
if strings.Contains(line, "MQ level:") && !foundMQLevel {
733+
total--
734+
foundMQLevel = true
735+
dataAr := strings.Split(line, " ")
736+
data := dataAr[len(dataAr)-1]
737+
738+
// Verify MQ version
739+
pattern := regexp.MustCompile("^p\\d{3}-.+$")
740+
if !pattern.MatchString(data) {
741+
t.Errorf("Failed to validate mq level (%v)", data)
742+
}
743+
}
744+
745+
if strings.Contains(line, "MQ license:") && !foundMQLicense {
746+
total--
747+
foundMQLicense = true
748+
dataAr := strings.Split(line, " ")
749+
data := dataAr[len(dataAr)-1]
750+
751+
// Verify MQ version
752+
if data != "Developer" && data != "Production" {
753+
t.Errorf("Failed to validate mq license (%v)", data)
754+
}
755+
}
756+
757+
// end loop early
758+
if total == 0 {
759+
break
760+
}
761+
}
762+
763+
if !foundCreated || !foundRevision || !foundSource || !foundMQVersion || !foundMQLevel || !foundMQLicense {
764+
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)
765+
}
766+
}

0 commit comments

Comments
 (0)