diff --git a/Dockerfile-server b/Dockerfile-server index f5ac626..dd67702 100644 --- a/Dockerfile-server +++ b/Dockerfile-server @@ -41,6 +41,7 @@ COPY pkg/ ./pkg COPY vendor/ ./vendor ENV CGO_CFLAGS="-I/opt/mqm/inc/" \ CGO_LDFLAGS_ALLOW="-Wl,-rpath.*" +ENV PATH="${PATH}:/opt/mqm/bin" RUN go build -ldflags "-X \"main.ImageCreated=$(date --iso-8601=seconds)\" -X \"main.ImageRevision=$IMAGE_REVISION\" -X \"main.ImageSource=$IMAGE_SOURCE\" -X \"main.ImageTag=$IMAGE_TAG\"" ./cmd/runmqserver/ RUN go build ./cmd/chkmqready/ RUN go build ./cmd/chkmqhealthy/ diff --git a/cmd/runmqserver/version.go b/cmd/runmqserver/version.go index ea7d6f8..fe57529 100644 --- a/cmd/runmqserver/version.go +++ b/cmd/runmqserver/version.go @@ -20,6 +20,7 @@ import ( "strings" "github.com/ibm-messaging/mq-container/internal/command" + "github.com/ibm-messaging/mq-container/internal/mqversion" ) var ( @@ -50,7 +51,7 @@ func logImageTag() { } func logMQVersion() { - mqVersion, _, err := command.Run("dspmqver", "-b", "-f", "2") + mqVersion, err := mqversion.Get() if err != nil { log.Printf("Error Getting MQ version: %v", strings.TrimSuffix(string(mqVersion), "\n")) } diff --git a/internal/mqversion/mqversion.go b/internal/mqversion/mqversion.go new file mode 100644 index 0000000..de4928c --- /dev/null +++ b/internal/mqversion/mqversion.go @@ -0,0 +1,51 @@ +/* +© Copyright IBM Corporation 2020 + +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 mqversion + +import ( + "fmt" + "strings" + + "github.com/ibm-messaging/mq-container/internal/command" +) + +// Get will return the current MQ version +func Get() (string, error) { + mqVersion, _, err := command.Run("dspmqver", "-b", "-f", "2") + if err != nil { + return "", fmt.Errorf("Error Getting MQ version: %v", err) + } + return strings.TrimSpace(mqVersion), nil +} + +// Compare returns an integer comparing two MQ version strings lexicographically. The result will be 0 if currentVersion==checkVersion, -1 if currentVersion < checkVersion, and +1 if currentVersion > checkVersion +func Compare(checkVersion string) (int, error) { + currentVersion, err := Get() + if err != nil { + return 0, err + } + // trim any suffix from MQ version x.x.x.x + currentVersion = currentVersion[0:7] + if currentVersion < checkVersion { + return -1, nil + } else if currentVersion == checkVersion { + return 0, nil + } else if currentVersion > checkVersion { + return 1, nil + } + return 0, fmt.Errorf("Failed to compare MQ versions") +} diff --git a/internal/mqversion/mqversion_test.go b/internal/mqversion/mqversion_test.go new file mode 100644 index 0000000..1496855 --- /dev/null +++ b/internal/mqversion/mqversion_test.go @@ -0,0 +1,55 @@ +/* +© Copyright IBM Corporation 2020 + +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 mqversion + +import "testing" + +func TestCompareLower(t *testing.T) { + checkVersion := "9.9.9.9" + mqVersionCheck, err := Compare(checkVersion) + if err != nil { + t.Fatalf("Failed to compare MQ versions: %v", err) + } + if mqVersionCheck != -1 { + t.Errorf("MQ version compare result failed. Expected -1, Got %v", mqVersionCheck) + } +} + +func TestCompareHigher(t *testing.T) { + checkVersion := "1.1.1.1" + mqVersionCheck, err := Compare(checkVersion) + if err != nil { + t.Fatalf("Failed to compare MQ versions: %v", err) + } + if mqVersionCheck != 1 { + t.Errorf("MQ version compare result failed. Expected 1, Got %v", mqVersionCheck) + } +} + +func TestCompareEqual(t *testing.T) { + checkVersion, err := Get() + if err != nil { + t.Fatalf("Failed to get current MQ version: %v", err) + } + mqVersionCheck, err := Compare(checkVersion) + if err != nil { + t.Fatalf("Failed to compare MQ versions: %v", err) + } + if mqVersionCheck != 0 { + t.Errorf("MQ version compare result failed. Expected 0, Got %v", mqVersionCheck) + } +}