Skip to content

Commit

Permalink
Run tests in internal/mode/static/state in parallel (1) (nginxinc#2534)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome authored Sep 11, 2024
1 parent e324a64 commit 7465e62
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 20 deletions.
5 changes: 5 additions & 0 deletions internal/mode/static/state/changed_predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

func TestFuncPredicate(t *testing.T) {
t.Parallel()
alwaysTrueFunc := func(_ ngftypes.ObjectType, _ types.NamespacedName) bool { return true }
emptyObject := &v1.Pod{}

Expand All @@ -25,6 +26,7 @@ func TestFuncPredicate(t *testing.T) {
}

func TestFuncPredicate_Panic(t *testing.T) {
t.Parallel()
alwaysTrueFunc := func(_ ngftypes.ObjectType, _ types.NamespacedName) bool { return true }

p := funcPredicate{stateChanged: alwaysTrueFunc}
Expand All @@ -38,13 +40,15 @@ func TestFuncPredicate_Panic(t *testing.T) {
}

func TestAnnotationChangedPredicate_Delete(t *testing.T) {
t.Parallel()
p := annotationChangedPredicate{}

g := NewWithT(t)
g.Expect(p.delete(nil, types.NamespacedName{})).To(BeTrue())
}

func TestAnnotationChangedPredicate_Update(t *testing.T) {
t.Parallel()
annotation := "test"

tests := []struct {
Expand Down Expand Up @@ -140,6 +144,7 @@ func TestAnnotationChangedPredicate_Update(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
if test.expPanic {
upsert := func() {
Expand Down
25 changes: 25 additions & 0 deletions internal/mode/static/state/dataplane/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func createFakePolicy(name string, kind string) policies.Policy {
}

func TestBuildConfiguration(t *testing.T) {
t.Parallel()
const (
invalidMatchesPath = "/not-valid-matches"
invalidFiltersPath = "/not-valid-filters"
Expand Down Expand Up @@ -2231,6 +2232,7 @@ func TestBuildConfiguration(t *testing.T) {

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)

result := BuildConfiguration(
Expand All @@ -2255,6 +2257,7 @@ func TestBuildConfiguration(t *testing.T) {
}

func TestGetPath(t *testing.T) {
t.Parallel()
tests := []struct {
path *v1.HTTPPathMatch
expected string
Expand Down Expand Up @@ -2284,6 +2287,7 @@ func TestGetPath(t *testing.T) {

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
result := getPath(test.path)
g.Expect(result).To(Equal(test.expected))
Expand All @@ -2292,6 +2296,7 @@ func TestGetPath(t *testing.T) {
}

func TestCreateFilters(t *testing.T) {
t.Parallel()
redirect1 := v1.HTTPRouteFilter{
Type: v1.HTTPRouteFilterRequestRedirect,
RequestRedirect: &v1.HTTPRequestRedirectFilter{
Expand Down Expand Up @@ -2451,6 +2456,7 @@ func TestCreateFilters(t *testing.T) {

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
result := createHTTPFilters(test.filters)

Expand All @@ -2460,6 +2466,7 @@ func TestCreateFilters(t *testing.T) {
}

func TestGetListenerHostname(t *testing.T) {
t.Parallel()
var emptyHostname v1.Hostname
var hostname v1.Hostname = "example.com"

Expand Down Expand Up @@ -2487,6 +2494,7 @@ func TestGetListenerHostname(t *testing.T) {

for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
result := getListenerHostname(test.hostname)
g.Expect(result).To(Equal(test.expected))
Expand All @@ -2509,6 +2517,7 @@ func refsToValidRules(refs ...[]graph.BackendRef) []graph.RouteRule {
}

func TestBuildUpstreams(t *testing.T) {
t.Parallel()
fooEndpoints := []resolver.Endpoint{
{
Address: "10.0.0.0",
Expand Down Expand Up @@ -2776,6 +2785,7 @@ func TestBuildUpstreams(t *testing.T) {
}

func TestBuildBackendGroups(t *testing.T) {
t.Parallel()
createBackendGroup := func(name string, ruleIdx int, backendNames ...string) BackendGroup {
backends := make([]Backend, len(backendNames))
for i, name := range backendNames {
Expand Down Expand Up @@ -2848,6 +2858,7 @@ func TestBuildBackendGroups(t *testing.T) {
}

func TestHostnameMoreSpecific(t *testing.T) {
t.Parallel()
tests := []struct {
host1 *v1.Hostname
host2 *v1.Hostname
Expand Down Expand Up @@ -2900,6 +2911,7 @@ func TestHostnameMoreSpecific(t *testing.T) {

for _, tc := range tests {
t.Run(tc.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)

g.Expect(listenerHostnameMoreSpecific(tc.host1, tc.host2)).To(Equal(tc.host1Wins))
Expand All @@ -2908,6 +2920,7 @@ func TestHostnameMoreSpecific(t *testing.T) {
}

func TestConvertBackendTLS(t *testing.T) {
t.Parallel()
btpCaCertRefs := &graph.BackendTLSPolicy{
Source: &v1alpha3.BackendTLSPolicy{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -2976,6 +2989,7 @@ func TestConvertBackendTLS(t *testing.T) {

for _, tc := range tests {
t.Run(tc.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)

g.Expect(convertBackendTLS(tc.btp)).To(Equal(tc.expected))
Expand All @@ -2984,6 +2998,7 @@ func TestConvertBackendTLS(t *testing.T) {
}

func TestBuildTelemetry(t *testing.T) {
t.Parallel()
telemetryConfigured := &graph.NginxProxy{
Source: &ngfAPI.NginxProxy{
Spec: ngfAPI.NginxProxySpec{
Expand Down Expand Up @@ -3206,6 +3221,7 @@ func TestBuildTelemetry(t *testing.T) {

for _, tc := range tests {
t.Run(tc.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
tel := buildTelemetry(tc.g)
sort.Slice(tel.Ratios, func(i, j int) bool {
Expand All @@ -3217,6 +3233,7 @@ func TestBuildTelemetry(t *testing.T) {
}

func TestBuildPolicies(t *testing.T) {
t.Parallel()
getPolicy := func(kind, name string) policies.Policy {
return &policiesfakes.FakePolicy{
GetNameStub: func() string {
Expand Down Expand Up @@ -3281,6 +3298,7 @@ func TestBuildPolicies(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)

pols := buildPolicies(test.policies)
Expand All @@ -3293,6 +3311,7 @@ func TestBuildPolicies(t *testing.T) {
}

func TestGetAllowedAddressType(t *testing.T) {
t.Parallel()
test := []struct {
msg string
ipFamily IPFamilyType
Expand Down Expand Up @@ -3322,18 +3341,21 @@ func TestGetAllowedAddressType(t *testing.T) {

for _, tc := range test {
t.Run(tc.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
g.Expect(getAllowedAddressType(tc.ipFamily)).To(Equal(tc.expected))
})
}
}

func TestCreateRatioVarName(t *testing.T) {
t.Parallel()
g := NewWithT(t)
g.Expect(CreateRatioVarName(25)).To(Equal("$otel_ratio_25"))
}

func TestCreatePassthroughServers(t *testing.T) {
t.Parallel()
getL4RouteKey := func(name string) graph.L4RouteKey {
return graph.L4RouteKey{
NamespacedName: types.NamespacedName{
Expand Down Expand Up @@ -3470,6 +3492,7 @@ func TestCreatePassthroughServers(t *testing.T) {
}

func TestBuildStreamUpstreams(t *testing.T) {
t.Parallel()
getL4RouteKey := func(name string) graph.L4RouteKey {
return graph.L4RouteKey{
NamespacedName: types.NamespacedName{
Expand Down Expand Up @@ -3601,6 +3624,7 @@ func TestBuildStreamUpstreams(t *testing.T) {
}

func TestBuildRewriteIPSettings(t *testing.T) {
t.Parallel()
tests := []struct {
msg string
g *graph.Graph
Expand Down Expand Up @@ -3713,6 +3737,7 @@ func TestBuildRewriteIPSettings(t *testing.T) {

for _, tc := range tests {
t.Run(tc.msg, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
baseConfig := buildBaseHTTPConfig(tc.g)
g.Expect(baseConfig.RewriteClientIPSettings).To(Equal(tc.expRewriteIPSettings))
Expand Down
26 changes: 19 additions & 7 deletions internal/mode/static/state/dataplane/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func TestConvertMatch(t *testing.T) {
t.Parallel()
path := v1.HTTPPathMatch{
Type: helpers.GetPointer(v1.PathMatchPathPrefix),
Value: helpers.GetPointer("/"),
Expand Down Expand Up @@ -115,6 +116,7 @@ func TestConvertMatch(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)

result := convertMatch(test.match)
Expand All @@ -124,6 +126,7 @@ func TestConvertMatch(t *testing.T) {
}

func TestConvertHTTPRequestRedirectFilter(t *testing.T) {
t.Parallel()
tests := []struct {
filter *v1.HTTPRequestRedirectFilter
expected *HTTPRequestRedirectFilter
Expand Down Expand Up @@ -153,6 +156,7 @@ func TestConvertHTTPRequestRedirectFilter(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)

result := convertHTTPRequestRedirectFilter(test.filter)
Expand All @@ -162,6 +166,7 @@ func TestConvertHTTPRequestRedirectFilter(t *testing.T) {
}

func TestConvertHTTPURLRewriteFilter(t *testing.T) {
t.Parallel()
tests := []struct {
filter *v1.HTTPURLRewriteFilter
expected *HTTPURLRewriteFilter
Expand Down Expand Up @@ -210,6 +215,7 @@ func TestConvertHTTPURLRewriteFilter(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)

result := convertHTTPURLRewriteFilter(test.filter)
Expand All @@ -219,6 +225,7 @@ func TestConvertHTTPURLRewriteFilter(t *testing.T) {
}

func TestConvertHTTPHeaderFilter(t *testing.T) {
t.Parallel()
tests := []struct {
filter *v1.HTTPHeaderFilter
expected *HTTPHeaderFilter
Expand Down Expand Up @@ -258,6 +265,7 @@ func TestConvertHTTPHeaderFilter(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
g := NewWithT(t)

result := convertHTTPHeaderFilter(test.filter)
Expand All @@ -267,7 +275,7 @@ func TestConvertHTTPHeaderFilter(t *testing.T) {
}

func TestConvertPathType(t *testing.T) {
g := NewWithT(t)
t.Parallel()

tests := []struct {
pathType v1.PathMatchType
Expand All @@ -289,11 +297,15 @@ func TestConvertPathType(t *testing.T) {
}

for _, tc := range tests {
if tc.panic {
g.Expect(func() { convertPathType(tc.pathType) }).To(Panic())
} else {
result := convertPathType(tc.pathType)
g.Expect(result).To(Equal(tc.expected))
}
t.Run(string(tc.pathType), func(t *testing.T) {
t.Parallel()
g := NewWithT(t)
if tc.panic {
g.Expect(func() { convertPathType(tc.pathType) }).To(Panic())
} else {
result := convertPathType(tc.pathType)
g.Expect(result).To(Equal(tc.expected))
}
})
}
}
1 change: 1 addition & 0 deletions internal/mode/static/state/dataplane/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestSort(t *testing.T) {
t.Parallel()
// timestamps
earlier := metav1.Now()
later := metav1.NewTime(earlier.Add(1 * time.Second))
Expand Down
Loading

0 comments on commit 7465e62

Please sign in to comment.