Ejemplo n.º 1
0
func StartAttachTether(t *testing.T, cfg *metadata.ExecutorConfig) (tether.Tether, extraconfig.DataSource, net.Conn) {
	store := map[string]string{}
	sink := extraconfig.MapSink(store)
	src := extraconfig.MapSource(store)
	extraconfig.Encode(sink, cfg)
	log.Debugf("Test configuration: %#v", sink)

	tthr := tether.New(src, sink, &Mocked)
	tthr.Register("mocker", &Mocked)
	tthr.Register("Attach", server)

	// run the tether to service the attach
	go func() {
		erR := tthr.Start()
		if erR != nil {
			t.Error(erR)
		}
	}()

	// create client on the mock pipe
	conn, err := mockBackChannel(context.Background())
	if err != nil && (err != io.EOF || server.(*testAttachServer).enabled) {
		// we accept the case where the error is end-of-file and the attach server is disabled because that's
		// expected when the tether is shut down.
		t.Error(err)
	}

	return tthr, src, conn
}
Ejemplo n.º 2
0
func TestToExtraConfig(t *testing.T) {
	exec := metadata.ExecutorConfig{
		Common: metadata.Common{
			ID:   "deadbeef",
			Name: "configtest",
		},
		Sessions: map[string]metadata.SessionConfig{
			"deadbeef": metadata.SessionConfig{
				Cmd: metadata.Cmd{
					Path: "/bin/bash",
					Args: []string{"/bin/bash", "-c", "echo hello"},
					Dir:  "/",
					Env:  []string{"HOME=/", "PATH=/bin"},
				},
			},
			"beefed": metadata.SessionConfig{
				Cmd: metadata.Cmd{
					Path: "/bin/bash",
					Args: []string{"/bin/bash", "-c", "echo goodbye"},
					Dir:  "/",
					Env:  []string{"HOME=/", "PATH=/bin"},
				},
			},
		},
		Networks: map[string]*metadata.NetworkEndpoint{
			"eth0": &metadata.NetworkEndpoint{
				Static: &net.IPNet{IP: localhost, Mask: lmask.Mask},
				Network: metadata.ContainerNetwork{
					Common: metadata.Common{
						Name: "notsure",
					},
					Gateway:     net.IPNet{IP: gateway, Mask: gmask.Mask},
					Nameservers: []net.IP{},
				},
			},
		},
	}

	// encode metadata package's ExecutorConfig
	encoded := map[string]string{}
	extraconfig.Encode(extraconfig.MapSink(encoded), exec)

	// decode into this package's ExecutorConfig
	var decoded ExecutorConfig
	extraconfig.Decode(extraconfig.MapSource(encoded), &decoded)

	// the networks should be identical
	assert.Equal(t, exec.Networks["eth0"], decoded.Networks["eth0"])

	// the source and destination structs are different - we're doing a sparse comparison
	expected := exec.Sessions["deadbeef"]
	actual := *decoded.Sessions["deadbeef"]

	assert.Equal(t, expected.Cmd.Path, actual.Cmd.Path)
	assert.Equal(t, expected.Cmd.Args, actual.Cmd.Args)
	assert.Equal(t, expected.Cmd.Dir, actual.Cmd.Dir)
	assert.Equal(t, expected.Cmd.Env, actual.Cmd.Env)
}
Ejemplo n.º 3
0
func testConfig() *Configuration {
	return &Configuration{
		source:     extraconfig.MapSource(map[string]string{}),
		sink:       extraconfig.MapSink(map[string]string{}),
		BridgeLink: &mockLink{},
		Network: config.Network{
			BridgeNetwork: "bridge",
			ContainerNetworks: map[string]*executor.ContainerNetwork{
				"bridge": {
					Common: executor.Common{
						Name: "bridge",
					},
					Type: constants.BridgeScopeType,
				},
				"bar7": {
					Common: executor.Common{
						Name: "external",
					},
					Gateway:     net.IPNet{IP: net.ParseIP("10.13.0.1"), Mask: net.CIDRMask(16, 32)},
					Nameservers: []net.IP{net.ParseIP("10.10.1.1")},
					Pools:       []ip.Range{*ip.ParseRange("10.13.1.0-255"), *ip.ParseRange("10.13.2.0-10.13.2.15")},
					Type:        constants.ExternalScopeType,
				},
				"bar71": {
					Common: executor.Common{
						Name: "external",
					},
					Gateway:     net.IPNet{IP: net.ParseIP("10.131.0.1"), Mask: net.CIDRMask(16, 32)},
					Nameservers: []net.IP{net.ParseIP("10.131.0.1"), net.ParseIP("10.131.0.2")},
					Pools:       []ip.Range{*ip.ParseRange("10.131.1.0/16")},
					Type:        constants.ExternalScopeType,
				},
				"bar72": {
					Common: executor.Common{
						Name: "external",
					},
					Type: constants.ExternalScopeType,
				},
				"bar73": {
					Common: executor.Common{
						Name: "external",
					},
					Gateway: net.IPNet{IP: net.ParseIP("10.133.0.1"), Mask: net.CIDRMask(16, 32)},
					Type:    constants.ExternalScopeType,
				},
			},
		},
		PortGroups: map[string]object.NetworkReference{
			"bridge": testBridgeNetwork,
			"bar7":   testExternalNetwork,
			"bar71":  testExternalNetwork,
			"bar72":  testExternalNetwork,
			"bar73":  testExternalNetwork,
		},
	}
}
Ejemplo n.º 4
0
// applianceConfiguration updates the configuration passed in with the latest from the appliance VM.
// there's no guarantee of consistency within the configuration at this time
func (d *Dispatcher) applianceConfiguration(conf *metadata.VirtualContainerHostConfigSpec) error {
	defer trace.End(trace.Begin(""))

	extraConfig, err := d.appliance.FetchExtraConfig(d.ctx)
	if err != nil {
		return err
	}

	extraconfig.Decode(extraconfig.MapSource(extraConfig), conf)
	return nil
}
Ejemplo n.º 5
0
// OptionValueSource is a convenience method to generate a MapSource source from
// and array of OptionValue's
func OptionValueSource(src []types.BaseOptionValue) extraconfig.DataSource {
	// create the key/value store from the extraconfig slice for lookups
	kv := make(map[string]string)
	for i := range src {
		k := src[i].GetOptionValue().Key
		v := src[i].GetOptionValue().Value.(string)
		if v == "<nil>" {
			v = ""
		}
		kv[k] = v
	}

	return extraconfig.MapSource(kv)
}
Ejemplo n.º 6
0
func RunTether(t *testing.T, cfg *metadata.ExecutorConfig) (Tether, extraconfig.DataSource, error) {
	store := map[string]string{}
	sink := extraconfig.MapSink(store)
	src := extraconfig.MapSource(store)
	extraconfig.Encode(sink, cfg)
	log.Debugf("Test configuration: %#v", sink)

	tthr := New(src, sink, &Mocked)
	tthr.Register("Mocker", &Mocked)

	// run the tether to service the attach
	erR := tthr.Start()

	return tthr, src, erR
}
Ejemplo n.º 7
0
func (d *Dispatcher) isVCH(vm *vm.VirtualMachine) (bool, error) {
	if vm == nil {
		return false, errors.New("nil parameter")
	}
	defer trace.End(trace.Begin(vm.InventoryPath))

	info, err := vm.FetchExtraConfig(d.ctx)
	if err != nil {
		err = errors.Errorf("Failed to fetch guest info of appliance vm: %s", err)
		return false, err
	}

	var remoteConf config.VirtualContainerHostConfigSpec
	extraconfig.Decode(extraconfig.MapSource(info), &remoteConf)

	// if the moref of the target matches where we expect to find it for a VCH, run with it
	if remoteConf.ExecutorConfig.ID == vm.Reference().String() {
		return true, nil
	}

	return false, nil
}
Ejemplo n.º 8
0
func (d *Dispatcher) GetVCHConfig(vm *vm.VirtualMachine) (*metadata.VirtualContainerHostConfigSpec, error) {
	defer trace.End(trace.Begin(""))

	//this is the appliance vm
	mapConfig, err := vm.FetchExtraConfig(d.ctx)
	if err != nil {
		err = errors.Errorf("Failed to get VM extra config of %s, %s", vm.Reference(), err)
		log.Errorf("%s", err)
		return nil, err
	}
	data := extraconfig.MapSource(mapConfig)
	vchConfig := &metadata.VirtualContainerHostConfigSpec{}
	result := extraconfig.Decode(data, vchConfig)
	if result == nil {
		err = errors.Errorf("Failed to decode VM configuration %s, %s", vm.Reference(), err)
		log.Errorf("%s", err)
		return nil, err
	}

	//	vchConfig.ID
	return vchConfig, nil
}
Ejemplo n.º 9
0
func TestToExtraConfig(t *testing.T) {
	exec := executor.ExecutorConfig{
		Common: executor.Common{
			ID:   "deadbeef",
			Name: "configtest",
		},
		Sessions: map[string]*executor.SessionConfig{
			"deadbeef": &executor.SessionConfig{
				Cmd: executor.Cmd{
					Path: "/bin/bash",
					Args: []string{"/bin/bash", "-c", "echo hello"},
					Dir:  "/",
					Env:  []string{"HOME=/", "PATH=/bin"},
				},
			},
			"beefed": &executor.SessionConfig{
				Cmd: executor.Cmd{
					Path: "/bin/bash",
					Args: []string{"/bin/bash", "-c", "echo goodbye"},
					Dir:  "/",
					Env:  []string{"HOME=/", "PATH=/bin"},
				},
			},
		},
		Networks: map[string]*executor.NetworkEndpoint{
			"eth0": &executor.NetworkEndpoint{
				Static: true,
				IP:     &net.IPNet{IP: localhost, Mask: lmask.Mask},
				Network: executor.ContainerNetwork{
					Common: executor.Common{
						Name: "notsure",
					},
					Gateway:     net.IPNet{IP: gateway, Mask: gmask.Mask},
					Nameservers: []net.IP{},
					Pools:       []ip.Range{},
					Aliases:     []string{},
				},
			},
		},
	}

	// encode exec package's ExecutorConfig
	encoded := map[string]string{}
	extraconfig.Encode(extraconfig.MapSink(encoded), exec)

	// decode into this package's ExecutorConfig
	var decoded ExecutorConfig
	extraconfig.Decode(extraconfig.MapSource(encoded), &decoded)

	// the source and destination structs are different - we're doing a sparse comparison
	expectedNet := exec.Networks["eth0"]
	actualNet := decoded.Networks["eth0"]

	assert.Equal(t, expectedNet.Common, actualNet.Common)
	assert.Equal(t, expectedNet.Static, actualNet.Static)
	assert.Equal(t, expectedNet.Assigned, actualNet.Assigned)
	assert.Equal(t, expectedNet.Network, actualNet.Network)

	expectedSession := exec.Sessions["deadbeef"]
	actualSession := decoded.Sessions["deadbeef"]

	assert.Equal(t, expectedSession.Cmd.Path, actualSession.Cmd.Path)
	assert.Equal(t, expectedSession.Cmd.Args, actualSession.Cmd.Args)
	assert.Equal(t, expectedSession.Cmd.Dir, actualSession.Cmd.Dir)
	assert.Equal(t, expectedSession.Cmd.Env, actualSession.Cmd.Env)
}