| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"io/fs" |
| 8 |
"path/filepath" |
| 9 |
"slices" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
) |
| 15 |
|
| 16 |
func TestValidate(t *testing.T) { |
| 17 |
tests := []struct { |
| 18 |
name string |
| 19 |
wantErr string |
| 20 |
}{ |
| 21 |
{ |
| 22 |
name: "testdata/no_type.yaml", |
| 23 |
wantErr: "missing type", |
| 24 |
}, |
| 25 |
{ |
| 26 |
name: "testdata/no_status.yaml", |
| 27 |
wantErr: "missing status", |
| 28 |
}, |
| 29 |
{ |
| 30 |
name: "testdata/no_class.yaml", |
| 31 |
wantErr: "missing class", |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "testdata/invalid_class.yaml", |
| 35 |
wantErr: "invalid class: incorrectclass", |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "testdata/no_stability.yaml", |
| 39 |
wantErr: "missing stability", |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "testdata/no_deprecation_info.yaml", |
| 43 |
wantErr: "deprecated component missing deprecation date and migration guide for traces", |
| 44 |
}, |
| 45 |
{ |
| 46 |
name: "testdata/no_deprecation_date_info.yaml", |
| 47 |
wantErr: "deprecated component missing date in YYYY-MM-DD format: traces", |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "testdata/no_deprecation_migration_info.yaml", |
| 51 |
wantErr: "deprecated component missing migration guide: traces", |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "testdata/deprecation_info_invalid_date.yaml", |
| 55 |
wantErr: "deprecated component missing valid date in YYYY-MM-DD format: traces", |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "testdata/invalid_stability.yaml", |
| 59 |
wantErr: "decoding failed due to the following error(s):\n\n'status.stability' unsupported stability level: \"incorrectstability\"", |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "testdata/no_stability_component.yaml", |
| 63 |
wantErr: "missing component for stability: Beta", |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "testdata/invalid_stability_component.yaml", |
| 67 |
wantErr: "invalid component: incorrectcomponent", |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "testdata/no_description_rattr.yaml", |
| 71 |
wantErr: "empty description for resource attribute: string.resource.attr", |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "testdata/no_type_rattr.yaml", |
| 75 |
wantErr: "empty type for resource attribute: string.resource.attr", |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "testdata/no_metric_description.yaml", |
| 79 |
wantErr: "metric \"default.metric\": missing metric description", |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "testdata/events/no_description.yaml", |
| 83 |
wantErr: "event \"default.event\": missing event description", |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "testdata/no_metric_unit.yaml", |
| 87 |
wantErr: "metric \"default.metric\": missing metric unit", |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "testdata/no_metric_type.yaml", |
| 91 |
wantErr: "metric \"system.cpu.time\": missing metric type key, " + |
| 92 |
"one of the following has to be specified: sum, gauge, histogram", |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "testdata/two_metric_types.yaml", |
| 96 |
wantErr: "metric \"system.cpu.time\": more than one metric type keys, " + |
| 97 |
"only one of the following has to be specified: sum, gauge, histogram", |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "testdata/invalid_input_type.yaml", |
| 101 |
wantErr: "metric \"system.cpu.time\": invalid `input_type` value \"double\", must be \"\" or \"string\"", |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "testdata/unknown_metric_attribute.yaml", |
| 105 |
wantErr: "metric \"system.cpu.time\" refers to undefined attributes: [missing]", |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "testdata/events/unknown_attribute.yaml", |
| 109 |
wantErr: "event \"system.event\" refers to undefined attributes: [missing]", |
| 110 |
}, |
| 111 |
{ |
| 112 |
name: "testdata/unused_attribute.yaml", |
| 113 |
wantErr: "unused attributes: [unused_attr]", |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: "testdata/no_description_attr.yaml", |
| 117 |
wantErr: "missing attribute description for: string_attr", |
| 118 |
}, |
| 119 |
{ |
| 120 |
name: "testdata/no_type_attr.yaml", |
| 121 |
wantErr: "empty type for attribute: used_attr", |
| 122 |
}, |
| 123 |
{ |
| 124 |
name: "testdata/entity_undefined_id_attribute.yaml", |
| 125 |
wantErr: `entity "host": identity refers to undefined resource attribute: host.missing`, |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "testdata/entity_undefined_description_attribute.yaml", |
| 129 |
wantErr: `entity "host": description refers to undefined resource attribute: host.missing`, |
| 130 |
}, |
| 131 |
{ |
| 132 |
name: "testdata/entity_empty_id_attributes.yaml", |
| 133 |
wantErr: `entity "host": identity is required`, |
| 134 |
}, |
| 135 |
{ |
| 136 |
name: "testdata/entity_duplicate_attributes.yaml", |
| 137 |
wantErr: `attribute host.name is already used by entity`, |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "testdata/entity_duplicate_types.yaml", |
| 141 |
wantErr: `duplicate entity type: host`, |
| 142 |
}, |
| 143 |
} |
| 144 |
for _, tt := range tests { |
| 145 |
t.Run(tt.name, func(t *testing.T) { |
| 146 |
_, err := LoadMetadata(tt.name) |
| 147 |
require.Error(t, err) |
| 148 |
require.ErrorContains(t, err, tt.wantErr) |
| 149 |
}) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func TestValidateMetricDuplicates(t *testing.T) { |
| 154 |
allowedMetrics := map[string][]string{ |
| 155 |
"container.cpu.utilization": {"docker_stats", "kubeletstats"}, |
| 156 |
"container.memory.rss": {"docker_stats", "kubeletstats"}, |
| 157 |
"container.uptime": {"docker_stats", "kubeletstats"}, |
| 158 |
} |
| 159 |
allMetrics := map[string][]string{} |
| 160 |
err := filepath.Walk("../../../receiver", func(path string, info fs.FileInfo, _ error) error { |
| 161 |
if info.Name() == "metadata.yaml" { |
| 162 |
md, err := LoadMetadata(path) |
| 163 |
require.NoError(t, err) |
| 164 |
if len(md.Metrics) > 0 { |
| 165 |
for metricName := range md.Metrics { |
| 166 |
allMetrics[md.Type] = append(allMetrics[md.Type], string(metricName)) |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
return nil |
| 171 |
}) |
| 172 |
require.NoError(t, err) |
| 173 |
|
| 174 |
seen := make(map[string]string) |
| 175 |
for receiver, metrics := range allMetrics { |
| 176 |
for _, metricName := range metrics { |
| 177 |
if val, exists := seen[metricName]; exists { |
| 178 |
receivers, allowed := allowedMetrics[metricName] |
| 179 |
assert.Truef( |
| 180 |
t, |
| 181 |
allowed && slices.Contains(receivers, receiver) && slices.Contains(receivers, val), |
| 182 |
"Duplicate metric %v in receivers %v and %v. Please validate that this is intentional by adding the metric name and receiver types in the allowedMetrics map in this test\n", metricName, receiver, val, |
| 183 |
) |
| 184 |
} |
| 185 |
seen[metricName] = receiver |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func TestSupportsSignal(t *testing.T) { |
| 191 |
md := Metadata{} |
| 192 |
assert.False(t, md.supportsSignal("logs")) |
| 193 |
} |
| 194 |
|
| 195 |
func TestCodeCovID(t *testing.T) { |
| 196 |
tests := []struct { |
| 197 |
md Metadata |
| 198 |
want string |
| 199 |
}{ |
| 200 |
{ |
| 201 |
md: Metadata{ |
| 202 |
Type: "aes", |
| 203 |
Status: &Status{ |
| 204 |
Class: "provider", |
| 205 |
CodeCovComponentID: "my_custom_id", |
| 206 |
}, |
| 207 |
}, |
| 208 |
want: "my_custom_id", |
| 209 |
}, |
| 210 |
{ |
| 211 |
md: Metadata{ |
| 212 |
Type: "count", |
| 213 |
Status: &Status{ |
| 214 |
Class: "connector", |
| 215 |
}, |
| 216 |
}, |
| 217 |
want: "connector_count", |
| 218 |
}, |
| 219 |
{ |
| 220 |
md: Metadata{ |
| 221 |
Type: "file", |
| 222 |
Status: &Status{ |
| 223 |
Class: "exporter", |
| 224 |
}, |
| 225 |
}, |
| 226 |
want: "exporter_file", |
| 227 |
}, |
| 228 |
} |
| 229 |
|
| 230 |
for _, tt := range tests { |
| 231 |
t.Run(tt.md.Type, func(t *testing.T) { |
| 232 |
got := tt.md.GetCodeCovComponentID() |
| 233 |
assert.Equal(t, tt.want, got) |
| 234 |
}) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func TestAttributeRequirementLevel(t *testing.T) { |
| 239 |
tests := []struct { |
| 240 |
name string |
| 241 |
requirementLevel AttributeRequirementLevel |
| 242 |
wantConditional bool |
| 243 |
}{ |
| 244 |
{ |
| 245 |
name: "required", |
| 246 |
requirementLevel: AttributeRequirementLevelRequired, |
| 247 |
wantConditional: false, |
| 248 |
}, |
| 249 |
{ |
| 250 |
name: "conditionally_required", |
| 251 |
requirementLevel: AttributeRequirementLevelConditionallyRequired, |
| 252 |
wantConditional: true, |
| 253 |
}, |
| 254 |
{ |
| 255 |
name: "recommended", |
| 256 |
requirementLevel: AttributeRequirementLevelRecommended, |
| 257 |
wantConditional: false, |
| 258 |
}, |
| 259 |
{ |
| 260 |
name: "opt_in", |
| 261 |
requirementLevel: AttributeRequirementLevelOptIn, |
| 262 |
wantConditional: false, |
| 263 |
}, |
| 264 |
} |
| 265 |
|
| 266 |
for _, tt := range tests { |
| 267 |
t.Run(tt.name, func(t *testing.T) { |
| 268 |
attr := Attribute{RequirementLevel: tt.requirementLevel} |
| 269 |
assert.Equal(t, tt.wantConditional, attr.IsConditional()) |
| 270 |
}) |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
func TestAttributeRequirementLevelUnmarshalText(t *testing.T) { |
| 275 |
tests := []struct { |
| 276 |
name string |
| 277 |
input string |
| 278 |
want AttributeRequirementLevel |
| 279 |
wantErr bool |
| 280 |
}{ |
| 281 |
{ |
| 282 |
name: "required", |
| 283 |
input: "required", |
| 284 |
want: AttributeRequirementLevelRequired, |
| 285 |
}, |
| 286 |
{ |
| 287 |
name: "conditionally_required", |
| 288 |
input: "conditionally_required", |
| 289 |
want: AttributeRequirementLevelConditionallyRequired, |
| 290 |
}, |
| 291 |
{ |
| 292 |
name: "recommended", |
| 293 |
input: "recommended", |
| 294 |
want: AttributeRequirementLevelRecommended, |
| 295 |
}, |
| 296 |
{ |
| 297 |
name: "opt_in", |
| 298 |
input: "opt_in", |
| 299 |
want: AttributeRequirementLevelOptIn, |
| 300 |
}, |
| 301 |
{ |
| 302 |
name: "empty defaults to recommended", |
| 303 |
input: "", |
| 304 |
want: AttributeRequirementLevelRecommended, |
| 305 |
}, |
| 306 |
{ |
| 307 |
name: "invalid value", |
| 308 |
input: "invalid", |
| 309 |
wantErr: true, |
| 310 |
}, |
| 311 |
} |
| 312 |
|
| 313 |
for _, tt := range tests { |
| 314 |
t.Run(tt.name, func(t *testing.T) { |
| 315 |
var rl AttributeRequirementLevel |
| 316 |
err := rl.UnmarshalText([]byte(tt.input)) |
| 317 |
if tt.wantErr { |
| 318 |
assert.Error(t, err) |
| 319 |
return |
| 320 |
} |
| 321 |
require.NoError(t, err) |
| 322 |
assert.Equal(t, tt.want, rl) |
| 323 |
}) |
| 324 |
} |
| 325 |
} |