88
99 "github.com/google/go-cmp/cmp"
1010 "github.com/google/uuid"
11+ "github.com/stackitcloud/stackit-cli/internal/pkg/auth"
1112)
1213
1314func TestGetObjectErrors (t * testing.T ) {
@@ -201,7 +202,60 @@ func TestDeleteObject(t *testing.T) {
201202 }
202203}
203204
205+ func clearKeys (t * testing.T ) {
206+ t .Helper ()
207+ err := auth .DeleteAuthField (auth .CACHE_ENCRYPTION_KEY )
208+ if err != nil {
209+ t .Fatalf ("delete cache encryption key: %v" , err )
210+ }
211+ err = auth .DeleteAuthField (auth .CACHE_ENCRYPTION_KEY_AGE )
212+ if err != nil {
213+ t .Fatalf ("delete cache encryption key age: %v" , err )
214+ }
215+ }
216+
204217func TestWriteAndRead (t * testing.T ) {
218+ for _ , tt := range []struct {
219+ name string
220+ clearKeys bool
221+ }{
222+ {
223+ name : "normal" ,
224+ },
225+ {
226+ name : "fresh keys" ,
227+ clearKeys : true ,
228+ },
229+ } {
230+ t .Run (tt .name , func (t * testing.T ) {
231+ if tt .clearKeys {
232+ clearKeys (t )
233+ }
234+ if err := Init (); err != nil {
235+ t .Fatalf ("cache init failed: %s" , err )
236+ }
237+
238+ id := "test-cycle-" + uuid .NewString ()
239+ data := []byte ("test-data" )
240+ err := PutObject (id , data )
241+ if err != nil {
242+ t .Fatalf ("putobject failed: %v" , err )
243+ }
244+
245+ readData , err := GetObject (id )
246+ if err != nil {
247+ t .Fatalf ("getobject failed: %v" , err )
248+ }
249+
250+ diff := cmp .Diff (data , readData )
251+ if diff != "" {
252+ t .Fatalf ("unexpected data diff: %v" , diff )
253+ }
254+ })
255+ }
256+ }
257+
258+ func TestCacheCleanup (t * testing.T ) {
205259 if err := Init (); err != nil {
206260 t .Fatalf ("cache init failed: %s" , err )
207261 }
@@ -213,13 +267,15 @@ func TestWriteAndRead(t *testing.T) {
213267 t .Fatalf ("putobject failed: %v" , err )
214268 }
215269
216- readData , err := GetObject (id )
217- if err != nil {
218- t .Fatalf ("getobject failed: %v" , err )
270+ clearKeys (t )
271+
272+ // initialize again to trigger cache cleanup
273+ if err := Init (); err != nil {
274+ t .Fatalf ("cache init failed: %s" , err )
219275 }
220276
221- diff := cmp . Diff ( data , readData )
222- if diff != "" {
223- t .Fatalf ("unexpected data diff : %v" , diff )
277+ _ , err = GetObject ( id )
278+ if ! errors . Is ( err , os . ErrNotExist ) {
279+ t .Fatalf ("getobject failed with unexpected error : %v" , err )
224280 }
225281}
0 commit comments