Skip to content

Commit 7ad6c8f

Browse files
committed
Adding tests for PID method
Signed-off-by: xibz <[email protected]>
1 parent a585107 commit 7ad6c8f

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

machine_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,3 +815,69 @@ func copyFile(src, dst string, uid, gid int) error {
815815

816816
return nil
817817
}
818+
819+
func TestPID(t *testing.T) {
820+
m := &Machine{}
821+
if _, err := m.PID(); err == nil {
822+
t.Errorf("expected an error, but received none")
823+
}
824+
825+
var nCpus int64 = 2
826+
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
827+
var memSz int64 = 256
828+
socketPath := filepath.Join(testDataPath, "TestPID.sock")
829+
defer os.Remove(socketPath)
830+
831+
vmlinuxPath := getVmlinuxPath(t)
832+
833+
cfg := Config{
834+
SocketPath: socketPath,
835+
KernelImagePath: vmlinuxPath,
836+
MachineCfg: models.MachineConfiguration{
837+
VcpuCount: Int64(nCpus),
838+
CPUTemplate: cpuTemplate,
839+
MemSizeMib: Int64(memSz),
840+
HtEnabled: Bool(false),
841+
},
842+
Drives: []models.Drive{
843+
models.Drive{
844+
DriveID: String("1"),
845+
IsRootDevice: Bool(true),
846+
IsReadOnly: Bool(false),
847+
PathOnHost: String(filepath.Join(testDataPath, "root-drive.img")),
848+
},
849+
},
850+
Debug: true,
851+
DisableValidation: true,
852+
}
853+
854+
m, err := NewMachine(context.Background(), cfg)
855+
if err != nil {
856+
t.Errorf("expected no error during create machine, but received %v", err)
857+
}
858+
859+
if err := m.Start(context.Background()); err != nil {
860+
t.Errorf("expected no error during Start, but received %v", err)
861+
}
862+
defer m.StopVMM()
863+
864+
pid, err := m.PID()
865+
if err != nil {
866+
t.Errorf("unexpected error during PID call, %v", err)
867+
}
868+
869+
if pid == 0 {
870+
t.Errorf("failed to retrieve correct PID")
871+
}
872+
873+
if err := m.StopVMM(); err != nil {
874+
t.Errorf("expected clean kill, but received %v", err)
875+
}
876+
877+
m.Wait(context.Background())
878+
879+
if _, err := m.PID(); err == nil {
880+
t.Errorf("expected an error, but received none")
881+
}
882+
883+
}

0 commit comments

Comments
 (0)