@@ -2,11 +2,13 @@ package cache
2
2
3
3
import (
4
4
"bytes"
5
+ "fmt"
5
6
"path/filepath"
6
7
"testing"
7
8
8
9
"github.com/deislabs/cnab-go/bundle"
9
10
"github.com/deislabs/porter/pkg/config"
11
+ "github.com/docker/cnab-to-oci/relocation"
10
12
"github.com/stretchr/testify/assert"
11
13
"github.com/stretchr/testify/require"
12
14
)
@@ -36,7 +38,7 @@ func TestFindBundleCacheExists(t *testing.T) {
36
38
cfg .TestContext .AddTestDirectory ("testdata" , cacheDir )
37
39
c := New (cfg .Config )
38
40
39
- _ , ok , err := c .FindBundle ("deislabs/kubekahn:latest" )
41
+ _ , _ , ok , err := c .FindBundle ("deislabs/kubekahn:latest" )
40
42
assert .NoError (t , err , "the cache dir should exist, no error should have happened" )
41
43
assert .False (t , ok , "the bundle shouldn't exist" )
42
44
}
@@ -49,7 +51,7 @@ func TestFindBundleCacheDoesNotExist(t *testing.T) {
49
51
cfg .TestContext .AddTestDirectory ("testdata" , cacheDir )
50
52
c := New (cfg .Config )
51
53
52
- _ , ok , err := c .FindBundle ("deislabs/kubekahn:latest" )
54
+ _ , _ , ok , err := c .FindBundle ("deislabs/kubekahn:latest" )
53
55
assert .NoError (t , err , "the cache dir doesn't exist, but this shouldn't be an error" )
54
56
assert .False (t , ok , "the bundle shouldn't exist" )
55
57
}
@@ -66,7 +68,7 @@ func TestFindBundleBundleCached(t *testing.T) {
66
68
foundIt , err := cfg .Config .FileSystem .Exists (expectedCacheFile )
67
69
require .True (t , foundIt , "test data not loaded" )
68
70
c := New (cfg .Config )
69
- path , ok , err := c .FindBundle (kahn1dot01 )
71
+ path , _ , ok , err := c .FindBundle (kahn1dot01 )
70
72
assert .NoError (t , err , "the cache dir should exist, no error should have happened" )
71
73
assert .True (t , ok , "the bundle should exist" )
72
74
assert .Equal (t , expectedCacheFile , path )
@@ -75,7 +77,7 @@ func TestFindBundleBundleCached(t *testing.T) {
75
77
func TestFindBundleBundleNotCached (t * testing.T ) {
76
78
cfg := config .NewTestConfig (t )
77
79
c := New (cfg .Config )
78
- path , ok , err := c .FindBundle (kahnlatest )
80
+ path , _ , ok , err := c .FindBundle (kahnlatest )
79
81
assert .NoError (t , err , "the cache dir should exist, no error should have happened" )
80
82
assert .False (t , ok , "the bundle should not exist" )
81
83
assert .Empty (t , path , "should not have a path" )
@@ -89,7 +91,8 @@ func TestCacheWriteNoCacheDir(t *testing.T) {
89
91
require .NoError (t , err , "bundle should have been valid" )
90
92
91
93
c := New (cfg .Config )
92
- path , err := c .StoreBundle (kahn1dot01 , & bun )
94
+ var reloMap relocation.ImageRelocationMap
95
+ path , _ , err := c .StoreBundle (kahn1dot01 , & bun , reloMap )
93
96
94
97
home , err := cfg .Config .GetHomeDir ()
95
98
require .NoError (t , err , "should have had a porter home dir" )
@@ -114,7 +117,8 @@ func TestCacheWriteCacheDirExists(t *testing.T) {
114
117
require .NoError (t , err , "bundle should have been valid" )
115
118
116
119
c := New (cfg .Config )
117
- path , err := c .StoreBundle (kahn1dot01 , & bun )
120
+ var reloMap relocation.ImageRelocationMap
121
+ path , _ , err := c .StoreBundle (kahn1dot01 , & bun , reloMap )
118
122
119
123
expectedCacheDirectory := filepath .Join (cacheDir , kahn1dot0Hash )
120
124
expectedCacheCNABDirectory := filepath .Join (expectedCacheDirectory , "cnab" )
@@ -123,3 +127,45 @@ func TestCacheWriteCacheDirExists(t *testing.T) {
123
127
assert .Equal (t , expectedCacheFile , path )
124
128
assert .NoError (t , err , "storing bundle should have succeeded" )
125
129
}
130
+
131
+ func TestStoreRelocationMapping (t * testing.T ) {
132
+
133
+ cfg := config .NewTestConfig (t )
134
+ home , _ := cfg .Config .GetHomeDir ()
135
+ cacheDir := filepath .Join (home , "cache" )
136
+
137
+ tests := []struct {
138
+ name string
139
+ relocationMapping relocation.ImageRelocationMap
140
+ tag string
141
+ bundle * bundle.Bundle
142
+ wantedReloPath string
143
+ err error
144
+ }{
145
+ {
146
+ name : "relocation file gets a path" ,
147
+ bundle : & bundle.Bundle {},
148
+ tag : kahn1dot01 ,
149
+ relocationMapping : relocation.ImageRelocationMap {
150
+ "asd" : "asdf" ,
151
+ },
152
+ wantedReloPath : filepath .Join (cacheDir , kahn1dot0Hash , "cnab" , "relocation-mapping.json" ),
153
+ },
154
+ {
155
+ name : "no relocation file gets no path" ,
156
+ tag : kahnlatest ,
157
+ bundle : & bundle.Bundle {},
158
+ wantedReloPath : "" ,
159
+ },
160
+ }
161
+
162
+ c := New (cfg .Config )
163
+ for _ , test := range tests {
164
+ _ , reloPath , err := c .StoreBundle (test .tag , test .bundle , test .relocationMapping )
165
+ assert .NoError (t , err , fmt .Sprintf ("didn't expect storage error for test %s" , test .name ))
166
+ assert .Equal (t , test .wantedReloPath , reloPath , "didn't get expected path for store on test: %s" , test .name )
167
+ _ , fetchedPath , _ , err := c .FindBundle (test .tag )
168
+ assert .Equal (t , test .wantedReloPath , fetchedPath , "didn't get expected path for load on test: %s" , test .name )
169
+
170
+ }
171
+ }
0 commit comments