|
@@ -7,43 +7,41 @@ import (
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing"
|
|
|
|
+
|
|
|
|
+ "gotest.tools/assert"
|
|
)
|
|
)
|
|
|
|
|
|
-func TestGetOperatingSystem(t *testing.T) {
|
|
|
|
- var backup = etcOsRelease
|
|
|
|
|
|
+type EtcReleaseParsingTest struct {
|
|
|
|
+ name string
|
|
|
|
+ content string
|
|
|
|
+ expected string
|
|
|
|
+ expectedErr string
|
|
|
|
+}
|
|
|
|
|
|
- invalids := []struct {
|
|
|
|
- content string
|
|
|
|
- errorExpected string
|
|
|
|
- }{
|
|
|
|
|
|
+func TestGetOperatingSystem(t *testing.T) {
|
|
|
|
+ tests := []EtcReleaseParsingTest{
|
|
{
|
|
{
|
|
- `PRETTY_NAME=Source Mage GNU/Linux
|
|
|
|
|
|
+ content: `PRETTY_NAME=Source Mage GNU/Linux
|
|
PRETTY_NAME=Ubuntu 14.04.LTS`,
|
|
PRETTY_NAME=Ubuntu 14.04.LTS`,
|
|
- "PRETTY_NAME needs to be enclosed by quotes if they have spaces: Source Mage GNU/Linux",
|
|
|
|
|
|
+ expectedErr: "PRETTY_NAME needs to be enclosed by quotes if they have spaces: Source Mage GNU/Linux",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `PRETTY_NAME="Ubuntu Linux
|
|
|
|
|
|
+ content: `PRETTY_NAME="Ubuntu Linux
|
|
PRETTY_NAME=Ubuntu 14.04.LTS`,
|
|
PRETTY_NAME=Ubuntu 14.04.LTS`,
|
|
- "PRETTY_NAME is invalid: invalid command line string",
|
|
|
|
|
|
+ expectedErr: "PRETTY_NAME is invalid: invalid command line string",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `PRETTY_NAME=Ubuntu'
|
|
|
|
|
|
+ content: `PRETTY_NAME=Ubuntu'
|
|
PRETTY_NAME=Ubuntu 14.04.LTS`,
|
|
PRETTY_NAME=Ubuntu 14.04.LTS`,
|
|
- "PRETTY_NAME is invalid: invalid command line string",
|
|
|
|
|
|
+ expectedErr: "PRETTY_NAME is invalid: invalid command line string",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `PRETTY_NAME'
|
|
|
|
|
|
+ content: `PRETTY_NAME'
|
|
PRETTY_NAME=Ubuntu 14.04.LTS`,
|
|
PRETTY_NAME=Ubuntu 14.04.LTS`,
|
|
- "PRETTY_NAME needs to be enclosed by quotes if they have spaces: Ubuntu 14.04.LTS",
|
|
|
|
|
|
+ expectedErr: "PRETTY_NAME needs to be enclosed by quotes if they have spaces: Ubuntu 14.04.LTS",
|
|
},
|
|
},
|
|
- }
|
|
|
|
-
|
|
|
|
- valids := []struct {
|
|
|
|
- content string
|
|
|
|
- expected string
|
|
|
|
- }{
|
|
|
|
{
|
|
{
|
|
- `NAME="Ubuntu"
|
|
|
|
|
|
+ content: `NAME="Ubuntu"
|
|
PRETTY_NAME_AGAIN="Ubuntu 14.04.LTS"
|
|
PRETTY_NAME_AGAIN="Ubuntu 14.04.LTS"
|
|
VERSION="14.04, Trusty Tahr"
|
|
VERSION="14.04, Trusty Tahr"
|
|
ID=ubuntu
|
|
ID=ubuntu
|
|
@@ -52,10 +50,10 @@ VERSION_ID="14.04"
|
|
HOME_URL="http://www.ubuntu.com/"
|
|
HOME_URL="http://www.ubuntu.com/"
|
|
SUPPORT_URL="http://help.ubuntu.com/"
|
|
SUPPORT_URL="http://help.ubuntu.com/"
|
|
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
|
|
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
|
|
- "Linux",
|
|
|
|
|
|
+ expected: "Linux",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `NAME="Ubuntu"
|
|
|
|
|
|
+ content: `NAME="Ubuntu"
|
|
VERSION="14.04, Trusty Tahr"
|
|
VERSION="14.04, Trusty Tahr"
|
|
ID=ubuntu
|
|
ID=ubuntu
|
|
ID_LIKE=debian
|
|
ID_LIKE=debian
|
|
@@ -63,10 +61,10 @@ VERSION_ID="14.04"
|
|
HOME_URL="http://www.ubuntu.com/"
|
|
HOME_URL="http://www.ubuntu.com/"
|
|
SUPPORT_URL="http://help.ubuntu.com/"
|
|
SUPPORT_URL="http://help.ubuntu.com/"
|
|
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
|
|
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
|
|
- "Linux",
|
|
|
|
|
|
+ expected: "Linux",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `NAME=Gentoo
|
|
|
|
|
|
+ content: `NAME=Gentoo
|
|
ID=gentoo
|
|
ID=gentoo
|
|
PRETTY_NAME="Gentoo/Linux"
|
|
PRETTY_NAME="Gentoo/Linux"
|
|
ANSI_COLOR="1;32"
|
|
ANSI_COLOR="1;32"
|
|
@@ -74,10 +72,10 @@ HOME_URL="http://www.gentoo.org/"
|
|
SUPPORT_URL="http://www.gentoo.org/main/en/support.xml"
|
|
SUPPORT_URL="http://www.gentoo.org/main/en/support.xml"
|
|
BUG_REPORT_URL="https://bugs.gentoo.org/"
|
|
BUG_REPORT_URL="https://bugs.gentoo.org/"
|
|
`,
|
|
`,
|
|
- "Gentoo/Linux",
|
|
|
|
|
|
+ expected: "Gentoo/Linux",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `NAME="Ubuntu"
|
|
|
|
|
|
+ content: `NAME="Ubuntu"
|
|
VERSION="14.04, Trusty Tahr"
|
|
VERSION="14.04, Trusty Tahr"
|
|
ID=ubuntu
|
|
ID=ubuntu
|
|
ID_LIKE=debian
|
|
ID_LIKE=debian
|
|
@@ -86,28 +84,77 @@ VERSION_ID="14.04"
|
|
HOME_URL="http://www.ubuntu.com/"
|
|
HOME_URL="http://www.ubuntu.com/"
|
|
SUPPORT_URL="http://help.ubuntu.com/"
|
|
SUPPORT_URL="http://help.ubuntu.com/"
|
|
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
|
|
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
|
|
- "Ubuntu 14.04 LTS",
|
|
|
|
|
|
+ expected: "Ubuntu 14.04 LTS",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `NAME="Ubuntu"
|
|
|
|
|
|
+ content: `NAME="Ubuntu"
|
|
VERSION="14.04, Trusty Tahr"
|
|
VERSION="14.04, Trusty Tahr"
|
|
ID=ubuntu
|
|
ID=ubuntu
|
|
ID_LIKE=debian
|
|
ID_LIKE=debian
|
|
PRETTY_NAME='Ubuntu 14.04 LTS'`,
|
|
PRETTY_NAME='Ubuntu 14.04 LTS'`,
|
|
- "Ubuntu 14.04 LTS",
|
|
|
|
|
|
+ expected: "Ubuntu 14.04 LTS",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `PRETTY_NAME=Source
|
|
|
|
|
|
+ content: `PRETTY_NAME=Source
|
|
NAME="Source Mage"`,
|
|
NAME="Source Mage"`,
|
|
- "Source",
|
|
|
|
|
|
+ expected: "Source",
|
|
},
|
|
},
|
|
{
|
|
{
|
|
- `PRETTY_NAME=Source
|
|
|
|
|
|
+ content: `PRETTY_NAME=Source
|
|
PRETTY_NAME="Source Mage"`,
|
|
PRETTY_NAME="Source Mage"`,
|
|
- "Source Mage",
|
|
|
|
|
|
+ expected: "Source Mage",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ runEtcReleaseParsingTests(t, tests, GetOperatingSystem)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func TestGetOperatingSystemVersion(t *testing.T) {
|
|
|
|
+ tests := []EtcReleaseParsingTest{
|
|
|
|
+ {
|
|
|
|
+ name: "invalid version id",
|
|
|
|
+ content: `VERSION_ID="18.04
|
|
|
|
+VERSION_ID=18.04`,
|
|
|
|
+ expectedErr: "VERSION_ID is invalid: invalid command line string",
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ name: "ubuntu 14.04",
|
|
|
|
+ content: `NAME="Ubuntu"
|
|
|
|
+PRETTY_NAME="Ubuntu 14.04.LTS"
|
|
|
|
+VERSION="14.04, Trusty Tahr"
|
|
|
|
+ID=ubuntu
|
|
|
|
+ID_LIKE=debian
|
|
|
|
+VERSION_ID="14.04"
|
|
|
|
+HOME_URL="http://www.ubuntu.com/"
|
|
|
|
+SUPPORT_URL="http://help.ubuntu.com/"
|
|
|
|
+BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
|
|
|
|
+ expected: "14.04",
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ name: "gentoo",
|
|
|
|
+ content: `NAME=Gentoo
|
|
|
|
+ID=gentoo
|
|
|
|
+PRETTY_NAME="Gentoo/Linux"
|
|
|
|
+ANSI_COLOR="1;32"
|
|
|
|
+HOME_URL="http://www.gentoo.org/"
|
|
|
|
+SUPPORT_URL="http://www.gentoo.org/main/en/support.xml"
|
|
|
|
+BUG_REPORT_URL="https://bugs.gentoo.org/"
|
|
|
|
+`,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ name: "dual version id",
|
|
|
|
+ content: `VERSION_ID="14.04"
|
|
|
|
+VERSION_ID=18.04`,
|
|
|
|
+ expected: "18.04",
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ runEtcReleaseParsingTests(t, tests, GetOperatingSystemVersion)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func runEtcReleaseParsingTests(t *testing.T, tests []EtcReleaseParsingTest, parsingFunc func() (string, error)) {
|
|
|
|
+ var backup = etcOsRelease
|
|
|
|
+
|
|
dir := os.TempDir()
|
|
dir := os.TempDir()
|
|
etcOsRelease = filepath.Join(dir, "etcOsRelease")
|
|
etcOsRelease = filepath.Join(dir, "etcOsRelease")
|
|
|
|
|
|
@@ -116,24 +163,19 @@ PRETTY_NAME="Source Mage"`,
|
|
etcOsRelease = backup
|
|
etcOsRelease = backup
|
|
}()
|
|
}()
|
|
|
|
|
|
- for _, elt := range invalids {
|
|
|
|
- if err := ioutil.WriteFile(etcOsRelease, []byte(elt.content), 0600); err != nil {
|
|
|
|
- t.Fatalf("failed to write to %s: %v", etcOsRelease, err)
|
|
|
|
- }
|
|
|
|
- s, err := GetOperatingSystem()
|
|
|
|
- if err == nil || err.Error() != elt.errorExpected {
|
|
|
|
- t.Fatalf("Expected an error %q, got %q (err: %v)", elt.errorExpected, s, err)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for _, elt := range valids {
|
|
|
|
- if err := ioutil.WriteFile(etcOsRelease, []byte(elt.content), 0600); err != nil {
|
|
|
|
- t.Fatalf("failed to write to %s: %v", etcOsRelease, err)
|
|
|
|
- }
|
|
|
|
- s, err := GetOperatingSystem()
|
|
|
|
- if err != nil || s != elt.expected {
|
|
|
|
- t.Fatalf("Expected %q, got %q (err: %v)", elt.expected, s, err)
|
|
|
|
- }
|
|
|
|
|
|
+ for _, test := range tests {
|
|
|
|
+ t.Run(test.name, func(t *testing.T) {
|
|
|
|
+ if err := ioutil.WriteFile(etcOsRelease, []byte(test.content), 0600); err != nil {
|
|
|
|
+ t.Fatalf("failed to write to %s: %v", etcOsRelease, err)
|
|
|
|
+ }
|
|
|
|
+ s, err := parsingFunc()
|
|
|
|
+ if test.expectedErr == "" {
|
|
|
|
+ assert.NilError(t, err)
|
|
|
|
+ } else {
|
|
|
|
+ assert.Error(t, err, test.expectedErr)
|
|
|
|
+ }
|
|
|
|
+ assert.Equal(t, s, test.expected)
|
|
|
|
+ })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|