Repository: istio/istio
File: pilot/pkg/serviceregistry/kube/controller/ambient/ambientindex_test.go (Go)

Code before:
		s.deleteService(t, "svc1")
		s.assertUnorderedEvent(t, s.svcXdsName("svc1"), s.seIPXdsName("se-2", "10.10.0.2"))
		s.assertAddresses(t, testNW+"/10.255.255.1")
		s.assertAddresses(t, testNW+"/10.255.0.2", "se-2")
		s.assertNoEvent(t)
	})
}

func TestAmbientIndex_ServiceAttachedWaypoints(t *testing.T) {
	cases := []struct {
		name         string
		multicluster bool
	}{
		{
			name: "base",
		},
		{
			name:         "multicluster (enabled but unused)",
			multicluster: true,
		},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if c.multicluster {
				test.SetForTest(t, &features.EnableAmbientMultiNetwork, true)
			}
			s := newAmbientTestServer(t, testC, testNW, "")
			s.addWaypoint(t, "10.0.0.10", "test-wp", "default", true)

			s.addPods(t, "127.0.0.1", "pod1", "sa1", map[string]string{"app": "a"}, nil, true, corev1.PodRunning)
			s.assertEvent(t, s.podXdsName("pod1"))

			// Now add a service that will select pods with label "a".
			s.addService(t, "svc1",
				map[string]string{},
				map[string]string{},
				[]int32{80}, map[string]string{"app": "a"}, "10.0.0.1")
			s.assertEvent(t, s.podXdsName("pod1"), s.svcXdsName("svc1"))

			s.labelService(t, "svc1", testNS, map[string]string{label.IoIstioUseWaypoint.Name: "test-wp"})
			s.assertEvent(t, s.svcXdsName("svc1"))
			s.assertNoEvent(t)

			// We should now see the waypoint service IP when we look up the annotated svc
			assert.Equal(t,
				s.lookup(s.addrXdsName("10.0.0.1"))[0].Address.GetService().Waypoint.GetHostname().GetHostname(),
				"test-wp.ns1.svc.company.com")
		})
	}
}


Review comment (bug):
Pretty rarely we seem to get an extra event.
```
2068 runs so far, 1 failures (99.95% pass rate). 231.016262ms avg, 322.440914ms max, 180.021857ms min

ambientindex_test.go:451: got unexpected event {Type:xds ID:ns1/svc1.ns1.svc.company.com Reason:map[] Namespace: Endpoints:[] EndpointCount: 0}

```

Code after:
			s.seIPXdsName("se-1", "10.10.0.1"))
		s.assertAddresses(t, testNW+"/10.255.0.1")
		s.assertAddresses(t, testNW+"/10.255.0.2", "se-2")
	})

	t.Run("serviceentry and service overlap", func(t *testing.T) {
		s := newAmbientTestServer(t, testC, testNW, "")

		// initial svc
		addService(s, 1, "svc1")
		s.assertEvent(t, s.svcXdsName("svc1"))
		s.assertAddresses(t, testNW+"/10.255.255.1", "svc1")
		s.assertNoEvent(t)

		// overlapping SE - the old one takes precedence, new one is not in indexes
		addServiceEntry(s, 2, "svc1.ns1.svc.company.com")
		s.assertNoEvent(t) // this should be totally filtered as duplicate with a Kubernetes Service
		s.assertAddresses(t, testNW+"/10.255.255.1", "svc1")
		s.assertAddresses(t, testNW+"/10.255.0.2")

		// the original one goes away, the new one takes over
		s.deleteService(t, "svc1")
		s.assertUnorderedEvent(t, s.svcXdsName("svc1"), s.seIPXdsName("se-2", "10.10.0.2"))
		s.assertAddresses(t, testNW+"/10.255.255.1")
		s.assertAddresses(t, testNW+"/10.255.0.2", "se-2")
		s.assertNoEvent(t)
	})
}

func TestAmbientIndex_ServiceAttachedWaypoints(t *testing.T) {
	cases := []struct {
		name         string
		multicluster bool
	}{
		{
			name: "base",
		},
		{
			name:         "multicluster (enabled but unused)",
			multicluster: true,
		},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if c.multicluster {
				test.SetForTest(t, &features.EnableAmbientMultiNetwork, true)
			}
			s := newAmbientTestServer(t, testC, testNW, "")
			s.addWaypoint(t, "10.0.0.10", "test-wp", "default", true)

			s.addPods(t, "127.0.0.1", "pod1", "sa1", map[string]string{"app": "a"}, nil, true, corev1.PodRunning)