Skip to content

Commit

Permalink
test: retry mechanism upon failed storage initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
MoeBensu committed Aug 18, 2024
1 parent 6ad2c32 commit a091d04
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/dex/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ghodss/yaml"
"github.com/kylelemons/godebug/pretty"
"github.com/stretchr/testify/require"

"github.com/dexidp/dex/connector/mock"
"github.com/dexidp/dex/connector/oidc"
Expand Down Expand Up @@ -447,3 +448,23 @@ logger:
t.Errorf("got!=want: %s", diff)
}
}

func TestUnmarshalConfigWithRetry(t *testing.T) {
rawConfig := []byte(`
storage:
type: postgres
config:
host: 10.0.0.1
port: 65432
retryAttempts: 10
retryDelay: "1s"
`)

var c Config
err := yaml.Unmarshal(rawConfig, &c)
require.NoError(t, err)

require.Equal(t, "postgres", c.Storage.Type)
require.Equal(t, 10, c.Storage.RetryAttempts)
require.Equal(t, "1s", c.Storage.RetryDelay)
}
52 changes: 52 additions & 0 deletions cmd/dex/serve_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package main

import (
"context"
"errors"
"log/slog"
"testing"

"github.com/stretchr/testify/require"

"github.com/dexidp/dex/storage"
"github.com/dexidp/dex/storage/memory"
)

func TestNewLogger(t *testing.T) {
Expand All @@ -27,3 +32,50 @@ func TestNewLogger(t *testing.T) {
require.Equal(t, (*slog.Logger)(nil), logger)
})
}

func TestStorageInitializationRetry(t *testing.T) {
_, cancel := context.WithCancel(context.Background())
defer cancel()

// Create a mock storage that fails a certain number of times before succeeding
mockStorage := &mockRetryStorage{
failuresLeft: 3,
}

config := Config{
Issuer: "http://127.0.0.1:5556/dex",
Storage: Storage{
Type: "mock",
Config: mockStorage,
RetryAttempts: 5,
RetryDelay: "1s",
},
Web: Web{
HTTP: "127.0.0.1:5556",
},
Logger: Logger{
Level: slog.LevelInfo,
Format: "json",
},
}

logger, _ := newLogger(config.Logger.Level, config.Logger.Format)

s, err := initializeStorageWithRetry(config.Storage, logger)
require.NoError(t, err)
require.NotNil(t, s)

require.Equal(t, 0, mockStorage.failuresLeft)
}

type mockRetryStorage struct {
failuresLeft int
}

func (m *mockRetryStorage) Open(logger *slog.Logger) (storage.Storage, error) {
if m.failuresLeft > 0 {
m.failuresLeft--
return nil, errors.New("mock storage failure")
}
return memory.New(logger), nil
}

0 comments on commit a091d04

Please sign in to comment.