Skip to content

Commit f3b2df7

Browse files
Assert based on layer shas
1 parent e766ad9 commit f3b2df7

File tree

1 file changed

+32
-54
lines changed

1 file changed

+32
-54
lines changed

src/test/cli.build.test.ts

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -65,60 +65,38 @@ describe('Dev Containers CLI', function () {
6565
});
6666
});
6767

68-
describe('caching', () => {
69-
let buildCommandRerunLog = '';
70-
let buildWithoutCacheLog = '';
71-
this.beforeEach(() => {
72-
buildCommandRerunLog = `${os.tmpdir()}/${crypto.randomUUID()}`;
73-
buildWithoutCacheLog = `${os.tmpdir()}/${crypto.randomUUID()}`;
74-
console.log(`rerun log: ${buildCommandRerunLog}`);
75-
console.log(`no cache log: ${buildWithoutCacheLog}`);
76-
});
77-
78-
this.afterEach(() => {
79-
for (const file in [buildCommandRerunLog, buildWithoutCacheLog]) {
80-
if (fs.existsSync(file)) {
81-
// fs.unlinkSync(file);
82-
}
83-
}
84-
});
85-
86-
it('should not use docker cache for features when `--no-cache` flag is passed', async () => {
87-
// Arrange
88-
const testFolder = `${__dirname}/configs/image-with-features`;
89-
const buildCommand = `${cli} build --workspace-folder ${testFolder}`;
90-
const buildWithoutCacheCommand = `${buildCommand} --no-cache`;
91-
const expectedFeatures = [
92-
'ghcr.io/devcontainers/feature-starter/hello',
93-
'ghcr.io/devcontainers/features/docker-in-docker'
94-
];
95-
96-
// Act
97-
await shellExec(`${buildCommand}`); // initial run of command
98-
await shellExec(`${buildCommand} > ${buildCommandRerunLog} 2>&1`); // rerun command using cache
99-
await shellExec(`${buildWithoutCacheCommand} > ${buildWithoutCacheLog} 2>&1`); // rerun command without cache
100-
101-
// Assert
102-
const buildCommandRerunLogContents = fs.readFileSync(buildCommandRerunLog, 'utf8');
103-
const buildWithoutCacheCommandLogContents = fs.readFileSync(buildWithoutCacheLog, 'utf8');
104-
105-
for (const logContent in [buildCommandRerunLogContents, buildWithoutCacheCommandLogContents]) {
106-
assert.notEqual(logContent, null);
107-
assert.notEqual(logContent, undefined);
108-
assert.notEqual(logContent, '');
109-
}
110-
111-
function countInstances(subject: string, search: string) {
112-
return subject.split(search).length - 1;
113-
}
114-
115-
for (const expectedFeature of expectedFeatures) {
116-
const featureNameInstanceCountInRerunOfBuildCommandLogs = countInstances(buildCommandRerunLogContents, expectedFeature);
117-
const featureNameInstanceCountInBuildWithNoCache = countInstances(buildWithoutCacheCommandLogContents, expectedFeature);
118-
assert.equal(featureNameInstanceCountInRerunOfBuildCommandLogs, 1, 'because a cached build prints features being installed in the image once at the start of the build when they are being resolved');
119-
assert.equal(featureNameInstanceCountInBuildWithNoCache, 2, 'because a non-cached build prints the features being installed in the image twice. Once at the start of the build when resolving the image, then again when installing the features in the image.');
120-
}
121-
});
68+
it.only('should not use docker cache for features when `--no-cache` flag is passed', async () => {
69+
// Arrange
70+
const testFolder = `${__dirname}/configs/image-with-features`;
71+
const originalImageName = 'feature-cache-test-original-image';
72+
const cachedImageName = 'feature-cache-test-rerun-image';
73+
const nonCachedImageName = 'feature-cache-test-no-cache-image';
74+
75+
const commandBase = `${cli} build --workspace-folder ${testFolder}`;
76+
const buildCommand = `${commandBase} --image-name ${originalImageName}`;
77+
const cachedBuildCommand = `${commandBase} --image-name ${cachedImageName}`;
78+
const buildWithoutCacheCommand = `${commandBase} --image-name ${nonCachedImageName} --no-cache`;
79+
80+
// Act
81+
await shellExec(buildCommand); // initial run of command
82+
await shellExec(cachedBuildCommand); // rerun command using cache
83+
await shellExec(buildWithoutCacheCommand); // rerun command without cache
84+
85+
// Assert
86+
const originalImageInspectCommandResult = await shellExec(`docker inspect ${originalImageName}`);
87+
const cachedImageInspectCommandResult = await shellExec(`docker inspect ${cachedImageName}`);
88+
const noCacheImageInspectCommandResult = await shellExec(`docker inspect ${nonCachedImageName}`);
89+
90+
const originalImageDetails = JSON.parse(originalImageInspectCommandResult.stdout);
91+
const cachedImageDetails = JSON.parse(cachedImageInspectCommandResult.stdout);
92+
const noCacheImageDetails = JSON.parse(noCacheImageInspectCommandResult.stdout);
93+
94+
const originalImageLayers: string[] = originalImageDetails[0].RootFS.Layers;
95+
const cachedImageLayers: string[] = cachedImageDetails[0].RootFS.Layers;
96+
const nonCachedImageLayers: string[] = noCacheImageDetails[0].RootFS.Layers;
97+
98+
assert.deepEqual(originalImageLayers, cachedImageLayers);
99+
assert.notDeepEqual(cachedImageLayers, nonCachedImageLayers);
122100
});
123101

124102
it('should fail with "not found" error when config is not found', async () => {

0 commit comments

Comments
 (0)