github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/test/dockerutil/profile_test.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 dockerutil 16 17 import ( 18 "context" 19 "fmt" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "testing" 24 "time" 25 ) 26 27 type testCase struct { 28 name string 29 profile profile 30 expectedFiles []string 31 } 32 33 func TestProfile(t *testing.T) { 34 // Basepath and expected file names for each type of profile. 35 tmpDir, err := ioutil.TempDir("", "") 36 if err != nil { 37 t.Fatalf("unable to create temporary directory: %v", err) 38 } 39 defer os.RemoveAll(tmpDir) 40 41 // All expected names. 42 basePath := tmpDir 43 block := "block.pprof" 44 cpu := "cpu.pprof" 45 heap := "heap.pprof" 46 mutex := "mutex.pprof" 47 48 testCases := []testCase{ 49 { 50 name: "One", 51 profile: profile{ 52 BasePath: basePath, 53 Types: []string{"cpu"}, 54 Duration: 2 * time.Second, 55 }, 56 expectedFiles: []string{cpu}, 57 }, 58 { 59 name: "All", 60 profile: profile{ 61 BasePath: basePath, 62 Types: []string{"block", "cpu", "heap", "mutex"}, 63 Duration: 2 * time.Second, 64 }, 65 expectedFiles: []string{block, cpu, heap, mutex}, 66 }, 67 } 68 for _, tc := range testCases { 69 t.Run(tc.name, func(t *testing.T) { 70 ctx := context.Background() 71 c := MakeContainer(ctx, t) 72 73 // Set basepath to include the container name so there are no conflicts. 74 localProfile := tc.profile // Copy it. 75 localProfile.BasePath = filepath.Join(localProfile.BasePath, tc.name) 76 77 // Set directly on the container, to avoid flags. 78 c.profile = &localProfile 79 80 func() { 81 defer c.CleanUp(ctx) 82 83 // Start a container. 84 if err := c.Spawn(ctx, RunOpts{ 85 Image: "basic/alpine", 86 }, "sleep", "1000"); err != nil { 87 t.Fatalf("run failed with: %v", err) 88 } 89 90 if status, err := c.Status(context.Background()); !status.Running { 91 t.Fatalf("container is not yet running: %+v err: %v", status, err) 92 } 93 94 // End early if the expected files exist and have data. 95 for start := time.Now(); time.Since(start) < localProfile.Duration; time.Sleep(100 * time.Millisecond) { 96 if err := checkFiles(localProfile.BasePath, tc.expectedFiles); err == nil { 97 break 98 } 99 } 100 }() 101 102 // Check all expected files exist and have data. 103 if err := checkFiles(localProfile.BasePath, tc.expectedFiles); err != nil { 104 t.Fatalf(err.Error()) 105 } 106 }) 107 } 108 } 109 110 func checkFiles(basePath string, expectedFiles []string) error { 111 for _, file := range expectedFiles { 112 stat, err := os.Stat(filepath.Join(basePath, file)) 113 if err != nil { 114 return fmt.Errorf("stat failed with: %v", err) 115 } else if stat.Size() < 1 { 116 return fmt.Errorf("file not written to: %+v", stat) 117 } 118 } 119 return nil 120 } 121 122 func TestMain(m *testing.M) { 123 EnsureSupportedDockerVersion() 124 os.Exit(m.Run()) 125 }