-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix jobsink name generator + add unit and fuzz tests
Signed-off-by: Pierangelo Di Pilato <[email protected]>
- Loading branch information
Showing
2 changed files
with
102 additions
and
12 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
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,90 @@ | ||
package main | ||
Check failure on line 1 in cmd/jobsink/main_test.go GitHub Actions / style / Golang / Boilerplate Check (go)
Check failure on line 1 in cmd/jobsink/main_test.go GitHub Actions / style / Golang / Auto-format and Check
|
||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/api/validation" | ||
|
||
"knative.dev/eventing/pkg/utils" | ||
) | ||
|
||
type testCase struct { | ||
JobSinkName string | ||
Source string | ||
Id string | ||
} | ||
|
||
func TestToJobName(t *testing.T) { | ||
testcases := []testCase{ | ||
{ | ||
JobSinkName: "job-sink-success", | ||
Source: "mysource3/myservice", | ||
Id: "2234-5678", | ||
}, | ||
{ | ||
JobSinkName: "a", | ||
Source: "0", | ||
Id: "0", | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.JobSinkName + "_" + tc.Source + "_" + tc.Id, func(t *testing.T) { | ||
if errs := validation.NameIsDNS1035Label(tc.JobSinkName, false); len(errs) != 0 { | ||
t.Errorf("Invalid JobSinkName: %v", errs) | ||
} | ||
|
||
name := toJobName(tc.JobSinkName, tc.Source, tc.Id) | ||
doubleName := toJobName(tc.JobSinkName, tc.Source, tc.Id) | ||
if name != doubleName { | ||
t.Errorf("Before: %q, after: %q", name, doubleName) | ||
} | ||
|
||
if got := utils.ToDNS1123Subdomain(name); got != name { | ||
t.Errorf("ToDNS1123Subdomain(Want) returns a different result, Want: %q, Got: %q", name, got) | ||
} | ||
|
||
if errs := validation.NameIsDNS1035Label(name, false); len(errs) != 0 { | ||
t.Errorf("toJobName produced invalid name %q given %q, %q, %q: errors: %#v", name, tc.JobSinkName, tc.Source, tc.Id, errs) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func FuzzToJobName(f *testing.F) { | ||
testcases := []testCase{ | ||
{ | ||
JobSinkName: "job-sink-success", | ||
Source: "mysource3/myservice", | ||
Id: "2234-5678", | ||
}, | ||
{ | ||
JobSinkName: "a", | ||
Source: "0", | ||
Id: "0", | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
f.Add(tc.JobSinkName, tc.Source, tc.Id) // Use f.Add to provide a seed corpus | ||
} | ||
f.Fuzz(func(t *testing.T, js, source, id string) { | ||
if errs := validation.NameIsDNS1035Label(js, false); len(errs) != 0 { | ||
t.Skip("Prerequisite: invalid jobsink name") | ||
} | ||
|
||
name := toJobName(js, source, id) | ||
doubleName := toJobName(js, source, id) | ||
if name != doubleName { | ||
t.Errorf("Before: %q, after: %q", name, doubleName) | ||
} | ||
|
||
if got := utils.ToDNS1123Subdomain(name); got != name { | ||
t.Errorf("ToDNS1123Subdomain(Want) returns a different result, Want: %q, Got: %q", name, got) | ||
} | ||
|
||
if errs := validation.NameIsDNS1035Label(name, false); len(errs) != 0 { | ||
t.Errorf("toJobName produced invalid name %q given %q, %q, %q: errors: %#v", name, js, source, id, errs) | ||
} | ||
}) | ||
} |