Skip to content

Commit

Permalink
chore: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangnv-bkhn committed Oct 17, 2024
1 parent 90a68f5 commit 1c17a54
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/unassign-issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ jobs:
const staleIssues = await github.paginate(github.rest.issues.listForRepo, {
owner, repo,
state: 'open',
labels: staleLabel,
assignee: '*'
labels: staleLabel
});
for (const issue of staleIssues) {
Expand Down
37 changes: 30 additions & 7 deletions cmd/relayproxy/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,28 @@ func (c *Config) IsValid() error {
return fmt.Errorf("invalid port %d", c.ListenPort)
}

if err := c.validateRetrievers(); err != nil {
return err
}

if err := c.validateExporters(); err != nil {
return err
}

if err := c.validateNotifiers(); err != nil {
return err
}

if c.LogLevel != "" {
if _, err := zapcore.ParseLevel(c.LogLevel); err != nil {
return err
}
}

return nil
}

func (c *Config) validateRetrievers() error {
if c.Retriever == nil && c.Retrievers == nil {
return fmt.Errorf("no retriever available in the configuration")
}
Expand All @@ -327,7 +349,10 @@ func (c *Config) IsValid() error {
}
}

// Exporter is optional
return nil
}

func (c *Config) validateExporters() error {
if c.Exporter != nil {
if err := c.Exporter.IsValid(); err != nil {
return err
Expand All @@ -342,19 +367,17 @@ func (c *Config) IsValid() error {
}
}

return nil
}

func (c *Config) validateNotifiers() error {
if c.Notifiers != nil {
for _, notif := range c.Notifiers {
if err := notif.IsValid(); err != nil {
return err
}
}
}
if c.LogLevel != "" {
if _, err := zapcore.ParseLevel(c.LogLevel); err != nil {
return err
}
}

return nil
}

Expand Down
82 changes: 82 additions & 0 deletions cmd/relayproxy/service/gofeatureflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,85 @@ func TestNewGoFeatureFlagClient_ProxyConfNil(t *testing.T) {
assert.Nil(t, goff, "Expected GoFeatureFlag client to be nil when proxyConf is nil")
assert.EqualError(t, err, "proxy config is empty", "Expected error message to indicate empty proxy config")
}

func TestNewGoFeatureFlagClient(t *testing.T) {
// Create a logger for testing
logger := zap.NewNop()

tests := []struct {
name string
proxyConf *config.Config
notifiers []notifier.Notifier
wantErr bool
}{
{
name: "Valid configuration with HTTP retriever and webhook exporter",
proxyConf: &config.Config{
ListenPort: 8080,
PollingInterval: 50000,
FileFormat: "yaml",
Retriever: &config.RetrieverConf{
Kind: "http",
URL: "https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/examples/retriever_file/flags.goff.yaml",
},
Exporter: &config.ExporterConf{
Kind: "webhook",
EndpointURL: "https://example.com/webhook",
Secret: "secret123",
},
},
notifiers: nil,
wantErr: false,
},
{
name: "Valid configuration with multiple retrievers and exporters",
proxyConf: &config.Config{
ListenPort: 8080,
PollingInterval: 60000,
FileFormat: "yaml",
Retrievers: &[]config.RetrieverConf{
{
Kind: "http",
URL: "https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/examples/retriever_file/flags.goff.yaml",
},
},
Exporters: &[]config.ExporterConf{
{
Kind: "log",
},
},
},
notifiers: nil,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewGoFeatureFlagClient(tt.proxyConf, logger, tt.notifiers)

if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, client)
} else {
assert.NoError(t, err)
assert.NotNil(t, client)

// Additional checks on the client configuration
assert.Equal(t, int64(tt.proxyConf.PollingInterval), client.GetPollingInterval())

// Check if the client is not offline
assert.False(t, client.IsOffline())

// Force a refresh and check if it succeeds
assert.True(t, client.ForceRefresh())

// Check if the cache refresh date is recent
assert.WithinDuration(t, time.Now(), client.GetCacheRefreshDate(), 5*time.Second)

// Clean up
client.Close()
}
})
}
}
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ type Config struct {
// DataExporter (optional) is the configuration where we store how we should output the flags variations results
DataExporter DataExporter

// DataExporters (optional) is the list of configurations where we store how we should output the flags variations results
// Multiple exporters can be used to send the data to multiple destinations in parallel without interfering with each other.
// DataExporters (optional) are configurations where we store how to output the flags variations results
// Multiple exporters can be used to send data to multiple destinations in parallel without interference.
DataExporters []DataExporter

// StartWithRetrieverError (optional) If true, the SDK will start even if we did not get any flags from the retriever.
Expand Down

0 comments on commit 1c17a54

Please sign in to comment.