Last active
May 28, 2025 14:53
-
-
Save cywang117/19a48e621113228bdbcbd6d588bdac74 to your computer and use it in GitHub Desktop.
Test script for dockerode.NetworkCreateOptions type support
This file contains hidden or 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
// Run script with `npx tsx test.ts` | |
import Docker from 'dockerode'; | |
import test from 'node:test'; | |
import assert from 'assert/strict'; | |
const docker = new Docker(); | |
test('should support all network create options', async () => { | |
try { | |
// Create network with full set of options between CreateRequest & CreateOptions | |
const network = await docker.createNetwork({ | |
// CreateRequest | |
Name: 'test-network', | |
CheckDuplicate: true, | |
// AbortSignal support | |
abortSignal: new AbortController().signal, | |
// CreateOptions | |
Scope: 'local', | |
Driver: 'bridge', | |
EnableIPv4: true, | |
EnableIPv6: false, | |
IPAM: { | |
Driver: 'default', | |
Config: [ | |
{ | |
Subnet: '172.28.0.0/16', | |
IPRange: '172.28.5.0/24', | |
Gateway: '172.28.5.254', | |
AuxiliaryAddresses: { | |
'host1': '172.28.1.5', | |
'host2': '172.28.1.6', | |
'host3': '172.28.1.7', | |
}, | |
}, | |
], | |
Options: { | |
ipamFoo: 'bar', | |
ipamBaz: '0', | |
}, | |
}, | |
Internal: false, | |
Attachable: false, | |
Ingress: false, | |
ConfigOnly: false, | |
ConfigFrom: {}, | |
Options: { | |
foo: 'bar', | |
baz: '0', | |
}, | |
Labels: { | |
'io.dockerode.test': 'true', | |
}, | |
// Cast as any as dockerode doesn't support all options yet | |
} as any); | |
const i = await network.inspect(); | |
assert.equal(i.Name, 'test-network'); | |
assert.equal(i.Scope, 'local'); | |
assert.equal(i.Driver, 'bridge'); | |
assert.equal(i.EnableIPv4, true); | |
assert.equal(i.EnableIPv6, false); | |
assert.deepEqual(i.IPAM, { | |
Driver: 'default', | |
Config: [ | |
{ | |
Subnet: '172.28.0.0/16', | |
IPRange: '172.28.5.0/24', | |
Gateway: '172.28.5.254', | |
AuxiliaryAddresses: { | |
host1: '172.28.1.5', | |
host2: '172.28.1.6', | |
host3: '172.28.1.7', | |
}, | |
}, | |
], | |
Options: { | |
ipamFoo: 'bar', | |
ipamBaz: '0', | |
}, | |
}); | |
assert.equal(i.Internal, false); | |
assert.equal(i.Attachable, false); | |
assert.equal(i.Ingress, false); | |
assert.equal(i.ConfigOnly, false); | |
assert.deepEqual(i.ConfigFrom, { Network: '' }); | |
assert.deepEqual(i.Options, { | |
foo: 'bar', | |
baz: '0', | |
}); | |
assert.deepEqual(i.Labels, { | |
'io.dockerode.test': 'true', | |
}); | |
} finally { | |
await cleanup(); | |
} | |
}); | |
test('should create network with config-only network as source', async () => { | |
try { | |
// Create config-only network for use with ConfigFrom CreateOption | |
const configOnlyNet = await docker.createNetwork({ | |
Name: 'config-only', | |
ConfigOnly: true, | |
Options: { | |
'foo': 'bar', | |
}, | |
Labels: { | |
'io.dockerode.foo': 'bar', | |
}, | |
// Cast as any as dockerode doesn't support all options yet | |
} as any); | |
const i = await configOnlyNet.inspect(); | |
assert.equal(i.Name, 'config-only'); | |
assert.equal(i.ConfigOnly, true); | |
assert.equal(i.Options.foo, 'bar'); | |
assert.deepEqual(i.Labels, { | |
'io.dockerode.foo': 'bar', | |
}); | |
// Create network with config-only network as source | |
const configFromNet = await docker.createNetwork({ | |
Name: 'config-from', | |
EnableIPv4: true, | |
EnableIPv6: false, | |
ConfigFrom: { | |
Network: 'config-only', | |
}, | |
} as any); | |
const i2 = await configFromNet.inspect(); | |
assert.equal(i2.Name, 'config-from'); | |
assert.equal(i2.ConfigFrom.Network, 'config-only'); | |
assert.equal(i2.ConfigOnly, false); | |
assert.equal(i2.Options.foo, 'bar'); | |
assert.deepEqual(i2.Labels, { | |
'io.dockerode.foo': 'bar', | |
}); | |
} finally { | |
await cleanup(); | |
} | |
}); | |
test('should support empty IPAM in create options', async () => { | |
try { | |
const network = await docker.createNetwork({ | |
Name: 'empty-ipam', | |
IPAM: {}, | |
} as any); | |
const { IPAM } = await network.inspect(); | |
assert.equal(IPAM.Driver, 'default'); | |
assert.equal(IPAM.Options, null); | |
assert.equal(IPAM.Config.length, 1); | |
// Docker/compose sets a default Subnet & Gateway if none are specified | |
assert.equal(IPAM.Config[0].Subnet.length > 0, true); | |
assert.equal(IPAM.Config[0].Gateway.length > 0, true); | |
assert.equal(IPAM.Config[0].IPRange, undefined); | |
assert.equal(IPAM.Config[0].AuxiliaryAddresses, undefined); | |
} finally { | |
await cleanup(); | |
} | |
}); | |
async function cleanup() { | |
console.debug('Cleaning up non-default networks'); | |
const networks = await docker.listNetworks(); | |
// Remove `ConfigOnly: false` networks first | |
networks.sort((a) => a.ConfigOnly ? 1 : -1); | |
for (const network of networks) { | |
// bridge, host, none are default networks | |
if (!['bridge', 'host', 'none'].includes(network.Name)) { | |
await docker.getNetwork(network.Id).remove(); | |
} | |
} | |
console.debug('Cleaned up non-default networks'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment