@@ -3,15 +3,17 @@ package apply
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "strings"
7
+ "testing"
8
+
9
+ "github.com/rancher/wrangler/pkg/objectset"
6
10
"github.com/stretchr/testify/assert"
7
11
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8
12
"k8s.io/apimachinery/pkg/labels"
9
13
"k8s.io/apimachinery/pkg/runtime"
10
14
"k8s.io/apimachinery/pkg/runtime/schema"
11
15
"k8s.io/client-go/dynamic/fake"
12
16
k8stesting "k8s.io/client-go/testing"
13
- "strings"
14
- "testing"
15
17
)
16
18
17
19
func Test_multiNamespaceList (t * testing.T ) {
@@ -106,3 +108,56 @@ func Test_multiNamespaceList(t *testing.T) {
106
108
})
107
109
}
108
110
}
111
+
112
+ func TestObjectSet_getDistinctNamespaces (t * testing.T ) {
113
+ tests := []struct {
114
+ name string
115
+ objects map [objectset.ObjectKey ]runtime.Object
116
+ wantNamespaces []string
117
+ }{
118
+ {
119
+ name : "empty" ,
120
+ objects : map [objectset.ObjectKey ]runtime.Object {},
121
+ wantNamespaces : nil ,
122
+ },
123
+ {
124
+ name : "1 namespace" ,
125
+ objects : map [objectset.ObjectKey ]runtime.Object {
126
+ objectset.ObjectKey {Namespace : "ns1" , Name : "a" }: nil ,
127
+ objectset.ObjectKey {Namespace : "ns1" , Name : "b" }: nil ,
128
+ },
129
+ wantNamespaces : []string {"ns1" },
130
+ },
131
+ {
132
+ name : "many namespaces" ,
133
+ objects : map [objectset.ObjectKey ]runtime.Object {
134
+ objectset.ObjectKey {Namespace : "ns1" , Name : "a" }: nil ,
135
+ objectset.ObjectKey {Namespace : "ns2" , Name : "b" }: nil ,
136
+ },
137
+ wantNamespaces : []string {"ns1" , "ns2" },
138
+ },
139
+ {
140
+ name : "many namespaces with duplicates" ,
141
+ objects : map [objectset.ObjectKey ]runtime.Object {
142
+ objectset.ObjectKey {Namespace : "ns1" , Name : "a" }: nil ,
143
+ objectset.ObjectKey {Namespace : "ns2" , Name : "b" }: nil ,
144
+ objectset.ObjectKey {Namespace : "ns1" , Name : "c" }: nil ,
145
+ },
146
+ wantNamespaces : []string {"ns1" , "ns2" },
147
+ },
148
+ {
149
+ name : "missing namespace" ,
150
+ objects : map [objectset.ObjectKey ]runtime.Object {
151
+ objectset.ObjectKey {Namespace : "ns1" , Name : "a" }: nil ,
152
+ objectset.ObjectKey {Name : "b" }: nil ,
153
+ },
154
+ wantNamespaces : []string {"" , "ns1" },
155
+ },
156
+ }
157
+ for _ , tt := range tests {
158
+ t .Run (tt .name , func (t * testing.T ) {
159
+ gotNamespaces := getDistinctNamespaces (tt .objects )
160
+ assert .ElementsMatchf (t , tt .wantNamespaces , gotNamespaces , "getDistinctNamespaces() = %v, want %v" , gotNamespaces , tt .wantNamespaces )
161
+ })
162
+ }
163
+ }
0 commit comments