66 "os"
77 "strings"
88 "testing"
9-
10- "github.com/opendatahub-io/models-as-a-service/maas-api/internal/constant"
119)
1210
1311// resetGlobalFlags replaces flag.CommandLine with a fresh FlagSet so that
@@ -16,50 +14,6 @@ func resetGlobalFlags() {
1614 flag .CommandLine = flag .NewFlagSet (os .Args [0 ], flag .ContinueOnError )
1715}
1816
19- func TestLoad_DefaultValues (t * testing.T ) {
20- resetGlobalFlags ()
21-
22- // Unset all env vars that Load() reads to ensure clean defaults.
23- for _ , key := range []string {
24- "DEBUG_MODE" , "GATEWAY_NAME" , "SECURE" , "INSTANCE_NAME" , "NAMESPACE" , "GATEWAY_NAMESPACE" , "ADDRESS" ,
25- "API_KEY_EXPIRATION_POLICY" , "PORT" , "TLS_CERT" , "TLS_KEY" , "TLS_SELF_SIGNED" ,
26- } {
27- t .Setenv (key , "" )
28- os .Unsetenv (key )
29- }
30-
31- cfg := Load ()
32-
33- tests := []struct {
34- name string
35- got any
36- expected any
37- }{
38- {"Name defaults to gateway name" , cfg .Name , constant .DefaultGatewayName },
39- {"Namespace" , cfg .Namespace , constant .DefaultNamespace },
40- {"GatewayName" , cfg .GatewayName , constant .DefaultGatewayName },
41- {"GatewayNamespace" , cfg .GatewayNamespace , constant .DefaultGatewayNamespace },
42- {"Address is empty" , cfg .Address , "" },
43- {"Secure is false" , cfg .Secure , false },
44- {"DebugMode is false" , cfg .DebugMode , false },
45- {"DBConnectionURL is empty" , cfg .DBConnectionURL , "" },
46- {"APIKeyExpirationPolicy" , cfg .APIKeyExpirationPolicy , "optional" },
47- {"deprecatedHTTPPort is empty" , cfg .deprecatedHTTPPort , "" },
48- {"TLS.Cert is empty" , cfg .TLS .Cert , "" },
49- {"TLS.Key is empty" , cfg .TLS .Key , "" },
50- {"TLS.SelfSigned is false" , cfg .TLS .SelfSigned , false },
51- {"TLS.MinVersion is TLS 1.2" , cfg .TLS .MinVersion , TLSVersion (tls .VersionTLS12 )},
52- }
53-
54- for _ , tt := range tests {
55- t .Run (tt .name , func (t * testing.T ) {
56- if tt .got != tt .expected {
57- t .Errorf ("expected %v, got %v" , tt .expected , tt .got )
58- }
59- })
60- }
61- }
62-
6317func TestLoad_EnvironmentVariables (t * testing.T ) {
6418 tests := []struct {
6519 name string
@@ -97,7 +51,7 @@ func TestLoad_EnvironmentVariables(t *testing.T) {
9751 allEnvVars := []string {
9852 "DEBUG_MODE" , "GATEWAY_NAME" , "SECURE" , "INSTANCE_NAME" ,
9953 "NAMESPACE" , "GATEWAY_NAMESPACE" , "ADDRESS" ,
100- "API_KEY_EXPIRATION_POLICY" , " PORT" ,
54+ "PORT" ,
10155 "TLS_CERT" , "TLS_KEY" , "TLS_SELF_SIGNED" ,
10256 }
10357
@@ -125,7 +79,6 @@ func TestLoad_EnvironmentVariables(t *testing.T) {
12579// TestValidate covers Config.Validate:
12680// required fields (DBConnectionURL),
12781// TLS consistency (secure without certs, cert without key),
128- // APIKeyExpirationPolicy values,
12982// APIKeyMaxExpirationDays bounds,
13083// and default address assignment.
13184func TestValidate (t * testing.T ) {
@@ -137,43 +90,31 @@ func TestValidate(t *testing.T) {
13790 {
13891 name : "missing DBConnectionURL returns error" ,
13992 cfg : Config {
140- DBConnectionURL : "" ,
141- APIKeyExpirationPolicy : "optional" ,
93+ DBConnectionURL : "" ,
14294 },
14395 expectError : "db connection URL is required" ,
14496 },
14597 {
14698 name : "secure without TLS returns error" ,
14799 cfg : Config {
148- DBConnectionURL : "postgresql://localhost/test" ,
149- Secure : true ,
150- APIKeyExpirationPolicy : "optional" ,
100+ DBConnectionURL : "postgresql://localhost/test" ,
101+ Secure : true ,
151102 },
152103 expectError : "--secure requires either --tls-cert/--tls-key or --tls-self-signed" ,
153104 },
154105 {
155106 name : "TLS cert without key returns error" ,
156107 cfg : Config {
157- DBConnectionURL : "postgresql://localhost/test" ,
158- TLS : TLSConfig {Cert : "/cert.pem" },
159- APIKeyExpirationPolicy : "optional" ,
108+ DBConnectionURL : "postgresql://localhost/test" ,
109+ TLS : TLSConfig {Cert : "/cert.pem" },
160110 },
161111 expectError : "--tls-cert and --tls-key must both be provided together" ,
162112 },
163- {
164- name : "invalid APIKeyExpirationPolicy returns error" ,
165- cfg : Config {
166- DBConnectionURL : "postgresql://localhost/test" ,
167- APIKeyExpirationPolicy : "invalid" ,
168- },
169- expectError : "API_KEY_EXPIRATION_POLICY must be 'optional' or 'required'" ,
170- },
171- {
113+ {
172114 name : "valid insecure config sets default address :8080" ,
173115 cfg : Config {
174116 DBConnectionURL : "postgresql://localhost/test" ,
175117 Secure : false ,
176- APIKeyExpirationPolicy : "optional" ,
177118 APIKeyMaxExpirationDays : 30 ,
178119 },
179120 },
@@ -182,7 +123,6 @@ func TestValidate(t *testing.T) {
182123 cfg : Config {
183124 DBConnectionURL : "postgresql://localhost/test" ,
184125 TLS : TLSConfig {SelfSigned : true , MinVersion : TLSVersion (tls .VersionTLS12 )},
185- APIKeyExpirationPolicy : "optional" ,
186126 APIKeyMaxExpirationDays : 30 ,
187127 },
188128 },
@@ -191,39 +131,34 @@ func TestValidate(t *testing.T) {
191131 cfg : Config {
192132 DBConnectionURL : "postgresql://localhost/test" ,
193133 TLS : TLSConfig {Cert : "/cert.pem" , Key : "/key.pem" , MinVersion : TLSVersion (tls .VersionTLS12 )},
194- APIKeyExpirationPolicy : "optional" ,
195134 APIKeyMaxExpirationDays : 30 ,
196135 },
197136 },
198137 {
199138 name : "APIKeyMaxExpirationDays valid minimum value" ,
200139 cfg : Config {
201140 DBConnectionURL : "postgresql://localhost/test" ,
202- APIKeyExpirationPolicy : "optional" ,
203141 APIKeyMaxExpirationDays : 1 ,
204142 },
205143 },
206144 {
207145 name : "APIKeyMaxExpirationDays valid default value" ,
208146 cfg : Config {
209147 DBConnectionURL : "postgresql://localhost/test" ,
210- APIKeyExpirationPolicy : "optional" ,
211148 APIKeyMaxExpirationDays : 30 ,
212149 },
213150 },
214151 {
215152 name : "APIKeyMaxExpirationDays valid large value" ,
216153 cfg : Config {
217154 DBConnectionURL : "postgresql://localhost/test" ,
218- APIKeyExpirationPolicy : "optional" ,
219155 APIKeyMaxExpirationDays : 365 ,
220156 },
221157 },
222158 {
223159 name : "APIKeyMaxExpirationDays zero returns error" ,
224160 cfg : Config {
225161 DBConnectionURL : "postgresql://localhost/test" ,
226- APIKeyExpirationPolicy : "optional" ,
227162 APIKeyMaxExpirationDays : 0 ,
228163 },
229164 expectError : "must be at least 1" ,
@@ -232,7 +167,6 @@ func TestValidate(t *testing.T) {
232167 name : "APIKeyMaxExpirationDays negative returns error" ,
233168 cfg : Config {
234169 DBConnectionURL : "postgresql://localhost/test" ,
235- APIKeyExpirationPolicy : "optional" ,
236170 APIKeyMaxExpirationDays : - 1 ,
237171 },
238172 expectError : "must be at least 1" ,
0 commit comments