Move mount configutation to RunOpts

Separate mount configuration from links and move it to
RunOpts, like the other options.

PiperOrigin-RevId: 317010158
This commit is contained in:
Fabricio Voznika
2020-06-17 18:43:26 -07:00
committed by gVisor bot
parent 57286eb642
commit 97f6b20e89
5 changed files with 77 additions and 35 deletions
+39 -14
View File
@@ -210,7 +210,6 @@ type Docker struct {
Runtime string
Name string
copyErr error
mounts []string
cleanups []func()
}
@@ -229,13 +228,8 @@ func MakeDocker(logger testutil.Logger) *Docker {
}
}
// Mount mounts the given source and makes it available in the container.
func (d *Docker) Mount(target, source string, mode MountMode) {
d.mounts = append(d.mounts, fmt.Sprintf("-v=%s:%s:%v", source, target, mode))
}
// CopyFiles copies in and mounts the given files. They are always ReadOnly.
func (d *Docker) CopyFiles(target string, sources ...string) {
func (d *Docker) CopyFiles(opts *RunOpts, targetDir string, sources ...string) {
dir, err := ioutil.TempDir("", d.Name)
if err != nil {
d.copyErr = fmt.Errorf("ioutil.TempDir failed: %v", err)
@@ -259,12 +253,33 @@ func (d *Docker) CopyFiles(target string, sources ...string) {
}
d.logger.Logf("copy: %s -> %s", src, dst)
}
d.Mount(target, dir, ReadOnly)
opts.Mounts = append(opts.Mounts, Mount{
Source: dir,
Target: targetDir,
Mode: ReadOnly,
})
}
// Link links the given target.
func (d *Docker) Link(target string, source *Docker) {
d.mounts = append(d.mounts, fmt.Sprintf("--link=%s:%s", source.Name, target))
// Mount describes a mount point inside the container.
type Mount struct {
// Source is the path outside the container.
Source string
// Target is the path inside the container.
Target string
// Mode tells whether the mount inside the container should be readonly.
Mode MountMode
}
// Link informs dockers that a given container needs to be made accessible from
// the container being configured.
type Link struct {
// Source is the container to connect to.
Source *Docker
// Target is the alias for the container.
Target string
}
// RunOpts are options for running a container.
@@ -310,6 +325,12 @@ type RunOpts struct {
// return value from the Run function.
Foreground bool
// Mounts is the list of directories/files to be mounted inside the container.
Mounts []Mount
// Links is the list of containers to be connected to the container.
Links []Link
// Extra are extra arguments that may be passed.
Extra []string
}
@@ -368,7 +389,13 @@ func (d *Docker) argsFor(r *RunOpts, command string, p []string) (rv []string) {
if isExec {
rv = append(rv, d.Name)
} else {
rv = append(rv, d.mounts...)
for _, m := range r.Mounts {
rv = append(rv, fmt.Sprintf("-v=%s:%s:%v", m.Source, m.Target, m.Mode))
}
for _, l := range r.Links {
rv = append(rv, fmt.Sprintf("--link=%s:%s", l.Source.Name, l.Target))
}
if len(d.Runtime) > 0 {
rv = append(rv, fmt.Sprintf("--runtime=%s", d.Runtime))
}
@@ -501,8 +528,6 @@ func (d *Docker) CleanUp() {
if err := d.Remove(); err != nil {
d.logger.Logf("error removing container %q: %v", d.Name, err)
}
// Forget all mounts.
d.mounts = nil
// Execute all cleanups.
for _, c := range d.cleanups {
c()
+22 -13
View File
@@ -111,11 +111,12 @@ func TestHttpd(t *testing.T) {
defer d.CleanUp()
// Start the container.
d.CopyFiles("/usr/local/apache2/htdocs", "test/image/latin10k.txt")
if err := d.Spawn(dockerutil.RunOpts{
opts := dockerutil.RunOpts{
Image: "basic/httpd",
Ports: []int{80},
}); err != nil {
}
d.CopyFiles(&opts, "/usr/local/apache2/htdocs", "test/image/latin10k.txt")
if err := d.Spawn(opts); err != nil {
t.Fatalf("docker run failed: %v", err)
}
@@ -138,11 +139,12 @@ func TestNginx(t *testing.T) {
defer d.CleanUp()
// Start the container.
d.CopyFiles("/usr/share/nginx/html", "test/image/latin10k.txt")
if err := d.Spawn(dockerutil.RunOpts{
opts := dockerutil.RunOpts{
Image: "basic/nginx",
Ports: []int{80},
}); err != nil {
}
d.CopyFiles(&opts, "/usr/share/nginx/html", "test/image/latin10k.txt")
if err := d.Spawn(opts); err != nil {
t.Fatalf("docker run failed: %v", err)
}
@@ -183,11 +185,17 @@ func TestMysql(t *testing.T) {
// Tell mysql client to connect to the server and execute the file in
// verbose mode to verify the output.
client.CopyFiles("/sql", "test/image/mysql.sql")
client.Link("mysql", server)
if _, err := client.Run(dockerutil.RunOpts{
opts := dockerutil.RunOpts{
Image: "basic/mysql",
}, "mysql", "-hmysql", "-uroot", "-pfoobar123", "-v", "-e", "source /sql/mysql.sql"); err != nil {
Links: []dockerutil.Link{
{
Source: server,
Target: "mysql",
},
},
}
client.CopyFiles(&opts, "/sql", "test/image/mysql.sql")
if _, err := client.Run(opts, "mysql", "-hmysql", "-uroot", "-pfoobar123", "-v", "-e", "source /sql/mysql.sql"); err != nil {
t.Fatalf("docker run failed: %v", err)
}
@@ -236,11 +244,12 @@ func TestRuby(t *testing.T) {
defer d.CleanUp()
// Execute the ruby workload.
d.CopyFiles("/src", "test/image/ruby.rb", "test/image/ruby.sh")
if err := d.Spawn(dockerutil.RunOpts{
opts := dockerutil.RunOpts{
Image: "basic/ruby",
Ports: []int{8080},
}, "/src/ruby.sh"); err != nil {
}
d.CopyFiles(&opts, "/src", "test/image/ruby.rb", "test/image/ruby.sh")
if err := d.Spawn(opts, "/src/ruby.sh"); err != nil {
t.Fatalf("docker run failed: %v", err)
}
+4 -3
View File
@@ -41,11 +41,12 @@ func singleTest(t *testing.T, test TestCase) {
defer d.CleanUp()
// Create and start the container.
d.CopyFiles("/runner", "test/iptables/runner/runner")
if err := d.Spawn(dockerutil.RunOpts{
opts := dockerutil.RunOpts{
Image: "iptables",
CapAdd: []string{"NET_ADMIN"},
}, "/runner/runner", "-name", test.Name()); err != nil {
}
d.CopyFiles(&opts, "/runner", "test/iptables/runner/runner")
if err := d.Spawn(opts, "/runner/runner", "-name", test.Name()); err != nil {
t.Fatalf("docker run failed: %v", err)
}
@@ -142,7 +142,7 @@ func TestOne(t *testing.T) {
}
const containerPosixServerBinary = "/packetimpact/posix_server"
dut.CopyFiles("/packetimpact", "/test/packetimpact/dut/posix_server")
dut.CopyFiles(&runOpts, "/packetimpact", "/test/packetimpact/dut/posix_server")
if err := dut.Create(runOpts, containerPosixServerBinary, "--ip=0.0.0.0", "--port="+ctrlPort); err != nil {
t.Fatalf("unable to create container %s: %s", dut.Name, err)
@@ -193,7 +193,13 @@ func TestOne(t *testing.T) {
tbb := path.Base(*testbenchBinary)
containerTestbenchBinary := "/packetimpact/" + tbb
testbench.CopyFiles("/packetimpact", "/test/packetimpact/tests/"+tbb)
runOpts = dockerutil.RunOpts{
Image: "packetimpact",
CapAdd: []string{"NET_ADMIN"},
Extra: []string{"--sysctl", "net.ipv6.conf.all.disable_ipv6=0", "--rm", "-v", tmpDir + ":" + testOutputDir},
Foreground: true,
}
testbench.CopyFiles(&runOpts, "/packetimpact", "/test/packetimpact/tests/"+tbb)
// Run tcpdump in the test bench unbuffered, without DNS resolution, just on
// the interface with the test packets.
+4 -3
View File
@@ -79,10 +79,11 @@ func runTests() int {
// getTests executes all tests as table tests.
func getTests(d *dockerutil.Docker, excludes map[string]struct{}) ([]testing.InternalTest, error) {
// Start the container.
d.CopyFiles("/proctor", "test/runtimes/proctor/proctor")
if err := d.Spawn(dockerutil.RunOpts{
opts := dockerutil.RunOpts{
Image: fmt.Sprintf("runtimes/%s", *image),
}, "/proctor/proctor", "--pause"); err != nil {
}
d.CopyFiles(&opts, "/proctor", "test/runtimes/proctor/proctor")
if err := d.Spawn(opts, "/proctor/proctor", "--pause"); err != nil {
return nil, fmt.Errorf("docker run failed: %v", err)
}