mirror of
https://github.com/hanzoai/telemetry.git
synced 2026-10-30 20:01:49 +00:00
#### Description Move some reusable modules to an internal package. Those modules can be used by `schemagen` and `ocb` to generate a big schema that users can use to validate OpenTelemetry Collector configuations. <!-- Issue number if applicable --> #### Link to tracking issue Updates #15010 --------- Signed-off-by: Israel Blancas <[email protected]>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package schemagen
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWriteJSONSchema(t *testing.T) {
|
|
dir := t.TempDir()
|
|
md := &ConfigMetadata{
|
|
Schema: schemaVersion,
|
|
Type: "object",
|
|
Properties: map[string]*ConfigMetadata{
|
|
"endpoint": {Type: "string"},
|
|
},
|
|
}
|
|
|
|
err := WriteJSONSchema(dir, md)
|
|
require.NoError(t, err)
|
|
|
|
content, err := os.ReadFile(filepath.Join(dir, fileName)) // #nosec G304
|
|
require.NoError(t, err)
|
|
require.Contains(t, string(content), `"$schema"`)
|
|
require.Contains(t, string(content), `"endpoint"`)
|
|
}
|
|
|
|
func TestWriteJSONSchema_OmitsInternalResolvedFrom(t *testing.T) {
|
|
dir := t.TempDir()
|
|
md := &ConfigMetadata{
|
|
Schema: schemaVersion,
|
|
Type: "object",
|
|
ResolvedFrom: "go.opentelemetry.io/collector/config/confighttp.ClientConfig",
|
|
Properties: map[string]*ConfigMetadata{
|
|
"endpoint": {
|
|
Type: "string",
|
|
ResolvedFrom: "string",
|
|
},
|
|
},
|
|
}
|
|
|
|
err := WriteJSONSchema(dir, md)
|
|
require.NoError(t, err)
|
|
|
|
content, err := os.ReadFile(filepath.Join(dir, fileName)) // #nosec G304
|
|
require.NoError(t, err)
|
|
require.NotContains(t, string(content), "ResolvedFrom")
|
|
}
|
|
|
|
func TestWriteJSONSchema_InvalidDir(t *testing.T) {
|
|
md := &ConfigMetadata{Type: "object"}
|
|
err := WriteJSONSchema("/nonexistent/path/that/does/not/exist", md)
|
|
require.Error(t, err)
|
|
}
|