Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/config: fix inconsistencies in tests #97

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestParseFile(t *testing.T) {
func TestParse(t *testing.T) {
tests := []struct {
name string
file string
Expand Down Expand Up @@ -406,10 +406,6 @@ func TestSeverity_MarshalText(t *testing.T) {
}
}

func ptr[V any](v V) *V {
return &v
}

func TestParseExpirationDate(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -463,3 +459,7 @@ func mustParseExpDate(date string) ExpirationDate {
}
return ExpirationDate{Time: t}
}

func ptr[V any](v V) *V {
return &v
}
54 changes: 27 additions & 27 deletions internal/config/configgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
"github.com/adevinta/lava/internal/config/dag"
)

func TestResolver_NewDAG(t *testing.T) {
func TestNewConfigGraph(t *testing.T) {
tests := []struct {
name string
URL string
want ConfigGraph
wantErr bool
}{
{
name: "File without includes",
URL: "testdata/includes/without_includes.yaml",
name: "no includes",
URL: "testdata/include/no_includes.yaml",
want: ConfigGraph{
configs: map[string]Config{
"testdata/includes/without_includes.yaml": {
"testdata/include/no_includes.yaml": {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
Expand All @@ -41,12 +41,12 @@ func TestResolver_NewDAG(t *testing.T) {
wantErr: false,
},
{
name: "Include a local file",
URL: "testdata/includes/include_local.yaml",
name: "local file",
URL: "testdata/include/local.yaml",
want: ConfigGraph{
configs: map[string]Config{
"testdata/includes/include_local.yaml": {
Includes: []string{"testdata/includes/without_includes.yaml"},
"testdata/include/local.yaml": {
Includes: []string{"testdata/include/no_includes.yaml"},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
Expand All @@ -58,7 +58,7 @@ func TestResolver_NewDAG(t *testing.T) {
},
},
},
"testdata/includes/without_includes.yaml": {
"testdata/include/no_includes.yaml": {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
Expand All @@ -75,19 +75,19 @@ func TestResolver_NewDAG(t *testing.T) {
wantErr: false,
},
{
name: "Import itself",
URL: "testdata/includes/autoinclude.yaml",
name: "autoinclude",
URL: "testdata/include/autoinclude.yaml",
wantErr: true,
},
{
name: "Include duplicated",
URL: "testdata/includes/include_duplicated.yaml",
name: "duplicated",
URL: "testdata/include/duplicated.yaml",
want: ConfigGraph{
configs: map[string]Config{
"testdata/includes/include_duplicated.yaml": {
"testdata/include/duplicated.yaml": {
Includes: []string{
"testdata/includes/without_includes.yaml",
"testdata/includes/without_includes.yaml",
"testdata/include/no_includes.yaml",
"testdata/include/no_includes.yaml",
},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
Expand All @@ -100,7 +100,7 @@ func TestResolver_NewDAG(t *testing.T) {
},
},
},
"testdata/includes/without_includes.yaml": {
"testdata/include/no_includes.yaml": {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
Expand All @@ -117,14 +117,14 @@ func TestResolver_NewDAG(t *testing.T) {
wantErr: false,
},
{
name: "Include two with common include",
URL: "testdata/includes/include_two_with_common_include.yaml",
name: "common includes",
URL: "testdata/include/common.yaml",
want: ConfigGraph{
configs: map[string]Config{
"testdata/includes/include_two_with_common_include.yaml": {
"testdata/include/common.yaml": {
Includes: []string{
"testdata/includes/include_a.yaml",
"testdata/includes/include_b.yaml",
"testdata/include/common_a.yaml",
"testdata/include/common_b.yaml",
},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
Expand All @@ -137,8 +137,8 @@ func TestResolver_NewDAG(t *testing.T) {
},
},
},
"testdata/includes/include_a.yaml": {
Includes: []string{"testdata/includes/without_includes.yaml"},
"testdata/include/common_a.yaml": {
Includes: []string{"testdata/include/no_includes.yaml"},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
Expand All @@ -150,8 +150,8 @@ func TestResolver_NewDAG(t *testing.T) {
},
},
},
"testdata/includes/include_b.yaml": {
Includes: []string{"testdata/includes/without_includes.yaml"},
"testdata/include/common_b.yaml": {
Includes: []string{"testdata/include/no_includes.yaml"},
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
Expand All @@ -163,7 +163,7 @@ func TestResolver_NewDAG(t *testing.T) {
},
},
},
"testdata/includes/without_includes.yaml": {
"testdata/include/no_includes.yaml": {
LavaVersion: ptr("v1.0.0"),
ChecktypeURLs: []string{
"checktypes.json",
Expand Down
42 changes: 14 additions & 28 deletions internal/config/dag/dag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func TestDAG_Contains(t *testing.T) {
want bool
}{
{
name: "Vertex has been added",
name: "existing vertex",
vertices: []string{"a", "b", "c"},
vertex: "a",
want: true,
},
{
name: "Vertex has not been added",
name: "unknown vertex",
vertices: []string{"a", "b", "c"},
vertex: "d",
want: false,
Expand All @@ -36,7 +36,7 @@ func TestDAG_Contains(t *testing.T) {
}
}
if got := dag.Contains(tt.vertex); got != tt.want {
t.Errorf("HasVertexBeenIncluded() = %v, want %v", got, tt.want)
t.Errorf("unexpected result: %v", got)
}
})
}
Expand All @@ -50,13 +50,13 @@ func TestDAG_AddVertex(t *testing.T) {
wantErr error
}{
{
name: "add unique vertex",
name: "unique vertex",
vertices: []string{"a"},
vertex: "b",
wantErr: nil,
},
{
name: "add duplicate vertex",
name: "duplicated vertex",
vertices: []string{"a"},
vertex: "a",
wantErr: ErrDuplicatedVertex,
Expand All @@ -70,15 +70,8 @@ func TestDAG_AddVertex(t *testing.T) {
t.Fatalf("failed to add vertex %s: %v", v, err)
}
}
_, err := dag.AddVertex(tt.vertex)
if tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) {
t.Errorf("unexpected error: got: %v, want: %v", err, tt.wantErr)
}
} else {
if err != nil {
t.Errorf("unexpected error adding the vertex: %v", err)
}
if _, err := dag.AddVertex(tt.vertex); !errors.Is(err, tt.wantErr) {
t.Errorf("unexpected error: got: %v, want: %v", err, tt.wantErr)
}
})
}
Expand All @@ -94,39 +87,39 @@ func TestDAG_AddEdge(t *testing.T) {
wantErr error
}{
{
name: "add edge",
name: "valid",
vertices: []string{"a", "b"},
edges: [][]string{},
from: "a",
to: "b",
wantErr: nil,
},
{
name: "add edge to invalid vertex",
name: "to invalid vertex",
vertices: []string{"a", "b"},
edges: [][]string{},
from: "a",
to: "c",
wantErr: ErrUnknownVertex,
},
{
name: "add edge from invalid vertex",
name: "from invalid vertex",
vertices: []string{"a", "b"},
edges: [][]string{},
from: "c",
to: "a",
wantErr: ErrUnknownVertex,
},
{
name: "add duplicate edge",
name: "duplicated edge",
vertices: []string{"a", "b"},
edges: [][]string{{"a", "b"}},
from: "a",
to: "b",
wantErr: ErrDuplicatedEdge,
},
{
name: "add a cycle",
name: "cycle",
vertices: []string{"a", "b", "c"},
edges: [][]string{{"a", "b"}, {"b", "c"}},
from: "c",
Expand All @@ -147,15 +140,8 @@ func TestDAG_AddEdge(t *testing.T) {
t.Errorf("failed to add edge %s, %s: %v", edge[0], edge[1], err)
}
}
err := dag.AddEdge(tt.from, tt.to)
if tt.wantErr != nil {
if !errors.Is(err, tt.wantErr) {
t.Errorf("unexpected error: got: %v, want: %v", err, tt.wantErr)
}
} else {
if err != nil {
t.Errorf("unexpected error adding the edge: %v", err)
}
if err := dag.AddEdge(tt.from, tt.to); !errors.Is(err, tt.wantErr) {
t.Errorf("unexpected error: got: %v, want: %v", err, tt.wantErr)
}
})
}
Expand Down
14 changes: 7 additions & 7 deletions internal/config/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestLavaMerger_Merge(t *testing.T) {
func TestMerge(t *testing.T) {
tests := []struct {
name string
dst Config
Expand All @@ -18,14 +18,14 @@ func TestLavaMerger_Merge(t *testing.T) {
wantErr bool
}{
{
name: "Two empty configurations",
name: "empty configurations",
dst: Config{},
src: Config{},
want: Config{},
wantErr: false,
},
{
name: "Simple case",
name: "simple",
dst: Config{},
src: Config{
LavaVersion: ptr("v1.0.0"),
Expand All @@ -36,7 +36,7 @@ func TestLavaMerger_Merge(t *testing.T) {
wantErr: false,
},
{
name: "Settings with default values won't override",
name: "default values do not override",
dst: Config{
LavaVersion: ptr("v1.0.0"),
AgentConfig: AgentConfig{
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestLavaMerger_Merge(t *testing.T) {
wantErr: false,
},
{
name: "Override value",
name: "override",
dst: Config{
LavaVersion: ptr("v1.0.0"),
AgentConfig: AgentConfig{
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestLavaMerger_Merge(t *testing.T) {
wantErr: false,
},
{
name: "Append Exclusions",
name: "append exclusions",
dst: Config{
ReportConfig: ReportConfig{
Exclusions: []Exclusion{
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestLavaMerger_Merge(t *testing.T) {
wantErr: false,
},
{
name: "Duplicated Exclusions",
name: "duplicated exclusions",
dst: Config{
ReportConfig: ReportConfig{
Exclusions: []Exclusion{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lava: v1.0.0
include:
- testdata/includes/autoinclude.yaml
- testdata/include/autoinclude.yaml
checktypes:
- checktypes.json
targets:
Expand Down
9 changes: 9 additions & 0 deletions internal/config/testdata/include/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
lava: v1.0.0
include:
- testdata/include/common_a.yaml
- testdata/include/common_b.yaml
checktypes:
- checktypes.json
targets:
- identifier: example.com
type: DomainName
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lava: v1.0.0
include:
- testdata/includes/without_includes.yaml
- testdata/include/no_includes.yaml
checktypes:
- checktypes.json
targets:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lava: v1.0.0
include:
- testdata/includes/without_includes.yaml
- testdata/include/no_includes.yaml
checktypes:
- checktypes.json
targets:
Expand Down
9 changes: 9 additions & 0 deletions internal/config/testdata/include/duplicated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
lava: v1.0.0
include:
- testdata/include/no_includes.yaml
- testdata/include/no_includes.yaml
checktypes:
- checktypes.json
targets:
- identifier: example.com
type: DomainName
Loading
Loading