go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/mmutex/lib/lock_file_helpers_test.go (about) 1 // Copyright 2017 The LUCI 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 lib 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/maruel/subcommands" 25 26 . "github.com/smartystreets/goconvey/convey" 27 . "go.chromium.org/luci/common/testing/assertions" 28 ) 29 30 func TestMain(t *testing.T) { 31 Convey("computeMutexPaths returns empty strings without environment variable set", t, func() { 32 env := subcommands.Env{ 33 "MMUTEX_LOCK_DIR": subcommands.EnvVar{"", false}, 34 } 35 lockFilePath, drainFilePath, err := computeMutexPaths(env) 36 So(err, ShouldBeNil) 37 So(lockFilePath, ShouldBeBlank) 38 So(drainFilePath, ShouldBeBlank) 39 }) 40 41 Convey("computeMutexPaths returns empty strings when lock dir doesn't exist", t, func() { 42 tempDir, err := ioutil.TempDir("", "") 43 So(err, ShouldBeNil) 44 So(os.Remove(tempDir), ShouldBeNil) 45 46 env := subcommands.Env{ 47 "MMUTEX_LOCK_DIR": subcommands.EnvVar{tempDir, true}, 48 } 49 lockFilePath, drainFilePath, err := computeMutexPaths(env) 50 So(lockFilePath, ShouldBeBlank) 51 So(drainFilePath, ShouldBeBlank) 52 So(err, ShouldBeNil) 53 }) 54 55 Convey("computeMutexPaths returns env variable based path", t, func() { 56 tempDir, err := ioutil.TempDir("", "") 57 defer os.Remove(tempDir) 58 So(err, ShouldBeNil) 59 60 env := subcommands.Env{ 61 "MMUTEX_LOCK_DIR": subcommands.EnvVar{tempDir, true}, 62 } 63 lockFilePath, drainFilePath, err := computeMutexPaths(env) 64 So(err, ShouldBeNil) 65 So(lockFilePath, ShouldEqual, filepath.Join(tempDir, "mmutex.lock")) 66 So(drainFilePath, ShouldEqual, filepath.Join(tempDir, "mmutex.drain")) 67 }) 68 69 Convey("computeMutexPaths returns error when lock dir is a relative path", t, func() { 70 path := filepath.Join("a", "b", "c") 71 env := subcommands.Env{ 72 "MMUTEX_LOCK_DIR": subcommands.EnvVar{path, true}, 73 } 74 lockFilePath, drainFilePath, err := computeMutexPaths(env) 75 So(err, ShouldErrLike, fmt.Sprintf("Lock file directory %s must be an absolute path", path)) 76 So(lockFilePath, ShouldBeBlank) 77 So(drainFilePath, ShouldBeBlank) 78 }) 79 }