Skip to content

Commit 751de09

Browse files
pellaredMrAlias
andauthored
config: Add fuzz tests (#6604)
Co-authored-by: Tyler Yahn <[email protected]>
1 parent dafdad1 commit 751de09

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

config/v0.3.0/fuzz_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package config
5+
6+
import (
7+
"context"
8+
"encoding/json"
9+
"os"
10+
"path/filepath"
11+
"testing"
12+
"time"
13+
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func FuzzJSON(f *testing.F) {
18+
b, err := os.ReadFile(filepath.Join("..", "testdata", "v0.3.json"))
19+
require.NoError(f, err)
20+
f.Add(b)
21+
22+
f.Fuzz(func(t *testing.T, data []byte) {
23+
t.Log("JSON:\n" + string(data))
24+
25+
var cfg OpenTelemetryConfiguration
26+
err := json.Unmarshal(b, &cfg)
27+
if err != nil {
28+
return
29+
}
30+
31+
sdk, err := NewSDK(WithOpenTelemetryConfiguration(cfg))
32+
if err != nil {
33+
return
34+
}
35+
36+
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
37+
defer cancel()
38+
_ = sdk.Shutdown(ctx)
39+
})
40+
}
41+
42+
func FuzzYAML(f *testing.F) {
43+
b, err := os.ReadFile(filepath.Join("..", "testdata", "v0.3.yaml"))
44+
require.NoError(f, err)
45+
f.Add(b)
46+
47+
f.Fuzz(func(t *testing.T, data []byte) {
48+
t.Log("YAML:\n" + string(data))
49+
50+
cfg, err := ParseYAML(data)
51+
if err != nil {
52+
return
53+
}
54+
55+
sdk, err := NewSDK(WithOpenTelemetryConfiguration(*cfg))
56+
if err != nil {
57+
return
58+
}
59+
60+
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
61+
defer cancel()
62+
_ = sdk.Shutdown(ctx)
63+
})
64+
}

0 commit comments

Comments
 (0)