Skip to content

Commit 0f34cff

Browse files
committed
feat(conformance): validate implementation flags
Signed-off-by: Mattia Lavacca <lavacca.mattia@gmail.com>
1 parent f1fd808 commit 0f34cff

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

conformance/conformance.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ func DefaultOptions(t *testing.T) suite.ConformanceOptions {
6868
namespaceAnnotations := suite.ParseKeyValuePairs(*flags.NamespaceAnnotations)
6969
conformanceProfiles := suite.ParseConformanceProfiles(*flags.ConformanceProfiles)
7070

71-
implementation := suite.ParseImplementation(
71+
implementation, err := suite.ParseImplementation(
7272
*flags.ImplementationOrganization,
7373
*flags.ImplementationProject,
7474
*flags.ImplementationURL,
7575
*flags.ImplementationVersion,
7676
*flags.ImplementationContact,
7777
)
78+
require.NoError(t, err, "error parsing implementation details")
7879

7980
return suite.ConformanceOptions{
8081
AllowCRDsMismatch: *flags.AllowCRDsMismatch,
@@ -88,7 +89,7 @@ func DefaultOptions(t *testing.T) suite.ConformanceOptions {
8889
ExemptFeatures: exemptFeatures,
8990
ManifestFS: []fs.FS{&Manifests},
9091
GatewayClassName: *flags.GatewayClassName,
91-
Implementation: implementation,
92+
Implementation: *implementation,
9293
Mode: *flags.Mode,
9394
NamespaceAnnotations: namespaceAnnotations,
9495
NamespaceLabels: namespaceLabels,

conformance/utils/suite/suite.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"io/fs"
24+
neturl "net/url"
2425
"slices"
2526
"sort"
2627
"strings"
@@ -539,14 +540,33 @@ func (suite *ConformanceTestSuite) Report() (*confv1.ConformanceReport, error) {
539540

540541
// ParseImplementation parses implementation-specific flag arguments and
541542
// creates a *confv1a1.Implementation.
542-
func ParseImplementation(org, project, url, version, contact string) confv1.Implementation {
543-
return confv1.Implementation{
543+
func ParseImplementation(org, project, url, version, contact string) (*confv1.Implementation, error) {
544+
if org == "" {
545+
return nil, errors.New("organization must be set")
546+
}
547+
if project == "" {
548+
return nil, errors.New("project must be set")
549+
}
550+
if url == "" {
551+
return nil, errors.New("url must be set")
552+
}
553+
if version == "" {
554+
return nil, errors.New("version must be set")
555+
}
556+
if contact == "" {
557+
return nil, errors.New("contact must be set")
558+
}
559+
if _, err := neturl.ParseRequestURI(url); err != nil {
560+
return nil, errors.New("url is malformed")
561+
}
562+
563+
return &confv1.Implementation{
544564
Organization: org,
545565
Project: project,
546566
URL: url,
547567
Version: version,
548568
Contact: strings.Split(contact, ","),
549-
}
569+
}, nil
550570
}
551571

552572
// ParseConformanceProfiles parses flag arguments and converts the string to

conformance/utils/suite/suite_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,88 @@ func TestSuiteReport(t *testing.T) {
407407
})
408408
}
409409
}
410+
func TestParseImplementation(t *testing.T) {
411+
testCases := []struct {
412+
name string
413+
org string
414+
project string
415+
url string
416+
version string
417+
contact string
418+
expected *confv1.Implementation
419+
expectedErr error
420+
}{
421+
{
422+
name: "missing organization",
423+
project: "test-project",
424+
url: "https://example.com",
425+
version: "v1.0.0",
426+
contact: "test@example.com",
427+
expectedErr: errors.New("organization must be set"),
428+
},
429+
{
430+
name: "missing project",
431+
org: "test-org",
432+
url: "https://example.com",
433+
version: "v1.0.0",
434+
contact: "test@example.com",
435+
expectedErr: errors.New("project must be set"),
436+
},
437+
{
438+
name: "missing url",
439+
org: "test-org",
440+
project: "test-project",
441+
version: "v1.0.0",
442+
contact: "test@example.com",
443+
expectedErr: errors.New("url must be set"),
444+
},
445+
{
446+
name: "missing version",
447+
org: "test-org",
448+
project: "test-project",
449+
url: "https://example.com",
450+
contact: "test@example.com",
451+
expectedErr: errors.New("version must be set"),
452+
},
453+
{
454+
name: "missing contact",
455+
org: "test-org",
456+
project: "test-project",
457+
url: "https://example.com",
458+
version: "v1.0.0",
459+
expectedErr: errors.New("contact must be set"),
460+
},
461+
{
462+
name: "malformed url",
463+
org: "test-org",
464+
project: "test-project",
465+
url: "invalid-url",
466+
version: "v1.0.0",
467+
contact: "test@example.com",
468+
expectedErr: errors.New("url is malformed"),
469+
},
470+
{
471+
name: "valid input",
472+
org: "test-org",
473+
project: "test-project",
474+
url: "https://example.com",
475+
version: "v1.0.0",
476+
contact: "test@example.com,test2@example.com",
477+
expected: &confv1.Implementation{
478+
Organization: "test-org",
479+
Project: "test-project",
480+
URL: "https://example.com",
481+
Version: "v1.0.0",
482+
Contact: []string{"test@example.com", "test2@example.com"},
483+
},
484+
},
485+
}
486+
487+
for _, tc := range testCases {
488+
t.Run(tc.name, func(t *testing.T) {
489+
result, err := ParseImplementation(tc.org, tc.project, tc.url, tc.version, tc.contact)
490+
assert.Equal(t, tc.expected, result)
491+
assert.Equal(t, tc.expectedErr, err)
492+
})
493+
}
494+
}

0 commit comments

Comments
 (0)