From nobody Thu May 2 05:49:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=tremily.us Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1539608652487626.3865004293859; Mon, 15 Oct 2018 06:04:12 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id ECFAC8B26A; Mon, 15 Oct 2018 13:04:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 115511062256; Mon, 15 Oct 2018 13:04:08 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id CC86E4BB79; Mon, 15 Oct 2018 13:04:03 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9BNuVdV029465 for ; Thu, 11 Oct 2018 19:56:31 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6F139891CD; Thu, 11 Oct 2018 23:56:31 +0000 (UTC) Received: from mx1.redhat.com (ext-mx16.extmail.prod.ext.phx2.redhat.com [10.5.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 66A1D891C8 for ; Thu, 11 Oct 2018 23:56:28 +0000 (UTC) Received: from resqmta-po-02v.sys.comcast.net (resqmta-po-02v.sys.comcast.net [96.114.154.161]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9552F3082A26 for ; Thu, 11 Oct 2018 23:56:26 +0000 (UTC) Received: from resomta-po-05v.sys.comcast.net ([96.114.154.229]) by resqmta-po-02v.sys.comcast.net with ESMTP id AiuvgSDL6JYXQAkocg7Qng; Thu, 11 Oct 2018 23:56:26 +0000 Received: from mail.tremily.us ([67.168.83.168]) by resomta-po-05v.sys.comcast.net with ESMTPSA id AkmfgKHbMfffuAkmggepQ2; Thu, 11 Oct 2018 23:54:26 +0000 Received: by mail.tremily.us (Postfix, from userid 1000) id B00BF19EECEE; Thu, 11 Oct 2018 16:58:54 -0700 (PDT) From: "W. Trevor King" To: libvir-list@redhat.com Date: Thu, 11 Oct 2018 16:58:41 -0700 Message-Id: <8863a1dbb679997d214d60beec252cfe9adfa743.1539301004.git.wking@tremily.us> X-CMAE-Envelope: MS4wfOWbRzCZX0RzwvhkZ17F+Chy99KvRAXOwtGEo6alqxQEXhKcdrIz3t4O14H6uyHiMjrqT2wO8ZAhZv8xKGjlMDcpQzlYJK5YjolPJ+IRAr1FnaNCBU+b 4qBYp5b5bug1DcMHtut22WKNXXYhbEXkoWQkQZulvV8pLF/obz1pfL9J X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 216 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Thu, 11 Oct 2018 23:56:27 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Thu, 11 Oct 2018 23:56:27 +0000 (UTC) for IP:'96.114.154.161' DOMAIN:'resqmta-po-02v.sys.comcast.net' HELO:'resqmta-po-02v.sys.comcast.net' FROM:'wking@tremily.us' RCPT:'' X-RedHat-Spam-Score: -0.701 (RCVD_IN_DNSWL_LOW, SPF_PASS) 96.114.154.161 resqmta-po-02v.sys.comcast.net 96.114.154.161 resqmta-po-02v.sys.comcast.net X-Scanned-By: MIMEDefang 2.84 on 10.5.110.45 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com X-Mailman-Approved-At: Mon, 15 Oct 2018 08:55:09 -0400 Cc: "W. Trevor King" Subject: [libvirt] [PATCH] version: Add ParseVersion and a Version struct X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 15 Oct 2018 13:04:11 +0000 (UTC) X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Make it easier to convert version integers to the more human-readable major.minor.release format. Reviewed-by: John Ferlan --- 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() { } =20 const ( + // VERSION_NUMBER is the version of the linked libvirt. VERSION_NUMBER =3D uint32(C.LIBVIR_VERSION_NUMBER) ) =20 @@ -306,7 +307,9 @@ func releaseConnectionData(c *Connect) { delete(connections, c.ptr) } =20 +// GetVersion returns the version of the linked libvirt. // See also https://libvirt.org/html/libvirt-libvirt-host.html#virGetVersi= on +// 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 } =20 +// 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#virConnectG= etLibVersion +// 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 s= tring, arch string, machine return C.GoString(ret), nil } =20 +// GetVersion returns the hypervisor version. // See also https://libvirt.org/html/libvirt-libvirt-host.html#virConnectG= etVersion +// 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"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * 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#virGetVersi= on +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 instanc= e. +// See also https://libvirt.org/html/libvirt-libvirt-host.html#virGetVersi= on +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"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * 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 :=3D Version{ + Major: 3, + Minor: 9, + Release: 0, + } + actual :=3D version.String() + expected :=3D "3.9.0" + if actual !=3D expected { + t.Fatalf("%q !=3D %q", actual, expected) + } +} + +func TestParseVersion(t *testing.T) { + for _, testCase :=3D 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 :=3D ParseVersion(testCase.input) + if !reflect.DeepEqual(actual, testCase.expected) { + t.Fatalf("%v !=3D %v", actual, testCase.expected) + } + } +} --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list