1package config
2
3import (
4	"testing"
5	"time"
6
7	"github.com/hashicorp/nomad/helper"
8	"github.com/stretchr/testify/require"
9)
10
11func TestAuditConfig_Merge(t *testing.T) {
12	c1 := &AuditConfig{
13		Enabled: helper.BoolToPtr(true),
14		Sinks: []*AuditSink{
15			{
16				DeliveryGuarantee: "enforced",
17				Name:              "file",
18				Type:              "file",
19				Format:            "json",
20				Path:              "/opt/nomad/audit.log",
21				RotateDuration:    24 * time.Hour,
22				RotateDurationHCL: "24h",
23				RotateBytes:       100,
24				RotateMaxFiles:    10,
25			},
26		},
27		Filters: []*AuditFilter{
28			{
29				Name:       "one",
30				Type:       "HTTPEvent",
31				Endpoints:  []string{"/v1/metrics"},
32				Stages:     []string{"*"},
33				Operations: []string{"*"},
34			},
35		},
36	}
37
38	c2 := &AuditConfig{
39		Sinks: []*AuditSink{
40			{
41				DeliveryGuarantee: "best-effort",
42				Name:              "file",
43				Type:              "file",
44				Format:            "json",
45				Path:              "/opt/nomad/audit.log",
46				RotateDuration:    48 * time.Hour,
47				RotateDurationHCL: "48h",
48				RotateBytes:       20,
49				RotateMaxFiles:    2,
50			},
51		},
52		Filters: []*AuditFilter{
53			{
54				Name:       "one",
55				Type:       "HTTPEvent",
56				Endpoints:  []string{"/v1/metrics"},
57				Stages:     []string{"OperationReceived"},
58				Operations: []string{"GET"},
59			},
60			{
61				Name:       "two",
62				Type:       "HTTPEvent",
63				Endpoints:  []string{"*"},
64				Stages:     []string{"OperationReceived"},
65				Operations: []string{"OPTIONS"},
66			},
67		},
68	}
69
70	e := &AuditConfig{
71		Enabled: helper.BoolToPtr(true),
72		Sinks: []*AuditSink{
73			{
74				DeliveryGuarantee: "best-effort",
75				Name:              "file",
76				Type:              "file",
77				Format:            "json",
78				Path:              "/opt/nomad/audit.log",
79				RotateDuration:    48 * time.Hour,
80				RotateDurationHCL: "48h",
81				RotateBytes:       20,
82				RotateMaxFiles:    2,
83			},
84		},
85		Filters: []*AuditFilter{
86			{
87				Name:       "one",
88				Type:       "HTTPEvent",
89				Endpoints:  []string{"/v1/metrics"},
90				Stages:     []string{"OperationReceived"},
91				Operations: []string{"GET"},
92			},
93			{
94				Name:       "two",
95				Type:       "HTTPEvent",
96				Endpoints:  []string{"*"},
97				Stages:     []string{"OperationReceived"},
98				Operations: []string{"OPTIONS"},
99			},
100		},
101	}
102
103	result := c1.Merge(c2)
104
105	require.Equal(t, e, result)
106}
107