1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
package locking
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"sort"
"testing"
"time"
"github.com/git-lfs/git-lfs/v3/config"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/lfsapi"
"github.com/git-lfs/git-lfs/v3/lfshttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type LocksById []Lock
func (a LocksById) Len() int { return len(a) }
func (a LocksById) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a LocksById) Less(i, j int) bool { return a[i].Id < a[j].Id }
func TestRemoteLocksWithCache(t *testing.T) {
var err error
tempDir := t.TempDir()
remoteQueries := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remoteQueries++
assert.Equal(t, "GET", r.Method)
assert.Equal(t, "/api/locks", r.URL.Path)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&lockList{
Locks: []Lock{
Lock{Id: "100", Path: "folder/test1.dat", Owner: &User{Name: "Alice"}},
Lock{Id: "101", Path: "folder/test2.dat", Owner: &User{Name: "Charles"}},
Lock{Id: "102", Path: "folder/test3.dat", Owner: &User{Name: "Fred"}},
},
})
assert.Nil(t, err)
}))
defer func() {
srv.Close()
}()
lfsclient, err := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{
"lfs.url": srv.URL + "/api",
"user.name": "Fred",
"user.email": "fred@bloggs.com",
}))
require.Nil(t, err)
client, err := NewClient("", lfsclient, config.New())
assert.Nil(t, err)
assert.Nil(t, client.SetupFileCache(tempDir))
client.RemoteRef = &git.Ref{Name: "refs/heads/master"}
cacheFile, err := client.prepareCacheDirectory("remote")
assert.Nil(t, err)
// Cache file should not exist
fi, err := os.Stat(cacheFile)
assert.True(t, os.IsNotExist(err))
// Querying non-existing cache file will report nothing
locks, err := client.SearchLocks(nil, 0, false, true)
assert.NotNil(t, err)
assert.Empty(t, locks)
assert.Equal(t, 0, remoteQueries)
// Need to include zero time in structure for equal to work
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
// REMOTE QUERY: No cache file will be created when querying with a filter
locks, err = client.SearchLocks(map[string]string{
"key": "value",
}, 0, false, false)
assert.Nil(t, err)
// Just make sure we have have received anything, content doesn't matter
assert.Equal(t, 3, len(locks))
assert.Equal(t, 1, remoteQueries)
fi, err = os.Stat(cacheFile)
assert.True(t, os.IsNotExist(err))
// REMOTE QUERY: No cache file will be created when querying with a limit
locks, err = client.SearchLocks(nil, 1, false, false)
assert.Nil(t, err)
// Just make sure we have have received anything, content doesn't matter
assert.Equal(t, 1, len(locks))
assert.Equal(t, 2, remoteQueries)
fi, err = os.Stat(cacheFile)
assert.True(t, os.IsNotExist(err))
// REMOTE QUERY: locks will be reported and cache file should be created
locks, err = client.SearchLocks(nil, 0, false, false)
assert.Nil(t, err)
assert.Equal(t, 3, remoteQueries)
fi, err = os.Stat(cacheFile)
assert.Nil(t, err)
const size int64 = 300
assert.Equal(t, size, fi.Size())
expectedLocks := []Lock{
Lock{Path: "folder/test1.dat", Id: "100", Owner: &User{Name: "Alice"}, LockedAt: zeroTime},
Lock{Path: "folder/test2.dat", Id: "101", Owner: &User{Name: "Charles"}, LockedAt: zeroTime},
Lock{Path: "folder/test3.dat", Id: "102", Owner: &User{Name: "Fred"}, LockedAt: zeroTime},
}
sort.Sort(LocksById(locks))
assert.Equal(t, expectedLocks, locks)
// Querying cache file should report same locks
locks, err = client.SearchLocks(nil, 0, false, true)
assert.Nil(t, err)
assert.Equal(t, 3, remoteQueries)
sort.Sort(LocksById(locks))
assert.Equal(t, expectedLocks, locks)
}
func TestRefreshCache(t *testing.T) {
var err error
tempDir := t.TempDir()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/api/locks/verify", r.URL.Path)
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(lockVerifiableList{
Theirs: []Lock{
Lock{Id: "99", Path: "folder/test3.dat", Owner: &User{Name: "Alice"}},
Lock{Id: "199", Path: "other/test1.dat", Owner: &User{Name: "Charles"}},
},
Ours: []Lock{
Lock{Id: "101", Path: "folder/test1.dat", Owner: &User{Name: "Fred"}},
Lock{Id: "102", Path: "folder/test2.dat", Owner: &User{Name: "Fred"}},
Lock{Id: "103", Path: "root.dat", Owner: &User{Name: "Fred"}},
},
})
assert.Nil(t, err)
}))
defer func() {
srv.Close()
}()
lfsclient, err := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{
"lfs.url": srv.URL + "/api",
"user.name": "Fred",
"user.email": "fred@bloggs.com",
}))
require.Nil(t, err)
client, err := NewClient("", lfsclient, config.New())
assert.Nil(t, err)
assert.Nil(t, client.SetupFileCache(tempDir))
// Should start with no cached items
locks, err := client.SearchLocks(nil, 0, true, false)
assert.Nil(t, err)
assert.Empty(t, locks)
client.RemoteRef = &git.Ref{Name: "refs/heads/master"}
_, _, err = client.SearchLocksVerifiable(100, false)
assert.Nil(t, err)
locks, err = client.SearchLocks(nil, 0, true, false)
assert.Nil(t, err)
// Need to include zero time in structure for equal to work
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
// Sort locks for stable comparison
sort.Sort(LocksById(locks))
assert.Equal(t, []Lock{
Lock{Path: "folder/test1.dat", Id: "101", Owner: &User{Name: "Fred"}, LockedAt: zeroTime},
Lock{Path: "folder/test2.dat", Id: "102", Owner: &User{Name: "Fred"}, LockedAt: zeroTime},
Lock{Path: "root.dat", Id: "103", Owner: &User{Name: "Fred"}, LockedAt: zeroTime},
Lock{Path: "other/test1.dat", Id: "199", Owner: &User{Name: "Charles"}, LockedAt: zeroTime},
Lock{Path: "folder/test3.dat", Id: "99", Owner: &User{Name: "Alice"}, LockedAt: zeroTime},
}, locks)
}
func TestSearchLocksVerifiableWithCache(t *testing.T) {
var err error
tempDir := t.TempDir()
remoteQueries := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remoteQueries++
assert.Equal(t, "POST", r.Method)
assert.Equal(t, "/api/locks/verify", r.URL.Path)
body := lockVerifiableRequest{}
if assert.Nil(t, json.NewDecoder(r.Body).Decode(&body)) {
w.Header().Set("Content-Type", "application/json")
list := lockVerifiableList{}
if body.Cursor == "1" {
list.Ours = []Lock{
Lock{Path: "folder/1/test1.dat", Id: "111"},
}
list.Theirs = []Lock{
Lock{Path: "folder/1/test2.dat", Id: "112"},
Lock{Path: "folder/1/test3.dat", Id: "113"},
}
} else {
list.Ours = []Lock{
Lock{Path: "folder/0/test1.dat", Id: "101"},
Lock{Path: "folder/0/test2.dat", Id: "102"},
}
list.Theirs = []Lock{
Lock{Path: "folder/0/test3.dat", Id: "103"},
}
list.NextCursor = "1"
}
err := json.NewEncoder(w).Encode(&list)
assert.Nil(t, err)
} else {
w.WriteHeader(500)
}
}))
defer srv.Close()
lfsclient, err := lfsapi.NewClient(lfshttp.NewContext(nil, nil, map[string]string{
"lfs.url": srv.URL + "/api",
"user.name": "Fred",
"user.email": "fred@bloggs.com",
}))
require.Nil(t, err)
client, err := NewClient("", lfsclient, config.New())
assert.Nil(t, client.SetupFileCache(tempDir))
client.RemoteRef = &git.Ref{Name: "refs/heads/master"}
cacheFile, err := client.prepareCacheDirectory("verifiable")
assert.Nil(t, err)
// Cache file should not exist
fi, err := os.Stat(cacheFile)
assert.True(t, os.IsNotExist(err))
// Querying non-existing cache file will report nothing
ourLocks, theirLocks, err := client.SearchLocksVerifiable(0, true)
assert.NotNil(t, err)
assert.Empty(t, ourLocks)
assert.Empty(t, theirLocks)
assert.Equal(t, 0, remoteQueries)
// REMOTE QUERY: No cache file will be created when querying with a limit
ourLocks, theirLocks, err = client.SearchLocksVerifiable(1, false)
assert.Nil(t, err)
// Just make sure we have have received anything, content doesn't matter
assert.Equal(t, 1, len(ourLocks))
assert.Equal(t, 0, len(theirLocks))
assert.Equal(t, 1, remoteQueries)
fi, err = os.Stat(cacheFile)
assert.True(t, os.IsNotExist(err))
// REMOTE QUERY: locks will be reported and cache file should be created
ourLocks, theirLocks, err = client.SearchLocksVerifiable(0, false)
assert.Nil(t, err)
assert.Equal(t, 3, remoteQueries)
fi, err = os.Stat(cacheFile)
assert.Nil(t, err)
const size int64 = 478
assert.Equal(t, size, fi.Size())
// Need to include zero time in structure for equal to work
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
// Sort locks for stable comparison
expectedOurLocks := []Lock{
Lock{Path: "folder/0/test1.dat", Id: "101", LockedAt: zeroTime},
Lock{Path: "folder/0/test2.dat", Id: "102", LockedAt: zeroTime},
Lock{Path: "folder/1/test1.dat", Id: "111", LockedAt: zeroTime},
}
expectedTheirLocks := []Lock{
Lock{Path: "folder/0/test3.dat", Id: "103", LockedAt: zeroTime},
Lock{Path: "folder/1/test2.dat", Id: "112", LockedAt: zeroTime},
Lock{Path: "folder/1/test3.dat", Id: "113", LockedAt: zeroTime},
}
sort.Sort(LocksById(ourLocks))
assert.Equal(t, expectedOurLocks, ourLocks)
sort.Sort(LocksById(theirLocks))
assert.Equal(t, expectedTheirLocks, theirLocks)
// Querying cache file should report same locks
ourLocks, theirLocks, err = client.SearchLocksVerifiable(0, true)
assert.Nil(t, err)
assert.Equal(t, 3, remoteQueries)
sort.Sort(LocksById(ourLocks))
assert.Equal(t, expectedOurLocks, ourLocks)
sort.Sort(LocksById(theirLocks))
assert.Equal(t, expectedTheirLocks, theirLocks)
}
|