Skip to content

Commit

Permalink
Adding ControllerName struct, and test cases (#84)
Browse files Browse the repository at this point in the history
* 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
marcus-chris-hines and Marcus Hines authored Aug 24, 2023
1 parent 4a0ab0f commit e4d9c12
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 29 deletions.
48 changes: 48 additions & 0 deletions pkg/controller/controllername/controllername.go
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)
}
79 changes: 79 additions & 0 deletions pkg/controller/controllername/controllername_test.go
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
}
9 changes: 0 additions & 9 deletions pkg/controller/testutils/testutils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package testutils

import (
"regexp"
"testing"

"github.com/Azure/aks-app-routing-operator/pkg/controller/metrics"
Expand Down Expand Up @@ -49,11 +48,3 @@ func StartTestingEnv() (*rest.Config, *envtest.Environment, error) {
func CleanupTestingEnv(env *envtest.Environment) error {
return env.Stop()
}

// 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
}
20 changes: 0 additions & 20 deletions pkg/controller/testutils/testutils_test.go

This file was deleted.

0 comments on commit e4d9c12

Please sign in to comment.