| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/otlp/otlplog/transform/attr_test.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
package transform |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
cpb "go.opentelemetry.io/proto/otlp/common/v1" |
| 16 |
) |
| 17 |
|
| 18 |
var ( |
| 19 |
attrBool = attribute.Bool("bool", true) |
| 20 |
attrBoolSlice = attribute.BoolSlice("bool slice", []bool{true, false}) |
| 21 |
attrInt = attribute.Int("int", 1) |
| 22 |
attrIntSlice = attribute.IntSlice("int slice", []int{-1, 1}) |
| 23 |
attrInt64 = attribute.Int64("int64", 1) |
| 24 |
attrInt64Slice = attribute.Int64Slice("int64 slice", []int64{-1, 1}) |
| 25 |
attrFloat64 = attribute.Float64("float64", 1) |
| 26 |
attrFloat64Slice = attribute.Float64Slice("float64 slice", []float64{-1, 1}) |
| 27 |
attrString = attribute.String("string", "o") |
| 28 |
attrStringSlice = attribute.StringSlice("string slice", []string{"o", "n"}) |
| 29 |
attrEmpty = attribute.KeyValue{ |
| 30 |
Key: attribute.Key("empty"), |
| 31 |
Value: attribute.Value{}, |
| 32 |
} |
| 33 |
|
| 34 |
valBoolTrue = &cpb.AnyValue{Value: &cpb.AnyValue_BoolValue{BoolValue: true}} |
| 35 |
valBoolFalse = &cpb.AnyValue{Value: &cpb.AnyValue_BoolValue{BoolValue: false}} |
| 36 |
valBoolSlice = &cpb.AnyValue{Value: &cpb.AnyValue_ArrayValue{ |
| 37 |
ArrayValue: &cpb.ArrayValue{ |
| 38 |
Values: []*cpb.AnyValue{valBoolTrue, valBoolFalse}, |
| 39 |
}, |
| 40 |
}} |
| 41 |
valIntOne = &cpb.AnyValue{Value: &cpb.AnyValue_IntValue{IntValue: 1}} |
| 42 |
valIntNOne = &cpb.AnyValue{Value: &cpb.AnyValue_IntValue{IntValue: -1}} |
| 43 |
valIntSlice = &cpb.AnyValue{Value: &cpb.AnyValue_ArrayValue{ |
| 44 |
ArrayValue: &cpb.ArrayValue{ |
| 45 |
Values: []*cpb.AnyValue{valIntNOne, valIntOne}, |
| 46 |
}, |
| 47 |
}} |
| 48 |
valDblOne = &cpb.AnyValue{Value: &cpb.AnyValue_DoubleValue{DoubleValue: 1}} |
| 49 |
valDblNOne = &cpb.AnyValue{Value: &cpb.AnyValue_DoubleValue{DoubleValue: -1}} |
| 50 |
valDblSlice = &cpb.AnyValue{Value: &cpb.AnyValue_ArrayValue{ |
| 51 |
ArrayValue: &cpb.ArrayValue{ |
| 52 |
Values: []*cpb.AnyValue{valDblNOne, valDblOne}, |
| 53 |
}, |
| 54 |
}} |
| 55 |
valStrO = &cpb.AnyValue{Value: &cpb.AnyValue_StringValue{StringValue: "o"}} |
| 56 |
valStrN = &cpb.AnyValue{Value: &cpb.AnyValue_StringValue{StringValue: "n"}} |
| 57 |
valStrSlice = &cpb.AnyValue{Value: &cpb.AnyValue_ArrayValue{ |
| 58 |
ArrayValue: &cpb.ArrayValue{ |
| 59 |
Values: []*cpb.AnyValue{valStrO, valStrN}, |
| 60 |
}, |
| 61 |
}} |
| 62 |
|
| 63 |
kvBool = &cpb.KeyValue{Key: "bool", Value: valBoolTrue} |
| 64 |
kvBoolSlice = &cpb.KeyValue{Key: "bool slice", Value: valBoolSlice} |
| 65 |
kvInt = &cpb.KeyValue{Key: "int", Value: valIntOne} |
| 66 |
kvIntSlice = &cpb.KeyValue{Key: "int slice", Value: valIntSlice} |
| 67 |
kvInt64 = &cpb.KeyValue{Key: "int64", Value: valIntOne} |
| 68 |
kvInt64Slice = &cpb.KeyValue{Key: "int64 slice", Value: valIntSlice} |
| 69 |
kvFloat64 = &cpb.KeyValue{Key: "float64", Value: valDblOne} |
| 70 |
kvFloat64Slice = &cpb.KeyValue{Key: "float64 slice", Value: valDblSlice} |
| 71 |
kvString = &cpb.KeyValue{Key: "string", Value: valStrO} |
| 72 |
kvStringSlice = &cpb.KeyValue{Key: "string slice", Value: valStrSlice} |
| 73 |
kvEmpty = &cpb.KeyValue{Key: "empty", Value: &cpb.AnyValue{}} |
| 74 |
) |
| 75 |
|
| 76 |
func TestAttrTransforms(t *testing.T) { |
| 77 |
type attrTest struct { |
| 78 |
name string |
| 79 |
in []attribute.KeyValue |
| 80 |
want []*cpb.KeyValue |
| 81 |
} |
| 82 |
|
| 83 |
for _, test := range []attrTest{ |
| 84 |
{"nil", nil, nil}, |
| 85 |
{"empty", []attribute.KeyValue{}, nil}, |
| 86 |
{ |
| 87 |
"empty value", |
| 88 |
[]attribute.KeyValue{attrEmpty}, |
| 89 |
[]*cpb.KeyValue{kvEmpty}, |
| 90 |
}, |
| 91 |
{ |
| 92 |
"bool", |
| 93 |
[]attribute.KeyValue{attrBool}, |
| 94 |
[]*cpb.KeyValue{kvBool}, |
| 95 |
}, |
| 96 |
{ |
| 97 |
"bool slice", |
| 98 |
[]attribute.KeyValue{attrBoolSlice}, |
| 99 |
[]*cpb.KeyValue{kvBoolSlice}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
"int", |
| 103 |
[]attribute.KeyValue{attrInt}, |
| 104 |
[]*cpb.KeyValue{kvInt}, |
| 105 |
}, |
| 106 |
{ |
| 107 |
"int slice", |
| 108 |
[]attribute.KeyValue{attrIntSlice}, |
| 109 |
[]*cpb.KeyValue{kvIntSlice}, |
| 110 |
}, |
| 111 |
{ |
| 112 |
"int64", |
| 113 |
[]attribute.KeyValue{attrInt64}, |
| 114 |
[]*cpb.KeyValue{kvInt64}, |
| 115 |
}, |
| 116 |
{ |
| 117 |
"int64 slice", |
| 118 |
[]attribute.KeyValue{attrInt64Slice}, |
| 119 |
[]*cpb.KeyValue{kvInt64Slice}, |
| 120 |
}, |
| 121 |
{ |
| 122 |
"float64", |
| 123 |
[]attribute.KeyValue{attrFloat64}, |
| 124 |
[]*cpb.KeyValue{kvFloat64}, |
| 125 |
}, |
| 126 |
{ |
| 127 |
"float64 slice", |
| 128 |
[]attribute.KeyValue{attrFloat64Slice}, |
| 129 |
[]*cpb.KeyValue{kvFloat64Slice}, |
| 130 |
}, |
| 131 |
{ |
| 132 |
"string", |
| 133 |
[]attribute.KeyValue{attrString}, |
| 134 |
[]*cpb.KeyValue{kvString}, |
| 135 |
}, |
| 136 |
{ |
| 137 |
"string slice", |
| 138 |
[]attribute.KeyValue{attrStringSlice}, |
| 139 |
[]*cpb.KeyValue{kvStringSlice}, |
| 140 |
}, |
| 141 |
{ |
| 142 |
"all", |
| 143 |
[]attribute.KeyValue{ |
| 144 |
attrBool, |
| 145 |
attrBoolSlice, |
| 146 |
attrInt, |
| 147 |
attrIntSlice, |
| 148 |
attrInt64, |
| 149 |
attrInt64Slice, |
| 150 |
attrFloat64, |
| 151 |
attrFloat64Slice, |
| 152 |
attrString, |
| 153 |
attrStringSlice, |
| 154 |
attrEmpty, |
| 155 |
}, |
| 156 |
[]*cpb.KeyValue{ |
| 157 |
kvBool, |
| 158 |
kvBoolSlice, |
| 159 |
kvInt, |
| 160 |
kvIntSlice, |
| 161 |
kvInt64, |
| 162 |
kvInt64Slice, |
| 163 |
kvFloat64, |
| 164 |
kvFloat64Slice, |
| 165 |
kvString, |
| 166 |
kvStringSlice, |
| 167 |
kvEmpty, |
| 168 |
}, |
| 169 |
}, |
| 170 |
} { |
| 171 |
t.Run(test.name, func(t *testing.T) { |
| 172 |
t.Run("Attrs", func(t *testing.T) { |
| 173 |
assert.ElementsMatch(t, test.want, Attrs(test.in)) |
| 174 |
}) |
| 175 |
t.Run("AttrIter", func(t *testing.T) { |
| 176 |
s := attribute.NewSet(test.in...) |
| 177 |
assert.ElementsMatch(t, test.want, AttrIter(s.Iter())) |
| 178 |
}) |
| 179 |
}) |
| 180 |
} |
| 181 |
} |