forked from containers/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images_test.go
94 lines (77 loc) · 2.41 KB
/
images_test.go
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
package storage
import (
"io/ioutil"
"testing"
"time"
digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/require"
)
func newTestImageStore(t *testing.T) ImageStore {
dir, err := ioutil.TempDir("", "storage")
require.Nil(t, err)
store, err := newImageStore(dir)
require.Nil(t, err)
return store
}
func addTestImage(t *testing.T, store ImageStore, id string, names []string) {
store.Lock()
defer store.Unlock()
_, err := store.Create(
id, []string{}, "", "", time.Now(), digest.FromString(""),
)
require.Nil(t, err)
require.Nil(t, store.SetNames(id, names))
}
func TestAddNameToHistorySuccess(t *testing.T) {
// Given
image := Image{}
// When
image.addNameToHistory("first")
image.addNameToHistory("first")
image.addNameToHistory("second")
// Then
require.Len(t, image.NamesHistory, 2)
}
func TestHistoryNames(t *testing.T) {
// Given
store := newTestImageStore(t)
// When
const firstImageID = "first"
addTestImage(t, store, firstImageID, []string{"1", "2"})
const secondImageID = "second"
addTestImage(t, store, secondImageID, []string{"2", "3"})
// Then
firstImage, err := store.Get(firstImageID)
require.Nil(t, err)
require.Len(t, firstImage.Names, 1)
require.Equal(t, firstImage.Names[0], "1")
require.Len(t, firstImage.NamesHistory, 2)
require.Equal(t, firstImage.NamesHistory[0], "2")
require.Equal(t, firstImage.NamesHistory[1], "1")
secondImage, err := store.Get(secondImageID)
require.Nil(t, err)
require.Len(t, secondImage.Names, 2)
require.Equal(t, secondImage.Names[0], "2")
require.Equal(t, secondImage.Names[1], "3")
require.Len(t, secondImage.NamesHistory, 2)
require.Equal(t, secondImage.NamesHistory[0], "3")
require.Equal(t, secondImage.NamesHistory[1], "2")
// And When
store.Lock()
defer store.Unlock()
require.Nil(t, store.SetNames(firstImageID, []string{"1", "2", "3", "4"}))
// Then
firstImage, err = store.Get(firstImageID)
require.Nil(t, err)
require.Len(t, firstImage.NamesHistory, 4)
require.Equal(t, firstImage.NamesHistory[0], "4")
require.Equal(t, firstImage.NamesHistory[1], "3")
require.Equal(t, firstImage.NamesHistory[2], "2")
require.Equal(t, firstImage.NamesHistory[3], "1")
secondImage, err = store.Get(secondImageID)
require.Nil(t, err)
require.Len(t, secondImage.Names, 0)
require.Len(t, secondImage.NamesHistory, 2)
require.Equal(t, secondImage.NamesHistory[0], "3")
require.Equal(t, secondImage.NamesHistory[1], "2")
}