-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factor parsing into internal pkg, dont error out when os cmd fails
- Loading branch information
1 parent
5cddc29
commit 8bf8823
Showing
6 changed files
with
211 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.uber.org/goleak" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
goleak.VerifyTestMain(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package util | ||
|
||
import "strings" | ||
|
||
// ParseDarwinDescription parses out the OS name and version for darwin machines. | ||
// 'input' should be the string representation from running the command 'sw_vers'. | ||
// An example of this command's desired output is below: | ||
// | ||
// ProductName: macOS | ||
// ProductVersion: 15.0.1 | ||
// BuildVersion: 24A348 | ||
func ParseDarwinDescription(input string) string { | ||
var productName string | ||
var productVersion string | ||
for _, l := range strings.Split(input, "\n") { | ||
line := strings.TrimSpace(l) | ||
if raw, ok := strings.CutPrefix(line, "ProductName:"); ok { | ||
productName = strings.TrimSpace(raw) | ||
} else if raw, ok = strings.CutPrefix(line, "ProductVersion:"); ok { | ||
productVersion = strings.TrimSpace(raw) | ||
} | ||
} | ||
if productName != "" && productVersion != "" { | ||
return productName + " " + productVersion | ||
} | ||
return "" | ||
} | ||
|
||
// ParseLinuxDescription parses out the OS name and version for linux machines. | ||
// 'input' should be the string representation from running the command 'lsb_release -d'. | ||
// An example of this command's desired output is below: | ||
// | ||
// Description: Ubuntu 20.04.6 LTS | ||
func ParseLinuxDescription(input string) string { | ||
if raw, ok := strings.CutPrefix(strings.TrimSpace(input), "Description:"); ok { | ||
return strings.TrimSpace(raw) | ||
} | ||
return "" | ||
} | ||
|
||
// ParseLinuxDescription parses out the OS name and version for windows machines. | ||
// 'input' should be the string representation from running the command 'cmd /c ver'. | ||
// An example of this command's desired output is below: | ||
// | ||
// Microsoft Windows [Version 10.0.20348.2700] | ||
func ParseWindowsDescription(input string) string { | ||
return strings.TrimSpace(input) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseDarwinDescription(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
desc: "basic use case", | ||
input: "ProductName: macOS\nProductVersion: 15.0.1\nBuildVersion: 24A348", | ||
expected: "macOS 15.0.1", | ||
}, | ||
{ | ||
desc: "excessive white space", | ||
input: " \n ProductName: macOS\nProductVersion: 15.0.1 \n \n BuildVersion: 24A348\n", | ||
expected: "macOS 15.0.1", | ||
}, | ||
{ | ||
desc: "random ordering & excessive white space", | ||
input: "\nProductVersion: 15.0.1 \n \n BuildVersion: 24A348\n \n ProductName: macOS", | ||
expected: "macOS 15.0.1", | ||
}, | ||
{ | ||
desc: "blank input", | ||
input: "", | ||
expected: "", | ||
}, | ||
{ | ||
desc: "missing ProductVersion", | ||
input: "ProductName: macOS\nBuildVersion: 24A348", | ||
expected: "", | ||
}, | ||
{ | ||
desc: "missing ProductName", | ||
input: "ProductVersion: 15.0.1\nBuildVersion: 24A348", | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
actual := ParseDarwinDescription(tc.input) | ||
require.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestParseLinuxDescription(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
desc: "basic use case", | ||
input: "Description: Ubuntu 20.04.6 LTS", | ||
expected: "Ubuntu 20.04.6 LTS", | ||
}, | ||
{ | ||
desc: "excessive white space", | ||
input: " \n Description: Ubuntu 20.04.6 LTS \n ", | ||
expected: "Ubuntu 20.04.6 LTS", | ||
}, | ||
{ | ||
desc: "blank input", | ||
input: "", | ||
expected: "", | ||
}, | ||
{ | ||
desc: "missing Description", | ||
input: "Foo: Ubuntu 20.04.6 LTS", | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
actual := ParseLinuxDescription(tc.input) | ||
require.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestParseWindowsDescription(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
desc: "basic use case", | ||
input: "Microsoft Windows [Version 10.0.20348.2700]", | ||
expected: "Microsoft Windows [Version 10.0.20348.2700]", | ||
}, | ||
{ | ||
desc: "excessive surrounding white space", | ||
input: " \n Microsoft Windows [Version 10.0.20348.2700] \n ", | ||
expected: "Microsoft Windows [Version 10.0.20348.2700]", | ||
}, | ||
{ | ||
desc: "excessive white space", | ||
input: " \n Microsoft Windows [Version 10.0.20348.2700] \n ", | ||
expected: "Microsoft Windows [Version 10.0.20348.2700]", | ||
}, | ||
{ | ||
desc: "blank input", | ||
input: "", | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
actual := ParseWindowsDescription(tc.input) | ||
require.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters