2016-05-20 03:05:37 +03:00
|
|
|
// Copyright 2016 The go-libvirt Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-05-20 05:56:27 +03:00
|
|
|
// Package libvirttest provides a mock libvirt server for RPC testing.
|
|
|
|
package libvirttest
|
2016-05-20 03:05:37 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"net"
|
|
|
|
"sync/atomic"
|
2016-05-20 05:56:27 +03:00
|
|
|
|
|
|
|
"github.com/digitalocean/go-libvirt/internal/constants"
|
2016-05-20 03:05:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var testDomainResponse = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x38, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x17, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
|
|
|
|
// domain name ("test")
|
|
|
|
0x00, 0x00, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74,
|
|
|
|
|
|
|
|
// uuid (dc229f87d4de47198cfd2e21c6105b01)
|
|
|
|
0xdc, 0x22, 0x9f, 0x87, 0xd4, 0xde, 0x47, 0x19,
|
|
|
|
0x8c, 0xfd, 0x2e, 0x21, 0xc6, 0x10, 0x5b, 0x01,
|
|
|
|
|
|
|
|
// domain id (14)
|
|
|
|
0x00, 0x00, 0x00, 0x0e,
|
|
|
|
}
|
|
|
|
|
|
|
|
var testRegisterEvent = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x20, // length
|
|
|
|
0x20, 0x00, 0x80, 0x87, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x04, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
0x00, 0x00, 0x00, 0x01, // callback id
|
|
|
|
}
|
|
|
|
|
|
|
|
var testDeregisterEvent = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x1c, // length
|
|
|
|
0x20, 0x00, 0x80, 0x87, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x05, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
}
|
|
|
|
|
|
|
|
var testAuthReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x1c, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x42, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
}
|
|
|
|
|
|
|
|
var testConnectReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x1c, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x01, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
}
|
|
|
|
|
|
|
|
var testDisconnectReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x1c, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x02, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
}
|
|
|
|
|
2016-08-19 21:31:59 +03:00
|
|
|
var testMigrateReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x20, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x01, 0x31, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
|
|
|
|
// cookie out: 0
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
}
|
|
|
|
|
2016-05-20 03:05:37 +03:00
|
|
|
var testRunReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x74, // length
|
|
|
|
0x20, 0x00, 0x80, 0x87, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x01, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
|
|
|
|
// {"return":{"qemu":{"micro":1,"minor":5,"major":2},"package":""},"id":"libvirt-53"}
|
|
|
|
0x00, 0x00, 0x00, 0x52, 0x7b, 0x22, 0x72, 0x65,
|
|
|
|
0x74, 0x75, 0x72, 0x6e, 0x22, 0x3a, 0x7b, 0x22,
|
|
|
|
0x71, 0x65, 0x6d, 0x75, 0x22, 0x3a, 0x7b, 0x22,
|
|
|
|
0x6d, 0x69, 0x63, 0x72, 0x6f, 0x22, 0x3a, 0x31,
|
|
|
|
0x2c, 0x22, 0x6d, 0x69, 0x6e, 0x6f, 0x72, 0x22,
|
|
|
|
0x3a, 0x35, 0x2c, 0x22, 0x6d, 0x61, 0x6a, 0x6f,
|
|
|
|
0x72, 0x22, 0x3a, 0x32, 0x7d, 0x2c, 0x22, 0x70,
|
|
|
|
0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x22, 0x3a,
|
|
|
|
0x22, 0x22, 0x7d, 0x2c, 0x22, 0x69, 0x64, 0x22,
|
|
|
|
0x3a, 0x22, 0x6c, 0x69, 0x62, 0x76, 0x69, 0x72,
|
|
|
|
0x74, 0x2d, 0x35, 0x33, 0x22, 0x7d,
|
|
|
|
|
|
|
|
// All trailing NULL characters should be removed
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
}
|
|
|
|
|
2016-07-19 19:27:31 +03:00
|
|
|
var testRunReplyFail = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x8c, // length
|
|
|
|
0x20, 0x00, 0x80, 0x87, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x01, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x0a, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
|
|
|
|
// {"id":"libvirt-68","error":{"class":"CommandNotFound","desc":"The command drive-foo has not been found"}}`
|
|
|
|
0x00, 0x00, 0x00, 0x69, 0x7b, 0x22, 0x69, 0x64,
|
|
|
|
0x22, 0x3a, 0x22, 0x6c, 0x69, 0x62, 0x76, 0x69,
|
|
|
|
0x72, 0x74, 0x2d, 0x36, 0x38, 0x22, 0x2c, 0x22,
|
|
|
|
0x65, 0x72, 0x72, 0x6f, 0x72, 0x22, 0x3a, 0x7b,
|
|
|
|
0x22, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x22, 0x3a,
|
|
|
|
0x22, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64,
|
|
|
|
0x4e, 0x6f, 0x74, 0x46, 0x6f, 0x75, 0x6e, 0x64,
|
|
|
|
0x22, 0x2c, 0x22, 0x64, 0x65, 0x73, 0x63, 0x22,
|
|
|
|
0x3a, 0x22, 0x54, 0x68, 0x65, 0x20, 0x63, 0x6f,
|
|
|
|
0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x20, 0x64, 0x72,
|
|
|
|
0x69, 0x76, 0x65, 0x2d, 0x66, 0x6f, 0x6f, 0x20,
|
|
|
|
0x68, 0x61, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20,
|
|
|
|
0x62, 0x65, 0x65, 0x6e, 0x20, 0x66, 0x6f, 0x75,
|
|
|
|
0x6e, 0x64, 0x22, 0x7d, 0x7d, 0x00, 0x00, 0x00,
|
|
|
|
}
|
|
|
|
|
2016-08-02 01:43:50 +03:00
|
|
|
var testSetSpeedReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x1c, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0xcf, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
}
|
|
|
|
|
2016-05-20 03:05:37 +03:00
|
|
|
var testDomainsReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x6c, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x01, 0x11, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
|
|
|
|
// struct of domains
|
|
|
|
0x00, 0x00, 0x00, 0x02,
|
|
|
|
|
|
|
|
// first domain
|
|
|
|
// name - aaaaaaa-1
|
|
|
|
0x00, 0x00, 0x00, 0x09, 0x61, 0x61, 0x61, 0x61,
|
|
|
|
0x61, 0x61, 0x61, 0x2d, 0x31, 0x00, 0x00, 0x00,
|
|
|
|
// uuid - dc:32:9f:87:d4:de:47:19:8c:fd:2e:21:c6:10:5b:01
|
|
|
|
0xdc, 0x32, 0x9f, 0x87, 0xd4, 0xde, 0x47, 0x19,
|
|
|
|
0x8c, 0xfd, 0x2e, 0x21, 0xc6, 0x10, 0x5b, 0x01,
|
|
|
|
// id
|
|
|
|
0x00, 0x00, 0x00, 0x01,
|
|
|
|
|
|
|
|
// second domain
|
|
|
|
// name - aaaaaaa-2
|
|
|
|
0x00, 0x00, 0x00, 0x09, 0x61, 0x61, 0x61, 0x61,
|
|
|
|
0x61, 0x61, 0x61, 0x2d, 0x32, 0x00, 0x00, 0x00,
|
|
|
|
// uuid - dc:22:9f:87:d4:de:47:19:8c:fd:2e:21:c6:10:5b:01
|
|
|
|
0xdc, 0x22, 0x9f, 0x87, 0xd4, 0xde, 0x47, 0x19, 0x8c,
|
|
|
|
0xfd, 0x2e, 0x21, 0xc6, 0x10, 0x5b, 0x01, 0x00, 0x00,
|
|
|
|
// id
|
|
|
|
0x00, 0x02, 0x00,
|
|
|
|
|
|
|
|
// count of domains returned
|
|
|
|
0x00, 0x00, 0x02,
|
|
|
|
}
|
|
|
|
|
2016-09-29 00:29:46 +03:00
|
|
|
var testUndefineReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x1c, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0xe7, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
}
|
|
|
|
|
2016-05-20 03:05:37 +03:00
|
|
|
var testVersionReply = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x24, // length
|
|
|
|
0x20, 0x00, 0x80, 0x86, // program
|
|
|
|
0x00, 0x00, 0x00, 0x01, // version
|
|
|
|
0x00, 0x00, 0x00, 0x9d, // procedure
|
|
|
|
0x00, 0x00, 0x00, 0x01, // type
|
|
|
|
0x00, 0x00, 0x00, 0x00, // serial
|
|
|
|
0x00, 0x00, 0x00, 0x00, // status
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x4d, 0xfc, // version (1003004)
|
|
|
|
}
|
|
|
|
|
2016-05-20 05:56:27 +03:00
|
|
|
// MockLibvirt provides a mock libvirt server for testing.
|
|
|
|
type MockLibvirt struct {
|
2016-05-20 03:05:37 +03:00
|
|
|
net.Conn
|
2016-05-20 05:56:27 +03:00
|
|
|
Test net.Conn
|
2016-07-19 19:27:31 +03:00
|
|
|
Fail bool
|
2016-05-20 03:05:37 +03:00
|
|
|
serial uint32
|
|
|
|
}
|
|
|
|
|
2016-05-20 05:56:27 +03:00
|
|
|
// New creates a new mock Libvirt server.
|
|
|
|
func New() *MockLibvirt {
|
2016-05-20 03:05:37 +03:00
|
|
|
serv, conn := net.Pipe()
|
|
|
|
|
2016-05-20 05:56:27 +03:00
|
|
|
m := &MockLibvirt{
|
2016-05-20 03:05:37 +03:00
|
|
|
Conn: conn,
|
2016-05-20 05:56:27 +03:00
|
|
|
Test: serv,
|
2016-05-20 03:05:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
go m.handle(serv)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2016-05-20 05:56:27 +03:00
|
|
|
func (m *MockLibvirt) handle(conn net.Conn) {
|
2016-05-20 03:05:37 +03:00
|
|
|
for {
|
2016-05-20 05:56:27 +03:00
|
|
|
// packetLengthSize + headerSize
|
|
|
|
buf := make([]byte, 28)
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Read(buf)
|
|
|
|
|
|
|
|
// extract program
|
|
|
|
prog := binary.BigEndian.Uint32(buf[4:8])
|
|
|
|
|
|
|
|
// extract procedure
|
|
|
|
proc := binary.BigEndian.Uint32(buf[12:16])
|
|
|
|
|
|
|
|
switch prog {
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.ProgramRemote:
|
2016-05-20 03:05:37 +03:00
|
|
|
m.handleRemote(proc, conn)
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.ProgramQEMU:
|
2016-05-20 03:05:37 +03:00
|
|
|
m.handleQEMU(proc, conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 05:56:27 +03:00
|
|
|
func (m *MockLibvirt) handleRemote(procedure uint32, conn net.Conn) {
|
2016-05-20 03:05:37 +03:00
|
|
|
switch procedure {
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.ProcAuthList:
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Write(m.reply(testAuthReply))
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.ProcConnectOpen:
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Write(m.reply(testConnectReply))
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.ProcConnectClose:
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Write(m.reply(testDisconnectReply))
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.ProcConnectGetLibVersion:
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Write(m.reply(testVersionReply))
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.ProcDomainLookupByName:
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Write(m.reply(testDomainResponse))
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.ProcConnectListAllDomains:
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Write(m.reply(testDomainsReply))
|
2016-08-02 01:43:50 +03:00
|
|
|
case constants.ProcDomainMigrateSetMaxSpeed:
|
|
|
|
conn.Write(m.reply(testSetSpeedReply))
|
2016-08-19 21:31:59 +03:00
|
|
|
case constants.ProcMigratePerformParams:
|
|
|
|
conn.Write(m.reply(testMigrateReply))
|
2016-09-29 00:29:46 +03:00
|
|
|
case constants.ProcDomainUndefineFlags:
|
|
|
|
conn.Write(m.reply(testUndefineReply))
|
2016-05-20 03:05:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 05:56:27 +03:00
|
|
|
func (m *MockLibvirt) handleQEMU(procedure uint32, conn net.Conn) {
|
2016-05-20 03:05:37 +03:00
|
|
|
switch procedure {
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.QEMUConnectDomainMonitorEventRegister:
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Write(m.reply(testRegisterEvent))
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.QEMUConnectDomainMonitorEventDeregister:
|
2016-05-20 03:05:37 +03:00
|
|
|
conn.Write(m.reply(testDeregisterEvent))
|
2016-05-20 05:56:27 +03:00
|
|
|
case constants.QEMUDomainMonitor:
|
2016-07-19 19:27:31 +03:00
|
|
|
if m.Fail {
|
|
|
|
conn.Write(m.reply(testRunReplyFail))
|
|
|
|
} else {
|
|
|
|
conn.Write(m.reply(testRunReply))
|
|
|
|
}
|
2016-05-20 03:05:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reply automatically injects the correct serial
|
|
|
|
// number into the provided response buffer.
|
2016-05-20 05:56:27 +03:00
|
|
|
func (m *MockLibvirt) reply(buf []byte) []byte {
|
2016-05-20 03:05:37 +03:00
|
|
|
atomic.AddUint32(&m.serial, 1)
|
|
|
|
binary.BigEndian.PutUint32(buf[20:24], m.serial)
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|