コード例 #1
1
ファイル: port_test.go プロジェクト: swarm-hooks/swarm
func makeBinding(ip, port string) nat.PortMap {
	binding := nat.PortBinding{
		HostIP:   ip,
		HostPort: port,
	}
	bindingMap := map[nat.Port][]nat.PortBinding{nat.Port(fmt.Sprintf("%s/tcp", port)): {binding}}
	return nat.PortMap(bindingMap)
}
コード例 #2
0
ファイル: port_test.go プロジェクト: swarm-hooks/swarm
func TestPortFilterRandomAssignment(t *testing.T) {
	var (
		p     = PortFilter{}
		nodes = []*node.Node{
			{
				ID:   "node-0-id",
				Name: "node-0-name",
				Addr: "node-0",
			},
			{
				ID:   "node-1-id",
				Name: "node-1-name",
				Addr: "node-1",
			},
			{
				ID:   "node-2-id",
				Name: "node-2-name",
				Addr: "node-2",
			},
		}
		result []*node.Node
		err    error
	)

	// Simulate a container that requested to map 80 to a random port.
	// In this case, HostConfig.PortBindings should contain a binding with no
	// HostPort defined and NetworkSettings.Ports should contain the actual
	// mapped port.
	container := &cluster.Container{
		Container: types.Container{ID: "c1"},
		Info: types.ContainerJSON{
			ContainerJSONBase: &types.ContainerJSONBase{
				HostConfig: &containertypes.HostConfig{
					PortBindings: nat.PortMap(
						map[nat.Port][]nat.PortBinding{
							nat.Port("80/tcp"): {
								{
									HostIP:   "",
									HostPort: "",
								},
							},
						},
					),
				}}}}

	container.Info.NetworkSettings = &types.NetworkSettings{
		NetworkSettingsBase: types.NetworkSettingsBase{
			Ports: nat.PortMap(
				map[nat.Port][]nat.PortBinding{
					nat.Port("80/tcp"): {
						{
							HostIP:   "127.0.0.1",
							HostPort: "1234",
						},
					},
				},
			),
		}}

	assert.NoError(t, nodes[0].AddContainer(container))

	// Request port 80.
	config := &cluster.ContainerConfig{Config: containertypes.Config{}, HostConfig: containertypes.HostConfig{
		PortBindings: makeBinding("", "80"),
	}, NetworkingConfig: networktypes.NetworkingConfig{}}

	// Since port "80" has been mapped to "1234", we should be able to request "80".
	result, err = p.Filter(config, nodes, true)
	assert.NoError(t, err)
	assert.Equal(t, result, nodes)

	// However, we should not be able to request "1234" since it has been used for a random assignment.
	config = &cluster.ContainerConfig{Config: containertypes.Config{}, HostConfig: containertypes.HostConfig{
		PortBindings: makeBinding("", "1234"),
	}, NetworkingConfig: networktypes.NetworkingConfig{}}
	result, err = p.Filter(config, nodes, true)
	assert.NoError(t, err)
	assert.NotContains(t, result, nodes[0])
}