|
| 1 | +// Copyright 2026 NetApp, Inc. All Rights Reserved. |
| 2 | + |
| 3 | +package crd |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "sort" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + k8sruntime "k8s.io/apimachinery/pkg/runtime" |
| 14 | + k8stesting "k8s.io/client-go/testing" |
| 15 | + |
| 16 | + "github.com/netapp/trident/config" |
| 17 | + tridentv1 "github.com/netapp/trident/persistent_store/crd/apis/netapp/v1" |
| 18 | + tridentfake "github.com/netapp/trident/persistent_store/crd/client/clientset/versioned/fake" |
| 19 | + "github.com/netapp/trident/utils/models" |
| 20 | +) |
| 21 | + |
| 22 | +// testPublicationCR builds a TridentVolumePublication the way the controller persists it. When |
| 23 | +// labeled is false the node-name label is removed, as on publications that predate the label. |
| 24 | +func testPublicationCR(t *testing.T, namespace, volume, node string, labeled bool) *tridentv1.TridentVolumePublication { |
| 25 | + t.Helper() |
| 26 | + |
| 27 | + cr, err := tridentv1.NewTridentVolumePublication(&models.VolumePublication{ |
| 28 | + Name: models.GenerateVolumePublishName(volume, node), |
| 29 | + VolumeName: volume, |
| 30 | + NodeName: node, |
| 31 | + }) |
| 32 | + require.NoError(t, err) |
| 33 | + cr.Namespace = namespace |
| 34 | + if !labeled { |
| 35 | + delete(cr.Labels, config.TridentNodeNameLabel) |
| 36 | + } |
| 37 | + return cr |
| 38 | +} |
| 39 | + |
| 40 | +func TestClient_GetDesiredPublications(t *testing.T) { |
| 41 | + const ( |
| 42 | + ns = "trident" |
| 43 | + node = "node-a" |
| 44 | + ) |
| 45 | + |
| 46 | + type publication struct { |
| 47 | + volume string |
| 48 | + node string |
| 49 | + labeled bool |
| 50 | + } |
| 51 | + |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + queryNode string // defaults to node |
| 55 | + publications []publication |
| 56 | + wantVolumeNames []string |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "no publications", |
| 60 | + publications: nil, |
| 61 | + wantVolumeNames: []string{}, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "this node, labeled", |
| 65 | + publications: []publication{{"vol1", node, true}}, |
| 66 | + wantVolumeNames: []string{"vol1"}, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "this node, unlabeled", |
| 70 | + publications: []publication{{"vol1", node, false}}, |
| 71 | + wantVolumeNames: []string{"vol1"}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "other node, labeled", |
| 75 | + publications: []publication{{"vol1", "node-b", true}}, |
| 76 | + wantVolumeNames: []string{}, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "other node, unlabeled", |
| 80 | + publications: []publication{{"vol1", "node-b", false}}, |
| 81 | + wantVolumeNames: []string{}, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "mix of labeled and unlabeled across nodes", |
| 85 | + publications: []publication{ |
| 86 | + {"vol1", node, true}, |
| 87 | + {"vol2", node, false}, |
| 88 | + {"vol3", "node-b", true}, |
| 89 | + {"vol4", "node-b", false}, |
| 90 | + {"vol5", node, false}, |
| 91 | + }, |
| 92 | + wantVolumeNames: []string{"vol1", "vol2", "vol5"}, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "same volume published to this node and another node", |
| 96 | + publications: []publication{ |
| 97 | + {"vol1", node, false}, |
| 98 | + {"vol1", "node-b", false}, |
| 99 | + }, |
| 100 | + wantVolumeNames: []string{"vol1"}, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "node name that is a dotted suffix of another node name", |
| 104 | + queryNode: "b", |
| 105 | + publications: []publication{ |
| 106 | + {"vol1", "b", false}, |
| 107 | + {"vol2", "a.b", false}, |
| 108 | + }, |
| 109 | + wantVolumeNames: []string{"vol1"}, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for _, tc := range tests { |
| 114 | + t.Run(tc.name, func(t *testing.T) { |
| 115 | + objects := make([]k8sruntime.Object, 0, len(tc.publications)) |
| 116 | + for _, p := range tc.publications { |
| 117 | + objects = append(objects, testPublicationCR(t, ns, p.volume, p.node, p.labeled)) |
| 118 | + } |
| 119 | + c := &Client{tridentClient: tridentfake.NewSimpleClientset(objects...), tridentNamespace: ns} |
| 120 | + |
| 121 | + queryNode := node |
| 122 | + if tc.queryNode != "" { |
| 123 | + queryNode = tc.queryNode |
| 124 | + } |
| 125 | + desired, err := c.GetDesiredPublications(context.Background(), queryNode) |
| 126 | + require.NoError(t, err) |
| 127 | + |
| 128 | + gotVolumeNames := make([]string, 0, len(desired)) |
| 129 | + for volumeName, pub := range desired { |
| 130 | + assert.Equal(t, volumeName, pub.VolumeName) |
| 131 | + assert.Equal(t, queryNode, pub.NodeName) |
| 132 | + gotVolumeNames = append(gotVolumeNames, volumeName) |
| 133 | + } |
| 134 | + sort.Strings(gotVolumeNames) |
| 135 | + assert.Equal(t, tc.wantVolumeNames, gotVolumeNames) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestClient_GetDesiredPublications_ListError(t *testing.T) { |
| 141 | + const ns = "trident" |
| 142 | + |
| 143 | + tridentClient := tridentfake.NewSimpleClientset() |
| 144 | + tridentClient.PrependReactor("list", "tridentvolumepublications", |
| 145 | + func(k8stesting.Action) (bool, k8sruntime.Object, error) { |
| 146 | + return true, nil, fmt.Errorf("apiserver unavailable") |
| 147 | + }) |
| 148 | + c := &Client{tridentClient: tridentClient, tridentNamespace: ns} |
| 149 | + |
| 150 | + desired, err := c.GetDesiredPublications(context.Background(), "node-a") |
| 151 | + |
| 152 | + assert.Error(t, err) |
| 153 | + assert.ErrorContains(t, err, "list volume publications") |
| 154 | + assert.ErrorContains(t, err, "apiserver unavailable") |
| 155 | + assert.Nil(t, desired) |
| 156 | +} |
0 commit comments