Cosmos Nicolaou | 0f0e877 | 2014-09-10 21:29:25 -0700 | [diff] [blame^] | 1 | package core_test |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "reflect" |
| 6 | "sort" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "veyron2/rt" |
| 11 | |
| 12 | "veyron/lib/expect" |
| 13 | "veyron/lib/modules" |
| 14 | "veyron/lib/modules/core" |
| 15 | _ "veyron/lib/testutil" |
| 16 | ) |
| 17 | |
| 18 | func TestCommands(t *testing.T) { |
| 19 | shell := core.NewShell() |
| 20 | defer shell.Cleanup(os.Stderr) |
| 21 | for _, c := range []string{core.RootMTCommand, core.MTCommand} { |
| 22 | if len(shell.Help(c)) == 0 { |
| 23 | t.Fatalf("missing command %q", c) |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func init() { |
| 29 | rt.Init() |
| 30 | } |
| 31 | |
| 32 | func TestRoot(t *testing.T) { |
| 33 | shell := core.NewShell() |
| 34 | defer shell.Cleanup(os.Stderr) |
| 35 | |
| 36 | root, err := shell.Start(core.RootMTCommand) |
| 37 | if err != nil { |
| 38 | t.Fatalf("unexpected error: %s", err) |
| 39 | } |
| 40 | s := expect.NewSession(t, root.Stdout(), time.Second) |
| 41 | s.ExpectVar("MT_NAME") |
| 42 | s.ExpectVar("PID") |
| 43 | root.Stdin().Close() |
| 44 | s.Expect("done") |
| 45 | } |
| 46 | |
| 47 | func getMatchingMountpoint(r [][]string) string { |
| 48 | if len(r) != 1 { |
| 49 | return "" |
| 50 | } |
| 51 | shortest := "" |
| 52 | for _, p := range r[0][1:] { |
| 53 | if len(p) > 0 { |
| 54 | if len(shortest) == 0 { |
| 55 | shortest = p |
| 56 | } |
| 57 | if len(shortest) > 0 && len(p) < len(shortest) { |
| 58 | shortest = p |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return shortest |
| 63 | } |
| 64 | |
| 65 | func TestMountTableAndGlob(t *testing.T) { |
| 66 | shell := core.NewShell() |
| 67 | if testing.Verbose() { |
| 68 | defer shell.Cleanup(os.Stderr) |
| 69 | } else { |
| 70 | defer shell.Cleanup(nil) |
| 71 | } |
| 72 | |
| 73 | // Start root mount table |
| 74 | root, err := shell.Start(core.RootMTCommand) |
| 75 | if err != nil { |
| 76 | t.Fatalf("unexpected error: %s", err) |
| 77 | } |
| 78 | rootSession := expect.NewSession(t, root.Stdout(), time.Second) |
| 79 | rootName := rootSession.ExpectVar("MT_NAME") |
| 80 | shell.SetVar("NAMESPACE_ROOT", rootName) |
| 81 | |
| 82 | if t.Failed() { |
| 83 | return |
| 84 | } |
| 85 | mountPoints := []string{"a", "b", "c"} |
| 86 | |
| 87 | // Start 3 mount tables |
| 88 | for _, mp := range mountPoints { |
| 89 | _, err := shell.Start(core.MTCommand, mp) |
| 90 | if err != nil { |
| 91 | t.Fatalf("unexpected error: %s", err) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ls, err := shell.Start(core.LSCommand, rootName+"/...") |
| 96 | if err != nil { |
| 97 | t.Fatalf("unexpected error: %s", err) |
| 98 | } |
| 99 | lsSession := expect.NewSession(t, ls.Stdout(), time.Second) |
| 100 | lsSession.Expect(rootName) |
| 101 | |
| 102 | // Look for names that correspond to the mountpoints above (i.e, a, b or c) |
| 103 | pattern := "" |
| 104 | for _, n := range mountPoints { |
| 105 | pattern = pattern + "(" + rootName + "/(" + n + ")$)|" |
| 106 | } |
| 107 | pattern = pattern[:len(pattern)-1] |
| 108 | |
| 109 | found := []string{} |
| 110 | found = append(found, getMatchingMountpoint(lsSession.ExpectRE(pattern, 1))) |
| 111 | found = append(found, getMatchingMountpoint(lsSession.ExpectRE(pattern, 1))) |
| 112 | found = append(found, getMatchingMountpoint(lsSession.ExpectRE(pattern, 1))) |
| 113 | sort.Strings(found) |
| 114 | sort.Strings(mountPoints) |
| 115 | if got, want := found, mountPoints; !reflect.DeepEqual(got, want) { |
| 116 | t.Errorf("got %v, want %v", got, want) |
| 117 | } |
| 118 | |
| 119 | // Run the ls command in a subprocess, with NAMESPACE_ROOT as set above. |
| 120 | lse, err := shell.Start(core.LSExternalCommand, "...") |
| 121 | if err != nil { |
| 122 | t.Fatalf("unexpected error: %s", err) |
| 123 | } |
| 124 | lseSession := expect.NewSession(t, lse.Stdout(), time.Second) |
| 125 | |
| 126 | pattern = "" |
| 127 | for _, n := range mountPoints { |
| 128 | // Since the LSExternalCommand runs in a subprocess with NAMESPACE_ROOT |
| 129 | // set to the name of the root mount table it sees the relative name |
| 130 | // format of the mounted mount tables. |
| 131 | pattern = pattern + "(^" + n + "$)|" |
| 132 | } |
| 133 | pattern = pattern[:len(pattern)-1] |
| 134 | found = []string{} |
| 135 | found = append(found, getMatchingMountpoint(lseSession.ExpectRE(pattern, 1))) |
| 136 | found = append(found, getMatchingMountpoint(lseSession.ExpectRE(pattern, 1))) |
| 137 | found = append(found, getMatchingMountpoint(lseSession.ExpectRE(pattern, 1))) |
| 138 | sort.Strings(found) |
| 139 | sort.Strings(mountPoints) |
| 140 | if got, want := found, mountPoints; !reflect.DeepEqual(got, want) { |
| 141 | t.Errorf("got %v, want %v", got, want) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestHelperProcess(t *testing.T) { |
| 146 | if !modules.IsTestHelperProcess() { |
| 147 | return |
| 148 | } |
| 149 | if err := modules.Dispatch(); err != nil { |
| 150 | t.Fatalf("failed: %v", err) |
| 151 | } |
| 152 | } |