blob: 195a871db98c728b4e5ccdf9654107296d4e09d3 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main_test
//go:generate v23 test generate .
import (
"bytes"
"io/ioutil"
"path/filepath"
"v.io/x/ref"
"v.io/x/ref/test/v23tests"
)
func V23TestTunneld(t *v23tests.T) {
v23tests.RunRootMT(t, "--v23.tcp.address=127.0.0.1:0")
tunneldBin := t.BuildV23Pkg("v.io/x/ref/examples/tunnel/tunneld")
vsh := t.BuildV23Pkg("v.io/x/ref/examples/tunnel/vsh")
mounttableBin := t.BuildV23Pkg("v.io/x/ref/cmd/mounttable")
// Start tunneld with a known endpoint.
tunnelEndpoint := tunneldBin.Start("--v23.tcp.address=127.0.0.1:0", "--name=tunnel/test").ExpectVar("NAME")
// Run remote command with the endpoint.
if want, got := "HELLO ENDPOINT\n", vsh.Start(tunnelEndpoint, "echo", "HELLO", "ENDPOINT").Output(); want != got {
t.Fatalf("unexpected output, got %s, want %s", got, want)
}
if want, got := "HELLO NAME\n", vsh.Start("tunnel/test", "echo", "HELLO", "NAME").Output(); want != got {
t.Fatalf("unexpected output, got %s, want %s", got, want)
}
// Send input to remote command.
want := "HELLO SERVER"
if got := vsh.WithStdin(bytes.NewBufferString(want)).Start(tunnelEndpoint, "cat").Output(); want != got {
t.Fatalf("unexpected output, got %s, want %s", got, want)
}
// And again with a file redirection this time.
outDir := t.NewTempDir("")
outPath := filepath.Join(outDir, "hello.txt")
// TODO(sjr): instead of using Output() here, we'd really rather do
// WaitOrDie(os.Stdout, os.Stderr). There is currently a race caused by
// WithStdin that makes this flaky.
vsh.WithStdin(bytes.NewBufferString(want)).Start(tunnelEndpoint, "cat > "+outPath).Output()
if got, err := ioutil.ReadFile(outPath); err != nil || string(got) != want {
if err != nil {
t.Fatalf("ReadFile(%v) failed: %v", outPath, err)
} else {
t.Fatalf("unexpected output, got %s, want %s", got, want)
}
}
// Verify that all published names are there.
root, _ := t.GetVar(ref.EnvNamespacePrefix)
inv := mounttableBin.Start("glob", root, "tunnel/test")
// Expect one entry: the tunnel name.
matches := inv.ExpectSetEventuallyRE("tunnel/test" + " (.*) \\(Deadline .*\\)")
// The full endpoint should be the one we saw originally.
if got, want := matches[0][1], tunnelEndpoint; "/"+got != want {
t.Fatalf("expected tunnel endpoint %s to be %s, but it was not", got, want)
}
}