-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ControllerName struct, and test cases (#84)
* add regex and unit test for checking if the given controller name is in line with prometheus best practices * added detail to comment * updating regex * moved to test idr * fixing import chnage * moving test case * add controller utils * fix trim on spaces * add test case for MetricsName * testing logger name also * fixing bug in clean * moved the test utils test to controller utisl * using a New function instead of clean * updated tests to use New * fixing value of test case string * update comment on conrollerName struc * fix typo in test cases * adding test cases for spaces * removing test that no longer has value * updated removespace to instead only allow letters * testing strip * fixing strip method * fixing stripped funciton * specialchars test --------- Co-authored-by: Marcus Hines <[email protected]>
- Loading branch information
1 parent
4a0ab0f
commit e4d9c12
Showing
4 changed files
with
127 additions
and
29 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,48 @@ | ||
package controllername | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
const ( | ||
metricsNameDelimiter = "_" | ||
loggerNameDelimiter = "-" | ||
) | ||
|
||
type ControllerNamer interface { | ||
MetricsName() string | ||
LoggerName() string | ||
} | ||
|
||
// controllerName ex. {"My","Controller", "Name"} -> MyControllerName | ||
type controllerName []string | ||
|
||
func NewControllerName(name []string) controllerName { | ||
cn := make(controllerName, len(name)) | ||
|
||
for i, w := range name { | ||
cn[i] = strip(strings.ToLower(w)) | ||
|
||
} | ||
return cn | ||
} | ||
|
||
// Strip removes spaces and non letters | ||
func strip(s string) string { | ||
rr := make([]rune, 0, len(s)) | ||
for _, r := range s { | ||
if unicode.IsLetter(r) { | ||
rr = append(rr, r) | ||
} | ||
} | ||
return string(rr) | ||
} | ||
|
||
func (c controllerName) MetricsName() string { | ||
return strings.Join(c, metricsNameDelimiter) | ||
} | ||
|
||
func (c controllerName) LoggerName() string { | ||
return strings.Join(c, loggerNameDelimiter) | ||
} |
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,79 @@ | ||
package controllername | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestMetricsName(t *testing.T) { | ||
|
||
cn1 := NewControllerName([]string{"SomeFakeControllerName"}) | ||
cn2 := NewControllerName([]string{"Some", "Controller", "Name"}) | ||
cn3 := NewControllerName([]string{" SomeName", "Entered ", "poorly"}) | ||
cn4 := NewControllerName([]string{"Some Spaces"}) | ||
cn5 := NewControllerName([]string{"Too Many Spaces"}) | ||
cn6 := NewControllerName([]string{"special!@characters"}) | ||
|
||
metricName1 := cn1.MetricsName() | ||
metricName2 := cn2.MetricsName() | ||
metricName3 := cn3.MetricsName() | ||
metricName4 := cn4.MetricsName() | ||
metricName5 := cn5.MetricsName() | ||
metricName6 := cn6.MetricsName() | ||
|
||
require.True(t, isPrometheusBestPracticeName(metricName1)) | ||
require.True(t, isPrometheusBestPracticeName(metricName2)) | ||
require.True(t, isPrometheusBestPracticeName(metricName3)) | ||
require.True(t, isPrometheusBestPracticeName(metricName4)) | ||
require.True(t, isPrometheusBestPracticeName(metricName5)) | ||
require.True(t, isPrometheusBestPracticeName(metricName6)) | ||
|
||
} | ||
|
||
func TestLoggerName(t *testing.T) { | ||
cn1 := NewControllerName([]string{"SomeFakeControllerName"}) | ||
cn2 := NewControllerName([]string{"Some", "Controller", "Name"}) | ||
cn3 := NewControllerName([]string{" SomeName", "Entered ", "poorly"}) | ||
cn4 := NewControllerName([]string{"Some Spaces"}) | ||
cn5 := NewControllerName([]string{"Too Many Spaces"}) | ||
cn6 := NewControllerName([]string{"special!@characters"}) | ||
|
||
loggerName1 := cn1.LoggerName() | ||
loggerName2 := cn2.LoggerName() | ||
loggerName3 := cn3.LoggerName() | ||
loggerName4 := cn4.LoggerName() | ||
loggerName5 := cn5.LoggerName() | ||
loggerName6 := cn6.LoggerName() | ||
|
||
require.True(t, isBestPracticeLoggerName(loggerName1)) | ||
require.True(t, isBestPracticeLoggerName(loggerName2)) | ||
require.True(t, isBestPracticeLoggerName(loggerName3)) | ||
require.True(t, isBestPracticeLoggerName(loggerName4)) | ||
require.True(t, isBestPracticeLoggerName(loggerName5)) | ||
require.True(t, isBestPracticeLoggerName(loggerName6)) | ||
|
||
} | ||
|
||
func TestStrip(t *testing.T) { | ||
str := "a *&b c " | ||
striped := strip(str) | ||
|
||
require.Equal(t, striped, "abc") | ||
} | ||
|
||
// IsPrometheusBestPracticeName - function returns true if the name given matches best practices for prometheus name, i.e. snake_case | ||
func isPrometheusBestPracticeName(controllerName string) bool { | ||
pattern := "^[a-z]+(_[a-z]+)*$" | ||
match, _ := regexp.MatchString(pattern, controllerName) | ||
|
||
return match | ||
} | ||
|
||
// IsBestPracticeLoggerName - function returns true if the name given matches best practices for prometheus name, i.e. kebab-case | ||
func isBestPracticeLoggerName(controllerName string) bool { | ||
pattern := "^[a-z]+(-[a-z]+)*$" | ||
match, _ := regexp.MatchString(pattern, controllerName) | ||
|
||
return match | ||
} |
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 was deleted.
Oops, something went wrong.