Initialize prefix remapping map to avoid panic (#2453)

A `nil` `target` passed to `AsOptionalMap` causes a panic.

Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
This commit is contained in:
Pierangelo Di Pilato 2022-03-10 19:24:48 +01:00 committed by GitHub
parent 78fdd53826
commit ce6f2877ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 16 deletions

View File

@ -105,6 +105,7 @@ func defaultConfig() *Config {
LeaseDuration: 60 * time.Second,
RenewDeadline: 40 * time.Second,
RetryPeriod: 10 * time.Second,
LeaseNamesPrefixMapping: make(map[string]string),
}
}

View File

@ -42,6 +42,7 @@ func okConfig() *Config {
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
LeaseNamesPrefixMapping: map[string]string{},
}
}
@ -126,6 +127,7 @@ func TestNewConfigMapFromData(t *testing.T) {
LeaseDuration: 2 * time.Second,
RenewDeadline: 3 * time.Second,
RetryPeriod: 4 * time.Second,
LeaseNamesPrefixMapping: map[string]string{},
},
}, {
name: "prioritize new keys",
@ -143,6 +145,7 @@ func TestNewConfigMapFromData(t *testing.T) {
LeaseDuration: 1 * time.Second,
RenewDeadline: 2 * time.Second,
RetryPeriod: 3 * time.Second,
LeaseNamesPrefixMapping: map[string]string{},
},
}}
@ -168,6 +171,57 @@ func TestNewConfigMapFromData(t *testing.T) {
}
}
func TestNewConfigFromMap(t *testing.T) {
tt := []struct {
name string
data map[string]string
want Config
wantErr bool
}{{
name: "ok config",
data: map[string]string{
"lease-duration": "15s",
"buckets": "5",
},
want: Config{
Buckets: 5,
LeaseDuration: 15 * time.Second,
RenewDeadline: 40 * time.Second,
RetryPeriod: 10 * time.Second,
LeaseNamesPrefixMapping: map[string]string{},
},
}, {
name: "ok config, prefix map",
data: map[string]string{
"lease-duration": "15s",
"buckets": "5",
"map-lease-prefix.reconciler": "reconciler1",
},
want: Config{
Buckets: 5,
LeaseDuration: 15 * time.Second,
RenewDeadline: 40 * time.Second,
RetryPeriod: 10 * time.Second,
LeaseNamesPrefixMapping: map[string]string{
"reconciler": "reconciler1",
},
},
}}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
c, err := NewConfigFromMap(tc.data)
if tc.wantErr != (err != nil) {
t.Fatalf("want err %v got %v", tc.wantErr, err)
}
if diff := cmp.Diff(tc.want, *c); diff != "" {
t.Fatal("(-want, +got)", diff)
}
})
}
}
func TestGetComponentConfig(t *testing.T) {
const expectedName = "the-component"
cases := []struct {