-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: removing only networks started by hedera instead of global prune (…
…#686) Signed-off-by: Mariusz Jasuwienas <[email protected]>
- Loading branch information
1 parent
4035713
commit c2a0a10
Showing
11 changed files
with
199 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*- | ||
* | ||
* Hedera Local Node | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import shell from 'shelljs'; | ||
import { IS_WINDOWS, NETWORK_PREFIX } from '../constants'; | ||
|
||
/** | ||
* Checks if the given string is a valid Docker network ID. | ||
* A valid Docker network ID is a 12-character hexadecimal string. | ||
* | ||
* @param {string} id - The string to be validated as a Docker network ID. | ||
* @returns {boolean} - Returns true if the string is a valid Docker network ID, false otherwise. | ||
* | ||
* @example | ||
* const id = "89ded1eca1d5"; | ||
* console.log(isCorrectDockerId(id)); // Output: true | ||
* | ||
* @example | ||
* const invalidId = "invalidID123"; | ||
* console.log(isCorrectDockerId(invalidId)); // Output: false | ||
*/ | ||
const isCorrectDockerId = (id: string) => id.trim() !== '' && /^[a-f0-9]{12}$/.test(id); | ||
|
||
/** | ||
* Provides utility methods for safe networks removal. | ||
*/ | ||
export class SafeDockerNetworkRemover { | ||
/** | ||
* Removes all the networks started by docker compose. Only networks with the "hedera-" prefix will be affected. | ||
*/ | ||
public static removeAll() { | ||
const result = shell.exec(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`); | ||
if (!result || result.stderr !== '') { | ||
return; | ||
} | ||
result.stdout.split('\n').filter(isCorrectDockerId).forEach((id) => { | ||
shell.exec(`docker network rm ${id} -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/*- | ||
* | ||
* Hedera Local Node | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { SafeDockerNetworkRemover } from '../../../src/utils/SafeDockerNetworkRemover'; | ||
import { IS_WINDOWS, NETWORK_PREFIX } from '../../../src/constants'; | ||
import { getTestBed } from '../testBed'; | ||
import { load } from 'js-yaml'; | ||
import { readdirSync, readFileSync } from 'fs'; | ||
import { join } from "path"; | ||
|
||
describe('SafeDockerNetworkRemover', () => { | ||
let shellExecStub: sinon.SinonStub; | ||
|
||
before(() => { | ||
let { | ||
shellStubs | ||
} = getTestBed({ | ||
workDir: 'testDir', | ||
}); | ||
|
||
shellExecStub = shellStubs.shellExecStub; | ||
}); | ||
|
||
after(() => { | ||
shellExecStub.restore(); | ||
}); | ||
|
||
describe('removeByName', () => { | ||
it('should not proceed if docker network ls command fails', () => { | ||
shellExecStub.withArgs(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`).returns({ stderr: 'error' }); | ||
|
||
SafeDockerNetworkRemover.removeAll(); | ||
expect(shellExecStub.calledOnce).to.be.true; | ||
expect(shellExecStub.calledWith(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`)).to.be.true; | ||
}); | ||
|
||
it('should not proceed if docker network ls command returns no result', () => { | ||
shellExecStub.withArgs(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`).returns({ stdout: '', stderr: '' }); | ||
|
||
SafeDockerNetworkRemover.removeAll(); | ||
expect(shellExecStub.calledWith(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`)).to.be.true; | ||
}); | ||
|
||
it('should remove valid Docker network IDs', () => { | ||
shellExecStub.withArgs(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`).returns({ stdout: '89ded1eca1d5\ninvalidID123\n', stderr: '' }); | ||
shellExecStub.withArgs(`docker network rm 89ded1eca1d5 -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`).returns({}); | ||
|
||
SafeDockerNetworkRemover.removeAll(); | ||
expect(shellExecStub.calledWith(`docker network rm 89ded1eca1d5 -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`)).to.be.true; | ||
}); | ||
|
||
it('should not remove invalid Docker network IDs', () => { | ||
shellExecStub.withArgs(`docker network ls --filter name=${NETWORK_PREFIX} --format "{{.ID}}"`).returns({ stdout: 'invalidID123\nanotherInvalid\n', stderr: '' }); | ||
|
||
SafeDockerNetworkRemover.removeAll(); | ||
expect(shellExecStub.calledWith(`docker network rm invalidID123 -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`)).to.be.false; | ||
expect(shellExecStub.calledWith(`docker network rm anotherInvalid -f 2>${IS_WINDOWS ? 'null' : '/dev/null'}`)).to.be.false; | ||
}); | ||
}); | ||
|
||
describe('config check', () => { | ||
it('check if all of the networks from composer yaml files are listed in the NETWORK_NAMES const', () => { | ||
const relativePath = '../../..'; | ||
const files = readdirSync(join(__dirname, relativePath)).filter(name => /^docker-compose.*\.yml$/.test(name)); | ||
for (const file of files) { | ||
const data = readFileSync(join(__dirname, `${relativePath}/${file}`)); | ||
const config = load(data.toString()); | ||
for (const network of Object.values(config.networks || {}).map((network: any) => network.name.trim())) { | ||
expect(network.startsWith(NETWORK_PREFIX), `Network '${network}' does not start with the prefix '${NETWORK_PREFIX}'. It won't be removed by 'npm run stop'`).to.be.true; | ||
} | ||
} | ||
}); | ||
it('check if all services have a network set and all network names start with "hedera-"', () => { | ||
const relativePath = '../../..'; | ||
const files = readdirSync(join(__dirname, relativePath)).filter(name => /^docker-compose(?!.*(evm|multinode)\.yml$).*\.yml$/.test(name)); | ||
for (const file of files) { | ||
const data = readFileSync(join(__dirname, `${relativePath}/${file}`)); | ||
const config = load(data.toString()); | ||
const services = config.services || {}; | ||
for (const [serviceName, serviceConfig] of Object.entries(services)) { | ||
if (serviceConfig.extends || (serviceConfig.network_mode || '' === 'none')) { | ||
continue; // The child service might have inherited the network. There is no network in non-network mode. | ||
} | ||
const networks = serviceConfig.networks; | ||
expect(networks, `Service '${serviceName}' does not have a network set.`).to.exist; | ||
} | ||
} | ||
}); | ||
}); | ||
}); |