Skip to content

Commit 242184b

Browse files
authored
Merge pull request #104 from xibz/pid_test
Adding tests for PID method
2 parents be936c6 + 7ad6c8f commit 242184b

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
@@ -836,3 +836,69 @@ func copyFile(src, dst string, uid, gid int) error {
836836

837837
return nil
838838
}
839+
840+
func TestPID(t *testing.T) {
841+
m := &Machine{}
842+
if _, err := m.PID(); err == nil {
843+
t.Errorf("expected an error, but received none")
844+
}
845+
846+
var nCpus int64 = 2
847+
cpuTemplate := models.CPUTemplate(models.CPUTemplateT2)
848+
var memSz int64 = 256
849+
socketPath := filepath.Join(testDataPath, "TestPID.sock")
850+
defer os.Remove(socketPath)
851+
852+
vmlinuxPath := getVmlinuxPath(t)
853+
854+
cfg := Config{
855+
SocketPath: socketPath,
856+
KernelImagePath: vmlinuxPath,
857+
MachineCfg: models.MachineConfiguration{
858+
VcpuCount: Int64(nCpus),
859+
CPUTemplate: cpuTemplate,
860+
MemSizeMib: Int64(memSz),
861+
HtEnabled: Bool(false),
862+
},
863+
Drives: []models.Drive{
864+
models.Drive{
865+
DriveID: String("1"),
866+
IsRootDevice: Bool(true),
867+
IsReadOnly: Bool(false),
868+
PathOnHost: String(filepath.Join(testDataPath, "root-drive.img")),
869+
},
870+
},
871+
Debug: true,
872+
DisableValidation: true,
873+
}
874+
875+
m, err := NewMachine(context.Background(), cfg)
876+
if err != nil {
877+
t.Errorf("expected no error during create machine, but received %v", err)
878+
}
879+
880+
if err := m.Start(context.Background()); err != nil {
881+
t.Errorf("expected no error during Start, but received %v", err)
882+
}
883+
defer m.StopVMM()
884+
885+
pid, err := m.PID()
886+
if err != nil {
887+
t.Errorf("unexpected error during PID call, %v", err)
888+
}
889+
890+
if pid == 0 {
891+
t.Errorf("failed to retrieve correct PID")
892+
}
893+
894+
if err := m.StopVMM(); err != nil {
895+
t.Errorf("expected clean kill, but received %v", err)
896+
}
897+
898+
m.Wait(context.Background())
899+
900+
if _, err := m.PID(); err == nil {
901+
t.Errorf("expected an error, but received none")
902+
}
903+
904+
}

0 commit comments

Comments
 (0)