github.com/openfga/openfga@v1.5.4-rc1/internal/server/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestVerifyConfig(t *testing.T) {
    11  	t.Run("UpstreamTimeout_cannot_be_less_than_ListObjectsDeadline", func(t *testing.T) {
    12  		cfg := DefaultConfig()
    13  		cfg.ListObjectsDeadline = 5 * time.Minute
    14  		cfg.RequestTimeout = 0
    15  		cfg.HTTP.UpstreamTimeout = 2 * time.Second
    16  
    17  		err := cfg.Verify()
    18  		require.EqualError(t, err, "configured request timeout (2s) cannot be lower than 'listObjectsDeadline' config (5m0s)")
    19  	})
    20  	t.Run("UpstreamTimeout_cannot_be_less_than_ListUsersDeadline", func(t *testing.T) {
    21  		cfg := DefaultConfig()
    22  		cfg.ListObjectsDeadline = 2 * time.Second
    23  		cfg.ListUsersDeadline = 5 * time.Minute
    24  		cfg.RequestTimeout = 0
    25  		cfg.HTTP.UpstreamTimeout = 2 * time.Second
    26  
    27  		err := cfg.Verify()
    28  		require.EqualError(t, err, "configured request timeout (2s) cannot be lower than 'listUsersDeadline' config (5m0s)")
    29  	})
    30  
    31  	t.Run("maxConcurrentReadsForListUsers_not_zero", func(t *testing.T) {
    32  		cfg := DefaultConfig()
    33  		cfg.MaxConcurrentReadsForListUsers = 0
    34  
    35  		err := cfg.Verify()
    36  		require.EqualError(t, err, "config 'maxConcurrentReadsForListUsers' cannot be 0")
    37  	})
    38  
    39  	t.Run("failing_to_set_http_cert_path_will_not_allow_server_to_start", func(t *testing.T) {
    40  		cfg := DefaultConfig()
    41  		cfg.HTTP.TLS = &TLSConfig{
    42  			Enabled: true,
    43  			KeyPath: "some/path",
    44  		}
    45  
    46  		err := cfg.Verify()
    47  		require.EqualError(t, err, "'http.tls.cert' and 'http.tls.key' configs must be set")
    48  	})
    49  
    50  	t.Run("failing_to_set_grpc_cert_path_will_not_allow_server_to_start", func(t *testing.T) {
    51  		cfg := DefaultConfig()
    52  		cfg.GRPC.TLS = &TLSConfig{
    53  			Enabled: true,
    54  			KeyPath: "some/path",
    55  		}
    56  
    57  		err := cfg.Verify()
    58  		require.EqualError(t, err, "'grpc.tls.cert' and 'grpc.tls.key' configs must be set")
    59  	})
    60  
    61  	t.Run("failing_to_set_http_key_path_will_not_allow_server_to_start", func(t *testing.T) {
    62  		cfg := DefaultConfig()
    63  		cfg.HTTP.TLS = &TLSConfig{
    64  			Enabled:  true,
    65  			CertPath: "some/path",
    66  		}
    67  
    68  		err := cfg.Verify()
    69  		require.EqualError(t, err, "'http.tls.cert' and 'http.tls.key' configs must be set")
    70  	})
    71  
    72  	t.Run("failing_to_set_grpc_key_path_will_not_allow_server_to_start", func(t *testing.T) {
    73  		cfg := DefaultConfig()
    74  		cfg.GRPC.TLS = &TLSConfig{
    75  			Enabled:  true,
    76  			CertPath: "some/path",
    77  		}
    78  
    79  		err := cfg.Verify()
    80  		require.EqualError(t, err, "'grpc.tls.cert' and 'grpc.tls.key' configs must be set")
    81  	})
    82  
    83  	t.Run("non_log_format", func(t *testing.T) {
    84  		cfg := DefaultConfig()
    85  		cfg.Log.Format = "notaformat"
    86  
    87  		err := cfg.Verify()
    88  		require.Error(t, err)
    89  	})
    90  
    91  	t.Run("non_log_level", func(t *testing.T) {
    92  		cfg := DefaultConfig()
    93  		cfg.Log.Level = "notalevel"
    94  
    95  		err := cfg.Verify()
    96  		require.Error(t, err)
    97  	})
    98  
    99  	t.Run("invalid_log_timestamp_format", func(t *testing.T) {
   100  		cfg := DefaultConfig()
   101  		cfg.Log.TimestampFormat = "notatimestampformat"
   102  
   103  		err := cfg.Verify()
   104  		require.Error(t, err)
   105  	})
   106  
   107  	t.Run("empty_request_duration_datastore_query_count_buckets", func(t *testing.T) {
   108  		cfg := DefaultConfig()
   109  		cfg.RequestDurationDatastoreQueryCountBuckets = []string{}
   110  
   111  		err := cfg.Verify()
   112  		require.Error(t, err)
   113  	})
   114  
   115  	t.Run("non_int_request_duration_datastore_query_count_buckets", func(t *testing.T) {
   116  		cfg := DefaultConfig()
   117  		cfg.RequestDurationDatastoreQueryCountBuckets = []string{"12", "45a", "66"}
   118  
   119  		err := cfg.Verify()
   120  		require.Error(t, err)
   121  	})
   122  
   123  	t.Run("negative_request_duration_datastore_query_count_buckets", func(t *testing.T) {
   124  		cfg := DefaultConfig()
   125  		cfg.RequestDurationDatastoreQueryCountBuckets = []string{"12", "-45", "66"}
   126  
   127  		err := cfg.Verify()
   128  		require.Error(t, err)
   129  	})
   130  
   131  	t.Run("empty_request_duration_dispatch_count_buckets", func(t *testing.T) {
   132  		cfg := DefaultConfig()
   133  		cfg.RequestDurationDispatchCountBuckets = []string{}
   134  
   135  		err := cfg.Verify()
   136  		require.Error(t, err)
   137  	})
   138  
   139  	t.Run("non_int_request_duration_dispatch_count_buckets", func(t *testing.T) {
   140  		cfg := DefaultConfig()
   141  		cfg.RequestDurationDispatchCountBuckets = []string{"12", "45a", "66"}
   142  
   143  		err := cfg.Verify()
   144  		require.Error(t, err)
   145  	})
   146  
   147  	t.Run("negative_request_duration_dispatch_count_buckets", func(t *testing.T) {
   148  		cfg := DefaultConfig()
   149  		cfg.RequestDurationDispatchCountBuckets = []string{"12", "-45", "66"}
   150  
   151  		err := cfg.Verify()
   152  		require.Error(t, err)
   153  	})
   154  
   155  	t.Run("non_positive_dispatch_throttling_frequency", func(t *testing.T) {
   156  		cfg := DefaultConfig()
   157  		cfg.DispatchThrottling = DispatchThrottlingConfig{
   158  			Enabled:   true,
   159  			Frequency: 0,
   160  			Threshold: 30,
   161  		}
   162  
   163  		err := cfg.Verify()
   164  		require.Error(t, err)
   165  	})
   166  
   167  	t.Run("non_positive_dispatch_threshold", func(t *testing.T) {
   168  		cfg := DefaultConfig()
   169  		cfg.DispatchThrottling = DispatchThrottlingConfig{
   170  			Enabled:   true,
   171  			Frequency: 10 * time.Microsecond,
   172  			Threshold: 0,
   173  		}
   174  
   175  		err := cfg.Verify()
   176  		require.Error(t, err)
   177  	})
   178  
   179  	t.Run("dispatch_throttling_threshold_larger_than_max_threshold", func(t *testing.T) {
   180  		cfg := DefaultConfig()
   181  		cfg.DispatchThrottling = DispatchThrottlingConfig{
   182  			Enabled:      true,
   183  			Frequency:    10 * time.Microsecond,
   184  			Threshold:    30,
   185  			MaxThreshold: 29,
   186  		}
   187  		err := cfg.Verify()
   188  		require.Error(t, err)
   189  	})
   190  
   191  	t.Run("negative_request_timeout_duration", func(t *testing.T) {
   192  		cfg := DefaultConfig()
   193  		cfg.RequestTimeout = -2 * time.Second
   194  
   195  		err := cfg.Verify()
   196  		require.Error(t, err)
   197  	})
   198  
   199  	t.Run("negative_upstream_timeout", func(t *testing.T) {
   200  		cfg := DefaultConfig()
   201  		cfg.RequestTimeout = 0
   202  		cfg.HTTP.Enabled = true
   203  		cfg.HTTP.UpstreamTimeout = -3 * time.Second
   204  		cfg.ListObjectsDeadline = -4 * time.Second
   205  
   206  		err := cfg.Verify()
   207  		require.Error(t, err)
   208  	})
   209  
   210  	t.Run("negative_list_objects_deadline", func(t *testing.T) {
   211  		cfg := DefaultConfig()
   212  		cfg.RequestTimeout = 0
   213  		cfg.HTTP.Enabled = true
   214  		cfg.HTTP.UpstreamTimeout = 3 * time.Second
   215  		cfg.ListObjectsDeadline = -4 * time.Second
   216  
   217  		err := cfg.Verify()
   218  		require.Error(t, err)
   219  	})
   220  
   221  	t.Run("list_objects_deadline_request_timeout", func(t *testing.T) {
   222  		cfg := DefaultConfig()
   223  		cfg.RequestTimeout = 500 * time.Millisecond
   224  		cfg.ListObjectsDeadline = 4 * time.Second
   225  
   226  		err := cfg.Verify()
   227  		require.Error(t, err)
   228  	})
   229  }
   230  
   231  func TestDefaultMaxConditionValuationCost(t *testing.T) {
   232  	// check to make sure DefaultMaxConditionEvaluationCost never drops below an explicit 100, because
   233  	// API compatibility can be impacted otherwise
   234  	require.GreaterOrEqual(t, DefaultMaxConditionEvaluationCost, 100)
   235  }
   236  
   237  func TestDefaultContextTimeout(t *testing.T) {
   238  	var testCases = map[string]struct {
   239  		config                 Config
   240  		expectedContextTimeout time.Duration
   241  	}{
   242  		"request_timeout_provided": {
   243  			config: Config{
   244  				RequestTimeout: 5 * time.Second,
   245  				HTTP: HTTPConfig{
   246  					Enabled:         true,
   247  					UpstreamTimeout: 1 * time.Second,
   248  				},
   249  			},
   250  			expectedContextTimeout: 5*time.Second + additionalUpstreamTimeout,
   251  		},
   252  		"only_http_config_timeout": {
   253  			config: Config{
   254  				HTTP: HTTPConfig{
   255  					Enabled:         true,
   256  					UpstreamTimeout: 1 * time.Second,
   257  				},
   258  			},
   259  			expectedContextTimeout: 1 * time.Second,
   260  		},
   261  		"http_not_enable": {
   262  			config: Config{
   263  				HTTP: HTTPConfig{
   264  					Enabled:         false,
   265  					UpstreamTimeout: 1 * time.Second,
   266  				},
   267  			},
   268  			expectedContextTimeout: 0,
   269  		},
   270  	}
   271  	for name, test := range testCases {
   272  		test := test
   273  		t.Run(name, func(t *testing.T) {
   274  			t.Parallel()
   275  			timeout := DefaultContextTimeout(&test.config)
   276  			require.Equal(t, test.expectedContextTimeout, timeout)
   277  		})
   278  	}
   279  }