Module tests.test_kubernetes

Functions

def delete_and_wait_statefulset_only(api, ss)
def provision_and_wait_pv(client, core_api, storage_class, pvc)

Provision a new Longhorn Volume via Storage Class and wait for the Volume and its associated resources to be created.

This method also waits for the Kubernetes Status to be properly set on the Volume.

:param client: An instance of the Longhorn client. :param core_api: An instance of the Kubernetes CoreV1API client. :param storage_class: A dict representing a Storage Class spec. :param pvc: A dict representing a Persistent Volume Claim spec. :return: The Persistent Volume that was provisioned.

def test_backup_kubernetes_status(set_random_backupstore, client, core_api, pod, storage_class)

Test that Backups have KubernetesStatus stored properly when there is an associated PersistentVolumeClaim and Pod.

  1. Setup a random backupstore
  2. Set settings Longhorn Static StorageClass to longhorn-test
  3. Create a volume and PV/PVC. Verify the StorageClass of PVC
  4. Create a Pod using the PVC.
  5. Check volume's Kubernetes status to reflect PV/PVC/Pod correctly.
  6. Create a backup for the volume.
  7. Verify the labels of created backup reflect PV/PVC/Pod status.
  8. Restore the backup to a volume. Wait for restoration to complete.
  9. Check the volume's Kubernetes Status
    1. Make sure the lastPodRefAt and lastPVCRefAt is snapshot created time
  10. Delete the backup and restored volume.
  11. Delete PV/PVC/Pod.
  12. Verify volume's Kubernetes Status updated to reflect history data.
  13. Attach the volume and create another backup. Verify the labels
  14. Verify the volume's Kubernetes status.
  15. Restore the previous backup to a new volume. Wait for restoration.
  16. Verify the restored volume's Kubernetes status.
    1. Make sure lastPodRefAt and lastPVCRefAt matched volume on step 12
def test_csi_umount_when_longhorn_block_device_is_disconnected_unexpectedly(client, core_api, statefulset, storage_class)

Test CSI umount when Longhorn block device is disconnected unexpectedly

GitHub ticket: https://github.com/longhorn/longhorn/issues/3778

  1. Deloy a statefulset that has volumeClaimTemplates with volumeMode: Block
  2. Crash the engine process of the volume to simulate Longhorn block device is disconnected unexpectedly
  3. Delete the workload pod
  4. Verify that the pod is able to terminated and a new pod is able start
def test_delete_provisioned_pvc(client, core_api, storage_class, pvc)

Test that deleting the Persistent Volume Claim for a dynamically provisioned Volume properly deletes the Volume and the associated Kubernetes resources.

  1. Create a Storage Class to test with.
  2. Create a Persistent Volume Claim that requests a Volume from that Storage Class.
  3. Wait for the Volume to be provisioned and for the Kubernetes Status to be updated correctly.
  4. Attempt to delete the Persistent Volume Claim.
  5. Verify that the associated Volume and its resources have been deleted.
def test_delete_with_provisioned_pv(client, core_api, storage_class, pvc)

Test that deleting a Volume with dynamically provisioned Persistent Volume and Persistent Volume Claim resources successfully deletes the Volume and cleans up those resources.

  1. Create a Storage Class to test with.
  2. Create a Persistent Volume Claim that requests a Volume from that Storage Class.
  3. Wait for the Volume to be provisioned and for the Kubernetes Status to be updated correctly.
  4. Attempt to delete the Volume.
  5. Verify that the Volume and its associated resources have been deleted.
def test_delete_with_static_pv(client, core_api, volume_name)

Test that deleting a Volume with related static Persistent Volume and Persistent Volume Claim resources successfully deletes the Volume and cleans up those resources.

  1. Create a Volume in Longhorn.
  2. Create a static Persistent Volume and Persistent Volume Claim for the Volume through Longhorn.
  3. Wait for the Kubernetes Status to indicate the existence of these resources.
  4. Attempt deletion of the Volume.
  5. Verify that the Volume and its associated resources have been deleted.
def test_kubernetes_status(client, core_api, storage_class, statefulset, csi_pv, pvc, pod)

Test Volume feature: Kubernetes Status

  1. Create StorageClass with reclaimPolicy = Retain
  2. Create a statefulset kubernetes-status-test with the StorageClass
    1. The statefulset has scale of 2.
  3. Get the volume name from the SECOND pod of the StateufulSet pod and create an extra_pod with the same volume on the same node
  4. Check the volumes that used by the StatefulSet
    1. The volume used by the FIRST StatefulSet pod will have one workload
    2. The volume used by the SECOND StatefulSet pod will have two workloads
    3. Validate related status, e.g. pv/pod name/state, workload name/type
  5. Check the volumes again
    1. PV/PVC should still be bound
    2. The volume used by the FIRST pod should have history data
    3. The volume used by the SECOND and extra pod should have current data point to the extra pod
  6. Delete the extra pod
    1. Now all the volume's should only have history data(lastPodRefAt set)
  7. Delete the PVC
    1. PVC should be updated with status Released and become history data
  8. Delete PV
    1. All the Kubernetes status information should be cleaned up.
  9. Reuse the two Longhorn volumes to create new pods
    1. Since the reclaimPolicy == Retain, volume won't be deleted by Longhorn
    2. Check the Kubernetes status now updated, with pod info but empty workload
    3. Default Longhorn Static StorageClass will remove the PV with PVC, but leave Longhorn volume
def test_pv_creation(client, core_api)

Test creating PV using Longhorn API

  1. Create volume
  2. Create PV for the volume
  3. Try to create another PV for the same volume. It should fail.
  4. Check Kubernetes Status for the volume since PV is created.
def test_pvc_creation_with_default_sc_set(client, core_api, storage_class, pod)

Test creating PVC with default StorageClass set

The target is to make sure the newly create PV/PVC won't use default StorageClass, and if there is no default StorageClass, PV/PVC can still be created.

  1. Create a StorageClass and set it to be the default StorageClass
  2. Update static StorageClass to longhorn-test
  3. Create volume then PV/PVC.
  4. Make sure the newly created PV/PVC using StorageClass longhorn-test
  5. Create pod with PVC.
  6. Verify volume's Kubernetes Status
  7. Remove PVC and Pod.
  8. Verify volume's Kubernetes Status only contains current PV and history
  9. Wait for volume to detach (since pod is deleted)
  10. Reuse the volume on a new pod. Wait for the pod to start
  11. Verify volume's Kubernetes Status reflect the new pod.
  12. Delete PV/PVC/Pod.
  13. Verify volume's Kubernetes Status only contains history
  14. Delete the default StorageClass.
  15. Create PV/PVC for the volume.
  16. Make sure the PV's StorageClass is static StorageClass