1// Copyright 2023 Google LLC 2// 3// Use of this source code is governed by a BSD-style license that can be 4// found in the LICENSE file. 5 6package common 7 8import ( 9 "context" 10 "testing" 11 12 exec_testutils "go.skia.org/infra/go/exec/testutils" 13 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 "go.skia.org/infra/go/exec" 17 "go.skia.org/infra/task_driver/go/td" 18 "go.skia.org/skia/infra/bots/task_drivers/testutils" 19) 20 21func TestBazelCleanIfLowDiskSpace_EnoughDiskSpace_BazelCachePreserved(t *testing.T) { 22 commandCollector := exec.CommandCollector{} 23 res := td.RunTestSteps(t, false, func(ctx context.Context) error { 24 ctx = td.WithExecRunFn(ctx, commandCollector.Run) 25 26 ctx = context.WithValue(ctx, BazelCleanIfLowDiskSpaceContextKey, BazelCleanIfLowDiskSpaceContextValue{ 27 GetPartitionMountpoints: func() ([]string, error) { 28 // Note that some of these mountpoints are prefixes of the actual mountpoint ("/mnt/pd0"). 29 // This test checks that BazelCleanIfLowDiskSpace correctly identifies the mountpoint. 30 return []string{"/", "/boot", "/mnt", "/mnt/pd0", "/var"}, nil 31 }, 32 FreeBytesOnPartition: func(mountpoint string) (uint64, error) { 33 require.Equal(t, "/mnt/pd0", mountpoint) 34 return uint64(20_000_000_000), nil 35 }, 36 }) 37 38 err := BazelCleanIfLowDiskSpace(ctx, "/mnt/pd0/bazel_cache", "/path/to/checkout", "/path/to/bazel") 39 40 assert.NoError(t, err) 41 return err 42 }) 43 44 require.Empty(t, res.Errors) 45 require.Empty(t, res.Exceptions) 46 testutils.AssertStepNames(t, res, 47 "Clean Bazel cache if disk space is too low", 48 "No need to clear the Bazel cache: free space on partition /mnt/pd0 is 20000000000 bytes, which is above the threshold of 15000000000 bytes", 49 ) 50 51 assert.Empty(t, commandCollector.Commands()) 52} 53 54func TestBazelCleanIfLowDiskSpace_LowDiskSpace_BazelCacheDeleted(t *testing.T) { 55 commandCollector := exec.CommandCollector{} 56 res := td.RunTestSteps(t, false, func(ctx context.Context) error { 57 ctx = td.WithExecRunFn(ctx, commandCollector.Run) 58 59 ctx = context.WithValue(ctx, BazelCleanIfLowDiskSpaceContextKey, BazelCleanIfLowDiskSpaceContextValue{ 60 GetPartitionMountpoints: func() ([]string, error) { 61 // Note that some of these mountpoints are prefixes of the actual mountpoint ("/mnt/pd0"). 62 // This test checks that BazelCleanIfLowDiskSpace correctly identifies the mountpoint. 63 return []string{"/", "/boot", "/mnt/pd0", "/var"}, nil 64 }, 65 FreeBytesOnPartition: func(mountpoint string) (uint64, error) { 66 require.Equal(t, "/mnt/pd0", mountpoint) 67 return 0, nil 68 }, 69 }) 70 71 err := BazelCleanIfLowDiskSpace(ctx, "/mnt/pd0", "/path/to/checkout", "/path/to/bazel") 72 73 assert.NoError(t, err) 74 return err 75 }) 76 77 require.Empty(t, res.Errors) 78 require.Empty(t, res.Exceptions) 79 testutils.AssertStepNames(t, res, 80 "Clean Bazel cache if disk space is too low", 81 "Free space on partition /mnt/pd0 is 0 bytes, which is below the threshold of 15000000000 bytes", 82 "/path/to/bazel clean", 83 ) 84 85 require.Len(t, commandCollector.Commands(), 1) 86 exec_testutils.AssertCommandsMatch(t, [][]string{ 87 { 88 "/path/to/bazel", 89 "clean", 90 }, 91 }, commandCollector.Commands()) 92 assert.Equal(t, "/path/to/checkout", commandCollector.Commands()[0].Dir) 93} 94