Skip to content

Commit f298dd7

Browse files
committed
Fix handler routes test
1 parent 4be63e6 commit f298dd7

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

server/service/endpoint_utils.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,12 @@ var pathReplacer = strings.NewReplacer(
448448
"}", "_",
449449
)
450450

451-
func getNameFromPathAndVerb(verb, path string) string {
452-
return strings.ToLower(verb) + "_" +
453-
pathReplacer.Replace(strings.TrimPrefix(strings.TrimRight(path, "/"), "/api/_version_/fleet/"))
451+
func getNameFromPathAndVerb(verb, path, startAt string) string {
452+
prefix := strings.ToLower(verb) + "_"
453+
if startAt != "" {
454+
prefix += pathReplacer.Replace(startAt) + "_"
455+
}
456+
return prefix + pathReplacer.Replace(strings.TrimPrefix(strings.TrimRight(path, "/"), "/api/_version_/fleet/"))
454457
}
455458

456459
func capabilitiesResponseFunc(capabilities fleet.CapabilityMap) kithttp.ServerOption {
@@ -560,14 +563,14 @@ func (e *authEndpointer) handlePathHandler(path string, pathHandler func(path st
560563
}
561564

562565
versionedPath := strings.Replace(path, "/_version_/", fmt.Sprintf("/{fleetversion:(?:%s)}/", strings.Join(versions, "|")), 1)
563-
nameAndVerb := getNameFromPathAndVerb(verb, path)
566+
nameAndVerb := getNameFromPathAndVerb(verb, path, e.startingAtVersion)
564567
if e.usePathPrefix {
565568
e.r.PathPrefix(versionedPath).Handler(pathHandler(versionedPath)).Name(nameAndVerb).Methods(verb)
566569
} else {
567570
e.r.Handle(versionedPath, pathHandler(versionedPath)).Name(nameAndVerb).Methods(verb)
568571
}
569572
for _, alias := range e.alternativePaths {
570-
nameAndVerb := getNameFromPathAndVerb(verb, alias)
573+
nameAndVerb := getNameFromPathAndVerb(verb, alias, e.startingAtVersion)
571574
versionedPath := strings.Replace(alias, "/_version_/", fmt.Sprintf("/{fleetversion:(?:%s)}/", strings.Join(versions, "|")), 1)
572575
if e.usePathPrefix {
573576
e.r.PathPrefix(versionedPath).Handler(pathHandler(versionedPath)).Name(nameAndVerb).Methods(verb)

server/service/handler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ func TestAPIRoutesConflicts(t *testing.T) {
7676
}
7777

7878
func TestAPIRoutesMetrics(t *testing.T) {
79-
t.Skip()
8079
ds := new(mock.Store)
8180

8281
svc, _ := newTestService(t, ds, nil, nil)
@@ -108,7 +107,8 @@ func TestAPIRoutesMetrics(t *testing.T) {
108107
routeNames := make(map[string]bool)
109108
err = router.Walk(func(route *mux.Route, _ *mux.Router, _ []*mux.Route) error {
110109
if _, ok := routeNames[route.GetName()]; ok {
111-
t.Errorf("duplicate route name: %s", route.GetName())
110+
path, _ := route.GetPathTemplate()
111+
t.Errorf("duplicate route name: %s (%s)", route.GetName(), path)
112112
}
113113
routeNames[route.GetName()] = true
114114
return nil
@@ -194,7 +194,7 @@ func TestAPIRoutesMetrics(t *testing.T) {
194194
"go_memstats_alloc_bytes_total": 1,
195195
"go_memstats_buck_hash_sys_bytes": 1,
196196
"go_memstats_frees_total": 1,
197-
"go_memstats_gc_cpu_fraction": 1,
197+
"go_memstats_gc_cpu_fraction": 0, // does not appear to be reported anymore
198198
"go_memstats_gc_sys_bytes": 1,
199199
"go_memstats_heap_alloc_bytes": 1,
200200
"go_memstats_heap_idle_bytes": 1,

0 commit comments

Comments
 (0)