| 1 | // Copyright 2016 CNI 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 disk |
| 16 | |
| 17 | import ( |
| 18 | "os" |
| 19 | "path/filepath" |
| 20 | |
| 21 | . "github.com/onsi/ginkgo/v2" |
| 22 | . "github.com/onsi/gomega" |
| 23 | ) |
| 24 | |
| 25 | var _ = Describe("Lock Operations", func() { |
| 26 | It("locks a file path", func() { |
| 27 | dir, err := os.MkdirTemp("", "") |
| 28 | Expect(err).ToNot(HaveOccurred()) |
| 29 | defer os.RemoveAll(dir) |
| 30 | |
| 31 | // create a dummy file to lock |
| 32 | path := filepath.Join(dir, "x") |
| 33 | f, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0o666) |
| 34 | Expect(err).ToNot(HaveOccurred()) |
| 35 | err = f.Close() |
| 36 | Expect(err).ToNot(HaveOccurred()) |
| 37 | |
| 38 | // now use it to lock |
| 39 | m, err := NewFileLock(path) |
| 40 | Expect(err).ToNot(HaveOccurred()) |
| 41 | |
| 42 | err = m.Lock() |
| 43 | Expect(err).ToNot(HaveOccurred()) |
| 44 | err = m.Unlock() |
| 45 | Expect(err).ToNot(HaveOccurred()) |
| 46 | }) |
| 47 | |
| 48 | It("locks a folder path", func() { |
| 49 | dir, err := os.MkdirTemp("", "") |
| 50 | Expect(err).ToNot(HaveOccurred()) |
| 51 | defer os.RemoveAll(dir) |
| 52 | |
| 53 | // use the folder to lock |
| 54 | m, err := NewFileLock(dir) |
| 55 | Expect(err).ToNot(HaveOccurred()) |
| 56 | |
| 57 | err = m.Lock() |
| 58 | Expect(err).ToNot(HaveOccurred()) |
| 59 | err = m.Unlock() |
| 60 | Expect(err).ToNot(HaveOccurred()) |
| 61 | }) |
| 62 | }) |