-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module versions to /version info
This commit adds module version info to the /version endpoint. Due to an open Go bug, the commit is not testable yet. A unit test has been added anyways, with a canary that should die when Go fixes the bug on their end. Signed-off-by: Eric Chlebek <eric@sensu.io>
- Loading branch information
Showing
5 changed files
with
71 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
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
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
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,41 @@ | ||
package types | ||
|
||
import ( | ||
"path" | ||
"runtime/debug" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var ( | ||
apiModuleVersions = map[string]string{} | ||
apiModuleVersionsOnce sync.Once | ||
) | ||
|
||
// APIModuleVersions returns a map of Sensu API modules that are compiled into | ||
// the product. | ||
func APIModuleVersions() map[string]string { | ||
apiModuleVersionsOnce.Do(loadAPIModuleVersions) | ||
mapCopy := make(map[string]string, len(apiModuleVersions)) | ||
for k, v := range apiModuleVersions { | ||
mapCopy[k] = v | ||
} | ||
return mapCopy | ||
} | ||
|
||
func loadAPIModuleVersions() { | ||
buildInfo, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return | ||
} | ||
packageMapMu.Lock() | ||
defer packageMapMu.Unlock() | ||
for k := range packageMap { | ||
for _, mod := range buildInfo.Deps { | ||
if strings.HasSuffix(mod.Path, path.Join("api", k)) { | ||
apiModuleVersions[k] = mod.Version | ||
break | ||
} | ||
} | ||
} | ||
} |
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,26 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"runtime/debug" | ||
|
||
types "github.com/sensu/sensu-go/types" | ||
) | ||
|
||
// TODO(eric): This test doesn't work yet because of https://github.com/golang/go/issues/33976 | ||
func TestAPIModuleVersions(t *testing.T) { | ||
_, ok := debug.ReadBuildInfo() | ||
if ok { | ||
t.Fatal("remove this if block, the test works now") | ||
} else { | ||
t.Skip() | ||
} | ||
modVersions := types.APIModuleVersions() | ||
if _, ok := modVersions["core/v2"]; !ok { | ||
t.Errorf("missing core/v2 module version") | ||
} | ||
if _, ok := modVersions["core/v3"]; !ok { | ||
t.Errorf("missing core/v3 module version") | ||
} | ||
} |