Skip to content

Commit

Permalink
Add event webhook config validation unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
window9u committed Jan 7, 2025
1 parent af0a536 commit 74d3fff
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 35 deletions.
2 changes: 1 addition & 1 deletion cmd/yorkie/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func init() {
cmd.Flags().DurationVar(
&eventWebhookRequestTimeout,
"project-event-webhook-request-timeout",
server.DefaultEventWebhookTimeout,
server.DefaultEventWebhookRequestTimeout,
"Time to wait for a response from the project webhook.",
)
cmd.Flags().IntVar(
Expand Down
24 changes: 24 additions & 0 deletions server/backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,30 @@ func (c *Config) Validate() error {
)
}

if _, err := time.ParseDuration(c.EventWebhookBaseWaitInterval); err != nil {
return fmt.Errorf(
`invalid argument "%s" for "--event-webhook-base-wait-interval" flag: %w`,
c.EventWebhookBaseWaitInterval,
err,
)
}

if _, err := time.ParseDuration(c.EventWebhookMaxWaitInterval); err != nil {
return fmt.Errorf(
`invalid argument "%s" for "--event-webhook-max-wait-interval" flag: %w`,
c.EventWebhookMaxWaitInterval,
err,
)

}
if _, err := time.ParseDuration(c.EventWebhookRequestTimeout); err != nil {
return fmt.Errorf(
`invalid argument "%s" for "--event-webhook-request-timeout" flag: %w`,
c.EventWebhookRequestTimeout,
err,
)
}

if _, err := time.ParseDuration(c.ProjectInfoCacheTTL); err != nil {
return fmt.Errorf(
`invalid argument "%s" for "--project-info-cache-ttl" flag: %w`,
Expand Down
27 changes: 21 additions & 6 deletions server/backend/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ import (
func TestConfig(t *testing.T) {
t.Run("validate test", func(t *testing.T) {
validConf := backend.Config{
ClientDeactivateThreshold: "1h",
AuthWebhookMaxWaitInterval: "0ms",
AuthWebhookCacheAuthTTL: "10s",
AuthWebhookCacheUnauthTTL: "10s",
ProjectInfoCacheTTL: "10m",
ClientDeactivateThreshold: "1h",
AuthWebhookMaxWaitInterval: "0ms",
AuthWebhookCacheAuthTTL: "10s",
AuthWebhookCacheUnauthTTL: "10s",
EventWebhookBaseWaitInterval: "10ms",
EventWebhookRequestTimeout: "10s",
EventWebhookMaxWaitInterval: "10ms",
ProjectInfoCacheTTL: "10m",
}
assert.NoError(t, validConf.Validate())

Expand All @@ -52,7 +55,19 @@ func TestConfig(t *testing.T) {
assert.Error(t, conf4.Validate())

conf5 := validConf
conf5.ProjectInfoCacheTTL = "10 minutes"
conf5.EventWebhookBaseWaitInterval = "10 ms"
assert.Error(t, conf5.Validate())

conf6 := validConf
conf6.EventWebhookRequestTimeout = "10 seconds"
assert.Error(t, conf6.Validate())

conf7 := validConf
conf7.EventWebhookMaxWaitInterval = "10 second"
assert.Error(t, conf7.Validate())

conf8 := validConf
conf8.ProjectInfoCacheTTL = "10 minutes"
assert.Error(t, conf8.Validate())
})
}
4 changes: 2 additions & 2 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const (
DefaultEventWebhookMaxRetries = 10
DefaultEventWebhookBaseWaitInterval = 3000 * time.Millisecond
DefaultEventWebhookMaxWaitInterval = 3000 * time.Millisecond
DefaultEventWebhookTimeout = 10 * time.Second
DefaultEventWebhookRequestTimeout = 10 * time.Second
DefaultProjectInfoCacheSize = 256
DefaultProjectInfoCacheTTL = 10 * time.Minute

Expand Down Expand Up @@ -211,7 +211,7 @@ func (c *Config) ensureDefaultValue() {
}

if c.Backend.EventWebhookRequestTimeout == "" {
c.Backend.EventWebhookRequestTimeout = DefaultEventWebhookTimeout.String()
c.Backend.EventWebhookRequestTimeout = DefaultEventWebhookRequestTimeout.String()
}

if c.Backend.ProjectInfoCacheSize == 0 {
Expand Down
13 changes: 13 additions & 0 deletions server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestNewConfigFromFile(t *testing.T) {
assert.Equal(t, conf.Backend.SnapshotThreshold, int64(server.DefaultSnapshotThreshold))
assert.Equal(t, conf.Backend.SnapshotInterval, int64(server.DefaultSnapshotInterval))
assert.Equal(t, conf.Backend.AuthWebhookMaxRetries, uint64(server.DefaultAuthWebhookMaxRetries))
assert.Equal(t, conf.Backend.EventWebhookMaxRetries, uint64(server.DefaultEventWebhookMaxRetries))

ClientDeactivateThreshold := conf.Backend.ClientDeactivateThreshold
assert.NoError(t, err)
Expand All @@ -78,6 +79,18 @@ func TestNewConfigFromFile(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, authWebhookCacheUnauthTTL, server.DefaultAuthWebhookCacheUnauthTTL)

eventWebhookBaseWaitInterval, err := time.ParseDuration(conf.Backend.EventWebhookBaseWaitInterval)
assert.NoError(t, err)
assert.Equal(t, eventWebhookBaseWaitInterval, server.DefaultEventWebhookBaseWaitInterval)

eventWebhookMaxWaitInterval, err := time.ParseDuration(conf.Backend.EventWebhookMaxWaitInterval)
assert.NoError(t, err)
assert.Equal(t, eventWebhookMaxWaitInterval, server.DefaultEventWebhookMaxWaitInterval)

eventWebhookRequestTimeout, err := time.ParseDuration(conf.Backend.EventWebhookRequestTimeout)
assert.NoError(t, err)
assert.Equal(t, eventWebhookRequestTimeout, server.DefaultEventWebhookRequestTimeout)

projectInfoCacheTTL, err := time.ParseDuration(conf.Backend.ProjectInfoCacheTTL)
assert.NoError(t, err)
assert.Equal(t, projectInfoCacheTTL, server.DefaultProjectInfoCacheTTL)
Expand Down
58 changes: 32 additions & 26 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,19 @@ var (
HousekeepingCandidatesLimitPerProject = 10
HousekeepingProjectFetchSize = 10

AdminTokenDuration = "10s"
ClientDeactivateThreshold = "10s"
SnapshotThreshold = int64(10)
SnapshotWithPurgingChanges = false
AuthWebhookMaxWaitInterval = 3 * gotime.Millisecond
AuthWebhookSize = 100
AuthWebhookCacheAuthTTL = 10 * gotime.Second
AuthWebhookCacheUnauthTTL = 10 * gotime.Second
ProjectInfoCacheSize = 256
ProjectInfoCacheTTL = 5 * gotime.Second
AdminTokenDuration = "10s"
ClientDeactivateThreshold = "10s"
SnapshotThreshold = int64(10)
SnapshotWithPurgingChanges = false
AuthWebhookMaxWaitInterval = 3 * gotime.Millisecond
AuthWebhookSize = 100
AuthWebhookCacheAuthTTL = 10 * gotime.Second
AuthWebhookCacheUnauthTTL = 10 * gotime.Second
EventWebhookBaseWaitInterval = 3 * gotime.Millisecond
EventWebhookMaxWaitInterval = 3 * gotime.Millisecond
EventWebhookRequestTimeout = 10 * gotime.Second
ProjectInfoCacheSize = 256
ProjectInfoCacheTTL = 5 * gotime.Second

MongoConnectionURI = "mongodb://localhost:27017"
MongoConnectionTimeout = "5s"
Expand Down Expand Up @@ -255,22 +258,25 @@ func TestConfig() *server.Config {
ProjectFetchSize: HousekeepingProjectFetchSize,
},
Backend: &backend.Config{
AdminUser: server.DefaultAdminUser,
AdminPassword: server.DefaultAdminPassword,
SecretKey: server.DefaultSecretKey,
AdminTokenDuration: server.DefaultAdminTokenDuration.String(),
UseDefaultProject: true,
ClientDeactivateThreshold: server.DefaultClientDeactivateThreshold,
SnapshotInterval: 10,
SnapshotThreshold: SnapshotThreshold,
SnapshotWithPurgingChanges: SnapshotWithPurgingChanges,
AuthWebhookMaxWaitInterval: AuthWebhookMaxWaitInterval.String(),
AuthWebhookCacheSize: AuthWebhookSize,
AuthWebhookCacheAuthTTL: AuthWebhookCacheAuthTTL.String(),
AuthWebhookCacheUnauthTTL: AuthWebhookCacheUnauthTTL.String(),
ProjectInfoCacheSize: ProjectInfoCacheSize,
ProjectInfoCacheTTL: ProjectInfoCacheTTL.String(),
GatewayAddr: fmt.Sprintf("localhost:%d", RPCPort+portOffset),
AdminUser: server.DefaultAdminUser,
AdminPassword: server.DefaultAdminPassword,
SecretKey: server.DefaultSecretKey,
AdminTokenDuration: server.DefaultAdminTokenDuration.String(),
UseDefaultProject: true,
ClientDeactivateThreshold: server.DefaultClientDeactivateThreshold,
SnapshotInterval: 10,
SnapshotThreshold: SnapshotThreshold,
SnapshotWithPurgingChanges: SnapshotWithPurgingChanges,
AuthWebhookMaxWaitInterval: AuthWebhookMaxWaitInterval.String(),
AuthWebhookCacheSize: AuthWebhookSize,
AuthWebhookCacheAuthTTL: AuthWebhookCacheAuthTTL.String(),
AuthWebhookCacheUnauthTTL: AuthWebhookCacheUnauthTTL.String(),
EventWebhookMaxWaitInterval: EventWebhookMaxWaitInterval.String(),
EventWebhookRequestTimeout: EventWebhookRequestTimeout.String(),
EventWebhookBaseWaitInterval: EventWebhookBaseWaitInterval.String(),
ProjectInfoCacheSize: ProjectInfoCacheSize,
ProjectInfoCacheTTL: ProjectInfoCacheTTL.String(),
GatewayAddr: fmt.Sprintf("localhost:%d", RPCPort+portOffset),
},
Mongo: &mongo.Config{
ConnectionURI: MongoConnectionURI,
Expand Down

0 comments on commit 74d3fff

Please sign in to comment.