-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/config: add merging configuration functionality
- Loading branch information
Showing
4 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2024 Adevinta | ||
|
||
package config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"dario.cat/mergo" | ||
) | ||
|
||
// Merger has the ability to merge two configurations. | ||
type Merger interface { | ||
Merge(dest, src Config) (Config, error) | ||
} | ||
|
||
// LavaMerger represents a merger for the Lava configuration. | ||
type LavaMerger struct{} | ||
|
||
// Merge merges two configurations. The values of the configuration | ||
// passed as first parameter will override those in the configuration | ||
// passed as second parameter. | ||
func (lm LavaMerger) Merge(dest, src Config) (Config, error) { | ||
merged := Config{} | ||
mergeOpts := []func(*mergo.Config){ | ||
mergo.WithOverride, | ||
mergo.WithoutDereference, | ||
mergo.WithAppendSlice, | ||
} | ||
if err := mergo.Merge(&merged, src, mergeOpts...); err != nil { | ||
return Config{}, fmt.Errorf("merging src config into new config: %w", err) | ||
} | ||
if err := mergo.Merge(&merged, dest, mergeOpts...); err != nil { | ||
return Config{}, fmt.Errorf("merging dest config into new config: %w", err) | ||
} | ||
return merged, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,283 @@ | ||
// Copyright 2024 Adevinta | ||
|
||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
agentconfig "github.com/adevinta/vulcan-agent/config" | ||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestLavaMerger_Merge(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg1 Config | ||
cfg2 Config | ||
want Config | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Two empty configurations", | ||
cfg1: Config{}, | ||
cfg2: Config{}, | ||
want: Config{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Simple case", | ||
cfg1: Config{ | ||
LavaVersion: ptr("1.0.0"), | ||
}, | ||
cfg2: Config{}, | ||
want: Config{ | ||
LavaVersion: ptr("1.0.0"), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Settings with default values won't override", | ||
cfg1: Config{}, | ||
cfg2: Config{ | ||
LavaVersion: ptr("1.0.0"), | ||
AgentConfig: AgentConfig{ | ||
PullPolicy: ptr(agentconfig.PullPolicyAlways), | ||
Parallel: ptr(4), | ||
Vars: map[string]string{ | ||
"VAR1": "value1", | ||
"VAR2": "value2", | ||
}, | ||
RegistryAuths: []RegistryAuth{ | ||
{ | ||
Server: "server", | ||
Username: "username", | ||
Password: "password", | ||
}, | ||
}, | ||
}, | ||
ReportConfig: ReportConfig{ | ||
Severity: ptr(SeverityCritical), | ||
ShowSeverity: ptr(SeverityLow), | ||
Format: ptr(OutputFormatJSON), | ||
OutputFile: ptr("outputfile.json"), | ||
ErrorOnStaleExclusions: ptr(true), | ||
Exclusions: []Exclusion{ | ||
{Summary: "Summary 1"}, | ||
}, | ||
Metrics: ptr("metrics.json"), | ||
}, | ||
}, | ||
want: Config{ | ||
LavaVersion: ptr("1.0.0"), | ||
AgentConfig: AgentConfig{ | ||
PullPolicy: ptr(agentconfig.PullPolicyAlways), | ||
Parallel: ptr(4), | ||
Vars: map[string]string{ | ||
"VAR1": "value1", | ||
"VAR2": "value2", | ||
}, | ||
RegistryAuths: []RegistryAuth{ | ||
{ | ||
Server: "server", | ||
Username: "username", | ||
Password: "password", | ||
}, | ||
}, | ||
}, | ||
ReportConfig: ReportConfig{ | ||
Severity: ptr(SeverityCritical), | ||
ShowSeverity: ptr(SeverityLow), | ||
Format: ptr(OutputFormatJSON), | ||
OutputFile: ptr("outputfile.json"), | ||
ErrorOnStaleExclusions: ptr(true), | ||
Exclusions: []Exclusion{ | ||
{ | ||
Summary: "Summary 1", | ||
}, | ||
}, | ||
Metrics: ptr("metrics.json"), | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Override value", | ||
cfg1: Config{ | ||
LavaVersion: ptr("1.0.1"), | ||
AgentConfig: AgentConfig{ | ||
PullPolicy: ptr(agentconfig.PullPolicyNever), | ||
Parallel: ptr(3), | ||
Vars: map[string]string{ | ||
"VAR1": "value1", | ||
"VAR2": "value2", | ||
}, | ||
RegistryAuths: []RegistryAuth{ | ||
{ | ||
Server: "server", | ||
Username: "username", | ||
Password: "password", | ||
}, | ||
}, | ||
}, | ||
ReportConfig: ReportConfig{ | ||
Severity: ptr(SeverityCritical), | ||
ShowSeverity: ptr(SeverityLow), | ||
Format: ptr(OutputFormatJSON), | ||
OutputFile: ptr("outputfile1.json"), | ||
ErrorOnStaleExclusions: ptr(false), | ||
Exclusions: []Exclusion{ | ||
{ | ||
Summary: "Summary 1", | ||
}, | ||
}, | ||
Metrics: ptr("metrics2.json"), | ||
}, | ||
}, | ||
cfg2: Config{ | ||
LavaVersion: ptr("1.0.0"), | ||
AgentConfig: AgentConfig{ | ||
PullPolicy: ptr(agentconfig.PullPolicyAlways), | ||
Parallel: ptr(4), | ||
Vars: map[string]string{ | ||
"VAR3": "value3", | ||
"VAR4": "value4", | ||
}, | ||
RegistryAuths: []RegistryAuth{ | ||
{ | ||
Server: "server2", | ||
Username: "username2", | ||
Password: "password2", | ||
}, | ||
}, | ||
}, | ||
ReportConfig: ReportConfig{ | ||
Severity: ptr(SeverityCritical), | ||
ShowSeverity: ptr(SeverityLow), | ||
Format: ptr(OutputFormatJSON), | ||
OutputFile: ptr("outputfile2.json"), | ||
ErrorOnStaleExclusions: ptr(true), | ||
Exclusions: []Exclusion{ | ||
{ | ||
Summary: "Summary 2", | ||
}, | ||
}, | ||
Metrics: ptr("metrics2.json"), | ||
}, | ||
}, | ||
want: Config{ | ||
LavaVersion: ptr("1.0.1"), | ||
AgentConfig: AgentConfig{ | ||
PullPolicy: ptr(agentconfig.PullPolicyNever), | ||
Parallel: ptr(3), | ||
Vars: map[string]string{ | ||
"VAR1": "value1", | ||
"VAR2": "value2", | ||
"VAR3": "value3", | ||
"VAR4": "value4", | ||
}, | ||
RegistryAuths: []RegistryAuth{ | ||
{ | ||
Server: "server2", | ||
Username: "username2", | ||
Password: "password2", | ||
}, | ||
{ | ||
Server: "server", | ||
Username: "username", | ||
Password: "password", | ||
}, | ||
}, | ||
}, | ||
ReportConfig: ReportConfig{ | ||
Severity: ptr(SeverityCritical), | ||
ShowSeverity: ptr(SeverityLow), | ||
Format: ptr(OutputFormatJSON), | ||
OutputFile: ptr("outputfile1.json"), | ||
ErrorOnStaleExclusions: ptr(false), | ||
Exclusions: []Exclusion{ | ||
{Summary: "Summary 2"}, | ||
{Summary: "Summary 1"}, | ||
}, | ||
Metrics: ptr("metrics2.json"), | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Append Exclusions", | ||
cfg1: Config{ | ||
ReportConfig: ReportConfig{ | ||
Exclusions: []Exclusion{ | ||
{ | ||
Summary: "Summary 1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
cfg2: Config{ | ||
ReportConfig: ReportConfig{ | ||
Exclusions: []Exclusion{ | ||
{ | ||
Summary: "Summary 2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: Config{ | ||
ReportConfig: ReportConfig{ | ||
Exclusions: []Exclusion{ | ||
{ | ||
Summary: "Summary 2", | ||
}, | ||
{ | ||
Summary: "Summary 1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Duplicated Exclusions", | ||
cfg1: Config{ | ||
ReportConfig: ReportConfig{ | ||
Exclusions: []Exclusion{ | ||
{ | ||
Summary: "Summary 1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
cfg2: Config{ | ||
ReportConfig: ReportConfig{ | ||
Exclusions: []Exclusion{ | ||
{ | ||
Summary: "Summary 1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: Config{ | ||
ReportConfig: ReportConfig{ | ||
Exclusions: []Exclusion{ | ||
{Summary: "Summary 1"}, | ||
{Summary: "Summary 1"}, | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
lm := LavaMerger{} | ||
got, err := lm.Merge(tt.cfg1, tt.cfg2) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("unexpected error: want: %v, got: %v", tt.wantErr, err) | ||
} | ||
if diff := cmp.Diff(tt.want, got); diff != "" { | ||
t.Errorf("configs mismatch (-want +got):\n%v", diff) | ||
} | ||
}) | ||
} | ||
} |