Skip to content

Commit

Permalink
Added ListAllInterfaces and integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
dorzheh authored and alexzorin committed May 11, 2014
1 parent 9a21043 commit 36cf8a5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
28 changes: 28 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,31 @@ func TestGetDomainCPUStats(t *testing.T) {
// return
// }
// }

func TestListAllInterfaces(t *testing.T) {
conn, err := NewVirConnection("lxc:///")
if err != nil {
t.Error(err)
return
}
defer conn.CloseConnection()
ifaces, err := conn.ListAllInterfaces(0)
if err != nil {
t.Fatal(err)
}
lookingFor := "lo"
found := false
for _, iface := range ifaces {
name, err := iface.GetName()
if err != nil {
t.Fatal(err)
}
if name == lookingFor {
found = true
break
}
}
if found == false {
t.Fatalf("interface %s not found", lookingFor)
}
}
21 changes: 21 additions & 0 deletions libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -617,3 +618,23 @@ func (c *VirConnection) LookupSecretByUsage(usageType int, usageID string) (VirS
}
return VirSecret{ptr: ptr}, nil
}

func (c *VirConnection) ListAllInterfaces(flags uint32) ([]VirInterface, error) {
var cList *C.virInterfacePtr
numIfaces := C.virConnectListAllInterfaces(c.ptr, (**C.virInterfacePtr)(&cList), C.uint(flags))
if numIfaces == -1 {
return nil, errors.New(GetLastError())
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numIfaces),
Cap: int(numIfaces),
}
var ifaces []VirInterface
slice := *(*[]C.virInterfacePtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
ifaces = append(ifaces, VirInterface{ptr})
}
C.free(unsafe.Pointer(cList))
return ifaces, nil
}

0 comments on commit 36cf8a5

Please sign in to comment.