-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathobject_test.go
More file actions
81 lines (73 loc) · 2.86 KB
/
object_test.go
File metadata and controls
81 lines (73 loc) · 2.86 KB
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
package storage
import (
"context"
"errors"
"testing"
"time"
//"github.com/distribution/distribution/v3/context"
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
"github.com/stretchr/testify/assert"
)
const (
objPath = "myobj"
)
func TestObjectExistsSuccess(t *testing.T) {
objInfo := storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{Path: objPath, Size: 1234}}
statter := &FakeObjectStatter{
Fn: func(context.Context, string) (storagedriver.FileInfo, error) {
return objInfo, nil
},
}
exists, err := ObjectExists(statter, objPath)
assert.Equal(t, err, nil)
assert.True(t, exists, "object not found when it should be present")
assert.Equal(t, len(statter.Calls), 1, "number of StatObject calls")
assert.Equal(t, statter.Calls[0].Path, objPath, "object key")
}
func TestObjectExistsNoObject(t *testing.T) {
statter := &FakeObjectStatter{
Fn: func(context.Context, string) (storagedriver.FileInfo, error) {
return storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{}}, storagedriver.PathNotFoundError{Path: objPath}
},
}
exists, err := ObjectExists(statter, objPath)
assert.Equal(t, err, nil)
assert.False(t, exists, "object found when it should be missing")
assert.Equal(t, len(statter.Calls), 1, "number of StatObject calls")
}
func TestObjectExistsOtherErr(t *testing.T) {
expectedErr := errors.New("other error")
statter := &FakeObjectStatter{
Fn: func(context.Context, string) (storagedriver.FileInfo, error) {
return storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{}}, expectedErr
},
}
exists, err := ObjectExists(statter, objPath)
assert.Error(t, err, expectedErr)
assert.False(t, exists, "object found when the statter errored")
}
func TestWaitForObjectMissing(t *testing.T) {
statter := &FakeObjectStatter{
Fn: func(context.Context, string) (storagedriver.FileInfo, error) {
return storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{}}, storagedriver.PathNotFoundError{Path: objPath}
},
}
err := WaitForObject(statter, objPath, 1*time.Millisecond, 2*time.Millisecond)
assert.True(t, err != nil, "no error received when there should have been")
// it should make 1 call immediately, then calls at 1ms and 2ms
assert.True(
t,
len(statter.Calls) >= 1,
"the statter was not called, but should have been called at least once",
)
}
func TestWaitForObjectExists(t *testing.T) {
statter := &FakeObjectStatter{
Fn: func(context.Context, string) (storagedriver.FileInfo, error) {
return storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{}}, nil
},
}
assert.Equal(t, WaitForObject(statter, objPath, 1*time.Millisecond, 2*time.Millisecond), nil)
// it should make 1 call immediately, then immediateley succeed
assert.Equal(t, len(statter.Calls), 1, "number of calls to the statter")
}