gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/harness/util.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package harness
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/docker/docker/api/types/mount"
    24  	"gvisor.dev/gvisor/pkg/cleanup"
    25  	"gvisor.dev/gvisor/pkg/test/dockerutil"
    26  	"gvisor.dev/gvisor/pkg/test/testutil"
    27  )
    28  
    29  //TODO(gvisor.dev/issue/3535): move to own package or move methods to harness struct.
    30  
    31  // WaitUntilContainerServing grabs a container from `machine` and waits for a server on
    32  // the given container and port.
    33  func WaitUntilContainerServing(ctx context.Context, machine Machine, container *dockerutil.Container, port int) error {
    34  	var logger testutil.DefaultLogger = "util"
    35  	netcat := machine.GetNativeContainer(ctx, logger)
    36  	defer netcat.CleanUp(ctx)
    37  
    38  	cmd := fmt.Sprintf("while ! wget -q --spider http://%s:%d; do true; done", "server", port)
    39  	_, err := netcat.Run(ctx, dockerutil.RunOpts{
    40  		Image: "benchmarks/util",
    41  		Links: []string{container.MakeLink("server")},
    42  	}, "sh", "-c", cmd)
    43  	return err
    44  }
    45  
    46  // DropCaches drops caches on the provided machine. Requires root.
    47  func DropCaches(machine Machine) error {
    48  	if out, err := machine.RunCommand("/bin/sh", "-c", "sync && sysctl vm.drop_caches=3"); err != nil {
    49  		return fmt.Errorf("failed to drop caches: %v logs: %s", err, out)
    50  	}
    51  	return nil
    52  }
    53  
    54  // DebugLog prints debug messages if the debug flag is set.
    55  func DebugLog(b *testing.B, msg string, args ...any) {
    56  	b.Helper()
    57  	if *debug {
    58  		b.Logf(msg, args...)
    59  	}
    60  }
    61  
    62  // FileSystemType represents a type container mount.
    63  type FileSystemType string
    64  
    65  const (
    66  	// BindFS indicates a bind mount should be created.
    67  	BindFS FileSystemType = "bindfs"
    68  	// TmpFS indicates a tmpfs mount should be created.
    69  	TmpFS FileSystemType = "tmpfs"
    70  	// RootFS indicates no mount should be created and the root mount should be used.
    71  	RootFS FileSystemType = "rootfs"
    72  	// FuseFS indicates a passthrough fuse server should be created.
    73  	FuseFS FileSystemType = "fusefs"
    74  )
    75  
    76  // MakeMount makes a mount and cleanup based on the requested type. Bind
    77  // and volume mounts are backed by a temp directory made with mktemp.
    78  // tmpfs mounts require no such backing and are just made.
    79  // rootfs mounts do not make a mount, but instead return a target directory at
    80  // root. It is up to the caller to call Clean on the passed *cleanup.Cleanup.
    81  func MakeMount(machine Machine, fsType FileSystemType, cu *cleanup.Cleanup) ([]mount.Mount, string, error) {
    82  	mounts := make([]mount.Mount, 0, 1)
    83  	target := "/data"
    84  	switch fsType {
    85  	case BindFS:
    86  		dir, err := machine.RunCommand("mktemp", "-d")
    87  		if err != nil {
    88  			return mounts, "", fmt.Errorf("failed to create tempdir: %v", err)
    89  		}
    90  		dir = strings.TrimSuffix(dir, "\n")
    91  		cu.Add(func() {
    92  			machine.RunCommand("rm", "-rf", dir)
    93  		})
    94  		out, err := machine.RunCommand("chmod", "777", dir)
    95  		if err != nil {
    96  			return mounts, "", fmt.Errorf("failed modify directory: %v %s", err, out)
    97  		}
    98  		mounts = append(mounts, mount.Mount{
    99  			Target: target,
   100  			Source: dir,
   101  			Type:   mount.TypeBind,
   102  		})
   103  		return mounts, target, nil
   104  	case RootFS:
   105  		return mounts, target, nil
   106  	case TmpFS:
   107  		mounts = append(mounts, mount.Mount{
   108  			Target: target,
   109  			Type:   mount.TypeTmpfs,
   110  		})
   111  		return mounts, target, nil
   112  	case FuseFS:
   113  		mounts = append(mounts,
   114  			mount.Mount{
   115  				Target: target,
   116  				Type:   mount.TypeTmpfs,
   117  			},
   118  			mount.Mount{
   119  				Target: "/fuse",
   120  				Type:   mount.TypeTmpfs,
   121  			})
   122  		return mounts, target, nil
   123  	default:
   124  		return mounts, "", fmt.Errorf("illegal mount type not supported: %v", fsType)
   125  	}
   126  }