cmdline_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package qemutils
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. var (
  20. cmd1 = `$QEMU_CMD $QEMU_CMD_KVM_ARG -cpu host,+kvm_pv_eoi -chardev socket,id=hmqmondev,port=55902,host=127.0.0.1,nodelay,server,nowait -mon chardev=hmqmondev,id=hmqmon,mode=readline -chardev socket,id=qmqmondev,port=56102,host=127.0.0.1,nodelay,server,nowait -mon chardev=qmqmondev,id=qmqmon,mode=control -rtc base=utc,clock=host,driftfix=none -daemonize -nodefaults -nodefconfig -no-kvm-pit-reinjection -global kvm-pit.lost_tick_policy=discard -machine pc,accel=kvm -k en-us -smp cpus=1,sockets=2,cores=64,maxcpus=128 -name tvm -uuid 3aad3b6b-ddf8-474a-8906-06d83ef318cb -m 1024M,slots=4,maxmem=524288M -boot order=cdn -device virtio-serial -usb -device usb-kbd -device usb-tablet -vga std -vnc :2,password -object iothread,id=iothread0 -device virtio-scsi-pci,id=scsi,num_queues=4,vectors=5 -drive file=$DISK_0,if=none,id=drive_0,cache=writeback,aio=threads,file.locking=off -device scsi-hd,drive=drive_0,bus=scsi.0,id=drive_0 -device ide-cd,drive=ide0-cd0,bus=ide.1,unit=1 -drive id=ide0-cd0,media=cdrom,if=none -netdev type=tap,id=vnet-100,ifname=vnet-100,vhost=on,vhostforce=off,script=/opt/cloud/workspace/servers/3aad3b6b-ddf8-474a-8906-06d83ef318cb/if-up-br0-vnet-100.sh,downscript=/opt/cloud/workspace/servers/3aad3b6b-ddf8-474a-8906-06d83ef318cb/if-down-br0-vnet-100.sh -device virtio-net-pci,id=netdev-vnet-100,netdev=vnet-100,mac=00:22:6a:9a:ef:8d,addr=0xf$(nic_speed 1000) -device qemu-xhci,id=usb -pidfile /opt/cloud/workspace/servers/3aad3b6b-ddf8-474a-8906-06d83ef318cb/pid -chardev socket,path=/opt/cloud/workspace/servers/3aad3b6b-ddf8-474a-8906-06d83ef318cb/qga.sock,server,nowait,id=qga0 -device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -incoming tcp:0:4397 -device pvpanic`
  21. )
  22. func TestNewCmdline(t *testing.T) {
  23. tests := []struct {
  24. name string
  25. content string
  26. want *Cmdline
  27. wantErr bool
  28. }{
  29. {
  30. name: "startvm CMD",
  31. content: cmd1,
  32. want: &Cmdline{
  33. options: []Option{
  34. {"$QEMU_CMD", "$QEMU_CMD_KVM_ARG"},
  35. {"cpu", "host,+kvm_pv_eoi"},
  36. {"chardev", "socket,id=hmqmondev,port=55902,host=127.0.0.1,nodelay,server,nowait"},
  37. {"mon", "chardev=hmqmondev,id=hmqmon,mode=readline"},
  38. {"chardev", "socket,id=qmqmondev,port=56102,host=127.0.0.1,nodelay,server,nowait"},
  39. {"mon", "chardev=qmqmondev,id=qmqmon,mode=control"},
  40. {"rtc", "base=utc,clock=host,driftfix=none"},
  41. {"daemonize", ""},
  42. {"nodefaults", ""},
  43. {"nodefconfig", ""},
  44. {"no-kvm-pit-reinjection", ""},
  45. {"global", "kvm-pit.lost_tick_policy=discard"},
  46. {"machine", "pc,accel=kvm"},
  47. {"k", "en-us"},
  48. {"smp", "cpus=1,sockets=2,cores=64,maxcpus=128"},
  49. {"name", "tvm"},
  50. {"uuid", "3aad3b6b-ddf8-474a-8906-06d83ef318cb"},
  51. {"m", "1024M,slots=4,maxmem=524288M"},
  52. {"boot", "order=cdn"},
  53. {"device", "virtio-serial"},
  54. {"usb", ""},
  55. {"device", "usb-kbd"},
  56. {"device", "usb-tablet"},
  57. {"vga", "std"},
  58. {"vnc", ":2,password"},
  59. {"object", "iothread,id=iothread0"},
  60. {"device", "virtio-scsi-pci,id=scsi,num_queues=4,vectors=5"},
  61. {"drive", "file=$DISK_0,if=none,id=drive_0,cache=writeback,aio=threads,file.locking=off"},
  62. {"device", "scsi-hd,drive=drive_0,bus=scsi.0,id=drive_0"},
  63. {"device", "ide-cd,drive=ide0-cd0,bus=ide.1,unit=1"},
  64. {"drive", "id=ide0-cd0,media=cdrom,if=none"},
  65. {"netdev", "type=tap,id=vnet-100,ifname=vnet-100,vhost=on,vhostforce=off,script=/opt/cloud/workspace/servers/3aad3b6b-ddf8-474a-8906-06d83ef318cb/if-up-br0-vnet-100.sh,downscript=/opt/cloud/workspace/servers/3aad3b6b-ddf8-474a-8906-06d83ef318cb/if-down-br0-vnet-100.sh"},
  66. {"device", "virtio-net-pci,id=netdev-vnet-100,netdev=vnet-100,mac=00:22:6a:9a:ef:8d,addr=0xf$(nic_speed 1000)"},
  67. {"device", "qemu-xhci,id=usb"},
  68. {"pidfile", "/opt/cloud/workspace/servers/3aad3b6b-ddf8-474a-8906-06d83ef318cb/pid"},
  69. {"chardev", "socket,path=/opt/cloud/workspace/servers/3aad3b6b-ddf8-474a-8906-06d83ef318cb/qga.sock,server,nowait,id=qga0"},
  70. {"device", "virtserialport,chardev=qga0,name=org.qemu.guest_agent.0"},
  71. {"chardev", "pty,id=charserial0"},
  72. {"device", "isa-serial,chardev=charserial0,id=serial0"},
  73. {"incoming", "tcp:0:4397"},
  74. {"device", "pvpanic"},
  75. },
  76. },
  77. wantErr: false,
  78. },
  79. }
  80. assert := assert.New(t)
  81. for _, tt := range tests {
  82. t.Run(tt.name, func(t *testing.T) {
  83. got, err := NewCmdline(tt.content)
  84. if (err != nil) != tt.wantErr {
  85. t.Errorf("NewCmdline() error = %v, wantErr %v", err, tt.wantErr)
  86. return
  87. }
  88. if !assert.Equal(tt.want, got) {
  89. t.Errorf("NewCmdline() = %v, want %v", got, tt.want)
  90. }
  91. assert.Equal(got.ToString(), tt.content)
  92. })
  93. }
  94. }