Пример #1
0
func TestTaskZombie(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("sleep", []string{"100"}, image)

	task.SetStdout(ioutil.Discard)

	if err := task.Start(); err != nil {
		t.Fatal("Could not start task: ", err)
	}
	assert.True(t, task.Alive())
	assert.False(t, task.Zombie())

	if err := task.Kill(); err != nil {
		t.Fatal("Could not kill task: ", err)
	}

	// Give the system a beat to kill the process
	time.Sleep(200 * time.Millisecond)

	assert.True(t, task.Alive())
	assert.True(t, task.Zombie())

	if err := task.Wait(); err != nil {
		t.Fatal("Could not wait task: ", err)
	}
	assert.False(t, task.Alive())
	assert.False(t, task.Zombie())
}
Пример #2
0
func TestKillTask(t *testing.T) {
	// https://code.google.com/p/go/issues/detail?id=7938

	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("sleep", []string{"100"}, image)

	task.SetStdout(ioutil.Discard)

	if err := task.Start(); err != nil {
		t.Fatal("Could not start task: ", err)
	}
	if err := task.Kill(); err != nil {
		t.Fatal("Could not kill task: ", err)
	}
	if err := task.Wait(); err != nil {
		t.Fatal("Could not wait task: ", err)
	}

	signaled, err := task.Signaled()
	if err != nil {
		t.Fatal("Could not get signaled: ", err)
	}

	assert.True(t, signaled)
}
Пример #3
0
func TestSignalWithSigterm(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("sleep", []string{"10"}, image)

	task.SetStdout(ioutil.Discard)

	if err := task.Start(); err != nil {
		t.Fatal("Could not start task: ", err)
	}

	if err := task.Command.Process.Signal(syscall.SIGTERM); err != nil {
		t.Fatal("Could not signal process: ", err)
	}

	if err := task.Wait(); err != nil {
		t.Fatal("Could not wait task: ", err)
	}

	signaled, err := task.Signaled()
	if err != nil {
		t.Fatal("Could not get signaled of task: ", err)
	}
	assert.True(t, signaled)

	signal, err := task.Signal()
	if err != nil {
		t.Fatal("Could not get signal of task: ", err)
	}
	assert.Equal(t, syscall.SIGTERM, signal)
}
Пример #4
0
func TestTaskWaitStatusNoWait(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("uname", []string{}, image)

	waitstatus, err := task.WaitStatus()
	assert.Nil(t, waitstatus)
	assert.NotNil(t, err)
}
Пример #5
0
func TestErrorProcessStateNoWait(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("uname", []string{}, image)

	task.SetStdout(ioutil.Discard)

	processstate, err := task.ProcessState()
	assert.Nil(t, processstate)
	assert.NotNil(t, err)
}
Пример #6
0
func start() {
	name := getName()
	imageDescriptor := getImageDescriptor()
	command, args := getCommand()

	image, err := chrootly.NewImage(imageDescriptor)
	if err != nil {
		fmt.Println(err)
		return
	}
	task := chrootly.NewTask(command, args, image)

	task.SetStdout(ioutil.Discard)
	task.SetStderr(ioutil.Discard)

	tasks[name] = task

	task.Start()
}
Пример #7
0
func TestTaskWaitStatusSignaled(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("uname", []string{}, image)

	task.SetStdout(ioutil.Discard)

	if err := task.Start(); err != nil {
		t.Fatal("Could not start task: ", err)
	}
	if err := task.Wait(); err != nil {
		t.Fatal("Could not wait task: ", err)
	}

	waitStatus, _ := task.WaitStatus()
	assert.False(t, waitStatus.Signaled())
}
Пример #8
0
func TestTaskProcessStateNoSuccess(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("ls", []string{"/notarealdir"}, image)

	task.SetStderr(ioutil.Discard)

	if err := task.Start(); err != nil {
		t.Fatal("Could not start task: ", err)
	}
	if err := task.Wait(); err == nil {
		t.Fatal("Task waited successfully: ", err)
	}

	processState, _ := task.ProcessState()
	assert.False(t, processState.Success())
}
Пример #9
0
func TestTaskProcessState(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("uname", []string{}, image)

	task.SetStdout(ioutil.Discard)

	if err := task.Start(); err != nil {
		t.Fatal("Could not start task: ", err)
	}
	if err := task.Wait(); err != nil {
		t.Fatal("Could not wait task: ", err)
	}

	processState, _ := task.ProcessState()
	assert.True(t, processState.Success())
}
Пример #10
0
func TestTaskAliveWithWait(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("sleep", []string{"3"}, image)

	task.SetStdout(ioutil.Discard)

	if err := task.Start(); err != nil {
		t.Fatal("Could not start task: ", err)
	}
	assert.True(t, task.Alive())

	if err := task.Wait(); err != nil {
		t.Fatal("Could not wait task: ", err)
	}
	assert.False(t, task.Alive())
}
Пример #11
0
func run() {
	imageDescriptor := getImageDescriptor()
	command, args := getCommand()

	image, err := chrootly.NewImage(imageDescriptor)
	if err != nil {
		fmt.Println(err)
		return
	}
	task := chrootly.NewTask(command, args, image)

	err = task.Start()
	if err != nil {
		fmt.Println(err)
		return
	}

	err = task.Wait()
	if err != nil {
		fmt.Println(err)
		return
	}
}
Пример #12
0
func TestTask(t *testing.T) {
	image, err := chrootly.NewImageFromLocal("test_image.tar")
	if err != nil {
		t.Fatal("Could not create image: ", err)
	}
	task := chrootly.NewTask("ls", []string{"-a", ".dockerenv"}, image)

	output := bytes.NewBuffer(nil)
	task.SetStdout(output)

	if err := task.Start(); err != nil {
		t.Fatal("Could not start task: ", err)
	}
	if err := task.Wait(); err != nil {
		t.Fatal("Could not wait task: ", err)
	}

	stdoutLine, err := output.ReadString('\n')
	if err != nil {
		t.Fatal("Could not read output: ", err)
	}

	assert.Equal(t, ".dockerenv\n", stdoutLine)
}