github.com/hernad/nomad@v1.6.112/nomad/structs/config/audit_test.go (about)

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