Merged in feature/log-version (pull request #185)

inject and print version at startup for all services

* inject and print version

* coverage
This commit is contained in:
Jay Brown
2025-09-18 21:06:21 +00:00
parent 8a13b6d3b1
commit a6e081e5cd
17 changed files with 233 additions and 5 deletions
+125
View File
@@ -1,7 +1,10 @@
package build
import (
"bytes"
"os"
"runtime/debug"
"strings"
"testing"
"time"
@@ -129,3 +132,125 @@ func TestGetVersion(t *testing.T) {
t.Errorf("Version not consistent between calls: '%s' != '%s'", version, version2)
}
}
func TestGetGitCommit(t *testing.T) {
// Test that GetGitCommit returns a non-empty string
commit := GetGitCommit()
if commit == "" {
t.Error("Git commit should not be empty")
}
// In test environment, it should return the default value
if commit != "unknown" && commit != gitCommit {
t.Errorf("Unexpected commit value: %s", commit)
}
}
func TestGetBuildTime(t *testing.T) {
// Test that GetBuildTime returns a non-empty string
bt := GetBuildTime()
if bt == "" {
t.Error("Build time should not be empty")
}
// In test environment, it should return the default value
if bt != "unknown" && bt != buildTime {
t.Errorf("Unexpected build time value: %s", bt)
}
}
func TestPrintVersionInfo(t *testing.T) {
// Save original stderr
oldStderr := os.Stderr
defer func() { os.Stderr = oldStderr }()
// Create a pipe to capture stderr
r, w, _ := os.Pipe()
os.Stderr = w
// Test 1: PrintVersionInfo with default values (devel)
t.Run("with devel version", func(t *testing.T) {
// Save original values
originalVersion := version
originalCommit := gitCommit
originalBuildTime := buildTime
// Set test values
version = "(devel)"
gitCommit = "testcommit"
buildTime = "testtime"
// Call PrintVersionInfo
PrintVersionInfo("testService")
// Close writer and read output
w.Close()
var buf bytes.Buffer
_, err := buf.ReadFrom(r)
if err != nil {
t.Fatalf("Failed to read from pipe: %v", err)
}
output := buf.String()
// Check output contains expected parts
if !strings.Contains(output, "Starting testService") {
t.Errorf("Output should contain service name, got: %s", output)
}
if !strings.Contains(output, "version=") {
t.Errorf("Output should contain version, got: %s", output)
}
// Restore original values
version = originalVersion
gitCommit = originalCommit
buildTime = originalBuildTime
})
// Create new pipe for second test
r2, w2, _ := os.Pipe()
os.Stderr = w2
// Test 2: PrintVersionInfo with custom values
t.Run("with custom version", func(t *testing.T) {
// Save original values
originalVersion := version
originalCommit := gitCommit
originalBuildTime := buildTime
// Set test values
version = "20231201.123456-abc123"
gitCommit = "abc123def456"
buildTime = "2023-12-01T12:34:56Z"
// Call PrintVersionInfo
PrintVersionInfo("customService")
// Close writer and read output
w2.Close()
var buf bytes.Buffer
_, err := buf.ReadFrom(r2)
if err != nil {
t.Fatalf("Failed to read from pipe: %v", err)
}
output := buf.String()
// Check output contains all expected parts
if !strings.Contains(output, "Starting customService") {
t.Errorf("Output should contain service name, got: %s", output)
}
if !strings.Contains(output, "version=20231201.123456-abc123") {
t.Errorf("Output should contain version, got: %s", output)
}
if !strings.Contains(output, "buildTime=2023-12-01T12:34:56Z") {
t.Errorf("Output should contain build time, got: %s", output)
}
if !strings.Contains(output, "commit=abc123def456") {
t.Errorf("Output should contain commit, got: %s", output)
}
// Restore original values
version = originalVersion
gitCommit = originalCommit
buildTime = originalBuildTime
})
}