-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update condition to select right pvc as child for statefulset (#550)
* Update if condition to select right pvc as child for statefulset Signed-off-by: hanzala <muhammad.hanzala@waltlabs.io> * fix indentation Signed-off-by: hanzala <muhammad.hanzala@waltlabs.io> * test(cache): Add tests for isStatefulSetChild function * test(pkg/cache): Replace JSON unmarshalling with structured approach in tests --------- Signed-off-by: hanzala <muhammad.hanzala@waltlabs.io> Co-authored-by: hanzala <muhammad.hanzala@waltlabs.io> Co-authored-by: Obinna Odirionye <odirionye@gmail.com>
- Loading branch information
1 parent
0aecd43
commit 8a3ce6d
Showing
2 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/argoproj/gitops-engine/pkg/utils/kube" | ||
"github.com/stretchr/testify/assert" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"testing" | ||
) | ||
|
||
func Test_isStatefulSetChild(t *testing.T) { | ||
type args struct { | ||
un *unstructured.Unstructured | ||
} | ||
|
||
statefulSet := &appsv1.StatefulSet{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "apps/v1", | ||
Kind: "StatefulSet", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sw-broker", | ||
}, | ||
Spec: appsv1.StatefulSetSpec{ | ||
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "emqx-data", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// Create a new unstructured object from the JSON string | ||
un, err := kube.ToUnstructured(statefulSet) | ||
if err != nil { | ||
t.Errorf("Failed to convert StatefulSet to unstructured: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
checkFunc func(func(kube.ResourceKey) bool) bool | ||
}{ | ||
{ | ||
name: "Valid PVC for sw-broker", | ||
args: args{un: un}, | ||
wantErr: false, | ||
checkFunc: func(fn func(kube.ResourceKey) bool) bool { | ||
// Check a valid PVC name for "sw-broker" | ||
return fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "emqx-data-sw-broker-0"}) | ||
}, | ||
}, | ||
{ | ||
name: "Invalid PVC for sw-broker", | ||
args: args{un: un}, | ||
wantErr: false, | ||
checkFunc: func(fn func(kube.ResourceKey) bool) bool { | ||
// Check an invalid PVC name that should belong to "sw-broker-internal" | ||
return !fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "emqx-data-sw-broker-internal-0"}) | ||
}, | ||
}, | ||
{ | ||
name: "Mismatch PVC for sw-broker", | ||
args: args{un: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "apps/v1", | ||
"kind": "StatefulSet", | ||
"metadata": map[string]interface{}{ | ||
"name": "sw-broker", | ||
}, | ||
"spec": map[string]interface{}{ | ||
"volumeClaimTemplates": []interface{}{ | ||
map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"name": "volume-2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}}, | ||
wantErr: false, | ||
checkFunc: func(fn func(kube.ResourceKey) bool) bool { | ||
// Check an invalid PVC name for "api-test" | ||
return !fn(kube.ResourceKey{Kind: "PersistentVolumeClaim", Name: "volume-2"}) | ||
}, | ||
}, | ||
} | ||
|
||
// Execute test cases | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := isStatefulSetChild(tt.args.un) | ||
assert.Equal(t, tt.wantErr, err != nil, "isStatefulSetChild() error = %v, wantErr %v", err, tt.wantErr) | ||
if err == nil { | ||
assert.True(t, tt.checkFunc(got), "Check function failed for %v", tt.name) | ||
} | ||
}) | ||
} | ||
} |