func TestUpdateOne(t *testing.T) {
	db := NewInMemDatabase()

	instance := new(types.VirtualMachine)
	instance.SetUuid(uuid.New())
	instance.SetName("instance")
	assert.NoError(t, db.Put(instance, nil, UIDList{}))

	vmi1 := new(types.VirtualMachineInterface)
	vmi1.SetUuid(uuid.New())
	vmi1.SetName("port1")
	assert.NoError(t, db.Put(vmi1, nil, GetReferenceList(vmi1)))

	vmi1.AddVirtualMachine(instance)
	assert.NoError(t, db.Update(vmi1, GetReferenceList(vmi1)))

	result, err := db.GetBackReferences(parseUID(instance.GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Contains(t, result, parseUID(vmi1.GetUuid()))

	vmi1.ClearVirtualMachine()
	assert.NoError(t, db.Update(vmi1, GetReferenceList(vmi1)))
	result, err = db.GetBackReferences(parseUID(instance.GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Len(t, result, 0)
}
func (m *InstanceManager) LocateInstance(tenant, podName, uid string) *types.VirtualMachine {
	obj, err := m.client.FindByUuid("virtual-machine", string(uid))
	if err == nil {
		return obj.(*types.VirtualMachine)
	}

	instance := new(types.VirtualMachine)
	instance.SetName(podName)
	instance.SetUuid(uid)
	err = m.client.Create(instance)
	if err != nil {
		glog.Errorf("Create %s: %v", podName, err)
		return nil
	}
	return instance
}
func TestUpdateRefs(t *testing.T) {
	db := NewInMemDatabase()

	var instances [8]*types.VirtualMachine
	for i := 0; i < 8; i++ {
		instance := new(types.VirtualMachine)
		instance.SetUuid(uuid.New())
		instance.SetName(fmt.Sprintf("instance-%d", i))
		assert.NoError(t, db.Put(instance, nil, UIDList{}))
		instances[i] = instance
	}

	vmi1 := new(types.VirtualMachineInterface)
	vmi1.SetUuid(uuid.New())
	vmi1.SetName("port1")
	vmi1.AddVirtualMachine(instances[0])
	vmi1.AddVirtualMachine(instances[1])
	vmi1.AddVirtualMachine(instances[2])
	assert.NoError(t, db.Put(vmi1, nil, GetReferenceList(vmi1)))

	vmi2 := new(types.VirtualMachineInterface)
	vmi2.SetUuid(uuid.New())
	vmi2.SetName("port2")
	vmi2.AddVirtualMachine(instances[2])
	vmi2.AddVirtualMachine(instances[4])
	vmi2.AddVirtualMachine(instances[3])
	assert.NoError(t, db.Put(vmi2, nil, GetReferenceList(vmi2)))

	r2, err := db.GetBackReferences(parseUID(instances[2].GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Len(t, r2, 2)
	assert.Contains(t, r2, parseUID(vmi1.GetUuid()))
	assert.Contains(t, r2, parseUID(vmi2.GetUuid()))

	r3, err := db.GetBackReferences(parseUID(instances[3].GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Len(t, r3, 1)

	vmi2.ClearVirtualMachine()
	vmi2.AddVirtualMachine(instances[4])
	vmi2.AddVirtualMachine(instances[6])
	assert.NoError(t, db.Update(vmi2, GetReferenceList(vmi2)))

	r2, err = db.GetBackReferences(parseUID(instances[2].GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Len(t, r2, 1)
	assert.Contains(t, r2, parseUID(vmi1.GetUuid()))

	r3, err = db.GetBackReferences(parseUID(instances[3].GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Len(t, r3, 0)

	r4, err := db.GetBackReferences(parseUID(instances[4].GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Len(t, r4, 1)
	assert.Contains(t, r4, parseUID(vmi2.GetUuid()))

	vmi1.ClearVirtualMachine()
	vmi1.AddVirtualMachine(instances[0])
	vmi1.AddVirtualMachine(instances[1])
	vmi1.AddVirtualMachine(instances[5])
	vmi1.AddVirtualMachine(instances[4])
	assert.NoError(t, db.Update(vmi1, GetReferenceList(vmi1)))

	r4, err = db.GetBackReferences(parseUID(instances[4].GetUuid()), "virtual_machine_interface")
	assert.NoError(t, err)
	assert.Len(t, r4, 2)
	assert.Contains(t, r4, parseUID(vmi1.GetUuid()))
	assert.Contains(t, r4, parseUID(vmi2.GetUuid()))
}
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)
}