Repository: moby/moby
File: daemon/libnetwork/agent_test.go (Go)

Code before:
package libnetwork

import (
	"net/netip"
	"slices"
	"testing"

	"gotest.tools/v3/assert"
)

func TestEndpointEvent_EquivalentTo(t *testing.T) {
	assert.Check(t, (&endpointEvent{}).EquivalentTo(&endpointEvent{}))

	a := endpointEvent{
		EndpointRecord: EndpointRecord{
			Name:        "foo",
			ServiceName: "bar",
			ServiceID:   "baz",
			IngressPorts: []*PortConfig{
				{
					Protocol:   ProtocolTCP,
					TargetPort: 80,
				},
				{
					Name:          "dns",
					Protocol:      ProtocolUDP,
					TargetPort:    5353,
					PublishedPort: 53,
				},
			},
		},
		VirtualIP:  netip.MustParseAddr("10.0.0.42"),
		EndpointIP: netip.MustParseAddr("192.168.69.42"),
	}
	assert.Check(t, a.EquivalentTo(&a))

	reflexiveEquiv := func(a, b *endpointEvent) bool {
		t.Helper()
		assert.Check(t, a.EquivalentTo(b) == b.EquivalentTo(a), "reflexive equivalence")
		return a.EquivalentTo(b)
	}

	b := a
	b.ServiceDisabled = true
	assert.Check(t, !reflexiveEquiv(&a, &b), "differing by ServiceDisabled")

	c := a
	c.IngressPorts = slices.Clone(a.IngressPorts)
	slices.Reverse(c.IngressPorts)
	assert.Check(t, reflexiveEquiv(&a, &c), "IngressPorts order should not matter")

	d := a
	d.IngressPorts = append(d.IngressPorts, a.IngressPorts[0])
	assert.Check(t, !reflexiveEquiv(&a, &d), "Differing number of copies of IngressPort entries should not be equivalent")
	d.IngressPorts = a.IngressPorts[:1]
	assert.Check(t, !reflexiveEquiv(&a, &d), "Removing an IngressPort entry should not be equivalent")

	e := a
	e.Aliases = []string{"alias1", "alias2"}
	assert.Check(t, !reflexiveEquiv(&a, &e), "Differing Aliases should not be equivalent")

	f := a
	f.TaskAliases = []string{"taskalias1", "taskalias2"}
	assert.Check(t, !reflexiveEquiv(&a, &f), "Adding TaskAliases should not be equivalent")
	g := a
	g.TaskAliases = []string{"taskalias2", "taskalias1"}
	assert.Check(t, reflexiveEquiv(&f, &g), "TaskAliases order should not matter")
	g.TaskAliases = g.TaskAliases[:1]
	assert.Check(t, !reflexiveEquiv(&f, &g), "Differing number of TaskAliases should not be equivalent")

	h := a
	h.EndpointIP = netip.MustParseAddr("192.168.69.43")
	assert.Check(t, !reflexiveEquiv(&a, &h), "Differing EndpointIP should not be equivalent")

	i := a
	i.VirtualIP = netip.MustParseAddr("10.0.0.69")
	assert.Check(t, !reflexiveEquiv(&a, &i), "Differing VirtualIP should not be equivalent")

	j := a
	j.ServiceID = "qux"
	assert.Check(t, !reflexiveEquiv(&a, &j), "Differing ServiceID should not be equivalent")

	k := a
	k.ServiceName = "quux"
	assert.Check(t, !reflexiveEquiv(&a, &k), "Differing ServiceName should not be equivalent")

	l := a
	l.Name = "aaaaa"
	assert.Check(t, !reflexiveEquiv(&a, &l), "Differing Name should not be equivalent")
}


Review comment (suggestion):
Nit; and no need to update (unless there's other reasons to update the PR); we mostly converged on using `tc` ("test case") for these, so would be good to use that as name for consistency.


```suggestion
	for _, tc := range tests {
```



Code after:
				"addContainerNameResolution(network1, endpoint1, ctr2, 172.18.1.2)",
			},
		},
		{
			name: "Replace/From=Container/To=Container/ServiceDisabled=ft",
			prev: &ctr1,
			ev:   &ctr2disabled,
			expectedActions: []string{
				"delContainerNameResolution(network1, endpoint1, ctr1, 172.18.1.1)",
			},
		},
		{
			name: "Replace/From=Container/To=Container/ServiceDisabled=tf",
			prev: &ctr1disabled,
			ev:   &ctr2,
			expectedActions: []string{
				"addContainerNameResolution(network1, endpoint1, ctr2, 172.18.1.2)",
			},
		},
		{
			name: "Replace/From=Container/To=Container/ServiceDisabled=tt",
			prev: &ctr1disabled,
			ev:   &ctr2disabled,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			msb := &mockServiceBinder{}
			event := networkdb.WatchEvent{
				NetworkID: "network1",
				Key:       "endpoint1",
			}
			var err error
			if tt.prev != nil {
				event.Prev, err = proto.Marshal(tt.prev)
				assert.NilError(t, err)
			}
			if tt.ev != nil {
				event.Value, err = proto.Marshal(tt.ev)
				assert.NilError(t, err)
			}
			handleEpTableEvent(msb, event)
			assert.DeepEqual(t, tt.expectedActions, msb.actions)
		})
	}
}