2018-12-08 02:32:44 +00:00
|
|
|
// Copyright 2018 Mikhail Klementev. All rights reserved.
|
|
|
|
// Use of this source code is governed by a AGPLv3 license
|
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-12-08 02:53:29 +00:00
|
|
|
func TestDockerRun(t *testing.T) {
|
2018-12-08 02:32:44 +00:00
|
|
|
tmp, err := ioutil.TempDir("/tmp/", "out-of-tree_test_")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
2018-12-08 02:53:29 +00:00
|
|
|
timeout := time.Second
|
|
|
|
|
|
|
|
_, err = dockerRun(timeout, "ubuntu", tmp, "sleep 5s")
|
2018-12-08 02:32:44 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("docker is not killed by timeout")
|
|
|
|
}
|
|
|
|
if time.Since(start) > 3*time.Second {
|
|
|
|
t.Fatal(fmt.Sprintf("timeout failed (%v instead of %v)",
|
|
|
|
time.Since(start), time.Second))
|
|
|
|
}
|
|
|
|
|
2018-12-08 02:53:29 +00:00
|
|
|
output, err := dockerRun(time.Minute, "ubuntu", tmp, "echo hello")
|
2018-12-08 02:32:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-08 02:53:29 +00:00
|
|
|
if !strings.Contains(output, "hello") {
|
|
|
|
t.Fatal("wrong output (" + output + ")")
|
2018-12-08 02:32:44 +00:00
|
|
|
}
|
|
|
|
}
|