<commit_before>/*
 * This file is part of the KubeVirt project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright 2017 Red Hat, Inc.
 *
 */

package tests_test

import (
	"flag"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	v12 "k8s.io/apimachinery/pkg/apis/meta/v1"

	// "github.com/onsi/ginkgo/extensions/table"

	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/util/rand"

	"time"

	"kubevirt.io/kubevirt/pkg/api/v1"
	"kubevirt.io/kubevirt/pkg/kubecli"
	"kubevirt.io/kubevirt/tests"
)

var _ = Describe("OfflineVirtualMachine", func() {

	flag.Parse()

	virtClient, err := kubecli.GetKubevirtClient()
	tests.PanicOnError(err)

	BeforeEach(func() {
		tests.BeforeTestCleanup()
	})

	Context("A valid OfflineVirtualMachine given", func() {

		newOfflineVirtualMachine := func() *v1.OfflineVirtualMachine {
			template := tests.NewRandomVMWithEphemeralDisk("kubevirt/cirros-registry-disk-demo:devel")
			newOVM := NewRandomOfflineVirtualMachine(template, true)
			newOVM, err = virtClient.OfflineVirtualMachine(tests.NamespaceTestDefault).Create(newOVM)
			Expect(err).ToNot(HaveOccurred())
			return newOVM
		}

		It("should update OfflineVirtualMachine once VMs are up", func() {
			newOVM := newOfflineVirtualMachine()
			Eventually(func() v1.OfflineVirtualMachineConditionType {
				ovm, err := virtClient.OfflineVirtualMachine(tests.NamespaceTestDefault).Get(newOVM.Name, &v12.GetOptions{})
				Expect(err).ToNot(HaveOccurred())
				return ovm.Status.Conditions[0].Type
			}, 120*time.Second, 1*time.Second).Should(Equal("Running"))
		})

		It("should remove VM once the OVM is marked for deletion", func() {
			newOVM := newOfflineVirtualMachine()
			// Create a offlinevm with vm
			// Delete it
			Expect(virtClient.OfflineVirtualMachine(newOVM.Namespace).Delete(newOVM.Name, &v12.DeleteOptions{})).To(Succeed())
			// Wait until VMs are gone
			Eventually(func() int {
				vms, err := virtClient.VM(newOVM.Namespace).List(v12.ListOptions{})
				Expect(err).ToNot(HaveOccurred())
				return len(vms.Items)
			}, 60*time.Second, 1*time.Second).Should(BeZero())
		})

		It("should remove owner references on the VM if it is orphan deleted", func() {
			newOVM := newOfflineVirtualMachine()

			// Check for owner reference
			vms, err := virtClient.VM(newOVM.Namespace).List(v12.ListOptions{})
			Expect(vms.Items).To(HaveLen(1))
			for _, vm := range vms.Items {
				Expect(vm.OwnerReferences).ToNot(BeEmpty())
			}

			// Delete it
			orphanPolicy := v12.DeletePropagationOrphan
			Expect(virtClient.OfflineVirtualMachine(newOVM.Namespace).
				Delete(newOVM.Name, &v12.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
			// Wait until the replica set is deleted
			Eventually(func() bool {
				_, err := virtClient.OfflineVirtualMachine(newOVM.Namespace).Get(newOVM.Name, &v12.GetOptions{})
				if errors.IsNotFound(err) {
					return true
				}
				return false
			}, 60*time.Second, 1*time.Second).Should(BeTrue())

			vms, err = virtClient.VM(newOVM.Namespace).List(v12.ListOptions{})
			Expect(vms.Items).To(HaveLen(2))
			for _, vm := range vms.Items {
				Expect(vm.OwnerReferences).To(BeEmpty())
			}
			Expect(err).ToNot(HaveOccurred())
		})
	})
})

// NewRandomOfflineVirtualMachine creates new OfflineVirtualMachine
func NewRandomOfflineVirtualMachine(vm *v1.VirtualMachine, running bool) *v1.OfflineVirtualMachine {
	name := "offlinevm" + rand.String(5)
	ovm := &v1.OfflineVirtualMachine{
		ObjectMeta: v12.ObjectMeta{Name: name},
		Spec: v1.OfflineVirtualMachineSpec{
			Template: &v1.VMTemplateSpec{
				ObjectMeta: v12.ObjectMeta{<commit_msg>Fix system tests for ovm<commit_after>/*
 * This file is part of the KubeVirt project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright 2017 Red Hat, Inc.
 *
 */

package tests_test

import (
	"flag"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	v12 "k8s.io/apimachinery/pkg/apis/meta/v1"

	// "github.com/onsi/ginkgo/extensions/table"

	"k8s.io/apimachinery/pkg/api/errors"

	"time"

	"kubevirt.io/kubevirt/pkg/api/v1"
	"kubevirt.io/kubevirt/pkg/kubecli"
	"kubevirt.io/kubevirt/tests"
)

var _ = Describe("OfflineVirtualMachine", func() {

	flag.Parse()

	virtClient, err := kubecli.GetKubevirtClient()
	tests.PanicOnError(err)

	BeforeEach(func() {
		tests.BeforeTestCleanup()
	})

	Context("A valid OfflineVirtualMachine given", func() {

		newOfflineVirtualMachine := func() *v1.OfflineVirtualMachine {
			template := tests.NewRandomVMWithEphemeralDisk("kubevirt/cirros-registry-disk-demo:devel")
			newOVM := NewRandomOfflineVirtualMachine(template, true)
			newOVM, err = virtClient.OfflineVirtualMachine(tests.NamespaceTestDefault).Create(newOVM)
			Expect(err).ToNot(HaveOccurred())
			return newOVM
		}

		It("should update OfflineVirtualMachine once VMs are up", func() {
			newOVM := newOfflineVirtualMachine()
			Eventually(func() v1.OfflineVirtualMachineConditionType {
				ovm, err := virtClient.OfflineVirtualMachine(tests.NamespaceTestDefault).Get(newOVM.Name, &v12.GetOptions{})
				Expect(err).ToNot(HaveOccurred())
				return ovm.Status.Conditions[0].Type
			}, 120*time.Second, 1*time.Second).Should(Equal(v1.OfflineVirtualMachineRunning))
		})

		It("should remove VM once the OVM is marked for deletion", func() {
			newOVM := newOfflineVirtualMachine()
			// Create a offlinevm with vm
			// Delete it
			Expect(virtClient.OfflineVirtualMachine(newOVM.Namespace).Delete(newOVM.Name, &v12.DeleteOptions{})).To(Succeed())
			// Wait until VMs are gone
			Eventually(func() int {
				vms, err := virtClient.VM(newOVM.Namespace).List(v12.ListOptions{})
				Expect(err).ToNot(HaveOccurred())
				return len(vms.Items)
			}, 120*time.Second, 2*time.Second).Should(BeZero())
		})

		It("should remove owner references on the VM if it is orphan deleted", func() {
			newOVM := newOfflineVirtualMachine()

			// Check for owner reference
			vms, _ := virtClient.VM(newOVM.Namespace).List(v12.ListOptions{})
			Expect(vms.Items).To(HaveLen(1))
			for _, vm := range vms.Items {
				Expect(vm.OwnerReferences).ToNot(BeEmpty())
			}

			// Delete it
			orphanPolicy := v12.DeletePropagationOrphan
			Expect(virtClient.OfflineVirtualMachine(newOVM.Namespace).
				Delete(newOVM.Name, &v12.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
			// Wait until the offlinevm is deleted
			Eventually(func() bool {
				_, err := virtClient.OfflineVirtualMachine(newOVM.Namespace).Get(newOVM.Name, &v12.GetOptions{})
				if errors.IsNotFound(err) {
					return true
				}
				return false
			}, 60*time.Second, 1*time.Second).Should(BeTrue())

			vms, err = virtClient.VM(newOVM.Namespace).List(v12.ListOptions{})
			Expect(vms.Items).To(HaveLen(1))
			for _, vm := range vms.Items {
				Expect(vm.OwnerReferences).To(BeEmpty())
			}
			Expect(err).ToNot(HaveOccurred())
		})
	})
})

// NewRandomOfflineVirtualMachine creates new OfflineVirtualMachine
func NewRandomOfflineVirtualMachine(vm *v1.VirtualMachine, running bool) *v1.OfflineVirtualMachine {
	name := vm.Name
	ovm := &v1.OfflineVirtualMachine{
		ObjectMeta: v12.ObjectMeta{Name: name},
		Spec: v1.OfflineVirtualMachineSpec{
			Running: true,
			Template: &v1.VMTemplateSpec{
				ObjectMeta: v12.ObjectMeta{
{'fraction_non_alphanumeric': 0.12809015996122153, 'fraction_numerical': 0.013572467280659235, 'mean_word_length': 4.3976455199476785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5478407', 'n_tokens_mistral': 3087, 'n_tokens_neox': 2731, 'n_words': 569}