gopkg.in/hugelgupf/u-root.v2@v2.0.0-20180831055005-3f8fdb0ce09d/xcmds/pox/pox_test.go (about) 1 // Copyright 2017-2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "io/ioutil" 9 "os" 10 "syscall" 11 "testing" 12 13 "github.com/u-root/u-root/pkg/testutil" 14 ) 15 16 var ( 17 uskip = len("2018/08/10 21:20:42 ") 18 ) 19 20 func TestSimple(t *testing.T) { 21 if os.Getuid() != 0 { 22 t.Skip("Must be root for this test") 23 } 24 25 tmpDir, err := ioutil.TempDir("", "pox") 26 if err != nil { 27 t.Fatal(err) 28 } 29 defer os.RemoveAll(tmpDir) 30 31 var tests = []struct { 32 args []string 33 name string 34 status int 35 out string 36 skip int 37 stdin *testutil.FakeStdin 38 }{ 39 // -c, --create create it (default true) 40 // -d, --debug enable debug prints 41 // -o, --output string Output file (default "/tmp/pox.tcz") 42 // -t, --test run a test with the first argument 43 { 44 args: []string{"-o", "/tmp/x/a/g/c/d/e/f/g", "bin/bash"}, 45 name: "Bad executable", 46 status: 1, 47 out: "open bin/bash: no such file or directory\n", 48 skip: uskip, 49 }, 50 { 51 args: []string{"-o", "/tmp/x/a/g/c/d/e/f/g", "/bin/bash"}, 52 name: "Bad output file", 53 status: 1, 54 out: "/tmp/x/a/g/c/d/e/f/g -noappend]: Could not stat destination file: Not a directory\n: exit status 1\n", 55 skip: uskip + len("[mksquashfs /tmp/pox373051153 "), // the tempname varies so skip it. 56 }, 57 { 58 args: []string{"-t", "/bin/bash"}, 59 name: "shellexit", 60 out: "shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory\n", 61 skip: 0, 62 stdin: testutil.NewFakeStdin("exit"), 63 }, 64 } 65 66 // Table-driven testing 67 for _, tt := range tests { 68 t.Run(tt.name, func(t *testing.T) { 69 c := testutil.Command(t, tt.args...) 70 // ignore the error, we deal with it via process status, 71 // and most of these commands are supposed to get an error. 72 out, _ := c.CombinedOutput() 73 status := c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() 74 if tt.status != status { 75 t.Errorf("err got: %v want %v", status, tt.status) 76 } 77 if len(out) < tt.skip { 78 t.Errorf("err got: %v wanted at least %d bytes", string(out), tt.skip) 79 return 80 } 81 m := string(out[tt.skip:]) 82 if m != tt.out { 83 t.Errorf("got:'%q'(%d bytes) want:'%q'(%d bytes)", m, len(m), tt.out, len(tt.out)) 84 } 85 }) 86 } 87 } 88 89 func TestMain(m *testing.M) { 90 testutil.Run(m, main) 91 }