-
Notifications
You must be signed in to change notification settings - Fork 243
/
images_test.go
109 lines (90 loc) · 3.04 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package storage
import (
"testing"
"time"
digest "github.com/opencontainers/go-digest"
"github.com/stretchr/testify/require"
)
func newTestImageStore(t *testing.T) rwImageStore {
t.Helper()
store, err := newImageStore(t.TempDir())
require.Nil(t, err)
return store
}
func addTestImage(t *testing.T, store rwImageStore, id string, names []string) {
err := store.startWriting()
require.NoError(t, err)
defer store.stopWriting()
options := ImageOptions{
CreationDate: time.Now(),
Digest: digest.FromString(""),
}
_, err = store.create(
id, []string{}, "", options,
)
require.Nil(t, err)
require.Nil(t, store.updateNames(id, names, setNames))
}
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
require.NoError(t, store.startWriting())
defer store.stopWriting()
require.Nil(t, store.updateNames(firstImageID, []string{"1", "2", "3", "4"}, setNames))
// 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")
// test independent add and remove operations
require.Nil(t, store.updateNames(firstImageID, []string{"5"}, addNames))
firstImage, err = store.Get(firstImageID)
require.Nil(t, err)
require.Equal(t, firstImage.NamesHistory, []string{"4", "3", "2", "1", "5"})
// history should still contain old values
require.Nil(t, store.updateNames(firstImageID, []string{"5"}, removeNames))
firstImage, err = store.Get(firstImageID)
require.Nil(t, err)
require.Equal(t, firstImage.NamesHistory, []string{"4", "3", "2", "1", "5"})
}