Discussion:
[libvirt] [PATCH] version: Add ParseVersion and a Version struct
W. Trevor King
2018-10-11 23:58:41 UTC
Permalink
Make it easier to convert version integers to the more human-readable
major.minor.release format.
---
connect.go | 8 ++++++++
version.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++
version_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 124 insertions(+)
create mode 100644 version.go
create mode 100644 version_test.go

diff --git a/connect.go b/connect.go
index 8cc7cc7..fac45da 100644
--- a/connect.go
+++ b/connect.go
@@ -46,6 +46,7 @@ func init() {
}

const (
+ // VERSION_NUMBER is the version of the linked libvirt.
VERSION_NUMBER = uint32(C.LIBVIR_VERSION_NUMBER)
)

@@ -306,7 +307,9 @@ func releaseConnectionData(c *Connect) {
delete(connections, c.ptr)
}

+// GetVersion returns the version of the linked libvirt.
// See also https://libvirt.org/html/libvirt-libvirt-host.html#virGetVersion
+// and ParseVersion.
func GetVersion() (uint32, error) {
var version C.ulong
var err C.virError
@@ -540,7 +543,10 @@ func (c *Connect) GetHostname() (string, error) {
return hostname, nil
}

+// GetLibVersion returns the version of libvirt used by the daemon
+// running on the connected host.
// See also https://libvirt.org/html/libvirt-libvirt-host.html#virConnectGetLibVersion
+// and ParseVersion.
func (c *Connect) GetLibVersion() (uint32, error) {
var version C.ulong
var err C.virError
@@ -2272,7 +2278,9 @@ func (c *Connect) GetDomainCapabilities(emulatorbin string, arch string, machine
return C.GoString(ret), nil
}

+// GetVersion returns the hypervisor version.
// See also https://libvirt.org/html/libvirt-libvirt-host.html#virConnectGetVersion
+// and ParseVersion.
func (c *Connect) GetVersion() (uint32, error) {
var hvVer C.ulong
var err C.virError
diff --git a/version.go b/version.go
new file mode 100644
index 0000000..1a07fa3
--- /dev/null
+++ b/version.go
@@ -0,0 +1,52 @@
+/*
+ * This file is part of the libvirt-go project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ */
+
+package libvirt
+
+import (
+ "fmt"
+)
+
+// Version represents the libvirt version.
+// See also https://libvirt.org/html/libvirt-libvirt-host.html#virGetVersion
+type Version struct {
+ Major uint32
+ Minor uint32
+ Release uint32
+}
+
+// String returns the "major.minor.release" version string.
+func (v *Version) String() string {
+ return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Release)
+}
+
+// ParseVersion converts a version integer to a structured Version instance.
+// See also https://libvirt.org/html/libvirt-libvirt-host.html#virGetVersion
+func ParseVersion(version uint32) *Version {
+ return &Version{
+ Major: version / 1000000,
+ Minor: (version / 1000) % 1000,
+ Release: version % 1000,
+ }
+}
diff --git a/version_test.go b/version_test.go
new file mode 100644
index 0000000..ecafe5d
--- /dev/null
+++ b/version_test.go
@@ -0,0 +1,64 @@
+/*
+ * This file is part of the libvirt-go project
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ */
+
+package libvirt
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestVersionString(t *testing.T) {
+ version := Version{
+ Major: 3,
+ Minor: 9,
+ Release: 0,
+ }
+ actual := version.String()
+ expected := "3.9.0"
+ if actual != expected {
+ t.Fatalf("%q != %q", actual, expected)
+ }
+}
+
+func TestParseVersion(t *testing.T) {
+ for _, testCase := range []struct{
+ input uint32
+ expected *Version
+ }{
+ {
+ input: 3009000,
+ expected: &Version{Major: 3, Minor: 9},
+ },
+ {
+ input: 4001002,
+ expected: &Version{Major: 4, Minor: 1, Release: 2},
+ },
+ } {
+ actual := ParseVersion(testCase.input)
+ if !reflect.DeepEqual(actual, testCase.expected) {
+ t.Fatalf("%v != %v", actual, testCase.expected)
+ }
+ }
+}
--
1.8.3.1
W. Trevor King
2018-11-06 17:15:23 UTC
Permalink
Anything I can do to help this along? I've adjusted the subject to
have "PATCH go" instead of just "PATCH"; sorry about that.

Cheers,
Trevor
--
This email may be signed or encrypted with GnuPG (http://www.gnupg.org).
For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy
John Ferlan
2018-12-06 21:04:06 UTC
Permalink
Post by W. Trevor King
Make it easier to convert version integers to the more human-readable
major.minor.release format.
---
connect.go | 8 ++++++++
version.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++
version_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 124 insertions(+)
create mode 100644 version.go
create mode 100644 version_test.go
Well I'm far from the expert here, but since this has been "out there"
for a while, I'll given it a go.

[...]
Post by W. Trevor King
diff --git a/version_test.go b/version_test.go
[...]
Post by W. Trevor King
+func TestParseVersion(t *testing.T) {
+ for _, testCase := range []struct{
+ input uint32
+ expected *Version
+ }{
+ {
+ input: 3009000,
+ expected: &Version{Major: 3, Minor: 9},
NIT: Should the above have the "Release: 0" if nothing more than to
prove your algorithm? I can add it or leave it as is.

Reviewed-by: John Ferlan <***@redhat.com>

John

[let me know either way on the above and I can push this]

[...]
John Ferlan
2018-12-06 21:06:58 UTC
Permalink
Post by John Ferlan
Post by W. Trevor King
Make it easier to convert version integers to the more human-readable
major.minor.release format.
Oh, and I'll need you to "OK" me adding an Signed-off-by: or you need to
provide one. See:

https://libvirt.org/hacking.html

and point 6 of general tips:

Contributors to libvirt projects must assert that they are in compliance
with the Developer Certificate of Origin 1.1. This is achieved by adding
a "Signed-off-by" line containing the contributor's name and e-mail to
every commit message. The presence of this line attests that the
contributor has read the above lined DCO and agrees with its statements.

https://developercertificate.org/

John
Post by John Ferlan
Post by W. Trevor King
---
connect.go | 8 ++++++++
version.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++
version_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 124 insertions(+)
create mode 100644 version.go
create mode 100644 version_test.go
Well I'm far from the expert here, but since this has been "out there"
for a while, I'll given it a go.
[...]
Post by W. Trevor King
diff --git a/version_test.go b/version_test.go
[...]
Post by W. Trevor King
+func TestParseVersion(t *testing.T) {
+ for _, testCase := range []struct{
+ input uint32
+ expected *Version
+ }{
+ {
+ input: 3009000,
+ expected: &Version{Major: 3, Minor: 9},
NIT: Should the above have the "Release: 0" if nothing more than to
prove your algorithm? I can add it or leave it as is.
John
[let me know either way on the above and I can push this]
[...]
--
libvir-list mailing list
https://www.redhat.com/mailman/listinfo/libvir-list
Loading...