本文整理汇总了Golang中github.com/coreos/rkt/Godeps/_workspace/src/github.com/ThomasRooney/gexpect.Spawn函数的典型用法代码示例。如果您正苦于以下问题:Golang Spawn函数的具体用法?Golang Spawn怎么用?Golang Spawn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Spawn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestImplicitFetch
// TestImplicitFetch tests that 'rkt run/prepare' will always bypass the on-disk store
// if the tag is "latest".
func TestImplicitFetch(t *testing.T) {
foundMsg := "found image in local store"
ctx := newRktRunCtx()
defer ctx.cleanup()
// 1. Fetch the image.
// TODO(yifan): Add other ACI with different schemes.
importImageAndFetchHash(t, ctx, "docker://busybox:ubuntu-12.04")
importImageAndFetchHash(t, ctx, "docker://busybox:latest")
// 2. Try run/prepare with specified tag, should get the $foundMsg.
cmds := []string{
fmt.Sprintf("%s --insecure-skip-verify run --mds-register=false docker://busybox:ubuntu-12.04", ctx.cmd()),
fmt.Sprintf("%s --insecure-skip-verify prepare docker://busybox:ubuntu-12.04", ctx.cmd()),
}
for _, cmd := range cmds {
t.Logf("Running test %v", cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Cannot exec rkt: %v", err)
}
if err := child.Expect(foundMsg); err != nil {
t.Fatalf("Expected %q but not found: %v", foundMsg, err)
}
if err := child.Wait(); err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
// 3. Try run/prepare with/without tag ':latest', should not get $foundMsg.
cmds = []string{
fmt.Sprintf("%s --insecure-skip-verify run --mds-register=false docker://busybox", ctx.cmd()),
fmt.Sprintf("%s --insecure-skip-verify run --mds-register=false docker://busybox", ctx.cmd()),
fmt.Sprintf("%s --insecure-skip-verify prepare docker://busybox:latest", ctx.cmd()),
fmt.Sprintf("%s --insecure-skip-verify prepare docker://busybox:latest", ctx.cmd()),
}
for _, cmd := range cmds {
t.Logf("Running test %v", cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Cannot exec rkt: %v", err)
}
if err := child.Expect(foundMsg); err == nil {
t.Fatalf("%q should not be found", foundMsg)
}
if err := child.Wait(); err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
}
示例2: TestRunPrepareLocal
// TestRunPrepareLocal tests that 'rkt run/prepare' will only use the on-disk store if flag is --local
func TestRunPrepareLocal(t *testing.T) {
notAvailableMsg := "not available in local store"
foundMsg := "using image in local store"
ctx := newRktRunCtx()
defer ctx.cleanup()
cmds := []string{
fmt.Sprintf("%s --insecure-skip-verify run --local --mds-register=false docker://busybox", ctx.cmd()),
fmt.Sprintf("%s --insecure-skip-verify run --local --mds-register=false docker://busybox:latest", ctx.cmd()),
fmt.Sprintf("%s --insecure-skip-verify prepare --local docker://busybox", ctx.cmd()),
fmt.Sprintf("%s --insecure-skip-verify prepare --local docker://busybox:latest", ctx.cmd()),
}
// 1. Try run/prepare with the image not available in the store, should get $notAvailableMsg.
for _, cmd := range cmds {
t.Logf("Running test %v", cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Cannot exec rkt: %v", err)
}
if err := expectWithOutput(child, notAvailableMsg); err != nil {
t.Fatalf("%q should be found", notAvailableMsg)
}
child.Wait()
}
// 2. Fetch the image
importImageAndFetchHash(t, ctx, "docker://busybox")
importImageAndFetchHash(t, ctx, "docker://busybox:latest")
// 3. Try run/prepare with the image available in the store, should get $foundMsg.
for _, cmd := range cmds {
t.Logf("Running test %v", cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Cannot exec rkt: %v", err)
}
if err := expectWithOutput(child, foundMsg); err != nil {
t.Fatalf("%q should be found", foundMsg)
}
if err := child.Wait(); err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
}
示例3: TestSuccess
func TestSuccess(t *testing.T) {
patchTestACI("rkt-inspect-exit0.aci", "--exec=/inspect --print-msg=Hello --exit-code=0")
defer os.Remove("rkt-inspect-exit0.aci")
ctx := newRktRunCtx()
defer ctx.cleanup()
child, err := gexpect.Spawn(fmt.Sprintf("%s --debug --insecure-skip-verify run ./rkt-inspect-exit0.aci", ctx.cmd()))
if err != nil {
t.Fatalf("Cannot exec rkt")
}
err = child.Expect("Hello")
if err != nil {
t.Fatalf("Missing hello")
}
forbidden := "main process exited, code=exited, status="
_, receiver := child.AsyncInteractChannels()
for {
msg, open := <-receiver
if !open {
break
}
if strings.Contains(msg, forbidden) {
t.Fatalf("Forbidden text received")
}
}
err = child.Wait()
if err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
示例4: TestExitCode
func TestExitCode(t *testing.T) {
for i := 0; i < 3; i++ {
t.Logf("%d\n", i)
imageFile := patchTestACI("rkt-inspect-exit.aci", fmt.Sprintf("--exec=/inspect --print-msg=Hello --exit-code=%d", i))
defer os.Remove(imageFile)
ctx := newRktRunCtx()
defer ctx.cleanup()
cmd := fmt.Sprintf(`/bin/sh -c "`+
`%s --debug --insecure-skip-verify run --mds-register=false %s ;`+
`UUID=$(%s list --full|grep exited|awk '{print $1}') ;`+
`echo -n 'status=' ;`+
`%s status $UUID|grep '^rkt-inspect.*=[0-9]*$'|cut -d= -f2"`,
ctx.cmd(), imageFile,
ctx.cmd(),
ctx.cmd())
t.Logf("%s\n", cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Cannot exec rkt")
}
err = expectWithOutput(child, fmt.Sprintf("status=%d", i))
if err != nil {
t.Fatalf("Failed to get the status")
}
err = child.Wait()
if err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
}
示例5: TestAppIsolatorCPU
func TestAppIsolatorCPU(t *testing.T) {
ok := cgroup.IsIsolatorSupported("cpu")
if !ok {
t.Skip("CPU isolator not supported.")
}
ctx := newRktRunCtx()
defer ctx.cleanup()
t.Logf("Running test: %v", cpuTest.testName)
aciFileName := patchTestACI("rkt-inspect-isolators.aci", cpuTest.aciBuildArgs...)
defer os.Remove(aciFileName)
rktCmd := fmt.Sprintf("%s --insecure-skip-verify run --mds-register=false %s", ctx.cmd(), aciFileName)
t.Logf("Command: %v", rktCmd)
child, err := gexpect.Spawn(rktCmd)
if err != nil {
t.Fatalf("Cannot exec rkt: %v", err)
}
expectedLine := "CPU Quota: " + strconv.Itoa(CPUQuota)
if err := expectWithOutput(child, expectedLine); err != nil {
t.Fatalf("Didn't receive expected output %q: %v", expectedLine, err)
}
err = child.Wait()
if err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
示例6: TestCgroups
func TestCgroups(t *testing.T) {
ctx := newRktRunCtx()
defer ctx.cleanup()
t.Logf("Running test: %v", cgroupsTest.testName)
aciFileName := patchTestACI("rkt-inspect-isolators.aci", cgroupsTest.aciBuildArgs...)
defer os.Remove(aciFileName)
rktCmd := fmt.Sprintf("%s --insecure-skip-verify run --mds-register=false %s", ctx.cmd(), aciFileName)
t.Logf("Command: %v", rktCmd)
child, err := gexpect.Spawn(rktCmd)
if err != nil {
t.Fatalf("Cannot exec rkt: %v", err)
}
expectedLine := "check-cgroups: SUCCESS"
if err := expectWithOutput(child, expectedLine); err != nil {
t.Fatalf("Didn't receive expected output %q: %v", expectedLine, err)
}
err = child.Wait()
if err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
示例7: TestAppIsolatorMemory
func TestAppIsolatorMemory(t *testing.T) {
ok := cgroup.IsIsolatorSupported("memory")
if !ok {
t.Skip("Memory isolator not supported.")
}
ctx := newRktRunCtx()
defer ctx.cleanup()
t.Logf("Running test: %v", memoryTest.testName)
aciFileName := "rkt-inspect-isolators.aci"
patchTestACI(aciFileName, memoryTest.aciBuildArgs...)
defer os.Remove(aciFileName)
rktCmd := fmt.Sprintf("%s %s", ctx.cmd(), memoryTest.rktArgs)
t.Logf("Command: %v", rktCmd)
child, err := gexpect.Spawn(rktCmd)
if err != nil {
t.Fatalf("Cannot exec rkt: %v", err)
}
expectedLine := "Memory Limit: " + strconv.Itoa(maxMemoryUsage)
if err := expectWithOutput(child, expectedLine); err != nil {
t.Fatalf("Didn't receive expected output %q: %v", expectedLine, err)
}
err = child.Wait()
if err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
示例8: TestPrivateNetDefaultRestrictedConnectivity
/*
* Default-restricted private-net
* ---
* Container launches http server on all its interfaces
* Host must be able to connects to container's http server via container's
* eth0's IPv4
* TODO: verify that the container isn't NATed
*/
func TestPrivateNetDefaultRestrictedConnectivity(t *testing.T) {
httpServeAddr := "0.0.0.0:54321"
iface := "eth0"
testImageArgs := []string{fmt.Sprintf("--exec=/inspect --print-ipv4=%v --serve-http=%v", iface, httpServeAddr)}
testImage := patchTestACI("rkt-inspect-networking.aci", testImageArgs...)
defer os.Remove(testImage)
ctx := newRktRunCtx()
defer ctx.cleanup()
defer ctx.reset()
cmd := fmt.Sprintf("%s --debug --insecure-skip-verify run --private-net=default-restricted --mds-register=false %s", ctx.cmd(), testImage)
t.Logf("Command: %v", cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Cannot exec rkt: %v", err)
}
expectedRegex := `IPv4: (.*)\r`
result, _ := child.ExpectRegexFind(expectedRegex)
if len(result) == 0 {
t.Fatalf("Expected %q but not found", expectedRegex)
}
httpGetAddr := fmt.Sprintf("http://%v:54321", result[1])
var wg sync.WaitGroup
// Child opens the server
wg.Add(1)
go func() {
defer wg.Done()
err = child.Wait()
if err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}()
// Host connects to the child
wg.Add(1)
go func() {
defer wg.Done()
expectedRegex := `serving on`
result, _ := child.ExpectRegexFind(expectedRegex)
if len(result) == 0 {
t.Fatalf("Expected %q but not found", expectedRegex)
}
body, err := test_netutils.HttpGet(httpGetAddr)
if err != nil {
log.Fatalf("%v\n", err)
}
log.Printf("HTTP-Get received: %s", body)
if err != nil {
log.Fatalf("%v\n", err)
}
}()
wg.Wait()
}
示例9: preparePidFileRace
func preparePidFileRace(t *testing.T, ctx *rktRunCtx, sleepImage string) (*gexpect.ExpectSubprocess, *gexpect.ExpectSubprocess, string, string) {
// Start the pod
runCmd := fmt.Sprintf("%s --debug --insecure-skip-verify run --mds-register=false --interactive %s", ctx.cmd(), sleepImage)
t.Logf("%s", runCmd)
runChild, err := gexpect.Spawn(runCmd)
if err != nil {
t.Fatalf("Cannot exec rkt")
}
err = expectWithOutput(runChild, "Enter text:")
if err != nil {
t.Fatalf("Waited for the prompt but not found: %v", err)
}
// Check the ppid file is really created
cmd := fmt.Sprintf(`%s list --full|grep running`, ctx.cmd())
output, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
if err != nil {
t.Fatalf("Couldn't list the pods: %v", err)
}
UUID := strings.Split(string(output), "\t")[0]
pidFileName := filepath.Join(ctx.dataDir(), "pods/run", UUID, "ppid")
if _, err := os.Stat(pidFileName); err != nil {
t.Fatalf("Pid file missing: %v", err)
}
// Temporarily move the ppid file away
pidFileNameBackup := pidFileName + ".backup"
if err := os.Rename(pidFileName, pidFileNameBackup); err != nil {
t.Fatalf("Cannot move ppid file away: %v", err)
}
// Start the "enter" command without the pidfile
enterCmd := fmt.Sprintf("%s --debug enter %s /inspect --print-msg=RktEnterWorksFine", ctx.cmd(), UUID)
t.Logf("%s", enterCmd)
enterChild, err := gexpect.Spawn(enterCmd)
if err != nil {
t.Fatalf("Cannot exec rkt enter")
}
// Enter should be able to wait until the ppid file appears
time.Sleep(1 * time.Second)
return runChild, enterChild, pidFileName, pidFileNameBackup
}
示例10: TestVolumes
func TestVolumes(t *testing.T) {
readFileImage := patchTestACI("rkt-inspect-read-file.aci", "--exec=/inspect --read-file")
defer os.Remove(readFileImage)
writeFileImage := patchTestACI("rkt-inspect-write-file.aci", "--exec=/inspect --write-file --read-file")
defer os.Remove(writeFileImage)
volRwReadFileImage := patchTestACI("rkt-inspect-vol-rw-read-file.aci", "--exec=/inspect --read-file", "--mounts=dir1,path=/dir1,readOnly=false")
defer os.Remove(volRwReadFileImage)
volRwWriteFileImage := patchTestACI("rkt-inspect-vol-rw-write-file.aci", "--exec=/inspect --write-file --read-file", "--mounts=dir1,path=/dir1,readOnly=false")
defer os.Remove(volRwWriteFileImage)
volRoReadFileImage := patchTestACI("rkt-inspect-vol-ro-read-file.aci", "--exec=/inspect --read-file", "--mounts=dir1,path=/dir1,readOnly=true")
defer os.Remove(volRoReadFileImage)
volRoWriteFileImage := patchTestACI("rkt-inspect-vol-ro-write-file.aci", "--exec=/inspect --write-file --read-file", "--mounts=dir1,path=/dir1,readOnly=true")
defer os.Remove(volRoWriteFileImage)
ctx := newRktRunCtx()
defer ctx.cleanup()
tmpdir, err := ioutil.TempDir("", "rkt-tests.")
if err != nil {
t.Fatalf("Cannot create temporary directory: %v", err)
}
defer os.RemoveAll(tmpdir)
tmpfile := filepath.Join(tmpdir, "file")
err = ioutil.WriteFile(tmpfile, []byte("host"), 0600)
if err != nil {
t.Fatalf("Cannot create temporary file: %v", err)
}
for i, tt := range volTests {
cmd := strings.Replace(tt.rktCmd, "^TMPDIR^", tmpdir, -1)
cmd = strings.Replace(cmd, "^RKT_BIN^", ctx.cmd(), -1)
cmd = strings.Replace(cmd, "^READ_FILE^", readFileImage, -1)
cmd = strings.Replace(cmd, "^WRITE_FILE^", writeFileImage, -1)
cmd = strings.Replace(cmd, "^VOL_RO_READ_FILE^", volRoReadFileImage, -1)
cmd = strings.Replace(cmd, "^VOL_RO_WRITE_FILE^", volRoWriteFileImage, -1)
cmd = strings.Replace(cmd, "^VOL_RW_READ_FILE^", volRwReadFileImage, -1)
cmd = strings.Replace(cmd, "^VOL_RW_WRITE_FILE^", volRwWriteFileImage, -1)
t.Logf("Running test #%v: %v", i, cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Cannot exec rkt #%v: %v", i, err)
}
err = expectWithOutput(child, tt.expect)
if err != nil {
fmt.Printf("Command: %s\n", cmd)
t.Fatalf("Expected %q but not found #%v: %v", tt.expect, i, err)
}
err = child.Wait()
if err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
}
示例11: main
func main() {
log.Printf("Testing Ping interact... \n")
child, err := gexpect.Spawn("ping -c8 127.0.0.1")
if err != nil {
panic(err)
}
child.Interact()
log.Printf("Success\n")
}
示例12: expectedRunRkt
// expectedRunRkt tries to fetch and run a prog.aci from host within
// given directory on host. Note that directory can be anything - it's
// useful for ensuring that image name is unique and for descriptive
// purposes.
func expectedRunRkt(ctx *rktRunCtx, t *testing.T, host, dir, line string) {
cmd := fmt.Sprintf(`%s --debug --insecure-skip-verify run --mds-register=false %s/%s/prog.aci`, ctx.cmd(), host, dir)
t.Logf("Running rkt: %s", cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Failed to run rkt: %v", err)
}
defer child.Wait()
if err := expectWithOutput(child, line); err != nil {
t.Fatalf("Didn't receive expected output %q: %v", line, err)
}
}
示例13: TestCaps
func TestCaps(t *testing.T) {
ctx := newRktRunCtx()
defer ctx.cleanup()
for i, tt := range capsTests {
var stage1FileName = "rkt-inspect-print-caps-stage1.aci"
var stage2FileName = "rkt-inspect-print-caps-stage2.aci"
stage1Args := []string{"--exec=/inspect --print-caps-pid=1 --print-user"}
stage2Args := []string{"--exec=/inspect --print-caps-pid=0 --print-user"}
if tt.capIsolator != "" {
stage1Args = append(stage1Args, "--capability="+tt.capIsolator)
stage2Args = append(stage2Args, "--capability="+tt.capIsolator)
}
patchTestACI(stage1FileName, stage1Args...)
patchTestACI(stage2FileName, stage2Args...)
defer os.Remove(stage1FileName)
defer os.Remove(stage2FileName)
for _, stage := range []int{1, 2} {
t.Logf("Running test #%v: %v [stage %v]", i, tt.testName, stage)
cmd := fmt.Sprintf("%s --debug --insecure-skip-verify run --set-env=CAPABILITY=%d ./rkt-inspect-print-caps-stage%d.aci", ctx.cmd(), int(tt.capa), stage)
t.Logf("Command: %v", cmd)
child, err := gexpect.Spawn(cmd)
if err != nil {
t.Fatalf("Cannot exec rkt #%v: %v", i, err)
}
expectedLine := tt.capa.String()
if (stage == 1 && tt.capInStage1Expected) || (stage == 2 && tt.capInStage2Expected) {
expectedLine += "=enabled"
} else {
expectedLine += "=disabled"
}
err = expectWithOutput(child, expectedLine)
if err != nil {
t.Fatalf("Expected %q but not found: %v", expectedLine, err)
}
err = expectWithOutput(child, "User: uid=0 euid=0 gid=0 egid=0")
if err != nil {
t.Fatalf("Expected user 0 but not found: %v", err)
}
err = child.Wait()
if err != nil {
t.Fatalf("rkt didn't terminate correctly: %v", err)
}
}
ctx.reset()
}
}
示例14: main
func main() {
waitChan := make(chan string)
fmt.Printf("Starting screen.. \n")
child, err := gexpect.Spawn("screen")
if err != nil {
panic(err)
}
sender, reciever := child.AsyncInteractChannels()
go func() {
waitString := ""
count := 0
for {
select {
case waitString = <-waitChan:
count++
case msg, open := <-reciever:
if !open {
return
}
fmt.Printf("Recieved: %s\n", msg)
if strings.Contains(msg, waitString) {
if count >= 1 {
waitChan <- msg
count -= 1
}
}
}
}
}()
wait := func(str string) {
waitChan <- str
<-waitChan
}
fmt.Printf("Waiting until started.. \n")
wait(" ")
fmt.Printf("Sending Enter.. \n")
sender <- "\n"
wait("$")
fmt.Printf("Sending echo.. \n")
sender <- "echo Hello World\n"
wait("Hello World")
fmt.Printf("Received echo. \n")
}
示例15: main
func main() {
fmt.Printf("Starting python.. \n")
child, err := gexpect.Spawn("python")
if err != nil {
panic(err)
}
fmt.Printf("Expecting >>>.. \n")
child.Expect(">>>")
fmt.Printf("print 'Hello World'..\n")
child.SendLine("print 'Hello World'")
child.Expect(">>>")
fmt.Printf("Interacting.. \n")
child.Interact()
fmt.Printf("Done \n")
child.Close()
}