github.com/bhojpur/cache@v0.0.4/pkg/ioutils/fswriters_test.go (about) 1 package ioutils 2 3 // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved. 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 import ( 24 "bytes" 25 "os" 26 "path/filepath" 27 "runtime" 28 "testing" 29 ) 30 31 var ( 32 testMode os.FileMode = 0640 33 ) 34 35 func init() { 36 // Windows does not support full Linux file mode 37 if runtime.GOOS == "windows" { 38 testMode = 0666 39 } 40 } 41 42 func TestAtomicWriteToFile(t *testing.T) { 43 tmpDir, err := os.MkdirTemp("", "atomic-writers-test") 44 if err != nil { 45 t.Fatalf("Error when creating temporary directory: %s", err) 46 } 47 defer os.RemoveAll(tmpDir) 48 49 expected := []byte("barbaz") 50 if err := AtomicWriteFile(filepath.Join(tmpDir, "foo"), expected, testMode); err != nil { 51 t.Fatalf("Error writing to file: %v", err) 52 } 53 54 actual, err := os.ReadFile(filepath.Join(tmpDir, "foo")) 55 if err != nil { 56 t.Fatalf("Error reading from file: %v", err) 57 } 58 59 if !bytes.Equal(actual, expected) { 60 t.Fatalf("Data mismatch, expected %q, got %q", expected, actual) 61 } 62 63 st, err := os.Stat(filepath.Join(tmpDir, "foo")) 64 if err != nil { 65 t.Fatalf("Error statting file: %v", err) 66 } 67 if expected := testMode; st.Mode() != expected { 68 t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode()) 69 } 70 } 71 72 func TestAtomicWriteSetCommit(t *testing.T) { 73 tmpDir, err := os.MkdirTemp("", "atomic-writerset-test") 74 if err != nil { 75 t.Fatalf("Error when creating temporary directory: %s", err) 76 } 77 defer os.RemoveAll(tmpDir) 78 79 if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0700); err != nil { 80 t.Fatalf("Error creating tmp directory: %s", err) 81 } 82 83 targetDir := filepath.Join(tmpDir, "target") 84 ws, err := NewAtomicWriteSet(filepath.Join(tmpDir, "tmp")) 85 if err != nil { 86 t.Fatalf("Error creating atomic write set: %s", err) 87 } 88 89 expected := []byte("barbaz") 90 if err := ws.WriteFile("foo", expected, testMode); err != nil { 91 t.Fatalf("Error writing to file: %v", err) 92 } 93 94 if _, err := os.ReadFile(filepath.Join(targetDir, "foo")); err == nil { 95 t.Fatalf("Expected error reading file where should not exist") 96 } 97 98 if err := ws.Commit(targetDir); err != nil { 99 t.Fatalf("Error committing file: %s", err) 100 } 101 102 actual, err := os.ReadFile(filepath.Join(targetDir, "foo")) 103 if err != nil { 104 t.Fatalf("Error reading from file: %v", err) 105 } 106 107 if !bytes.Equal(actual, expected) { 108 t.Fatalf("Data mismatch, expected %q, got %q", expected, actual) 109 } 110 111 st, err := os.Stat(filepath.Join(targetDir, "foo")) 112 if err != nil { 113 t.Fatalf("Error statting file: %v", err) 114 } 115 if expected := testMode; st.Mode() != expected { 116 t.Fatalf("Mode mismatched, expected %o, got %o", expected, st.Mode()) 117 } 118 119 } 120 121 func TestAtomicWriteSetCancel(t *testing.T) { 122 tmpDir, err := os.MkdirTemp("", "atomic-writerset-test") 123 if err != nil { 124 t.Fatalf("Error when creating temporary directory: %s", err) 125 } 126 defer os.RemoveAll(tmpDir) 127 128 if err := os.Mkdir(filepath.Join(tmpDir, "tmp"), 0700); err != nil { 129 t.Fatalf("Error creating tmp directory: %s", err) 130 } 131 132 ws, err := NewAtomicWriteSet(filepath.Join(tmpDir, "tmp")) 133 if err != nil { 134 t.Fatalf("Error creating atomic write set: %s", err) 135 } 136 137 expected := []byte("barbaz") 138 if err := ws.WriteFile("foo", expected, testMode); err != nil { 139 t.Fatalf("Error writing to file: %v", err) 140 } 141 142 if err := ws.Cancel(); err != nil { 143 t.Fatalf("Error committing file: %s", err) 144 } 145 146 if _, err := os.ReadFile(filepath.Join(tmpDir, "target", "foo")); err == nil { 147 t.Fatalf("Expected error reading file where should not exist") 148 } else if !os.IsNotExist(err) { 149 t.Fatalf("Unexpected error reading file: %s", err) 150 } 151 }