func (m *InstanceManager) LocateInterface( network *types.VirtualNetwork, instance *types.VirtualMachine) *types.VirtualMachineInterface { tenant := network.GetFQName()[len(network.GetFQName())-2] fqn := interfaceFQName(m.config.DefaultDomain, tenant, instance.GetName()) obj, err := m.client.FindByName( "virtual-machine-interface", strings.Join(fqn, ":")) if err == nil { nic := obj.(*types.VirtualMachineInterface) // TODO(prm): ensure network is as expected, else update. return nic } nic := new(types.VirtualMachineInterface) nic.SetFQName("project", fqn) nic.AddVirtualMachine(instance) if network != nil { nic.AddVirtualNetwork(network) } err = m.client.Create(nic) if err != nil { glog.Errorf("Create interface %s: %v", instance.GetName(), err) return nil } obj, err = m.client.FindByUuid(nic.GetType(), nic.GetUuid()) if err != nil { glog.Errorf("Get vmi %s: %v", nic.GetUuid(), err) return nil } return obj.(*types.VirtualMachineInterface) }
func TestReadChildren(t *testing.T) { client := new(ApiClient) client.Init() project := new(types.Project) project.SetFQName("domain", []string{"default-domain", "p1"}) assert.NoError(t, client.Create(project)) net := new(types.VirtualNetwork) net.SetFQName("project", []string{"default-domain", "p1", "n1"}) assert.NoError(t, client.Create(net)) vmi1 := new(types.VirtualMachineInterface) vmi1.SetFQName("project", []string{"default-domain", "p1", "port1"}) vmi1.AddVirtualNetwork(net) assert.NoError(t, client.Create(vmi1)) vmi2 := new(types.VirtualMachineInterface) vmi2.SetFQName("project", []string{"default-domain", "p1", "port2"}) vmi2.AddVirtualNetwork(net) assert.NoError(t, client.Create(vmi2)) refs, err := project.GetVirtualMachineInterfaces() assert.NoError(t, err) assert.Len(t, refs, 2) }
func TestConsistencyStaleInterface(t *testing.T) { client := createTestClient() podStore := new(mocks.Store) serviceStore := new(mocks.Store) checker := NewConsistencyChecker(client, NewConfig(), podStore, serviceStore, nil) kube := mocks.NewKubeClient() controller := NewTestController(kube, client, nil, nil) netnsProject := new(types.Project) netnsProject.SetFQName("domain", []string{"default-domain", "testns"}) client.Create(netnsProject) installPods(controller, &kube.PodInterface.Mock, &podStore.Mock, "testns", 3) shutdown := make(chan struct{}) go controller.Run(shutdown) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} time.Sleep(100 * time.Millisecond) serviceStore.On("List").Return([]interface{}{}) assert.True(t, checker.Check()) vmi := new(types.VirtualMachineInterface) vmi.SetFQName("project", []string{"default-domain", "testns", "pod03"}) assert.NoError(t, client.Create(vmi)) assert.False(t, checker.Check()) }
func TestUpdateClearVM(t *testing.T) { client := new(ApiClient) client.Init() project := new(types.Project) project.SetFQName("domain", []string{"default-domain", "tenant"}) assert.NoError(t, client.Create(project)) vm := new(types.VirtualMachine) vm.SetFQName("project", []string{"default-domain", "tenant", "instance"}) assert.NoError(t, client.Create(vm)) vmi := new(types.VirtualMachineInterface) vmi.SetFQName("project", []string{"default-domain", "tenant", "instance"}) vmi.AddVirtualMachine(vm) assert.NoError(t, client.Create(vmi)) vmi.ClearVirtualMachine() assert.NoError(t, client.Update(vmi)) assert.NoError(t, client.Delete(vm)) }
func TestReadRefs(t *testing.T) { client := new(ApiClient) client.Init() project := new(types.Project) project.SetFQName("domain", []string{"default-domain", "p1"}) assert.NoError(t, client.Create(project)) net := new(types.VirtualNetwork) net.SetFQName("project", []string{"default-domain", "p1", "n1"}) assert.NoError(t, client.Create(net)) vmi1 := new(types.VirtualMachineInterface) vmi1.SetFQName("project", []string{"default-domain", "p1", "port1"}) vmi1.AddVirtualNetwork(net) assert.NoError(t, client.Create(vmi1)) refs, err := vmi1.GetVirtualNetworkRefs() assert.NoError(t, err) assert.Len(t, refs, 1) assert.Equal(t, net.GetUuid(), refs[0].Uuid) }
func TestReadBackRefs(t *testing.T) { client := new(ApiClient) client.Init() project := new(types.Project) project.SetFQName("domain", []string{"default-domain", "p1"}) assert.NoError(t, client.Create(project)) net := new(types.VirtualNetwork) net.SetFQName("project", []string{"default-domain", "p1", "n1"}) props := new(types.VirtualNetworkType) props.ForwardingMode = "turbo" net.SetVirtualNetworkProperties(props) assert.NoError(t, client.Create(net)) vmi1 := new(types.VirtualMachineInterface) vmi1.SetFQName("project", []string{"default-domain", "p1", "port1"}) vmi1.AddVirtualNetwork(net) assert.NoError(t, client.Create(vmi1)) vmi2 := new(types.VirtualMachineInterface) vmi2.SetFQName("project", []string{"default-domain", "p1", "port2"}) vmi2.AddVirtualNetwork(net) assert.NoError(t, client.Create(vmi2)) refs, err := net.GetVirtualMachineInterfaceBackRefs() assert.NoError(t, err) assert.Len(t, refs, 2) idList := make([]string, len(refs)) for i, ref := range refs { idList[i] = ref.Uuid } assert.Contains(t, idList, vmi1.GetUuid()) assert.Contains(t, idList, vmi2.GetUuid()) rProps := net.GetVirtualNetworkProperties() assert.Equal(t, "turbo", rProps.ForwardingMode) }
func TestConsistencyStaleInterface(t *testing.T) { env := new(TestFramework) env.SetUp("192.0.2.0/24") client := env.client netnsProject := new(types.Project) netnsProject.SetFQName("domain", []string{"default-domain", "testns"}) client.Create(netnsProject) env.Start() installPods(env, "testns", 3) env.SyncBarrier() env.Shutdown() assert.True(t, env.checker.Check()) vmi := new(types.VirtualMachineInterface) vmi.SetFQName("project", []string{"default-domain", "testns", "pod03"}) assert.NoError(t, client.Create(vmi)) assert.False(t, env.checker.Check()) }
func TestPodDelete(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() allocator := new(mocks.AddressAllocator) networkMgr := new(mocks.NetworkManager) controller := NewTestController(nil, client, allocator, networkMgr) pod := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "testnet", }, }, } netnsProject := new(types.Project) netnsProject.SetUuid(uuid.New()) netnsProject.SetFQName("", []string{"default-domain", "testns"}) assert.NoError(t, client.Create(netnsProject)) instance := new(types.VirtualMachine) fqn := []string{"default-domain", "testns", "test"} instance.SetFQName("project", fqn) instance.SetUuid(string(pod.ObjectMeta.UID)) assert.NoError(t, client.Create(instance)) vmi := new(types.VirtualMachineInterface) vmi.SetFQName("project", fqn) vmi.AddVirtualMachine(instance) assert.NoError(t, client.Create(vmi)) ipObj := new(types.InstanceIp) ipObj.SetName("testns_test") assert.NoError(t, client.Create(ipObj)) allocator.On("ReleaseIpAddress", string(pod.ObjectMeta.UID)).Return() networkMgr.On("ReleaseNetworkIfEmpty", "testns", "testnet").Return(true, nil) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.DeletePod(pod) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} if obj, err := client.FindByName("virtual-machine", strings.Join(fqn, ":")); err == nil { t.Errorf("virtual-machine object still present %s", obj.GetUuid()) } if obj, err := client.FindByUuid("virtual-machine-interface", vmi.GetUuid()); err == nil { t.Errorf("virtual-machine-interface object still present %s", obj.GetUuid()) } if obj, err := client.FindByUuid("instance-ip", ipObj.GetUuid()); err == nil { t.Errorf("instance-ip object still present %s", obj.GetUuid()) } allocator.AssertExpectations(t) }