Add some negative test cases for qmp.go

This commit adds a couple of negative test cases for qmp.go, one
which checks that failed commands return errors and the other
checks that QMPStart exits gracefully when passed an invalid
socket path.

Signed-off-by: Mark Ryan <mark.d.ryan@intel.com>
This commit is contained in:
Mark Ryan 2018-08-20 15:31:09 +01:00
parent 17cacc7238
commit ed34f61664

View File

@ -251,6 +251,22 @@ func TestQMPStartStopLoop(t *testing.T) {
<-disconnectedCh
}
// Checks that a call to QMPStart with an invalid path exits gracefully.
//
// We call QMPStart with an invalid path.
//
// An error should be returned and the disconnected channel should be closed.
func TestQMPStartBadPath(t *testing.T) {
cfg := QMPConfig{Logger: qmpTestLogger{}}
disconnectedCh := make(chan struct{})
q, _, err := QMPStart(context.Background(), "", cfg, disconnectedCh)
if err == nil {
t.Errorf("Expected error")
q.Shutdown()
}
<-disconnectedCh
}
// Checks that the qmp_capabilities command is correctly sent.
//
// We start a QMPLoop, send the qmp_capabilities command and stop the
@ -274,6 +290,28 @@ func TestQMPCapabilities(t *testing.T) {
<-disconnectedCh
}
// Checks that an error returned by a QMP command is correctly handled.
//
// We start a QMPLoop, send the qmp_capabilities command and stop the
// loop.
//
// The qmp_capabilities command fails and yet we should exit gracefully.
func TestQMPBadCapabilities(t *testing.T) {
connectedCh := make(chan *QMPVersion)
disconnectedCh := make(chan struct{})
buf := newQMPTestCommandBuffer(t)
buf.AddCommand("qmp_capabilities", nil, "error", nil)
cfg := QMPConfig{Logger: qmpTestLogger{}}
q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh)
checkVersion(t, connectedCh)
err := q.ExecuteQMPCapabilities(context.Background())
if err == nil {
t.Fatalf("Expected error")
}
q.Shutdown()
<-disconnectedCh
}
// Checks that the stop command is correctly sent.
//
// We start a QMPLoop, send the stop command and stop the