#!/bin/bash

set -ex

######
# STEP 1. Create a VM and a volume to hotplug
######

cat << END > test-volume.yaml
apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
  name: test-volume
spec:
  source:
    blank: {}
  storage:
    storageClassName: ocs-storagecluster-ceph-rbd
    resources:
      requests:
        storage: 1Gi
END

cat << EOF > test-vm.yaml
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  annotations:
  name: test-vm
spec:
  dataVolumeTemplates:
  - metadata:
      name: test-vm-dv
    spec:
      source:
        registry:
          pullMethod: node
          url: docker://quay.io/kubevirt/fedora-with-test-tooling-container-disk:devel
      storage:
        storageClassName: ocs-storagecluster-ceph-rbd
        resources:
          requests:
            storage: 16Gi
  runStrategy: Always
  template:
    spec:
      architecture: amd64
      domain:
        cpu:
          cores: 2
        devices:
          rng: {}
          disks:
          - disk:
              bus: virtio
            name: fedora
        memory:
          guest: 2Gi
        resources: {}
      volumes:
      - dataVolume:
          name: test-vm-dv 
        name: fedora
EOF

oc create -f test-vm.yaml
oc create -f test-volume.yaml

# wait for vm to come online
sleep 120

oc get vm

######
# STEP 2. Live Migrate vm, which should result in success
######
virtctl migrate test-vm
sleep 60
oc get vmim


######
# STEP 3. Hotplug volume to vm then remove volume
######

virtctl addvolume test-vm --volume-name=test-volume
sleep 30
#virtctl removevolume test-vm --volume-name=test-volume
#sleep 30

######
# STEP 4. Live Migrate, should end in failure
######

virtctl migrate test-vm
sleep 60
oc get vmim
