func TestNamespaceDelete(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() allocator := new(mocks.AddressAllocator) networkMgr := new(mocks.NetworkManager) controller := NewTestController(nil, client, allocator, networkMgr) namespace := &api.Namespace{ ObjectMeta: api.ObjectMeta{ Name: "netns", UID: kubetypes.UID(uuid.New()), }, } project := new(types.Project) project.SetFQName("domain", []string{DefaultDomain, "netns"}) project.SetUuid(string(namespace.ObjectMeta.UID)) client.Create(project) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.DeleteNamespace(namespace) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} _, err := client.FindByUuid("project", string(namespace.ObjectMeta.UID)) assert.NotNil(t, err) }
// The Public network subnet configuration may change. func TestPublicNetworkSubnetChange(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() config := new(Config) config.PublicNetwork = "default-domain:default-project:Public" config.PublicSubnet = "192.0.2.0/24" netman := NewNetworkManager(client, config) // No public IP addresses are assigned: expect the public network to // have a single subnet. config.PublicSubnet = "198.51.100.0/24" netman = NewNetworkManager(client, config) network := netman.GetPublicNetwork() refs, err := network.GetNetworkIpamRefs() require.NoError(t, err) assert.Len(t, refs, 1) if len(refs) > 0 { attr := refs[0].Attr.(types.VnSubnetsType) assert.Len(t, attr.IpamSubnets, 1) if len(attr.IpamSubnets) > 0 { prefix := fmt.Sprintf("%s/%d", attr.IpamSubnets[0].Subnet.IpPrefix, attr.IpamSubnets[0].Subnet.IpPrefixLen) assert.Equal(t, config.PublicSubnet, prefix) } } }
func TestNamespaceAdd(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() allocator := new(mocks.AddressAllocator) networkMgr := new(mocks.NetworkManager) controller := NewTestController(nil, client, allocator, networkMgr) namespace := &api.Namespace{ ObjectMeta: api.ObjectMeta{ Name: "netns", UID: kubetypes.UID(uuid.New()), }, } shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddNamespace(namespace) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} obj, err := client.FindByUuid("project", string(namespace.ObjectMeta.UID)) if err != nil { t.Fatalf("Namespace %s: Not found", string(namespace.ObjectMeta.UID)) } assert.Equal(t, namespace.Name, obj.GetName()) }
func TestPodUsesNonExistingService(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) allocator := new(mocks.AddressAllocator) controller := NewTestController(kube, client, allocator, nil) pod1 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz1", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "testpod", "uses": "nonexisting", }, }, } netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) allocator.On("LocateIpAddress", string(pod1.ObjectMeta.UID)).Return("10.0.0.1", nil) allocator.On("ReleaseIpAddress", string(pod1.ObjectMeta.UID)).Return() kube.PodInterface.On("Update", pod1).Return(pod1, nil) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddPod(pod1) time.Sleep(100 * time.Millisecond) _, err := types.NetworkPolicyByName(client, "default-domain:testns:nonexisting") assert.NoError(t, err) _, err = types.VirtualNetworkByName(client, "default-domain:testns:testpod") assert.NoError(t, err) controller.DeletePod(pod1) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} _, err = types.VirtualNetworkByName(client, "default-domain:testns:testpod") assert.Error(t, err) _, err = types.NetworkPolicyByName(client, "default-domain:testns:nonexisting") assert.Error(t, err) allocator.AssertExpectations(t) }
func createTestClient() contrail.ApiClient { client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) client.AddInterceptor("instance-ip", &IpInterceptor{}) client.AddInterceptor("floating-ip", &FloatingIpInterceptor{}) return client }
func TestPodCreate(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) allocator := new(mocks.AddressAllocator) networkMgr := new(mocks.NetworkManager) controller := NewTestController(kube, 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"}) client.Create(netnsProject) testnet := new(types.VirtualNetwork) testnet.SetFQName("project", []string{"default-domain", "testns", "testnet"}) client.Create(testnet) allocator.On("LocateIpAddress", string(pod.ObjectMeta.UID)).Return("10.0.0.42", nil) networkMgr.On("LocateNetwork", "testns", "testnet", controller.config.PrivateSubnet).Return(testnet, nil) networkMgr.On("GetGatewayAddress", testnet).Return("10.0.255.254", nil) kube.PodInterface.On("Update", pod).Return(pod, nil) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddPod(pod) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} kube.PodInterface.AssertExpectations(t) }
func TestAllocator(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("instance-ip", &IpInterceptor{}) allocator := NewAddressAllocator(client, NewConfig()) id := uuid.New() addr, err := allocator.LocateIpAddress(id) assert.NoError(t, err) assert.Equal(t, "10.254.0.1", addr) ipObj, err := types.InstanceIpByName(client, id) assert.NoError(t, err) glog.Infof(ipObj.GetInstanceIpAddress()) allocator.ReleaseIpAddress(id) _, err = types.InstanceIpByName(client, id) assert.Error(t, err) }
func TestNetworkLocate(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() config := new(Config) config.PublicNetwork = "default-domain:default-project:Public" netman := NewNetworkManager(client, config) project := new(types.Project) project.SetUuid(uuid.New()) project.SetFQName("", []string{config.DefaultDomain, "p1"}) client.Create(project) network, err := netman.LocateNetwork("p1", "n1", "10.0.1.0/24") assert.NoError(t, err, "LocateNetwork") n2, err := netman.LocateNetwork("p1", "n1", "10.0.1.0/24") assert.NoError(t, err, "LocateNetwork -- exists") assert.Equal(t, network, n2) }
// When the subnet is in use it can't be deleted. func TestPublicNetworkSubnetChangeWhenInUse(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() config := new(Config) config.PublicNetwork = "default-domain:default-project:Public" config.PublicSubnet = "192.0.2.0/24" netman := NewNetworkManager(client, config) _, err := netman.LocateFloatingIp(netman.GetPublicNetwork(), "test", "192.0.2.1") require.NoError(t, err) config.PublicSubnet = "198.51.100.0/24" netman = NewNetworkManager(client, config) network := netman.GetPublicNetwork() refs, err := network.GetNetworkIpamRefs() require.NoError(t, err) assert.Len(t, refs, 1) if len(refs) > 0 { attr := refs[0].Attr.(types.VnSubnetsType) assert.Len(t, attr.IpamSubnets, 2) } netman.DeleteFloatingIp(network, "test") }
func TestServiceDeleteConnections(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() config := NewConfig() networkMgr := NewNetworkManager(client, config) serviceMgr := NewServiceManager(client, config, networkMgr) netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) globalProject := new(types.Project) globalProject.SetFQName("", []string{"default-domain", "global"}) client.Create(globalProject) network, err := networkMgr.LocateNetwork("testns", "client", config.PrivateSubnet) services := []string{ "testns/s1", "testns/s2", "global/s3", "global/s4", } for _, svc := range services { parts := strings.Split(svc, "/") namespace := parts[0] svcName := parts[1] err := serviceMgr.Create(namespace, svcName) if err != nil { t.Error(err) continue } err = serviceMgr.Connect(namespace, svcName, network) if err != nil { t.Error(err) } } serviceList := MakeServiceIdList() serviceList.Add("testns", "s1") serviceList.Add("global", "s3") purgeList := make([]string, 0) policyRefs, err := network.GetNetworkPolicyRefs() require.NoError(t, err) for _, ref := range policyRefs { require.Len(t, ref.To, 3) svcName, err := serviceNameFromPolicyName(ref.To[2]) require.NoError(t, err) if serviceList.Contains(ref.To[1], svcName) { continue } purgeList = append(purgeList, ref.Uuid) } err = serviceMgr.DeleteConnections(network, purgeList) if err != nil { t.Fatal(err) } refs, err := network.GetNetworkPolicyRefs() if err != nil { t.Fatal(err) } if len(refs) != len(serviceList) { t.Errorf("expected %d policy refs, got %d", len(serviceList), len(refs)) } actual := make([]string, 0) for _, ref := range refs { svc, err := serviceNameFromPolicyName(ref.To[2]) if err != nil { continue } actual = append(actual, ref.To[1]+"/"+svc) } assert.EqualValues(t, []string{"testns/s1", "global/s3"}, actual) }
func TestServiceWithMultipleUsers(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) allocator := new(mocks.AddressAllocator) controller := NewTestController(kube, client, allocator, nil) pod1 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-server", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "server", }, }, } pod2 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz1", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "client1", "uses": "x1", }, }, } pod3 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz2", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "client1", "uses": "x1", }, }, } pod4 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz3", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "client2", "uses": "x1", }, }, } service := &api.Service{ ObjectMeta: api.ObjectMeta{ Name: "s1", Namespace: "testns", Labels: map[string]string{ "name": "x1", }, }, Spec: api.ServiceSpec{ Selector: map[string]string{ "name": "server", }, ClusterIP: "10.254.42.42", }, } netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) allocator.On("LocateIpAddress", string(pod1.ObjectMeta.UID)).Return("10.0.0.1", nil) allocator.On("LocateIpAddress", string(pod2.ObjectMeta.UID)).Return("10.0.0.2", nil) allocator.On("LocateIpAddress", string(pod3.ObjectMeta.UID)).Return("10.0.0.3", nil) allocator.On("LocateIpAddress", string(pod4.ObjectMeta.UID)).Return("10.0.0.4", nil) allocator.On("ReleaseIpAddress", string(pod1.ObjectMeta.UID)).Return() allocator.On("ReleaseIpAddress", string(pod2.ObjectMeta.UID)).Return() allocator.On("ReleaseIpAddress", string(pod3.ObjectMeta.UID)).Return() allocator.On("ReleaseIpAddress", string(pod4.ObjectMeta.UID)).Return() kube.PodInterface.On("Update", pod1).Return(pod1, nil) kube.PodInterface.On("Update", pod2).Return(pod2, nil) kube.PodInterface.On("Update", pod3).Return(pod3, nil) kube.PodInterface.On("Update", pod4).Return(pod4, nil) kube.PodInterface.On("List", mock.Anything, mock.Anything).Return(&api.PodList{Items: []api.Pod{*pod1}}, nil) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddPod(pod1) controller.AddService(service) controller.AddPod(pod2) controller.AddPod(pod3) controller.AddPod(pod4) time.Sleep(100 * time.Millisecond) policy, err := types.NetworkPolicyByName(client, "default-domain:testns:x1") assert.NoError(t, err) if err == nil { assert.Len(t, policy.GetNetworkPolicyEntries().PolicyRule, 2) assert.True(t, policyHasRule(policy, "default-domain:testns:client1", "default-domain:testns:service-x1")) assert.True(t, policyHasRule(policy, "default-domain:testns:client2", "default-domain:testns:service-x1")) } _, err = types.VirtualNetworkByName(client, "default-domain:testns:client2") assert.NoError(t, err) controller.DeletePod(pod3) time.Sleep(100 * time.Millisecond) policy, err = types.NetworkPolicyByName(client, "default-domain:testns:x1") assert.NoError(t, err) if err == nil { assert.Len(t, policy.GetNetworkPolicyEntries().PolicyRule, 2) } controller.DeletePod(pod4) time.Sleep(100 * time.Millisecond) _, err = types.VirtualNetworkByName(client, "default-domain:testns:client2") assert.Error(t, err) policy, err = types.NetworkPolicyByName(client, "default-domain:testns:x1") assert.NoError(t, err) if err == nil { assert.Len(t, policy.GetNetworkPolicyEntries().PolicyRule, 1) assert.True(t, policyHasRule(policy, "default-domain:testns:client1", "default-domain:testns:service-x1")) } controller.DeletePod(pod2) time.Sleep(100 * time.Millisecond) policy, err = types.NetworkPolicyByName(client, "default-domain:testns:x1") assert.NoError(t, err) if err == nil { assert.Len(t, policy.GetNetworkPolicyEntries().PolicyRule, 0) } controller.DeletePod(pod1) controller.DeleteService(service) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} policy, err = types.NetworkPolicyByName(client, "default-domain:testns:x1") assert.Error(t, err) }
func TestServiceDeleteWithPod(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) allocator := new(mocks.AddressAllocator) controller := NewTestController(kube, client, allocator, nil) pod1 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz1", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "testpod", }, }, } pod2 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz2", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "testpod", }, }, } service := &api.Service{ ObjectMeta: api.ObjectMeta{ Name: "s1", Namespace: "testns", Labels: map[string]string{ "name": "x1", }, }, Spec: api.ServiceSpec{ Selector: map[string]string{ "name": "testpod", }, ClusterIP: "10.254.42.42", }, } netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) allocator.On("LocateIpAddress", string(pod1.ObjectMeta.UID)).Return("10.0.0.1", nil) allocator.On("LocateIpAddress", string(pod2.ObjectMeta.UID)).Return("10.0.0.2", nil) kube.PodInterface.On("Update", pod1).Return(pod1, nil) kube.PodInterface.On("Update", pod2).Return(pod2, nil) kube.PodInterface.On("List", mock.Anything, mock.Anything).Return(&api.PodList{Items: []api.Pod{*pod1, *pod2}}, nil) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddPod(pod1) controller.AddPod(pod2) controller.AddService(service) time.Sleep(100 * time.Millisecond) obj, err := client.FindByName("virtual-network", "default-domain:testns:service-x1") assert.NoError(t, err) serviceNet := obj.(*types.VirtualNetwork) sip, err := controller.networkMgr.LocateFloatingIp(serviceNet, service.Name, service.Spec.ClusterIP) sipName := sip.GetFQName() assert.NoError(t, err) refList, err := sip.GetVirtualMachineInterfaceRefs() assert.Nil(t, err) assert.NotEmpty(t, refList) obj, err = client.FindByName("network-policy", "default-domain:testns:x1") assert.NoError(t, err) controller.DeleteService(service) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} obj, err = client.FindByName("virtual-network", "default-domain:testns:service-x1") assert.Error(t, err) obj, err = client.FindByName("floating-ip", strings.Join(sipName, ":")) assert.Error(t, err) obj, err = client.FindByName("network-policy", "default-domain:testns:x1") assert.Error(t, err) }
func TestServiceWithLoadBalancer(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) client.AddInterceptor("instance-ip", &IpInterceptor{}) client.AddInterceptor("floating-ip", &FloatingIpInterceptor{}) controller := NewTestController(kube, client, nil, nil) pod1 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-sv1", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "backend", }, }, } pod2 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-sv2", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "backend", }, }, } service := &api.Service{ ObjectMeta: api.ObjectMeta{ Name: "service", Namespace: "testns", Labels: map[string]string{ "name": "svc", }, }, Spec: api.ServiceSpec{ Selector: map[string]string{ "name": "backend", }, ClusterIP: "10.254.42.42", Type: api.ServiceTypeLoadBalancer, }, } netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) store := new(mocks.Store) controller.SetServiceStore(store) kube.PodInterface.On("Update", pod1).Return(pod1, nil) kube.PodInterface.On("Update", pod2).Return(pod2, nil) kube.PodInterface.On("List", mock.Anything, mock.Anything).Return(&api.PodList{Items: []api.Pod{*pod1}}, nil) kube.ServiceInterface.On("Update", service).Return(service, nil) store.On("List").Return([]interface{}{service}) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddPod(pod1) controller.AddService(service) time.Sleep(100 * time.Millisecond) controller.AddPod(pod2) time.Sleep(100 * time.Millisecond) fqn := strings.Split(controller.config.PublicNetwork, ":") fqn = append(fqn, fqn[len(fqn)-1]) fqn = append(fqn, service.Name) fip, err := types.FloatingIpByName(client, strings.Join(fqn, ":")) assert.NoError(t, err) if err == nil { refs, err := fip.GetVirtualMachineInterfaceRefs() assert.NoError(t, err) assert.Len(t, refs, 2) } controller.DeleteService(service) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} _, err = types.FloatingIpByName(client, strings.Join(fqn, ":")) assert.Error(t, err) }
func TestPurgeServiceList(t *testing.T) { client := new(contrail_mocks.ApiClient) client.Init() config := NewConfig() networkMgr := NewNetworkManager(client, config) serviceMgr := NewServiceManager(client, config, networkMgr) netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) globalProject := new(types.Project) globalProject.SetFQName("", []string{"default-domain", "global"}) client.Create(globalProject) network, err := networkMgr.LocateNetwork("testns", "client", config.PrivateSubnet) services := []string{ "testns/s1", "testns/s2", "global/s3", "global/s4", } for _, svc := range services { parts := strings.Split(svc, "/") namespace := parts[0] svcName := parts[1] err := serviceMgr.Create(namespace, svcName) if err != nil { t.Error(err) continue } err = serviceMgr.Connect(namespace, svcName, network) if err != nil { t.Error(err) } } serviceList := MakeServiceIdList() serviceList.Add("testns", "s1") serviceList.Add("global", "s3") err = serviceMgr.PurgeStalePolicyRefs(network, serviceList, func(namespace, name string) bool { return true }) if err != nil { t.Fatal(err) } refs, err := network.GetNetworkPolicyRefs() if err != nil { t.Fatal(err) } if len(refs) != len(serviceList) { t.Errorf("expected %d policy refs, got %d", len(serviceList), len(refs)) } actual := make([]string, 0) for _, ref := range refs { actual = append(actual, ref.To[1]+"/"+ref.To[2]) } assert.EqualValues(t, []string{"testns/s1", "global/s3"}, actual) }
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) }
func TestServiceUpdatePublicIp(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) client.AddInterceptor("instance-ip", &IpInterceptor{}) client.AddInterceptor("floating-ip", &FloatingIpInterceptor{}) controller := NewTestController(kube, client, nil, nil) pod1 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-sv1", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "service", }, }, } pod2 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-sv2", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "service", }, }, } pod3 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz3", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "client", "uses": "svc", }, }, } service := &api.Service{ ObjectMeta: api.ObjectMeta{ Name: "service", Namespace: "testns", Labels: map[string]string{ "name": "svc", }, }, Spec: api.ServiceSpec{ Selector: map[string]string{ "name": "service", }, ClusterIP: "10.254.42.42", Type: api.ServiceTypeLoadBalancer, }, } netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) kube.PodInterface.On("Update", pod1).Return(pod1, nil) kube.PodInterface.On("Update", pod2).Return(pod2, nil) kube.PodInterface.On("Update", pod3).Return(pod3, nil) selectPods := labels.Set(map[string]string{"name": "service"}).AsSelector() kube.PodInterface.On("List", selectPods, mock.Anything).Return(&api.PodList{Items: []api.Pod{*pod1, *pod2}}, nil) kube.ServiceInterface.On("Update", service).Return(service, nil) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddPod(pod1) controller.AddPod(pod2) controller.AddPod(pod3) controller.AddService(service) time.Sleep(100 * time.Millisecond) fqn := strings.Split(controller.config.PublicNetwork, ":") fqn = append(fqn, fqn[len(fqn)-1]) fqn = append(fqn, service.Name) fip, err := types.FloatingIpByName(client, strings.Join(fqn, ":")) assert.NoError(t, err) if err == nil { vmList, err := getFloatingIpToInstanceList(client, fip) assert.NoError(t, err) assert.Len(t, vmList, 2) assert.Contains(t, vmList, string(pod1.UID)) assert.Contains(t, vmList, string(pod2.UID)) } nService := new(api.Service) *nService = *service nService.Spec.Type = api.ServiceTypeClusterIP controller.UpdateService(service, nService) time.Sleep(100 * time.Millisecond) _, err = types.FloatingIpByName(client, strings.Join(fqn, ":")) assert.Error(t, err) controller.UpdateService(nService, service) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} fip, err = types.FloatingIpByName(client, strings.Join(fqn, ":")) assert.NoError(t, err) if err == nil { vmList, err := getFloatingIpToInstanceList(client, fip) assert.NoError(t, err) assert.Len(t, vmList, 2) assert.Contains(t, vmList, string(pod1.UID)) assert.Contains(t, vmList, string(pod2.UID)) } policy, err := types.NetworkPolicyByName(client, "default-domain:testns:svc") assert.NoError(t, err) if err == nil { assert.Len(t, policy.GetNetworkPolicyEntries().PolicyRule, 1) assert.True(t, policyHasRule(policy, "default-domain:testns:client", "default-domain:testns:service-svc")) } }
func TestServiceUpdateLabel(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) client.AddInterceptor("instance-ip", &IpInterceptor{}) client.AddInterceptor("floating-ip", &FloatingIpInterceptor{}) controller := NewTestController(kube, client, nil, nil) pod1 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-sv1", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "server", }, }, } pod2 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz2", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "client1", "uses": "red", }, }, } pod3 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-xz3", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "client2", "uses": "blue", }, }, } service := &api.Service{ ObjectMeta: api.ObjectMeta{ Name: "service", Namespace: "testns", Labels: map[string]string{ "name": "red", }, }, Spec: api.ServiceSpec{ Selector: map[string]string{ "name": "server", }, ClusterIP: "10.254.42.42", Type: api.ServiceTypeLoadBalancer, }, } netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) kube.PodInterface.On("Update", pod1).Return(pod1, nil) kube.PodInterface.On("Update", pod2).Return(pod2, nil) kube.PodInterface.On("Update", pod3).Return(pod3, nil) selectServer := labels.Set(map[string]string{"name": "server"}).AsSelector() kube.PodInterface.On("List", selectServer, mock.Anything).Return(&api.PodList{Items: []api.Pod{*pod1}}, nil) kube.ServiceInterface.On("Update", service).Return(service, nil) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddPod(pod1) controller.AddPod(pod2) controller.AddPod(pod3) controller.AddService(service) time.Sleep(100 * time.Millisecond) redPolicy, err := types.NetworkPolicyByName(client, "default-domain:testns:red") assert.NoError(t, err) if err == nil { assert.Len(t, redPolicy.GetNetworkPolicyEntries().PolicyRule, 1) assert.True(t, policyHasRule(redPolicy, "default-domain:testns:client1", "default-domain:testns:service-red")) refs, err := redPolicy.GetVirtualNetworkBackRefs() assert.NoError(t, err) nameList := getReferenceListNames(refs) assert.Contains(t, nameList, "default-domain:testns:client1") assert.Contains(t, nameList, "default-domain:testns:service-red") } bluePolicy, err := types.NetworkPolicyByName(client, "default-domain:testns:blue") assert.NoError(t, err) if err == nil { assert.Len(t, bluePolicy.GetNetworkPolicyEntries().PolicyRule, 0) refs, err := bluePolicy.GetVirtualNetworkBackRefs() assert.NoError(t, err) assert.Len(t, refs, 1) } nService := new(api.Service) *nService = *service nService.Labels = map[string]string{ "name": "blue", } // The service will receive a different PublicIP because this is translated into a service delete operation, // followed by an add. kube.ServiceInterface.On("Update", nService).Return(nService, nil) controller.UpdateService(service, nService) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} bluePolicy, err = types.NetworkPolicyByName(client, "default-domain:testns:blue") assert.NoError(t, err) if err == nil { assert.Len(t, bluePolicy.GetNetworkPolicyEntries().PolicyRule, 1) assert.True(t, policyHasRule(bluePolicy, "default-domain:testns:client2", "default-domain:testns:service-blue")) refs, err := bluePolicy.GetVirtualNetworkBackRefs() assert.NoError(t, err) nameList := getReferenceListNames(refs) assert.Contains(t, nameList, "default-domain:testns:client2") assert.Contains(t, nameList, "default-domain:testns:service-blue") } redPolicy, err = types.NetworkPolicyByName(client, "default-domain:testns:red") assert.NoError(t, err) if err == nil { assert.Len(t, redPolicy.GetNetworkPolicyEntries().PolicyRule, 0) refs, err := redPolicy.GetVirtualNetworkBackRefs() assert.NoError(t, err) assert.Len(t, refs, 1) } fip, err := types.FloatingIpByName(client, "default-domain:testns:service-blue:service-blue:service") assert.NoError(t, err) if err == nil { vmList, err := getFloatingIpToInstanceList(client, fip) assert.NoError(t, err) assert.Len(t, vmList, 1) assert.Contains(t, vmList, string(pod1.UID)) } }
func TestPodAddWithService(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) allocator := new(mocks.AddressAllocator) controller := NewTestController(kube, client, allocator, nil) pod := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "testpod", }, }, } service := &api.Service{ ObjectMeta: api.ObjectMeta{ Name: "s1", Namespace: "testns", Labels: map[string]string{ "name": "x1", }, }, Spec: api.ServiceSpec{ Selector: map[string]string{ "name": "testpod", }, ClusterIP: "10.254.42.42", }, } netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) allocator.On("LocateIpAddress", string(pod.ObjectMeta.UID)).Return("10.0.0.1", nil) kube.PodInterface.On("Update", pod).Return(pod, nil) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.serviceStore.Add(service) controller.AddPod(pod) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} kube.PodInterface.AssertExpectations(t) obj, err := client.FindByName("virtual-network", "default-domain:testns:service-x1") assert.NoError(t, err) serviceNet := obj.(*types.VirtualNetwork) sip, err := controller.networkMgr.LocateFloatingIp(serviceNet, service.Name, service.Spec.ClusterIP) assert.NoError(t, err) refList, err := sip.GetVirtualMachineInterfaceRefs() assert.Nil(t, err) assert.NotEmpty(t, refList) }
func TestServiceWithMultipleBackends(t *testing.T) { kube := mocks.NewKubeClient() client := new(contrail_mocks.ApiClient) client.Init() client.AddInterceptor("virtual-machine-interface", &VmiInterceptor{}) client.AddInterceptor("virtual-network", &NetworkInterceptor{}) client.AddInterceptor("instance-ip", &IpInterceptor{}) controller := NewTestController(kube, client, nil, nil) pod1 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-sv1", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "backend", }, }, } pod2 := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "test-sv2", Namespace: "testns", UID: kubetypes.UID(uuid.New()), Labels: map[string]string{ "name": "backend", }, }, } service := &api.Service{ ObjectMeta: api.ObjectMeta{ Name: "service", Namespace: "testns", Labels: map[string]string{ "name": "svc", }, }, Spec: api.ServiceSpec{ Selector: map[string]string{ "name": "backend", }, ClusterIP: "10.254.42.42", }, } netnsProject := new(types.Project) netnsProject.SetFQName("", []string{"default-domain", "testns"}) client.Create(netnsProject) store := new(mocks.Store) controller.SetServiceStore(store) kube.PodInterface.On("Update", pod1).Return(pod1, nil) kube.PodInterface.On("Update", pod2).Return(pod2, nil) kube.PodInterface.On("List", mock.Anything, mock.Anything).Return(&api.PodList{Items: []api.Pod{*pod1}}, nil) store.On("List").Return([]interface{}{service}) shutdown := make(chan struct{}) go controller.Run(shutdown) controller.AddPod(pod1) controller.AddService(service) time.Sleep(100 * time.Millisecond) controller.AddPod(pod2) time.Sleep(100 * time.Millisecond) fip, err := types.FloatingIpByName(client, "default-domain:testns:service-svc:service-svc:service") assert.NoError(t, err) if err == nil { refs, err := fip.GetVirtualMachineInterfaceRefs() assert.NoError(t, err) assert.Len(t, refs, 2) } controller.DeletePod(pod1) time.Sleep(100 * time.Millisecond) fip, err = types.FloatingIpByName(client, "default-domain:testns:service-svc:service-svc:service") assert.NoError(t, err) if err == nil { refs, err := fip.GetVirtualMachineInterfaceRefs() assert.NoError(t, err) assert.Len(t, refs, 1) var uids []string for _, ref := range refs { uids = append(uids, ref.Uuid) } vmi, err := types.VirtualMachineInterfaceByName(client, "default-domain:testns:test-sv2") assert.NoError(t, err) if err == nil { assert.Contains(t, uids, vmi.GetUuid()) } } controller.AddPod(pod1) time.Sleep(100 * time.Millisecond) fip, err = types.FloatingIpByName(client, "default-domain:testns:service-svc:service-svc:service") assert.NoError(t, err) if err == nil { refs, err := fip.GetVirtualMachineInterfaceRefs() assert.NoError(t, err) assert.Len(t, refs, 2) } controller.DeletePod(pod1) controller.DeletePod(pod2) time.Sleep(100 * time.Millisecond) fip, err = types.FloatingIpByName(client, "default-domain:testns:service-svc:service-svc:service") assert.NoError(t, err) if err == nil { refs, err := fip.GetVirtualMachineInterfaceRefs() assert.NoError(t, err) assert.Len(t, refs, 0) } controller.DeleteService(service) time.Sleep(100 * time.Millisecond) type shutdownMsg struct { } shutdown <- shutdownMsg{} _, err = types.FloatingIpByName(client, "default-domain:testns:service-svc:service-svc:service") assert.Error(t, err) }