| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resource_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"io" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/sdk/resource" |
| 14 |
) |
| 15 |
|
| 16 |
func TestParsePlistFile(t *testing.T) { |
| 17 |
standardPlist := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?> |
| 18 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 19 |
<plist version="1.0"> |
| 20 |
<dict> |
| 21 |
<key>ProductBuildVersion</key> |
| 22 |
<string>20E232</string> |
| 23 |
<key>ProductCopyright</key> |
| 24 |
<string>1983-2021 Apple Inc.</string> |
| 25 |
<key>ProductName</key> |
| 26 |
<string>macOS</string> |
| 27 |
<key>ProductUserVisibleVersion</key> |
| 28 |
<string>11.3</string> |
| 29 |
<key>ProductVersion</key> |
| 30 |
<string>11.3</string> |
| 31 |
<key>iOSSupportVersion</key> |
| 32 |
<string>14.5</string> |
| 33 |
</dict> |
| 34 |
</plist>`) |
| 35 |
|
| 36 |
parsedPlist := map[string]string{ |
| 37 |
"ProductBuildVersion": "20E232", |
| 38 |
"ProductCopyright": "1983-2021 Apple Inc.", |
| 39 |
"ProductName": "macOS", |
| 40 |
"ProductUserVisibleVersion": "11.3", |
| 41 |
"ProductVersion": "11.3", |
| 42 |
"iOSSupportVersion": "14.5", |
| 43 |
} |
| 44 |
|
| 45 |
emptyPlist := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?> |
| 46 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 47 |
<plist version="1.0"> |
| 48 |
<dict> |
| 49 |
</dict> |
| 50 |
</plist>`) |
| 51 |
|
| 52 |
missingDictPlist := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?> |
| 53 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 54 |
<plist version="1.0"> |
| 55 |
</plist>`) |
| 56 |
|
| 57 |
unknownElementsPlist := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?> |
| 58 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 59 |
<plist version="1.0"> |
| 60 |
<dict> |
| 61 |
<a> |
| 62 |
<b>123</b> |
| 63 |
</a> |
| 64 |
<key>ProductBuildVersion</key> |
| 65 |
<c>Value</c> |
| 66 |
<string>20E232</string> |
| 67 |
<d attr="1"></d> |
| 68 |
</dict> |
| 69 |
</plist>`) |
| 70 |
|
| 71 |
parsedUnknownElementsPlist := map[string]string{ |
| 72 |
"ProductBuildVersion": "20E232", |
| 73 |
} |
| 74 |
|
| 75 |
tt := []struct { |
| 76 |
Name string |
| 77 |
Plist io.Reader |
| 78 |
Parsed map[string]string |
| 79 |
}{ |
| 80 |
{"Standard", standardPlist, parsedPlist}, |
| 81 |
{"Empty", emptyPlist, map[string]string{}}, |
| 82 |
{"Missing dict", missingDictPlist, map[string]string{}}, |
| 83 |
{"Unknown elements", unknownElementsPlist, parsedUnknownElementsPlist}, |
| 84 |
} |
| 85 |
|
| 86 |
for _, tc := range tt { |
| 87 |
t.Run(tc.Name, func(t *testing.T) { |
| 88 |
result, err := resource.ParsePlistFile(tc.Plist) |
| 89 |
|
| 90 |
require.Equal(t, tc.Parsed, result) |
| 91 |
require.NoError(t, err) |
| 92 |
}) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func TestParsePlistFileUnevenKeys(t *testing.T) { |
| 97 |
plist := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?> |
| 98 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 99 |
<plist version="1.0"> |
| 100 |
<dict> |
| 101 |
<key>ProductBuildVersion</key> |
| 102 |
<string>20E232</string> |
| 103 |
<key>ProductCopyright</key> |
| 104 |
</dict> |
| 105 |
</plist>`) |
| 106 |
|
| 107 |
result, err := resource.ParsePlistFile(plist) |
| 108 |
|
| 109 |
require.Nil(t, result) |
| 110 |
require.Error(t, err) |
| 111 |
} |
| 112 |
|
| 113 |
func TestParsePlistFileMalformed(t *testing.T) { |
| 114 |
plist := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?> |
| 115 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 116 |
<plist version="1.0"> |
| 117 |
<dict> |
| 118 |
<key>Product |
| 119 |
</dict> |
| 120 |
</plist>`) |
| 121 |
|
| 122 |
result, err := resource.ParsePlistFile(plist) |
| 123 |
|
| 124 |
require.Nil(t, result) |
| 125 |
require.Error(t, err) |
| 126 |
} |
| 127 |
|
| 128 |
func TestBuildOSRelease(t *testing.T) { |
| 129 |
tt := []struct { |
| 130 |
Name string |
| 131 |
Properties map[string]string |
| 132 |
OSRelease string |
| 133 |
}{ |
| 134 |
{"Empty properties", map[string]string{}, ""}, |
| 135 |
{"Empty properties (nil)", nil, ""}, |
| 136 |
{"Missing product name", map[string]string{ |
| 137 |
"ProductVersion": "11.3", |
| 138 |
"ProductBuildVersion": "20E232", |
| 139 |
}, ""}, |
| 140 |
{"Missing product version", map[string]string{ |
| 141 |
"ProductName": "macOS", |
| 142 |
"ProductBuildVersion": "20E232", |
| 143 |
}, ""}, |
| 144 |
{"Missing product build version", map[string]string{ |
| 145 |
"ProductName": "macOS", |
| 146 |
"ProductVersion": "11.3", |
| 147 |
}, ""}, |
| 148 |
{"All properties available", map[string]string{ |
| 149 |
"ProductName": "macOS", |
| 150 |
"ProductVersion": "11.3", |
| 151 |
"ProductBuildVersion": "20E232", |
| 152 |
}, "macOS 11.3 (20E232)"}, |
| 153 |
} |
| 154 |
|
| 155 |
for _, tc := range tt { |
| 156 |
t.Run(tc.Name, func(t *testing.T) { |
| 157 |
result := resource.BuildOSRelease(tc.Properties) |
| 158 |
require.Equal(t, tc.OSRelease, result) |
| 159 |
}) |
| 160 |
} |
| 161 |
} |