Skip to content

Commit e1021fb

Browse files
committed
rewriting unit tests
Signed-off-by: Joe Lanford <[email protected]>
1 parent f2e7a9e commit e1021fb

File tree

9 files changed

+988
-842
lines changed

9 files changed

+988
-842
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ vendor/
2828

2929
# editor and IDE paraphernalia
3030
.idea/
31+
.run/
3132
*.swp
3233
*.swo
3334
*~

catalogd/internal/controllers/core/clustercatalog_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ func TestPollingRequeue(t *testing.T) {
813813
}
814814
require.NoError(t, reconciler.setupFinalizers())
815815
res, _ := reconciler.reconcile(context.Background(), tc.catalog)
816-
assert.InDelta(t, tc.expectedRequeueAfter, res.RequeueAfter, requeueJitterMaxFactor*float64(tc.expectedRequeueAfter))
816+
assert.InDelta(t, tc.expectedRequeueAfter, res.RequeueAfter, 2*requeueJitterMaxFactor*float64(tc.expectedRequeueAfter))
817817
})
818818
}
819819
}

internal/util/image/cache.go

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,40 @@ const ConfigDirLabel = "operators.operatorframework.io.index.configs.v1"
3939

4040
func CatalogCache(basePath string) Cache {
4141
return &diskCache{
42-
basePath: basePath,
43-
filterFunc: func(ctx context.Context, srcRef reference.Named, image ocispecv1.Image) (archive.Filter, error) {
44-
_, specIsCanonical := srcRef.(reference.Canonical)
45-
dirToUnpack, ok := image.Config.Labels[ConfigDirLabel]
46-
if !ok {
47-
// If the spec is a tagged ref, retries could end up resolving a new digest, where the label
48-
// might show up. If the spec is canonical, no amount of retries will make the label appear.
49-
// Therefore, we treat the error as terminal if the reference from the spec is canonical.
50-
return nil, errorutil.WrapTerminal(fmt.Errorf("catalog image is missing the required label %q", ConfigDirLabel), specIsCanonical)
51-
}
42+
basePath: basePath,
43+
filterFunc: filterForCatalogImage(),
44+
}
45+
}
5246

53-
return allFilters(
54-
onlyPath(dirToUnpack),
55-
forceOwnershipRWX(),
56-
), nil
57-
},
47+
func filterForCatalogImage() func(ctx context.Context, srcRef reference.Named, image ocispecv1.Image) (archive.Filter, error) {
48+
return func(ctx context.Context, srcRef reference.Named, image ocispecv1.Image) (archive.Filter, error) {
49+
_, specIsCanonical := srcRef.(reference.Canonical)
50+
51+
dirToUnpack, ok := image.Config.Labels[ConfigDirLabel]
52+
if !ok {
53+
// If the spec is a tagged keep, retries could end up resolving a new digest, where the label
54+
// might show up. If the spec is canonical, no amount of retries will make the label appear.
55+
// Therefore, we treat the error as terminal if the reference from the spec is canonical.
56+
return nil, errorutil.WrapTerminal(fmt.Errorf("catalog image is missing the required label %q", ConfigDirLabel), specIsCanonical)
57+
}
58+
59+
return allFilters(
60+
onlyPath(dirToUnpack),
61+
forceOwnershipRWX(),
62+
), nil
5863
}
5964
}
6065

6166
func BundleCache(basePath string) Cache {
6267
return &diskCache{
63-
basePath: basePath,
64-
filterFunc: func(_ context.Context, _ reference.Named, _ ocispecv1.Image) (archive.Filter, error) {
65-
return forceOwnershipRWX(), nil
66-
},
68+
basePath: basePath,
69+
filterFunc: filterForBundleImage(),
70+
}
71+
}
72+
73+
func filterForBundleImage() func(ctx context.Context, srcRef reference.Named, image ocispecv1.Image) (archive.Filter, error) {
74+
return func(ctx context.Context, srcRef reference.Named, image ocispecv1.Image) (archive.Filter, error) {
75+
return forceOwnershipRWX(), nil
6776
}
6877
}
6978

@@ -111,26 +120,30 @@ func (a *diskCache) Store(ctx context.Context, ownerID string, srcRef reference.
111120
if err := fsutil.EnsureEmptyDirectory(dest, 0700); err != nil {
112121
return nil, time.Time{}, fmt.Errorf("error ensuring empty unpack directory: %w", err)
113122
}
114-
modTime, err := func() (time.Time, error) {
123+
124+
if err := func() error {
115125
l := log.FromContext(ctx)
116126
l.Info("unpacking image", "path", dest)
117127
for layer := range layers {
118128
if layer.Err != nil {
119-
return time.Time{}, fmt.Errorf("error reading layer[%d]: %w", layer.Index, layer.Err)
129+
return fmt.Errorf("error reading layer[%d]: %w", layer.Index, layer.Err)
120130
}
121131
if _, err := archive.Apply(ctx, dest, layer.Reader, applyOpts...); err != nil {
122-
return time.Time{}, fmt.Errorf("error applying layer[%d]: %w", layer.Index, err)
132+
return fmt.Errorf("error applying layer[%d]: %w", layer.Index, err)
123133
}
124134
l.Info("applied layer", "layer", layer.Index)
125135
}
126136
if err := fsutil.SetReadOnlyRecursive(dest); err != nil {
127-
return time.Time{}, fmt.Errorf("error making unpack directory read-only: %w", err)
137+
return fmt.Errorf("error making unpack directory read-only: %w", err)
128138
}
129-
return fsutil.GetDirectoryModTime(dest)
130-
}()
131-
if err != nil {
139+
return nil
140+
}(); err != nil {
132141
return nil, time.Time{}, errors.Join(err, fsutil.DeleteReadOnlyRecursive(dest))
133142
}
143+
modTime, err := fsutil.GetDirectoryModTime(dest)
144+
if err != nil {
145+
return nil, time.Time{}, fmt.Errorf("error getting mod time of unpack directory: %w", err)
146+
}
134147
return os.DirFS(dest), modTime, nil
135148
}
136149

@@ -142,17 +155,31 @@ func (a *diskCache) GarbageCollect(_ context.Context, ownerID string, keep refer
142155
ownerIDPath := a.ownerIDPath(ownerID)
143156
dirEntries, err := os.ReadDir(ownerIDPath)
144157
if err != nil {
145-
return fmt.Errorf("error reading image directories: %w", err)
158+
if os.IsNotExist(err) {
159+
return nil
160+
}
161+
return fmt.Errorf("error reading owner directory: %w", err)
146162
}
147163

164+
foundKeep := false
148165
dirEntries = slices.DeleteFunc(dirEntries, func(entry os.DirEntry) bool {
149-
return entry.Name() == keep.Digest().String()
166+
found := entry.Name() == keep.Digest().String()
167+
if found {
168+
foundKeep = true
169+
}
170+
return found
150171
})
151172

152173
for _, dirEntry := range dirEntries {
153174
if err := fsutil.DeleteReadOnlyRecursive(filepath.Join(ownerIDPath, dirEntry.Name())); err != nil {
154175
return fmt.Errorf("error removing entry %s: %w", dirEntry.Name(), err)
155176
}
156177
}
178+
179+
if !foundKeep {
180+
if err := fsutil.DeleteReadOnlyRecursive(ownerIDPath); err != nil {
181+
return fmt.Errorf("error deleting unused owner data: %w", err)
182+
}
183+
}
157184
return nil
158185
}

0 commit comments

Comments
 (0)