github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/telemetry/serialize_cals_test.go (about)

     1  package telemetry
     2  
     3  import (
     4  	"testing"
     5  
     6  	sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestSerializeCALs(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	tests := []struct {
    14  		name     string
    15  		input    []sm.CostAttributionLabel
    16  		expected string
    17  	}{
    18  		{
    19  			name:     "nil slice returns __MISSING__",
    20  			input:    nil,
    21  			expected: CalNilStringTerminator,
    22  		},
    23  		{
    24  			name:     "empty slice returns __MISSING__",
    25  			input:    []sm.CostAttributionLabel{},
    26  			expected: CalNilStringTerminator,
    27  		},
    28  		{
    29  			name:     "single label",
    30  			input:    []sm.CostAttributionLabel{{Name: "team", Value: "a"}},
    31  			expected: "team=a",
    32  		},
    33  		{
    34  			name:     "multiple labels already sorted",
    35  			input:    []sm.CostAttributionLabel{{Name: "env", Value: "prod"}, {Name: "team", Value: "a"}},
    36  			expected: "env=prod,team=a",
    37  		},
    38  		{
    39  			name:     "multiple labels unsorted",
    40  			input:    []sm.CostAttributionLabel{{Name: "team", Value: "a"}, {Name: "env", Value: "prod"}},
    41  			expected: "env=prod,team=a",
    42  		},
    43  		{
    44  			name:     "three labels unsorted",
    45  			input:    []sm.CostAttributionLabel{{Name: "region", Value: "us"}, {Name: "env", Value: "prod"}, {Name: "team", Value: "a"}},
    46  			expected: "env=prod,region=us,team=a",
    47  		},
    48  		{
    49  			name:     "single label with complex value",
    50  			input:    []sm.CostAttributionLabel{{Name: "key", Value: "value-with-dashes_and_underscores"}},
    51  			expected: "key=value-with-dashes_and_underscores",
    52  		},
    53  	}
    54  
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			result := serializeCALs(tt.input)
    58  			require.Equal(t, tt.expected, result)
    59  
    60  			// Test immutability: input slice should not be modified (skip for nil)
    61  			if tt.input != nil {
    62  				original := make([]sm.CostAttributionLabel, len(tt.input))
    63  				copy(original, tt.input)
    64  
    65  				_ = serializeCALs(tt.input)
    66  
    67  				require.Equal(t, original, tt.input, "input slice should not be modified")
    68  			}
    69  		})
    70  	}
    71  }