Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: Add fuzz tests #6604

Merged
merged 14 commits into from
Jan 17, 2025
64 changes: 64 additions & 0 deletions config/v0.3.0/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package config

import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func FuzzJSON(f *testing.F) {
b, err := os.ReadFile(filepath.Join("..", "testdata", "v0.3.json"))
require.NoError(f, err)
f.Add(b)

f.Fuzz(func(t *testing.T, data []byte) {
t.Log("JSON:\n" + string(data))

var cfg OpenTelemetryConfiguration
err := json.Unmarshal(b, &cfg)
if err != nil {
return
}

sdk, err := NewSDK(WithOpenTelemetryConfiguration(cfg))
if err != nil {
return
}

ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
_ = sdk.Shutdown(ctx)
})
}

func FuzzYAML(f *testing.F) {
b, err := os.ReadFile(filepath.Join("..", "testdata", "v0.3.yaml"))
require.NoError(f, err)
f.Add(b)

f.Fuzz(func(t *testing.T, data []byte) {
t.Log("YAML:\n" + string(data))

cfg, err := ParseYAML(data)
if err != nil {
return
}

sdk, err := NewSDK(WithOpenTelemetryConfiguration(*cfg))
if err != nil {
return
}

ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
_ = sdk.Shutdown(ctx)
})
}
Loading