|
4 | 4 | "context" |
5 | 5 | "fmt" |
6 | 6 | "github.com/otterize/network-mapper/src/shared/testbase" |
| 7 | + "github.com/samber/lo" |
7 | 8 | "github.com/stretchr/testify/suite" |
8 | 9 | corev1 "k8s.io/api/core/v1" |
9 | 10 | "testing" |
@@ -38,35 +39,40 @@ func (s *KubeFinderTestSuite) TestResolveIpToPod() { |
38 | 39 | } |
39 | 40 |
|
40 | 41 | func (s *KubeFinderTestSuite) TestResolveServiceAddressToIps() { |
41 | | - _, _, err := s.kubeFinder.ResolveServiceAddressToIps(context.Background(), "www.google.com") |
| 42 | + _, _, err := s.kubeFinder.ResolveServiceAddressToPods(context.Background(), "www.google.com") |
42 | 43 | s.Require().Error(err) |
43 | 44 |
|
44 | | - _, _, err = s.kubeFinder.ResolveServiceAddressToIps(context.Background(), fmt.Sprintf("svc-service1.%s.svc.cluster.local", s.TestNamespace)) |
| 45 | + _, _, err = s.kubeFinder.ResolveServiceAddressToPods(context.Background(), fmt.Sprintf("svc-service1.%s.svc.cluster.local", s.TestNamespace)) |
45 | 46 | s.Require().Error(err) |
46 | 47 |
|
47 | 48 | podIp0 := "1.1.1.1" |
48 | 49 | podIp1 := "1.1.1.2" |
49 | 50 | podIp2 := "1.1.1.3" |
50 | 51 | s.Require().NoError(s.Mgr.GetClient().List(context.Background(), &corev1.EndpointsList{})) // Workaround: make then client start caching Endpoints, so when we do "WaitForCacheSync" it will actually sync cache" |
51 | 52 | s.AddDeploymentWithService("service0", []string{podIp0}, map[string]string{"app": "service0"}, "10.0.0.10") |
52 | | - s.AddDeploymentWithService("service1", []string{podIp1, podIp2}, map[string]string{"app": "service1"}, "10.0.0.11") |
| 53 | + _, _, retPods := s.AddDeploymentWithService("service1", []string{podIp1, podIp2}, map[string]string{"app": "service1"}, "10.0.0.11") |
53 | 54 | s.Require().True(s.Mgr.GetCache().WaitForCacheSync(context.Background())) |
54 | 55 |
|
55 | | - ips, service, err := s.kubeFinder.ResolveServiceAddressToIps(context.Background(), fmt.Sprintf("svc-service1.%s.svc.cluster.local", s.TestNamespace)) |
| 56 | + pods, service, err := s.kubeFinder.ResolveServiceAddressToPods(context.Background(), fmt.Sprintf("svc-service1.%s.svc.cluster.local", s.TestNamespace)) |
56 | 57 | s.Require().NoError(err) |
57 | 58 | s.Require().Equal("svc-service1", service.Name) |
58 | | - s.Require().ElementsMatch(ips, []string{podIp1, podIp2}) |
| 59 | + s.Require().ElementsMatch(lo.Map(pods, func(p corev1.Pod, _ int) string { return p.Status.PodIP }), lo.Map(retPods, func(p *corev1.Pod, _ int) string { return p.Status.PodIP })) |
59 | 60 |
|
60 | 61 | // make sure we don't fail on the longer forms of k8s service addresses, listed on this page: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service |
61 | | - ips, service, err = s.kubeFinder.ResolveServiceAddressToIps(context.Background(), fmt.Sprintf("4-4-4-4.svc-service1.%s.svc.cluster.local", s.TestNamespace)) |
| 62 | + pods, service, err = s.kubeFinder.ResolveServiceAddressToPods(context.Background(), fmt.Sprintf("4-4-4-4.svc-service1.%s.svc.cluster.local", s.TestNamespace)) |
62 | 63 | s.Require().Equal("svc-service1", service.Name) |
63 | 64 | s.Require().NoError(err) |
64 | | - s.Require().ElementsMatch(ips, []string{podIp1, podIp2}) |
| 65 | + s.Require().ElementsMatch(lo.Map(pods, func(p corev1.Pod, _ int) string { return p.Status.PodIP }), lo.Map(retPods, func(p *corev1.Pod, _ int) string { return p.Status.PodIP })) |
65 | 66 |
|
66 | | - ips, service, err = s.kubeFinder.ResolveServiceAddressToIps(context.Background(), fmt.Sprintf("4-4-4-4.%s.pod.cluster.local", s.TestNamespace)) |
| 67 | + _, _, err = s.kubeFinder.ResolveServiceAddressToPods(context.Background(), fmt.Sprintf("4-4-4-4.%s.pod.cluster.local", s.TestNamespace)) |
| 68 | + s.Require().Error(err) |
| 69 | + s.Require().True(s.Mgr.GetCache().WaitForCacheSync(context.Background())) |
| 70 | + |
| 71 | + _, pods4444 := s.AddDeployment("depl", []string{"4.4.4.4"}, map[string]string{"app": "4444"}) |
| 72 | + pods, service, err = s.kubeFinder.ResolveServiceAddressToPods(context.Background(), fmt.Sprintf("4-4-4-4.%s.pod.cluster.local", s.TestNamespace)) |
67 | 73 | s.Require().NoError(err) |
68 | 74 | s.Require().Empty(service) |
69 | | - s.Require().ElementsMatch(ips, []string{"4.4.4.4"}) |
| 75 | + s.Require().ElementsMatch(lo.Map(pods, func(p corev1.Pod, _ int) string { return p.Status.PodIP }), lo.Map(pods4444, func(p *corev1.Pod, _ int) string { return p.Status.PodIP })) |
70 | 76 | } |
71 | 77 |
|
72 | 78 | func TestKubeFinderTestSuite(t *testing.T) { |
|
0 commit comments