Skip to content

Commit

Permalink
config: Add fuzz tests (#6604)
Browse files Browse the repository at this point in the history
Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
pellared and MrAlias authored Jan 17, 2025
1 parent dafdad1 commit 751de09
Showing 1 changed file with 64 additions and 0 deletions.
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)
})
}

0 comments on commit 751de09

Please sign in to comment.