@@ -24,6 +24,7 @@ import (
24
24
"os"
25
25
"path/filepath"
26
26
"regexp"
27
+ "strings"
27
28
"testing"
28
29
29
30
apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -43,9 +44,13 @@ const fixturesDir = "./fixtures"
43
44
func TestFileConvertion (t * testing.T ) {
44
45
ctx := context .Background ()
45
46
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 {
49
54
ProviderSpecificFlags : map [string ]map [string ]string {
50
55
"openapi3" : {
51
56
"gateway-class-name" : "external" ,
@@ -54,16 +59,24 @@ func TestFileConvertion(t *testing.T) {
54
59
},
55
60
},
56
61
},
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
+ },
64
74
},
65
75
},
66
76
},
77
+ "invalid-spec.yaml" : {
78
+ expectedReadFileError : fmt .Errorf ("failed to read resources from file: invalid OpenAPI 3.x spec" ),
79
+ },
67
80
}
68
81
69
82
filepath .WalkDir (filepath .Join (fixturesDir , "input" ), func (path string , d fs.DirEntry , err error ) error {
@@ -74,15 +87,32 @@ func TestFileConvertion(t *testing.T) {
74
87
return nil
75
88
}
76
89
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
+ }
80
102
}
81
- provider := NewProvider (conf )
82
103
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 ())
86
116
}
87
117
88
118
gotGatewayResources , errList := provider .ToGatewayAPI ()
0 commit comments