forked from kuskoman/logstash-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
var ( | ||
// Version is the current version of Logstash Exporter. | ||
Version = "unknown" | ||
|
||
// GitCommit is the git commit hash of the current build. | ||
GitCommit = "unknown" | ||
|
||
// BuildDate is the date of the current build. | ||
BuildDate = "unknown" | ||
) | ||
|
||
// GetBuildInfo returns a VersionInfo struct with the current build information. | ||
func GetBuildInfo() *VersionInfo { | ||
return &VersionInfo{ | ||
Version: Version, | ||
GitCommit: GitCommit, | ||
GoVersion: runtime.Version(), | ||
BuildArch: runtime.GOARCH, | ||
BuildOS: runtime.GOOS, | ||
BuildDate: BuildDate, | ||
} | ||
} | ||
|
||
// VersionInfo contains the current build information. | ||
type VersionInfo struct { | ||
Version string | ||
GitCommit string | ||
GoVersion string | ||
BuildArch string | ||
BuildOS string | ||
BuildDate string | ||
} | ||
|
||
// String returns a string representation of the VersionInfo struct. | ||
func (v *VersionInfo) String() string { | ||
return fmt.Sprintf("%+v", *v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package config | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestGetBuildInfo(t *testing.T) { | ||
versionInfo := GetBuildInfo() | ||
|
||
if versionInfo.Version == "" { | ||
t.Error("Expected Version to be set") | ||
} | ||
|
||
if versionInfo.GitCommit == "" { | ||
t.Error("Expected GitCommit to be set") | ||
} | ||
|
||
if versionInfo.GoVersion != runtime.Version() { | ||
t.Errorf("Expected GoVersion: %s, but got: %s", runtime.Version(), versionInfo.GoVersion) | ||
} | ||
|
||
if versionInfo.BuildArch != runtime.GOARCH { | ||
t.Errorf("Expected BuildArch: %s, but got: %s", runtime.GOARCH, versionInfo.BuildArch) | ||
} | ||
|
||
if versionInfo.BuildOS != runtime.GOOS { | ||
t.Errorf("Expected BuildOS: %s, but got: %s", runtime.GOOS, versionInfo.BuildOS) | ||
} | ||
|
||
if versionInfo.BuildDate == "" { | ||
t.Error("Expected BuildDate to be set") | ||
} | ||
} | ||
|
||
func TestVersionInfoString(t *testing.T) { | ||
versionInfo := &VersionInfo{ | ||
Version: "test-version", | ||
GitCommit: "test-commit", | ||
GoVersion: "test-go-version", | ||
BuildArch: "test-arch", | ||
BuildOS: "test-os", | ||
BuildDate: "test-date", | ||
} | ||
|
||
expectedString := "{Version:test-version GitCommit:test-commit GoVersion:test-go-version BuildArch:test-arch BuildOS:test-os BuildDate:test-date}" | ||
versionInfoString := versionInfo.String() | ||
|
||
if versionInfoString != expectedString { | ||
t.Errorf("Expected string: %s, but got: %s", expectedString, versionInfoString) | ||
} | ||
} |