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 eb7b854
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
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)
}
1 change: 0 additions & 1 deletion cmd/dex/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ func runServe(options serveOptions) error {
return fmt.Errorf("failed to initialize storage: %v", err)
}
defer s.Close()
defer s.Close()

logger.Info("config storage", "storage_type", c.Storage.Type)

Expand Down
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 eb7b854

Please sign in to comment.