libvirt-go

repository·master·Indexed 43 days ago

https://github.com/libvirt/libvirt-go

Go language bindings for the libvirt C API, enabling Go developers to manage virtualization technologies such as QEMU and LXC.

Tokens
818
Snippets
3
Records
7
Agent score
6%

What's inside libvirt-go

  1. Best practices for using 'flags uint32' parameters

    master

    To ensure your code remains compatible with future versions of libvirt-go, follow this rule for methods containing a flags uint32 parameter:

    If the libvirt API defines specific constants for these flags, the parameter type in the Go binding may change to a specific typedef. To avoid breakage caused by type mismatches, always pass a literal 0 to any flags uint32 parameter. This allows the value to auto-cast to any future typedef introduced.

  2. Configure libvirt-go build with specific drivers

    master

    By default, the bindings support libvirt.so, libvirt-qemu.so, and libvirt-lxc.so. You can exclude specific drivers during the build process using Go build tags:

    • Use -tags without_qemu to drop support for the QEMU driver.
    • Use -tags without_lxc to drop support for the LXC driver.
  3. Setup libvirtd for integration testing (TCP/SASL)

    master

    Some integration tests require libvirtd to listen for TCP connections on localhost using SASL authentication.

    1. Edit /etc/libvirt/libvirtd.conf with the following settings:
    listen_tls=0
    listen_tcp=1
    auth_tcp=sasl
    listen_addr="127.0.0.1"
    1. Start libvirtd with the --listen flag (you can add this to /etc/sysconfig/libvirtd for persistence).
    2. Ensure SASL is configured to use digest-md5 or scram-sha-1.
    3. Create a SASL user:
    saslpasswd2 -a libvirt user

    (Then enter your desired password when prompted).

  4. Run integration tests using Vagrant

    master

    If you prefer not to configure your host environment, you can use the provided Vagrantfile to provision a virtual machine for testing:

    1. Navigate to the vagrant directory: cd ./vagrant
    2. Provision the VM: vagrant up
    3. Login to the VM: vagrant ssh
    4. Run tests inside the VM: sudo su - followed by go test -tags integration libvirt.
    cd ./vagrant
    vagrant up
    vagrant ssh
    # Inside the VM:
    sudo su -
    go test -tags integration libvirt
  5. Configure passwordless libvirt access for testing

    master

    To run unit tests, your user account must have read-write access to libvirt without a password. You can configure this using a polkit configuration file. Replace YOUR_USERNAME with your actual UNIX username.

    cat > /etc/polkit-1/localauthority/50-local.d/50-libvirt.pkla  <<EOF
    [Passwordless libvirt access]
    Identity=unix-group:YOUR_USERNAME
    Action=org.libvirt.unix.manage
    ResultAny=yes
    ResultInactive=yes
    ResultActive=yes
    EOF
  6. Run libvirt-go API and integration tests

    master

    The project provides different levels of testing using Go build tags:

    1. API Coverage Tests: Verifies coverage of libvirt C library APIs and constants. Run with: go test -tags api

    2. Integration Tests: Exercises code using QEMU or LXC drivers (for areas where the built-in test driver lacks functionality). These tests are located in integration_test.go and must be run with: go test -tags integration

    3. Unit Tests: Use the built-in test driver (test:///default) and do not interact with the host OS.

    go test -tags api
    go test -tags integration