Skip to content

Commit b174c23

Browse files
committed
return error in case of invalid OpenAPI 3.x spec
1 parent 48dc906 commit b174c23

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

pkg/i2gw/providers/openapi3/converter_test.go

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"path/filepath"
2626
"regexp"
27+
"strings"
2728
"testing"
2829

2930
apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -43,9 +44,13 @@ const fixturesDir = "./fixtures"
4344
func TestFileConvertion(t *testing.T) {
4445
ctx := context.Background()
4546

46-
//nolint:gofmt
47-
providerConf := map[string]*i2gw.ProviderConf{
48-
"default": &i2gw.ProviderConf{
47+
type testData struct {
48+
providerConf *i2gw.ProviderConf
49+
expectedReadFileError error
50+
}
51+
52+
defaultTestData := testData{
53+
providerConf: &i2gw.ProviderConf{
4954
ProviderSpecificFlags: map[string]map[string]string{
5055
"openapi3": {
5156
"gateway-class-name": "external",
@@ -54,16 +59,24 @@ func TestFileConvertion(t *testing.T) {
5459
},
5560
},
5661
},
57-
"reference-grants.yaml": &i2gw.ProviderConf{
58-
Namespace: "networking",
59-
ProviderSpecificFlags: map[string]map[string]string{
60-
"openapi3": {
61-
"gateway-class-name": "external",
62-
"gateway-tls-secret": "secrets/gateway-tls-cert",
63-
"backend": "apps/backend-1",
62+
}
63+
64+
customTestData := map[string]testData{
65+
"reference-grants.yaml": {
66+
providerConf: &i2gw.ProviderConf{
67+
Namespace: "networking",
68+
ProviderSpecificFlags: map[string]map[string]string{
69+
"openapi3": {
70+
"gateway-class-name": "external",
71+
"gateway-tls-secret": "secrets/gateway-tls-cert",
72+
"backend": "apps/backend-1",
73+
},
6474
},
6575
},
6676
},
77+
"invalid-spec.yaml": {
78+
expectedReadFileError: fmt.Errorf("failed to read resources from file: invalid OpenAPI 3.x spec"),
79+
},
6780
}
6881

6982
filepath.WalkDir(filepath.Join(fixturesDir, "input"), func(path string, d fs.DirEntry, err error) error {
@@ -74,15 +87,32 @@ func TestFileConvertion(t *testing.T) {
7487
return nil
7588
}
7689

77-
conf, ok := providerConf[regexp.MustCompile(`\d+-(.+\.(json|yaml))$`).FindAllStringSubmatch(d.Name(), -1)[0][1]]
78-
if !ok {
79-
conf = providerConf["default"]
90+
providerConf := defaultTestData.providerConf
91+
expectedReadFileError := defaultTestData.expectedReadFileError
92+
93+
inputFileName := regexp.MustCompile(`\d+-(.+\.(json|yaml))$`).FindAllStringSubmatch(d.Name(), -1)[0][1]
94+
data, ok := customTestData[inputFileName]
95+
if ok {
96+
if data.providerConf != nil {
97+
providerConf = data.providerConf
98+
}
99+
if data.expectedReadFileError != nil {
100+
expectedReadFileError = data.expectedReadFileError
101+
}
80102
}
81-
provider := NewProvider(conf)
82103

83-
err = provider.ReadResourcesFromFile(ctx, path)
84-
if err != nil {
85-
t.Fatalf("Failed to read input from file %v: %v", d.Name(), err.Error())
104+
provider := NewProvider(providerConf)
105+
106+
if readFileErr := provider.ReadResourcesFromFile(ctx, path); readFileErr != nil {
107+
if expectedReadFileError == nil {
108+
t.Fatalf("unexpected error during reading test file %v: %v", d.Name(), readFileErr.Error())
109+
} else if !strings.Contains(readFileErr.Error(), expectedReadFileError.Error()) {
110+
t.Fatalf("unexpected error during reading test file %v: '%v' does not contain expected '%v'", d.Name(), readFileErr.Error(), expectedReadFileError.Error())
111+
} else {
112+
return nil // success
113+
}
114+
} else if expectedReadFileError != nil {
115+
t.Fatalf("missing expected error during reading test file %v: %v", d.Name(), expectedReadFileError.Error())
86116
}
87117

88118
gotGatewayResources, errList := provider.ToGatewayAPI()

pkg/i2gw/providers/openapi3/openapi.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package openapi3
1919
import (
2020
"context"
2121
"fmt"
22-
"log"
2322

2423
"github.com/getkin/kin-openapi/openapi3"
2524
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -103,8 +102,7 @@ func readSpecFromFile(ctx context.Context, filename string) (*openapi3.T, error)
103102
}
104103

105104
if err := spec.Validate(ctx); err != nil {
106-
log.Printf("%s provider: invalid OpenAPI 3.x spec: %v", ProviderName, err)
107-
return nil, nil
105+
return nil, fmt.Errorf("invalid OpenAPI 3.x spec: %w", err)
108106
}
109107

110108
return spec, nil

0 commit comments

Comments
 (0)