@@ -1818,6 +1818,12 @@ func testSetup(t *testing.T) (context.Context, *store, *integration.ClusterV3) {
1818
1818
func testPropogateStore (ctx context.Context , t * testing.T , store * store , obj * example.Pod ) (string , * example.Pod ) {
1819
1819
// Setup store with a key and grab the output for returning.
1820
1820
key := "/testkey"
1821
+ return key , testPropogateStoreWithKey (ctx , t , store , key , obj )
1822
+ }
1823
+
1824
+ // testPropogateStoreWithKey helps propagate store with objects, the given object will be stored at the specified key.
1825
+ func testPropogateStoreWithKey (ctx context.Context , t * testing.T , store * store , key string , obj * example.Pod ) * example.Pod {
1826
+ // Setup store with the specified key and grab the output for returning.
1821
1827
v , err := conversion .EnforcePtr (obj )
1822
1828
if err != nil {
1823
1829
panic ("unable to convert output object to pointer" )
@@ -1830,7 +1836,7 @@ func testPropogateStore(ctx context.Context, t *testing.T, store *store, obj *ex
1830
1836
if err := store .Create (ctx , key , obj , setOutput , 0 ); err != nil {
1831
1837
t .Fatalf ("Set failed: %v" , err )
1832
1838
}
1833
- return key , setOutput
1839
+ return setOutput
1834
1840
}
1835
1841
1836
1842
func TestPrefix (t * testing.T ) {
@@ -2075,3 +2081,41 @@ func TestConsistentList(t *testing.T) {
2075
2081
}
2076
2082
2077
2083
}
2084
+
2085
+ func TestCount (t * testing.T ) {
2086
+ ctx , store , cluster := testSetup (t )
2087
+ defer cluster .Terminate (t )
2088
+
2089
+ resourceA := "/foo.bar.io/abc"
2090
+
2091
+ // resourceA is intentionally a prefix of resourceB to ensure that the count
2092
+ // for resourceA does not include any objects from resourceB.
2093
+ resourceB := fmt .Sprintf ("%sdef" , resourceA )
2094
+
2095
+ resourceACountExpected := 5
2096
+ for i := 1 ; i <= resourceACountExpected ; i ++ {
2097
+ obj := & example.Pod {ObjectMeta : metav1.ObjectMeta {Name : fmt .Sprintf ("foo-%d" , i )}}
2098
+
2099
+ key := fmt .Sprintf ("%s/%d" , resourceA , i )
2100
+ testPropogateStoreWithKey (ctx , t , store , key , obj )
2101
+ }
2102
+
2103
+ resourceBCount := 4
2104
+ for i := 1 ; i <= resourceBCount ; i ++ {
2105
+ obj := & example.Pod {ObjectMeta : metav1.ObjectMeta {Name : fmt .Sprintf ("foo-%d" , i )}}
2106
+
2107
+ key := fmt .Sprintf ("%s/%d" , resourceB , i )
2108
+ testPropogateStoreWithKey (ctx , t , store , key , obj )
2109
+ }
2110
+
2111
+ resourceACountGot , err := store .Count (resourceA )
2112
+ if err != nil {
2113
+ t .Fatalf ("store.Count failed: %v" , err )
2114
+ }
2115
+
2116
+ // count for resourceA should not include the objects for resourceB
2117
+ // even though resourceA is a prefix of resourceB.
2118
+ if int64 (resourceACountExpected ) != resourceACountGot {
2119
+ t .Fatalf ("store.Count for resource %s: expected %d but got %d" , resourceA , resourceACountExpected , resourceACountGot )
2120
+ }
2121
+ }
0 commit comments