// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resource_test
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/sdk/resource"
)
func TestParsePlistFile(t *testing.T) {
standardPlist := bytes.NewBufferString(`
ProductBuildVersion
20E232
ProductCopyright
1983-2021 Apple Inc.
ProductName
macOS
ProductUserVisibleVersion
11.3
ProductVersion
11.3
iOSSupportVersion
14.5
`)
parsedPlist := map[string]string{
"ProductBuildVersion": "20E232",
"ProductCopyright": "1983-2021 Apple Inc.",
"ProductName": "macOS",
"ProductUserVisibleVersion": "11.3",
"ProductVersion": "11.3",
"iOSSupportVersion": "14.5",
}
emptyPlist := bytes.NewBufferString(`
`)
missingDictPlist := bytes.NewBufferString(`
`)
unknownElementsPlist := bytes.NewBufferString(`
123
ProductBuildVersion
Value
20E232
`)
parsedUnknownElementsPlist := map[string]string{
"ProductBuildVersion": "20E232",
}
tt := []struct {
Name string
Plist io.Reader
Parsed map[string]string
}{
{"Standard", standardPlist, parsedPlist},
{"Empty", emptyPlist, map[string]string{}},
{"Missing dict", missingDictPlist, map[string]string{}},
{"Unknown elements", unknownElementsPlist, parsedUnknownElementsPlist},
}
for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
result, err := resource.ParsePlistFile(tc.Plist)
require.Equal(t, tc.Parsed, result)
require.NoError(t, err)
})
}
}
func TestParsePlistFileUnevenKeys(t *testing.T) {
plist := bytes.NewBufferString(`
ProductBuildVersion
20E232
ProductCopyright
`)
result, err := resource.ParsePlistFile(plist)
require.Nil(t, result)
require.Error(t, err)
}
func TestParsePlistFileMalformed(t *testing.T) {
plist := bytes.NewBufferString(`
Product
`)
result, err := resource.ParsePlistFile(plist)
require.Nil(t, result)
require.Error(t, err)
}
func TestBuildOSRelease(t *testing.T) {
tt := []struct {
Name string
Properties map[string]string
OSRelease string
}{
{"Empty properties", map[string]string{}, ""},
{"Empty properties (nil)", nil, ""},
{"Missing product name", map[string]string{
"ProductVersion": "11.3",
"ProductBuildVersion": "20E232",
}, ""},
{"Missing product version", map[string]string{
"ProductName": "macOS",
"ProductBuildVersion": "20E232",
}, ""},
{"Missing product build version", map[string]string{
"ProductName": "macOS",
"ProductVersion": "11.3",
}, ""},
{"All properties available", map[string]string{
"ProductName": "macOS",
"ProductVersion": "11.3",
"ProductBuildVersion": "20E232",
}, "macOS 11.3 (20E232)"},
}
for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
result := resource.BuildOSRelease(tc.Properties)
require.Equal(t, tc.OSRelease, result)
})
}
}