Repository: microsoft/retina
File: pkg/capture/crd_to_job_test.go (Go)

Code before:
				"node1": {CaptureNodeInterface: true},
			},
		},
		{
			existingTargets: map[string]CaptureTarget{"node1": {CaptureNodeInterface: true}},
			newTargetNode:   "node1",
			wantCaptureTargetsOnNode: CaptureTargetsOnNode{
				"node1": {CaptureNodeInterface: true},
			},
		},
	}
	for _, c := range cases {
		gotCaptureTargetsOnNode := CaptureTargetsOnNode{}
		for k, v := range c.existingTargets {
			gotCaptureTargetsOnNode[k] = v
		}
		gotCaptureTargetsOnNode.AddNodeInterface(c.newTargetNode)

		if diff := cmp.Diff(c.wantCaptureTargetsOnNode, gotCaptureTargetsOnNode); diff != "" {
			t.Errorf("AddPod() mismatch (-want, +got):\n%s", diff)
		}
	}
}

func Test_CaptureToPodTranslator_GetCaptureTargetsOnNode(t *testing.T) {
	ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM)
	defer cancel()

	cases := []struct {
		name                     string
		captureTarget            retinav1alpha1.CaptureTarget
		nodeList                 *corev1.NodeList
		namespaceList            *corev1.NamespaceList
		podList                  *corev1.PodList
		wantCaptureTargetsOnNode *CaptureTargetsOnNode
		wantErr                  bool
	}{
		{
			name:                     "No selector is specified",
			captureTarget:            retinav1alpha1.CaptureTarget{},
			wantCaptureTargetsOnNode: nil,
			wantErr:                  true,
		},
		{
			name: "either node or pod selector is specified",
			captureTarget: retinav1alpha1.CaptureTarget{
				NodeSelector: &metav1.LabelSelector{
					MatchLabels: map[string]string{corev1.LabelHostname: "node1"},
				},
				PodSelector: &metav1.LabelSelector{
					MatchLabels: map[string]string{"type": "server"},

Review comment (refactor):
In tests, you can do this slightly better with a `context.WithDeadline`. You can get the deadline with `testing.(*T).Deadline()` which is bound to the `-timeout` value of `go test`. I usually wrap that all up in a `testContext` helper or something that I just pass the `testing.(*T)` to.

Code after:
		{
			existingTargets: map[string]CaptureTarget{"node1": {CaptureNodeInterface: true}},
			newTargetNode:   "node1",
			wantCaptureTargetsOnNode: CaptureTargetsOnNode{
				"node1": {CaptureNodeInterface: true},
			},
		},
	}
	for _, c := range cases {
		gotCaptureTargetsOnNode := CaptureTargetsOnNode{}
		for k, v := range c.existingTargets {
			gotCaptureTargetsOnNode[k] = v
		}
		gotCaptureTargetsOnNode.AddNodeInterface(c.newTargetNode)

		if diff := cmp.Diff(c.wantCaptureTargetsOnNode, gotCaptureTargetsOnNode); diff != "" {
			t.Errorf("AddPod() mismatch (-want, +got):\n%s", diff)
		}
	}
}

func Test_CaptureToPodTranslator_GetCaptureTargetsOnNode(t *testing.T) {
	ctx, cancel := TestContext(t)
	defer cancel()

	cases := []struct {
		name                     string
		captureTarget            retinav1alpha1.CaptureTarget
		nodeList                 *corev1.NodeList
		namespaceList            *corev1.NamespaceList
		podList                  *corev1.PodList
		wantCaptureTargetsOnNode *CaptureTargetsOnNode
		wantErr                  bool
	}{
		{
			name:                     "No selector is specified",
			captureTarget:            retinav1alpha1.CaptureTarget{},
			wantCaptureTargetsOnNode: nil,
			wantErr:                  true,
		},
		{
			name: "either node or pod selector is specified",
			captureTarget: retinav1alpha1.CaptureTarget{
				NodeSelector: &metav1.LabelSelector{
					MatchLabels: map[string]string{corev1.LabelHostname: "node1"},
				},
				PodSelector: &metav1.LabelSelector{
					MatchLabels: map[string]string{"type": "server"},
				},
			},
			wantCaptureTargetsOnNode: nil,