Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ControllerName struct, and test cases #84

Merged
merged 28 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
740b217
add regex and unit test for checking if the given controller name is …
Aug 14, 2023
88173e1
added detail to comment
Aug 14, 2023
b92c97e
updating regex
Aug 15, 2023
cc185cc
moved to test idr
Aug 15, 2023
df92839
fixing import chnage
Aug 15, 2023
2842e2b
Merge branch 'main' into main
marcus-chris-hines Aug 15, 2023
59a919e
moving test case
Aug 15, 2023
e22bbc0
Merge branch 'main' of https://github.com/Marcus-Hines/aks-app-routin…
Aug 15, 2023
ce3312a
add controller utils
Aug 21, 2023
b90a767
fix trim on spaces
Aug 22, 2023
4feb03b
add test case for MetricsName
Aug 22, 2023
6ee11c7
testing logger name also
Aug 22, 2023
0ed635f
Merge branch 'main' into main
marcus-chris-hines Aug 22, 2023
f39b76e
fixing bug in clean
Aug 22, 2023
71d25fe
Merge branch 'main' of https://github.com/Marcus-Hines/aks-app-routin…
Aug 22, 2023
ae35c2c
moved the test utils test to controller utisl
Aug 22, 2023
7612113
using a New function instead of clean
Aug 22, 2023
b5b3e31
updated tests to use New
Aug 22, 2023
2342361
fixing value of test case string
Aug 22, 2023
997b29b
update comment on conrollerName struc
Aug 22, 2023
ac6dffc
fix typo in test cases
Aug 23, 2023
b45da93
adding test cases for spaces
Aug 23, 2023
b7cc8b0
removing test that no longer has value
Aug 23, 2023
9520695
updated removespace to instead only allow letters
Aug 23, 2023
c4fa8c3
testing strip
Aug 23, 2023
38a9dd3
fixing strip method
Aug 23, 2023
83bd476
fixing stripped funciton
Aug 23, 2023
0e5659b
specialchars test
Aug 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Marcus-Hines marked this conversation as resolved.
Show resolved Hide resolved
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"})
Marcus-Hines marked this conversation as resolved.
Show resolved Hide resolved
cn2 := NewControllerName([]string{"Some", "Controller", "Name"})
cn3 := NewControllerName([]string{" SomeName", "Entered ", "poorly"})
cn4 := NewControllerName([]string{"Some Spaces"})
Marcus-Hines marked this conversation as resolved.
Show resolved Hide resolved
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.