gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/benchmarks/harness/machine.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 "errors" 20 "net" 21 "os/exec" 22 23 "gvisor.dev/gvisor/pkg/test/dockerutil" 24 "gvisor.dev/gvisor/pkg/test/testutil" 25 ) 26 27 // Machine describes a real machine for use in benchmarks. 28 type Machine interface { 29 // GetContainer gets a container from the machine. The container uses the 30 // runtime under test and is profiled if requested by flags. 31 GetContainer(ctx context.Context, log testutil.Logger) *dockerutil.Container 32 33 // GetNativeContainer gets a native container from the machine. Native containers 34 // use runc by default and are not profiled. 35 GetNativeContainer(ctx context.Context, log testutil.Logger) *dockerutil.Container 36 37 // RunCommand runs cmd on this machine. 38 RunCommand(cmd string, args ...string) (string, error) 39 40 // Returns IP Address for the machine. 41 IPAddress() (net.IP, error) 42 43 // CleanUp cleans up this machine. 44 CleanUp() 45 } 46 47 // localMachine describes this machine. 48 type localMachine struct { 49 } 50 51 // GetContainer implements Machine.GetContainer for localMachine. 52 func (l *localMachine) GetContainer(ctx context.Context, logger testutil.Logger) *dockerutil.Container { 53 return dockerutil.MakeContainer(ctx, logger) 54 } 55 56 // GetContainer implements Machine.GetContainer for localMachine. 57 func (l *localMachine) GetNativeContainer(ctx context.Context, logger testutil.Logger) *dockerutil.Container { 58 return dockerutil.MakeNativeContainer(ctx, logger) 59 } 60 61 // RunCommand implements Machine.RunCommand for localMachine. 62 func (l *localMachine) RunCommand(cmd string, args ...string) (string, error) { 63 c := exec.Command(cmd, args...) 64 out, err := c.CombinedOutput() 65 return string(out), err 66 } 67 68 // IPAddress implements Machine.IPAddress. 69 func (l *localMachine) IPAddress() (net.IP, error) { 70 addrs, err := net.InterfaceAddrs() 71 if err != nil { 72 return net.IP{}, err 73 } 74 for _, a := range addrs { 75 if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { 76 if ipnet.IP.To4() != nil { 77 return ipnet.IP, nil 78 } 79 } 80 } 81 // Unable to locate non-loopback address. 82 return nil, errors.New("no IPAddress available") 83 } 84 85 // CleanUp implements Machine.CleanUp and does nothing for localMachine. 86 func (*localMachine) CleanUp() { 87 }