Skip to content

Commit 4c99a9f

Browse files
authored
fix: add extension to cached image (#350)
1 parent e7ca3b6 commit 4c99a9f

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

src/cache.spec.ts

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,55 @@
1818

1919
import { resolve } from 'node:path';
2020

21+
import { env } from '@podman-desktop/api';
2122
import { vol } from 'memfs';
2223
import { beforeEach, expect, test, vi } from 'vitest';
2324

2425
import { ImageCache } from './cache';
2526

2627
vi.mock('node:fs');
2728
vi.mock('node:fs/promises');
29+
vi.mock('@podman-desktop/api');
2830

2931
beforeEach(() => {
32+
vi.resetAllMocks();
3033
vol.reset();
34+
vi.mocked(env).isWindows = false;
35+
vi.mocked(env).isMac = false;
36+
vi.mocked(env).isLinux = false;
3137
});
3238

33-
test('images/image and images/rhel9_5 are deleted during init if they exist, rhel9_§ is not deleted', async () => {
39+
test('images/image and images/rhel9_5 are deleted during init if they exist, rhel9_6 is not deleted, on Windows', async () => {
40+
vi.mocked(env).isWindows = true;
3441
const cachePath = resolve('/', 'path', 'to', 'cache');
3542
const cache = new ImageCache(cachePath);
3643
vol.fromJSON({
3744
'/path/to/cache/images/image': '',
38-
'/path/to/cache/images/rhel9_5': '',
39-
'/path/to/cache/images/rhel9_6': '',
45+
'/path/to/cache/images/rhel9_5.tar.gz': '',
46+
'/path/to/cache/images/rhel9_6.tar.gz': '',
4047
});
4148

4249
await cache.init();
4350

4451
expect(vol.toJSON()).toEqual({
45-
'/path/to/cache/images/rhel9_6': '',
52+
'/path/to/cache/images/rhel9_6.tar.gz': '',
53+
});
54+
});
55+
56+
test('images/image and images/rhel9_5 are deleted during init if they exist, rhel9_6 is not deleted, on Mac', async () => {
57+
vi.mocked(env).isMac = true;
58+
const cachePath = resolve('/', 'path', 'to', 'cache');
59+
const cache = new ImageCache(cachePath);
60+
vol.fromJSON({
61+
'/path/to/cache/images/image': '',
62+
'/path/to/cache/images/rhel9_5.tar.gz': '',
63+
'/path/to/cache/images/rhel9_6.qcow2': '',
64+
});
65+
66+
await cache.init();
67+
68+
expect(vol.toJSON()).toEqual({
69+
'/path/to/cache/images/rhel9_6.qcow2': '',
4670
});
4771
});
4872

@@ -56,11 +80,28 @@ test('images directory is created', async () => {
5680
});
5781
});
5882

59-
test('getPath returns path for known image', async () => {
83+
test('getPath returns path for known image on Windows', async () => {
84+
vi.mocked(env).isWindows = true;
85+
const cache = new ImageCache(resolve('/', 'path', 'to', 'cache'));
86+
await cache.init();
87+
const path = cache.getPath('RHEL 10.0');
88+
expect(path).toBe(resolve('/', 'path', 'to', 'cache', 'images', 'rhel10_0.tar.gz'));
89+
});
90+
91+
test('getPath returns path for known image on Linux', async () => {
92+
vi.mocked(env).isMac = true;
93+
const cache = new ImageCache(resolve('/', 'path', 'to', 'cache'));
94+
await cache.init();
95+
const path = cache.getPath('RHEL 10.0');
96+
expect(path).toBe(resolve('/', 'path', 'to', 'cache', 'images', 'rhel10_0.qcow2'));
97+
});
98+
99+
test('getPath returns path for known image on Mac', async () => {
100+
vi.mocked(env).isMac = true;
60101
const cache = new ImageCache(resolve('/', 'path', 'to', 'cache'));
61102
await cache.init();
62103
const path = cache.getPath('RHEL 10.0');
63-
expect(path).toBe(resolve('/', 'path', 'to', 'cache', 'images', 'rhel10_0'));
104+
expect(path).toBe(resolve('/', 'path', 'to', 'cache', 'images', 'rhel10_0.qcow2'));
64105
});
65106

66107
test('getPath raises an exception for an unknown image', async () => {

src/cache.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
import { mkdir, readdir, rm } from 'node:fs/promises';
2020
import { resolve } from 'node:path';
2121

22+
import { env } from '@podman-desktop/api';
23+
2224
import { RHEL_VMS_IMAGE_PROPERTY_VALUE_RHEL_9, RHEL_VMS_IMAGE_PROPERTY_VALUE_RHEL_10 } from './constants';
2325

2426
export class ImageCache {
2527
#cachedImageDir: string;
2628

29+
#extension = env.isWindows ? '.tar.gz' : '.qcow2';
30+
2731
#cachedImageNames: Record<string, string> = {
28-
[RHEL_VMS_IMAGE_PROPERTY_VALUE_RHEL_10]: 'rhel10_0',
29-
[RHEL_VMS_IMAGE_PROPERTY_VALUE_RHEL_9]: 'rhel9_6',
32+
[RHEL_VMS_IMAGE_PROPERTY_VALUE_RHEL_10]: `rhel10_0${this.#extension}`,
33+
[RHEL_VMS_IMAGE_PROPERTY_VALUE_RHEL_9]: `rhel9_6${this.#extension}`,
3034
};
3135

3236
constructor(storagePath: string) {

0 commit comments

Comments
 (0)