-
Notifications
You must be signed in to change notification settings - Fork 38
CMP-3805: Add test for configurable access modes and Storage class (34928) #999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6ba0a05
a075c80
5f89136
b981913
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -613,3 +613,114 @@ func getTestMetricsCMD(namespace string) string { | |
| func GetPoolNodeRoleSelector() map[string]string { | ||
| return utils.GetNodeRoleSelector(TestPoolName) | ||
| } | ||
|
|
||
| // GetDefaultStorageClassProvisioner retrieves the provisioner from the default storage class | ||
| func (f *Framework) GetDefaultStorageClassProvisioner() (string, error) { | ||
| var scList unstructured.UnstructuredList | ||
| scList.SetGroupVersionKind(schema.GroupVersionKind{ | ||
| Group: "storage.k8s.io", | ||
| Version: "v1", | ||
| Kind: "StorageClassList", | ||
| }) | ||
|
|
||
| if err := f.Client.List(context.TODO(), &scList); err != nil { | ||
| return "", fmt.Errorf("failed to list storage classes: %w", err) | ||
| } | ||
|
|
||
| for _, sc := range scList.Items { | ||
| annotations := sc.GetAnnotations() | ||
| if annotations != nil { | ||
| if isDefault, ok := annotations["storageclass.kubernetes.io/is-default-class"]; ok && isDefault == "true" { | ||
| provisioner, found, err := unstructured.NestedString(sc.Object, "provisioner") | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get provisioner from storage class: %w", err) | ||
| } | ||
| if !found { | ||
| return "", fmt.Errorf("provisioner field not found in default storage class") | ||
| } | ||
| return provisioner, nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return "", fmt.Errorf("no default storage class found in the cluster") | ||
| } | ||
|
|
||
| // CreateCustomStorageClass creates a custom StorageClass object | ||
| func (f *Framework) CreateCustomStorageClass(name, provisioner string) (*unstructured.Unstructured, error) { | ||
| sc := &unstructured.Unstructured{} | ||
| sc.SetGroupVersionKind(schema.GroupVersionKind{ | ||
| Group: "storage.k8s.io", | ||
| Version: "v1", | ||
| Kind: "StorageClass", | ||
| }) | ||
| sc.SetName(name) | ||
| if err := unstructured.SetNestedField(sc.Object, provisioner, "provisioner"); err != nil { | ||
| return nil, fmt.Errorf("failed to set provisioner field: %w", err) | ||
| } | ||
| return sc, nil | ||
| } | ||
|
|
||
| // AssertScanPVCHasStorageConfig verifies that the PVC for a scan has the expected storage configuration | ||
| func (f *Framework) AssertScanPVCHasStorageConfig(scanName, namespace, expectedStorageClassName string, expectedAccessMode corev1.PersistentVolumeAccessMode) error { | ||
rhmdnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // List all PVCs in the namespace | ||
| pvcList := &corev1.PersistentVolumeClaimList{} | ||
| listOpts := &client.ListOptions{ | ||
| Namespace: namespace, | ||
| } | ||
| if err := f.Client.List(context.TODO(), pvcList, listOpts); err != nil { | ||
| return fmt.Errorf("failed to list PVCs: %w", err) | ||
| } | ||
|
|
||
| // Find the PVC for the scan | ||
| var scanPVC *corev1.PersistentVolumeClaim | ||
| for i := range pvcList.Items { | ||
| pvc := &pvcList.Items[i] | ||
| // Check if this PVC belongs to our scan | ||
| if pvc.Labels != nil { | ||
| if scanLabel, ok := pvc.Labels[compv1alpha1.ComplianceScanLabel]; ok && scanLabel == scanName { | ||
| scanPVC = pvc | ||
| break | ||
| } | ||
| } | ||
| // Alternative: check if the PVC name contains our scan name | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might not be necessary since the label should always map to the scan name (a containment check on strings will be more fragile than checking an exact match).
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If one scan is named |
||
| if strings.Contains(pvc.Name, scanName) { | ||
| scanPVC = pvc | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if scanPVC == nil { | ||
| return fmt.Errorf("no PVC found for scan %s", scanName) | ||
| } | ||
|
|
||
| // Verify PVC has the correct storageClassName | ||
| if scanPVC.Spec.StorageClassName == nil { | ||
| return fmt.Errorf("PVC %s has nil storageClassName", scanPVC.Name) | ||
| } | ||
| if *scanPVC.Spec.StorageClassName != expectedStorageClassName { | ||
| return fmt.Errorf("expected PVC storageClassName to be %s, got %s", expectedStorageClassName, *scanPVC.Spec.StorageClassName) | ||
| } | ||
|
|
||
| // Verify PVC has the correct accessModes | ||
| if len(scanPVC.Spec.AccessModes) == 0 { | ||
| return fmt.Errorf("PVC %s has no access modes", scanPVC.Name) | ||
| } | ||
|
|
||
| found := false | ||
| for _, mode := range scanPVC.Spec.AccessModes { | ||
| if mode == expectedAccessMode { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if !found { | ||
| return fmt.Errorf("expected PVC to have access mode %s, got %v", expectedAccessMode, scanPVC.Spec.AccessModes) | ||
| } | ||
|
|
||
| log.Printf("Successfully verified PVC %s has storageClassName=%s and accessModes=%v\n", | ||
| scanPVC.Name, *scanPVC.Spec.StorageClassName, scanPVC.Spec.AccessModes) | ||
|
|
||
| return nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.