integration_test.go 28.9 KB
Newer Older
alexzorin's avatar
alexzorin committed
1 2 3 4 5
// +build integration

package libvirt

import (
dorzheh's avatar
dorzheh committed
6 7
	"io/ioutil"
	"os"
8
	"strings"
alexzorin's avatar
alexzorin committed
9 10 11 12
	"testing"
	"time"
)

13 14
func buildTestQEMUConnection() *Connect {
	conn, err := NewConnect("qemu:///system")
15 16 17 18 19 20
	if err != nil {
		panic(err)
	}
	return conn
}

21
func buildTestQEMUDomain() (*Domain, *Connect) {
22 23
	conn := buildTestQEMUConnection()
	dom, err := conn.DomainDefineXML(`<domain type="qemu">
24
		<name>libvirt-go-test-` + strings.Replace(time.Now().String(), " ", "_", -1) + `</name>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		<memory unit="KiB">128</memory>
		<os>
			<type>hvm</type>
		</os>
	</domain>`)
	if err != nil {
		panic(err)
	}
	return dom, conn
}

func TestMultipleCloseCallback(t *testing.T) {
	nbCall1 := 0
	nbCall2 := 0
	nbCall3 := 0
	conn := buildTestQEMUConnection()
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
		if nbCall1 != 0 || nbCall2 != 0 || nbCall3 != 1 {
			t.Errorf("Wrong number of calls to callback, got %v, expected %v",
				[]int{nbCall1, nbCall2, nbCall3},
				[]int{0, 0, 1})
		}
	}()

52
	callback := func(conn Connect, reason ConnectCloseReason) {
53
		if reason != CONNECT_CLOSE_REASON_KEEPALIVE {
54
			t.Errorf("Expected close reason to be %d, got %d",
55
				CONNECT_CLOSE_REASON_KEEPALIVE, reason)
56 57
		}
	}
58 59
	err := conn.RegisterCloseCallback(func(conn Connect, reason ConnectCloseReason) {
		callback(conn, reason)
60 61 62 63 64
		nbCall1++
	})
	if err != nil {
		t.Fatalf("Unable to register close callback: %+v", err)
	}
65 66
	err = conn.RegisterCloseCallback(func(conn Connect, reason ConnectCloseReason) {
		callback(conn, reason)
67 68 69 70 71
		nbCall2++
	})
	if err != nil {
		t.Fatalf("Unable to register close callback: %+v", err)
	}
72 73
	err = conn.RegisterCloseCallback(func(conn Connect, reason ConnectCloseReason) {
		callback(conn, reason)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		nbCall3++
	})
	if err != nil {
		t.Fatalf("Unable to register close callback: %+v", err)
	}

	// To trigger a disconnect, we use a keepalive
	if err := conn.SetKeepAlive(1, 1); err != nil {
		t.Fatalf("Unable to enable keeplive: %+v", err)
	}
	EventRunDefaultImpl()
	time.Sleep(2 * time.Second)
	EventRunDefaultImpl()
}

func TestUnregisterCloseCallback(t *testing.T) {
	nbCall := 0
	conn := buildTestQEMUConnection()
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
		if nbCall != 0 {
			t.Errorf("Expected no call to close callback, got %d", nbCall)
		}
	}()

101
	callback := func(conn Connect, reason ConnectCloseReason) {
102 103
		nbCall++
	}
104
	err := conn.RegisterCloseCallback(callback)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	if err != nil {
		t.Fatalf("Unable to register close callback: %+v", err)
	}
	err = conn.UnregisterCloseCallback()
	if err != nil {
		t.Fatalf("Unable to unregister close callback: %+v", err)
	}
}

func TestSetKeepalive(t *testing.T) {
	EventRegisterDefaultImpl()        // We need the event loop for keepalive
	conn := buildTestQEMUConnection() // The test driver doesn't support keepalives
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
	if err := conn.SetKeepAlive(1, 1); err != nil {
		t.Error(err)
		return
	}

	// It should block until we have a keepalive message
	done := make(chan struct{})
	timeout := time.After(5 * time.Second)
	go func() {
		EventRunDefaultImpl()
		close(done)
	}()
	select {
	case <-done: // OK!
	case <-timeout:
		t.Fatalf("timeout reached while waiting for keepalive")
	}
}

func TestConnectionWithAuth(t *testing.T) {
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	callback := func(creds []*ConnectCredential) {
		for _, cred := range creds {
			if cred.Type == CRED_AUTHNAME {
				cred.Result = "user"
				cred.ResultLen = len(cred.Result)
			} else if cred.Type == CRED_PASSPHRASE {
				cred.Result = "pass"
				cred.ResultLen = len(cred.Result)
			}
		}
	}
	auth := &ConnectAuth{
		CredType: []ConnectCredentialType{
			CRED_AUTHNAME, CRED_PASSPHRASE,
		},
		Callback: callback,
	}
	conn, err := NewConnectWithAuth("test+tcp://127.0.0.1/default", auth, 0)
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	if err != nil {
		t.Error(err)
		return
	}
	res, err := conn.CloseConnection()
	if err != nil {
		t.Error(err)
		return
	}
	if res != 0 {
		t.Errorf("CloseConnection() == %d, expected 0", res)
	}
}

func TestConnectionWithWrongCredentials(t *testing.T) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	callback := func(creds []*ConnectCredential) {
		for _, cred := range creds {
			if cred.Type == CRED_AUTHNAME {
				cred.Result = "user"
				cred.ResultLen = len(cred.Result)
			} else if cred.Type == CRED_PASSPHRASE {
				cred.Result = "wrongpass"
				cred.ResultLen = len(cred.Result)
			}
		}
	}
	auth := &ConnectAuth{
		CredType: []ConnectCredentialType{
			CRED_AUTHNAME, CRED_PASSPHRASE,
		},
		Callback: callback,
	}
	conn, err := NewConnectWithAuth("test+tcp://127.0.0.1/default", auth, 0)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	if err == nil {
		conn.CloseConnection()
		t.Error(err)
		return
	}
}

func TestQemuMonitorCommand(t *testing.T) {
	dom, conn := buildTestQEMUDomain()
	defer func() {
		dom.Destroy()
		dom.Undefine()
		dom.Free()
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()

	if err := dom.Create(); err != nil {
		t.Error(err)
		return
	}

216
	if _, err := dom.QemuMonitorCommand("{\"execute\" : \"query-cpus\"}", DOMAIN_QEMU_MONITOR_COMMAND_DEFAULT); err != nil {
217 218 219 220
		t.Error(err)
		return
	}

221
	if _, err := dom.QemuMonitorCommand("info cpus", DOMAIN_QEMU_MONITOR_COMMAND_HMP); err != nil {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		t.Error(err)
		return
	}
}

func TestDomainCreateWithFlags(t *testing.T) {
	dom, conn := buildTestQEMUDomain()
	defer func() {
		dom.Destroy()
		dom.Undefine()
		dom.Free()
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()

238
	if err := dom.CreateWithFlags(DOMAIN_START_PAUSED); err != nil {
239
		state, reason, err := dom.GetState()
240 241 242 243 244
		if err != nil {
			t.Error(err)
			return
		}

245
		if state != DOMAIN_PAUSED {
246
			t.Fatalf("Domain should be paused")
247 248 249 250 251
			return
		}
		if DomainPausedReason(reason) != DOMAIN_PAUSED_STARTING_UP {
			t.Fatal("Domain reason should be starting up")
			return
252 253 254 255
		}
	}
}

256
func defineTestLxcDomain(conn *Connect, title string) (*Domain, error) {
257 258
	if title == "" {
		title = time.Now().String()
alexzorin's avatar
alexzorin committed
259 260
	}
	xml := `<domain type='lxc'>
261
	  <name>libvirt-go-test-` + title + `</name>
alexzorin's avatar
alexzorin committed
262 263 264 265 266 267 268 269 270 271 272
	  <title>` + title + `</title>
	  <memory>102400</memory>
	  <os>
	    <type>exe</type>
	    <init>/bin/sh</init>
	  </os>
	  <devices>
	    <console type='pty'/>
	  </devices>
	</domain>`
	dom, err := conn.DomainDefineXML(xml)
273 274 275 276 277 278 279 280 281 282
	return dom, err
}

// Integration tests are run against LXC using Libvirt 1.2.x
// on Debian Wheezy (libvirt from wheezy-backports)
//
// To run,
// 		go test -tags integration

func TestIntegrationGetMetadata(t *testing.T) {
283
	conn, err := NewConnect("lxc:///")
284 285 286 287
	if err != nil {
		t.Error(err)
		return
	}
288 289 290 291 292
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
293 294
	title := time.Now().String()
	dom, err := defineTestLxcDomain(conn, title)
alexzorin's avatar
alexzorin committed
295 296 297 298
	if err != nil {
		t.Error(err)
		return
	}
alexzorin's avatar
alexzorin committed
299
	defer dom.Free()
alexzorin's avatar
alexzorin committed
300 301 302 303
	if err := dom.Create(); err != nil {
		t.Error(err)
		return
	}
304
	v, err := dom.GetMetadata(DOMAIN_METADATA_TITLE, "", 0)
alexzorin's avatar
alexzorin committed
305 306 307 308 309 310 311 312 313
	dom.Destroy()
	if err != nil {
		t.Error(err)
		return
	}
	if v != title {
		t.Fatal("title didnt match: expected %s, got %s", title, v)
		return
	}
314 315 316 317
	if err := dom.Undefine(); err != nil {
		t.Error(err)
		return
	}
alexzorin's avatar
alexzorin committed
318
}
319 320

func TestIntegrationSetMetadata(t *testing.T) {
321
	conn, err := NewConnect("lxc:///")
322 323 324 325
	if err != nil {
		t.Error(err)
		return
	}
326 327 328 329 330
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
331 332 333 334 335
	dom, err := defineTestLxcDomain(conn, "")
	if err != nil {
		t.Error(err)
		return
	}
alexzorin's avatar
alexzorin committed
336 337 338 339
	defer func() {
		dom.Undefine()
		dom.Free()
	}()
340
	const domTitle = "newtitle"
341
	if err := dom.SetMetadata(DOMAIN_METADATA_TITLE, domTitle, "", "", 0); err != nil {
342 343 344
		t.Error(err)
		return
	}
345
	v, err := dom.GetMetadata(DOMAIN_METADATA_TITLE, "", 0)
346 347 348 349 350
	if err != nil {
		t.Error(err)
		return
	}
	if v != domTitle {
351
		t.Fatalf("DOMAIN_METADATA_TITLE should have been %s, not %s", domTitle, v)
352 353 354
		return
	}
}
355 356

func TestIntegrationGetSysinfo(t *testing.T) {
357
	conn, err := NewConnect("lxc:///")
358 359 360 361
	if err != nil {
		t.Error(err)
		return
	}
362 363 364 365 366
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
367 368 369 370 371 372 373 374 375 376
	info, err := conn.GetSysinfo(0)
	if err != nil {
		t.Error(err)
		return
	}
	if strings.Index(info, "<sysinfo") != 0 {
		t.Fatalf("Sysinfo not valid: %s", info)
		return
	}
}
377 378 379 380 381 382 383 384 385 386 387 388 389 390

func testNWFilterXML(name, chain string) string {
	defName := name
	if defName == "" {
		defName = time.Now().String()
	}
	return `<filter name='` + defName + `' chain='` + chain + `'>
            <rule action='drop' direction='out' priority='500'>
            <ip match='no' srcipaddr='$IP'/>
            </rule>
			</filter>`
}

func TestIntergrationDefineUndefineNWFilterXML(t *testing.T) {
391
	conn, err := NewConnect("lxc:///")
392 393 394 395
	if err != nil {
		t.Error(err)
		return
	}
396 397 398 399 400
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	filter, err := conn.NWFilterDefineXML(testNWFilterXML("", "ipv4"))
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		if err := filter.Undefine(); err != nil {
			t.Fatal(err)
		}
		filter.Free()
	}()
	_, err = conn.NWFilterDefineXML(testNWFilterXML("", "bad"))
	if err == nil {
		t.Fatal("Should have had an error")
	}
}

func TestIntegrationNWFilterGetName(t *testing.T) {
418
	conn, err := NewConnect("lxc:///")
419 420 421 422
	if err != nil {
		t.Error(err)
		return
	}
423 424 425 426 427
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	filter, err := conn.NWFilterDefineXML(testNWFilterXML("", "ipv4"))
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		filter.Undefine()
		filter.Free()
	}()
	if _, err := filter.GetName(); err != nil {
		t.Error(err)
	}
}

func TestIntegrationNWFilterGetUUID(t *testing.T) {
443
	conn, err := NewConnect("lxc:///")
444 445 446 447
	if err != nil {
		t.Error(err)
		return
	}
448 449 450 451 452
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
	filter, err := conn.NWFilterDefineXML(testNWFilterXML("", "ipv4"))
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		filter.Undefine()
		filter.Free()
	}()
	if _, err := filter.GetUUID(); err != nil {
		t.Error(err)
	}
}

func TestIntegrationNWFilterGetUUIDString(t *testing.T) {
468
	conn, err := NewConnect("lxc:///")
469 470 471 472
	if err != nil {
		t.Error(err)
		return
	}
473 474 475 476 477
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	filter, err := conn.NWFilterDefineXML(testNWFilterXML("", "ipv4"))
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		filter.Undefine()
		filter.Free()
	}()
	if _, err := filter.GetUUIDString(); err != nil {
		t.Error(err)
	}
}

func TestIntegrationNWFilterGetXMLDesc(t *testing.T) {
493
	conn, err := NewConnect("lxc:///")
494 495 496 497
	if err != nil {
		t.Error(err)
		return
	}
498 499 500 501 502
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	filter, err := conn.NWFilterDefineXML(testNWFilterXML("", "ipv4"))
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		filter.Undefine()
		filter.Free()
	}()
	if _, err := filter.GetXMLDesc(0); err != nil {
		t.Error(err)
	}
}

func TestIntegrationLookupNWFilterByName(t *testing.T) {
518
	conn, err := NewConnect("lxc:///")
519 520 521 522
	if err != nil {
		t.Error(err)
		return
	}
523 524 525 526 527
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
528 529 530 531 532 533 534 535 536 537
	origName := time.Now().String()
	filter, err := conn.NWFilterDefineXML(testNWFilterXML(origName, "ipv4"))
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		filter.Undefine()
		filter.Free()
	}()
538
	filter2, err := conn.LookupNWFilterByName(origName)
539 540 541 542
	if err != nil {
		t.Error(err)
		return
	}
543
	defer filter2.Free()
544
	var newName string
545
	newName, err = filter2.GetName()
546 547 548 549 550 551 552 553 554 555
	if err != nil {
		t.Error(err)
		return
	}
	if newName != origName {
		t.Fatalf("expected filter name: %s ,got: %s", origName, newName)
	}
}

func TestIntegrationLookupNWFilterByUUIDString(t *testing.T) {
556
	conn, err := NewConnect("lxc:///")
557 558 559 560
	if err != nil {
		t.Error(err)
		return
	}
561 562 563 564 565
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
566 567 568 569 570 571 572 573 574 575
	origName := time.Now().String()
	filter, err := conn.NWFilterDefineXML(testNWFilterXML(origName, "ipv4"))
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		filter.Undefine()
		filter.Free()
	}()
576
	filter2, err := conn.LookupNWFilterByName(origName)
577 578 579 580
	if err != nil {
		t.Error(err)
		return
	}
581
	defer filter2.Free()
582
	var filterUUID string
583
	filterUUID, err = filter2.GetUUIDString()
584 585 586 587
	if err != nil {
		t.Error(err)
		return
	}
588 589
	filter3, err := conn.LookupNWFilterByUUIDString(filterUUID)
	defer filter3.Free()
590 591 592 593
	if err != nil {
		t.Error(err)
		return
	}
594
	name, err := filter3.GetName()
595 596 597 598 599 600 601 602
	if err != nil {
		t.Error(err)
		return
	}
	if name != origName {
		t.Fatalf("fetching by UUID: expected filter name: %s ,got: %s", name, origName)
	}
}
603 604

func TestIntegrationDomainAttachDetachDevice(t *testing.T) {
605
	conn, err := NewConnect("lxc:///")
606 607 608 609
	if err != nil {
		t.Error(err)
		return
	}
610 611 612 613 614
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

	dom, err := defineTestLxcDomain(conn, "")
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		dom.Undefine()
		dom.Free()
	}()
	const nwXml = `<interface type='network'>
		<mac address='52:54:00:37:aa:c7'/>
		<source network='default'/>
		<model type='virtio'/>
		</interface>`
630
	if err := dom.AttachDeviceFlags(nwXml, DOMAIN_DEVICE_MODIFY_CONFIG); err != nil {
631 632 633
		t.Error(err)
		return
	}
634
	if err := dom.DetachDeviceFlags(nwXml, DOMAIN_DEVICE_MODIFY_CONFIG); err != nil {
635 636 637 638
		t.Error(err)
		return
	}
}
dorzheh's avatar
dorzheh committed
639 640

func TestStorageVolResize(t *testing.T) {
641
	conn, err := NewConnect("lxc:///")
dorzheh's avatar
dorzheh committed
642 643 644 645
	if err != nil {
		t.Error(err)
		return
	}
646 647 648 649 650
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
dorzheh's avatar
dorzheh committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

	poolPath, err := ioutil.TempDir("", "default-pool-test-1")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.RemoveAll(poolPath)
	pool, err := conn.StoragePoolDefineXML(`<pool type='dir'>
                                          <name>default-pool-test-1</name>
                                          <target>
                                          <path>`+poolPath+`</path>
                                          </target>
                                          </pool>`, 0)
	defer func() {
		pool.Undefine()
		pool.Free()
	}()
	if err := pool.Create(0); err != nil {
		t.Error(err)
		return
	}
	defer pool.Destroy()
	vol, err := pool.StorageVolCreateXML(testStorageVolXML("", poolPath), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
679
		vol.Delete(STORAGE_VOL_DELETE_NORMAL)
dorzheh's avatar
dorzheh committed
680 681 682
		vol.Free()
	}()
	const newCapacityInBytes = 12582912
683
	if err := vol.Resize(newCapacityInBytes, STORAGE_VOL_RESIZE_ALLOCATE); err != nil {
dorzheh's avatar
dorzheh committed
684 685 686 687 688
		t.Fatal(err)
	}
}

func TestStorageVolWipe(t *testing.T) {
689
	conn, err := NewConnect("lxc:///")
dorzheh's avatar
dorzheh committed
690 691 692 693
	if err != nil {
		t.Error(err)
		return
	}
694 695 696 697 698
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
dorzheh's avatar
dorzheh committed
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

	poolPath, err := ioutil.TempDir("", "default-pool-test-1")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.RemoveAll(poolPath)
	pool, err := conn.StoragePoolDefineXML(`<pool type='dir'>
                                          <name>default-pool-test-1</name>
                                          <target>
                                          <path>`+poolPath+`</path>
                                          </target>
                                          </pool>`, 0)
	defer func() {
		pool.Undefine()
		pool.Free()
	}()
	if err := pool.Create(0); err != nil {
		t.Error(err)
		return
	}
	defer pool.Destroy()
	vol, err := pool.StorageVolCreateXML(testStorageVolXML("", poolPath), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
727
		vol.Delete(STORAGE_VOL_DELETE_NORMAL)
dorzheh's avatar
dorzheh committed
728 729 730 731 732 733 734 735
		vol.Free()
	}()
	if err := vol.Wipe(0); err != nil {
		t.Fatal(err)
	}
}

func TestStorageVolWipePattern(t *testing.T) {
736
	conn, err := NewConnect("lxc:///")
dorzheh's avatar
dorzheh committed
737 738 739 740
	if err != nil {
		t.Error(err)
		return
	}
741 742 743 744 745
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
dorzheh's avatar
dorzheh committed
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

	poolPath, err := ioutil.TempDir("", "default-pool-test-1")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.RemoveAll(poolPath)
	pool, err := conn.StoragePoolDefineXML(`<pool type='dir'>
                                          <name>default-pool-test-1</name>
                                          <target>
                                          <path>`+poolPath+`</path>
                                          </target>
                                          </pool>`, 0)
	defer func() {
		pool.Undefine()
		pool.Free()
	}()
	if err := pool.Create(0); err != nil {
		t.Error(err)
		return
	}
	defer pool.Destroy()
	vol, err := pool.StorageVolCreateXML(testStorageVolXML("", poolPath), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
774
		vol.Delete(STORAGE_VOL_DELETE_NORMAL)
dorzheh's avatar
dorzheh committed
775 776
		vol.Free()
	}()
777
	if err := vol.WipePattern(STORAGE_VOL_WIPE_ALG_ZERO, 0); err != nil {
dorzheh's avatar
dorzheh committed
778 779 780
		t.Fatal(err)
	}
}
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

func testSecretTypeCephFromXML(name string) string {
	var setName string
	if name == "" {
		setName = time.Now().String()
	} else {
		setName = name
	}
	return `<secret ephemeral='no' private='no'>
            <usage type='ceph'>
            <name>` + setName + `</name>
            </usage>
            </secret>`
}

func TestIntegrationSecretDefineUndefine(t *testing.T) {
797
	conn, err := NewConnect("lxc:///")
798 799 800 801
	if err != nil {
		t.Error(err)
		return
	}
802 803 804 805 806
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
807 808 809 810 811 812 813 814 815 816 817 818
	sec, err := conn.SecretDefineXML(testSecretTypeCephFromXML(""), 0)
	if err != nil {
		t.Fatal(err)
	}
	defer sec.Free()

	if err := sec.Undefine(); err != nil {
		t.Fatal(err)
	}
}

func TestIntegrationSecretGetUUID(t *testing.T) {
819
	conn, err := NewConnect("lxc:///")
820 821 822 823
	if err != nil {
		t.Error(err)
		return
	}
824 825 826 827 828
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
	sec, err := conn.SecretDefineXML(testSecretTypeCephFromXML(""), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		sec.Undefine()
		sec.Free()
	}()
	if _, err := sec.GetUUID(); err != nil {
		t.Error(err)
	}
}

func TestIntegrationSecretGetUUIDString(t *testing.T) {
844
	conn, err := NewConnect("lxc:///")
845 846 847 848
	if err != nil {
		t.Error(err)
		return
	}
849 850 851 852 853
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
	sec, err := conn.SecretDefineXML(testSecretTypeCephFromXML(""), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		sec.Undefine()
		sec.Free()
	}()
	if _, err := sec.GetUUIDString(); err != nil {
		t.Error(err)
	}
}

func TestIntegrationSecretGetXMLDesc(t *testing.T) {
869
	conn, err := NewConnect("lxc:///")
870 871 872 873
	if err != nil {
		t.Error(err)
		return
	}
874 875 876 877 878
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
	sec, err := conn.SecretDefineXML(testSecretTypeCephFromXML(""), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		sec.Undefine()
		sec.Free()
	}()
	if _, err := sec.GetXMLDesc(0); err != nil {
		t.Error(err)
	}
}

func TestIntegrationSecretGetUsageType(t *testing.T) {
894
	conn, err := NewConnect("lxc:///")
895 896 897 898
	if err != nil {
		t.Error(err)
		return
	}
899 900 901 902 903
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
904 905 906 907 908 909 910 911 912 913 914 915 916 917
	sec, err := conn.SecretDefineXML(testSecretTypeCephFromXML(""), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		sec.Undefine()
		sec.Free()
	}()
	uType, err := sec.GetUsageType()
	if err != nil {
		t.Error(err)
		return
	}
918
	if uType != SECRET_USAGE_TYPE_CEPH {
919 920 921 922 923
		t.Fatal("unexpected usage type.Expected usage type is Ceph")
	}
}

func TestIntegrationSecretGetUsageID(t *testing.T) {
924
	conn, err := NewConnect("lxc:///")
925 926 927 928
	if err != nil {
		t.Error(err)
		return
	}
929 930 931 932 933
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
	setUsageID := time.Now().String()
	sec, err := conn.SecretDefineXML(testSecretTypeCephFromXML(setUsageID), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		sec.Undefine()
		sec.Free()
	}()
	recUsageID, err := sec.GetUsageID()
	if err != nil {
		t.Error(err)
		return
	}
	if recUsageID != setUsageID {
		t.Fatalf("exepected usage ID: %s, got: %s", setUsageID, recUsageID)
	}
}

func TestIntegrationLookupSecretByUsage(t *testing.T) {
955
	conn, err := NewConnect("lxc:///")
956 957 958 959
	if err != nil {
		t.Error(err)
		return
	}
960 961 962 963 964
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
965 966 967 968 969 970 971 972 973 974
	usageID := time.Now().String()
	sec, err := conn.SecretDefineXML(testSecretTypeCephFromXML(usageID), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		sec.Undefine()
		sec.Free()
	}()
975
	sec2, err := conn.LookupSecretByUsage(SECRET_USAGE_TYPE_CEPH, usageID)
976 977 978
	if err != nil {
		t.Fatal(err)
	}
979
	sec2.Free()
980 981
}

982
func TestIntegrationGetDomainCPUStats(t *testing.T) {
983
	conn, err := NewConnect("lxc:///")
984 985 986
	if err != nil {
		t.Fatal(err)
	}
987 988 989 990 991
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	dom, err := defineTestLxcDomain(conn, "")
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		dom.Undefine()
		dom.Free()
	}()

	if err := dom.Create(); err != nil {
		t.Fatal(err)
	}
	defer dom.Destroy()

1006
	stats, err := dom.GetCPUStats(0, 0, 0)
1007 1008 1009 1010
	if err != nil {
		t.Fatal(err)
	}

1011 1012
	if len(stats) < 1 {
		t.Errorf("Expected stats for at least one CPU")
1013 1014
	}

1015 1016
	if !stats[0].CpuTimeSet {
		t.Errorf("Expected CpuTime to be set")
1017 1018 1019 1020 1021 1022 1023 1024 1025
	}
}

// Not supported on libvirt driver, so no integration test
// func TestGetInterfaceParameters(t *testing.T) {
// 	dom, conn := buildTestDomain()
// 	defer func() {
// 		dom.Undefine()
// 		dom.Free()
1026 1027 1028
// 		if res, _ := conn.CloseConnection(); res != 0 {
// 			t.Errorf("CloseConnection() == %d, expected 0", res)
// 		}
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
// 	}()
// 	iface := "either mac or path to interface"
// 	nparams := int(0)
// 	if _, err := dom.GetInterfaceParameters(iface, nil, &nparams, 0); err != nil {
// 		t.Error(err)
// 		return
// 	}

// 	var params VirTypedParameters
// 	if _, err := dom.GetInterfaceParameters(iface, &params, &nparams, 0); err != nil {
// 		t.Error(err)
// 		return
// 	}
// }
1043

1044
func TestIntegrationListAllInterfaces(t *testing.T) {
1045
	conn, err := NewConnect("lxc:///")
1046 1047 1048 1049
	if err != nil {
		t.Error(err)
		return
	}
1050 1051 1052 1053 1054
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	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
		}
1069
		iface.Free()
1070 1071 1072 1073 1074
	}
	if found == false {
		t.Fatalf("interface %s not found", lookingFor)
	}
}
1075 1076

func TestIntergrationListAllNWFilters(t *testing.T) {
1077
	conn, err := NewConnect("lxc:///")
1078 1079 1080 1081
	if err != nil {
		t.Error(err)
		return
	}
1082 1083 1084 1085 1086
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

	testNWFilterName := time.Now().String()
	filter, err := conn.NWFilterDefineXML(testNWFilterXML(testNWFilterName, "ipv4"))
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		filter.Undefine()
		filter.Free()
	}()

	filters, err := conn.ListAllNWFilters(0)
	if len(filters) == 0 {
1101
		t.Fatal("length of []NWFilter shouldn't be 0")
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	}

	found := false
	for _, f := range filters {
		name, _ := f.GetName()
		if name == testNWFilterName {
			found = true
		}
		f.Free()
	}
	if found == false {
		t.Fatalf("NWFilter %s not found", testNWFilterName)
	}
}
1116

Brian Akins's avatar
Brian Akins committed
1117
func TestIntegrationDomainInterfaceStats(t *testing.T) {
1118
	conn, err := NewConnect("lxc:///")
Brian Akins's avatar
Brian Akins committed
1119 1120 1121
	if err != nil {
		t.Fatal(err)
	}
1122 1123 1124 1125 1126
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
Brian Akins's avatar
Brian Akins committed
1127

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	net, err := conn.LookupNetworkByName("default")
	if err != nil {
		return
	}

	defer net.Free()

	active, err := net.IsActive()
	if err != nil {
		t.Fatal(err)
	}

	if !active {
		return
	}

Brian Akins's avatar
Brian Akins committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	dom, err := defineTestLxcDomain(conn, "")
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		dom.Undefine()
		dom.Free()
	}()
	const nwXml = `<interface type='network'>
		<mac address='52:54:00:37:aa:c7'/>
		<source network='default'/>
		<model type='virtio'/>
1156
                <target dev="lvgotest0"/>
Brian Akins's avatar
Brian Akins committed
1157
		</interface>`
1158
	if err := dom.AttachDeviceFlags(nwXml, DOMAIN_DEVICE_MODIFY_CONFIG); err != nil {
Brian Akins's avatar
Brian Akins committed
1159 1160 1161 1162 1163 1164 1165
		t.Fatal(err)
	}

	if err := dom.Create(); err != nil {
		t.Fatal(err)
	}

1166
	if _, err := dom.InterfaceStats("lvgotest0"); err != nil {
Brian Akins's avatar
Brian Akins committed
1167 1168 1169 1170 1171 1172 1173
		t.Error(err)
	}

	if err := dom.Destroy(); err != nil {
		t.Fatal(err)
	}

1174
	if err := dom.DetachDeviceFlags(nwXml, DOMAIN_DEVICE_MODIFY_CONFIG); err != nil {
Brian Akins's avatar
Brian Akins committed
1175 1176 1177
		t.Fatal(err)
	}
}
1178 1179

func TestStorageVolUploadDownload(t *testing.T) {
1180
	conn, err := NewConnect("lxc:///")
1181 1182 1183 1184
	if err != nil {
		t.Error(err)
		return
	}
1185 1186 1187 1188 1189
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

	poolPath, err := ioutil.TempDir("", "default-pool-test-1")
	if err != nil {
		t.Error(err)
		return
	}
	defer os.RemoveAll(poolPath)
	pool, err := conn.StoragePoolDefineXML(`<pool type='dir'>
                                          <name>default-pool-test-1</name>
                                          <target>
                                          <path>`+poolPath+`</path>
                                          </target>
                                          </pool>`, 0)
	defer func() {
		pool.Undefine()
		pool.Free()
	}()
	if err := pool.Create(0); err != nil {
		t.Error(err)
		return
	}
	defer pool.Destroy()
	vol, err := pool.StorageVolCreateXML(testStorageVolXML("", poolPath), 0)
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
1218
		vol.Delete(STORAGE_VOL_DELETE_NORMAL)
1219 1220 1221 1222 1223 1224 1225
		vol.Free()
	}()

	data := []byte{1, 2, 3, 4, 5, 6}

	// write above data to the vol
	// 1. create a stream
1226
	stream, err := conn.NewStream(0)
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		stream.Free()
	}()

	// 2. set it up to upload from stream
	if err := vol.Upload(stream, 0, uint64(len(data)), 0); err != nil {
		stream.Abort()
		t.Fatal(err)
	}

	// 3. do the actual writing
	if n, err := stream.Write(data); err != nil || n != len(data) {
		t.Fatal(err, n)
	}

	// 4. finish!
	if err := stream.Close(); err != nil {
		t.Fatal(err)
	}

	// read back the data
	// 1. create a stream
1252
	downStream, err := conn.NewStream(0)
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		downStream.Free()
	}()

	// 2. set it up to download from stream
	if err := vol.Download(downStream, 0, uint64(len(data)), 0); err != nil {
		downStream.Abort()
		t.Fatal(err)
	}

	// 3. do the actual reading
	buf := make([]byte, 1024)
	if n, err := downStream.Read(buf); err != nil || n != len(data) {
		t.Fatal(err, n)
	}

	t.Logf("read back: %#v", buf[:len(data)])

	// 4. finish!
	if err := downStream.Close(); err != nil {
		t.Fatal(err)
	}
}
1279 1280

/*func TestDomainMemoryStats(t *testing.T) {
1281
	conn, err := NewConnect("lxc:///")
1282 1283 1284 1285
	if err != nil {
		t.Error(err)
		return
	}
1286 1287 1288 1289 1290
	defer func() {
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313

	dom, err := defineTestLxcDomain(conn, "")
	if err != nil {
		t.Error(err)
		return
	}
	defer func() {
		dom.Undefine()
		dom.Free()
	}()
	if err := dom.Create(); err != nil {
		t.Fatal(err)
	}
	defer dom.Destroy()

	ms, err := dom.MemoryStats(1, 0)
	if err != nil {
		t.Fatal(err)
	}
	if len(ms) != 1 {
		t.Fatal("Should have got one result, got", len(ms))
	}
}*/
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336

func TestDomainListAllInterfaceAddresses(t *testing.T) {
	dom, conn := buildTestQEMUDomain()
	defer func() {
		dom.Free()
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
	if err := dom.Create(); err != nil {
		t.Error(err)
		return
	}
	defer dom.Destroy()
	ifaces, err := dom.ListAllInterfaceAddresses(0)
	if err != nil {
		t.Fatal(err)
	}

	if len(ifaces) != 0 {
		t.Fatal("should have 0 interfaces", len(ifaces))
	}
}
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358

func TestDomainGetAllStats(t *testing.T) {
	dom, conn := buildTestQEMUDomain()
	defer func() {
		dom.Free()
		if res, _ := conn.CloseConnection(); res != 0 {
			t.Errorf("CloseConnection() == %d, expected 0", res)
		}
	}()
	if err := dom.Create(); err != nil {
		t.Error(err)
		return
	}
	defer dom.Destroy()

	_, err := conn.GetAllDomainStats([]*Domain{}, DOMAIN_STATS_STATE|DOMAIN_STATS_CPU_TOTAL|DOMAIN_STATS_INTERFACE|DOMAIN_STATS_BALLOON|DOMAIN_STATS_BLOCK|DOMAIN_STATS_PERF|DOMAIN_STATS_VCPU, 0)

	if err != nil {
		t.Error(err)
		return
	}
}