<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 2018 Red Hat, Inc.
 *
 */

package tests_test

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime"

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

var _ = Describe("Templates", func() {
	flag.Parse()

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

	var (
		template         *tests.Template
		templateParams   TemplateParams
		templateJsonFile string
		vmJsonFile       string
	)

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

	Describe("Launching VMI from VM Template", func() {

		assertGeneratedVMJson := func() func() {
			return func() {
				By("Generating VM JSON from the Template via oc-process command")
				_, err := runOcProcessCommand(templateJsonFile, templateParams, vmJsonFile)
				Expect(err).ToNot(HaveOccurred())
				Expect(vmJsonFile).To(BeAnExistingFile())
			}
		}

		assertCreatedVM := func() func() {
			return func() {
				By("Creating VM via oc-create command")
				out, err := runOcCreateCommand(vmJsonFile)
				Expect(err).ToNot(HaveOccurred())
				message := fmt.Sprintf("virtualmachine.kubevirt.io \"%s\" created\n", templateParams.Name)
				Expect(out).To(Equal(message))

				By("Checking if the VM exists via oc-get command.")
				Eventually(func() bool {
					out, err := runOcGetCommand("vms")
					Expect(err).ToNot(HaveOccurred())
					return strings.Contains(out, templateParams.Name)
				}, time.Duration(60)*time.Second).Should(BeTrue(), "Timed out waiting for VM to apppear")
			}
		}

		assertDeletedVM := func() func() {
			return func() {
				By("Deleting the VM via oc-delete command")
				out, err := runOcDeleteCommand(templateParams.Name)
				Expect(err).ToNot(HaveOccurred())
				message := fmt.Sprintf("virtualmachine.kubevirt.io \"%s\" deleted\n", templateParams.Name)
				Expect(out).To(Equal(message))

				By("Checking if the VM does not exist anymore via oc-get command.")
				Eventually(func() bool {
					out, err := runOcGetCommand("vms")
					Expect(err).ToNot(HaveOccurred())
					return out == "No resources found.\n"
				}, time.Duration(60)*time.Second).Should(BeTrue(), "Timed out waiting for VM to disappear")
			}
		}

		assertLaunchedVMI := func() func() {
			return func() {
				By("Launching VMI via oc-patch command")
				out, err := runOcPatchCommand(templateParams.Name, "{\"spec\":{\"running\":true}}")
				Expect(err).ToNot(HaveOccurred())
				message := fmt.Sprintf("virtualmachine.kubevirt.io \"%s\" patched\n", templateParams.Name)
				Expect(out).To(Equal(message))

				By("Checking if the VMI does exist via oc-get command")
				Eventually(func() bool {
					out, err := runOcGetCommand("vmis")
					Expect(err).ToNot(HaveOccurred())
					return strings.Contains(out, templateParams.Name)
				}, time.Duration(60)*time.Second).Should(BeTrue(), "Timed out waiting for VMI to appear")
			}
		}

		assertTerminatedVMI := func() func() {
			return func() {
				By("Terminating the VMI via oc-patch command")
				out, err := runOcPatchCommand(templateParams.Name, "{\"spec\":{\"running\":false}}")
				Expect(err).ToNot(HaveOccurred())
				message := fmt.Sprintf("virtualmachine.kubevirt.io \"%s\" patched\n", templateParams.Name)
				Expect(out).To(Equal(message))

				By("Checking if the VMI does not exist anymore via oc-get command")
				Eventually(func() bool {
					out, err := runOcGetCommand("vmis")
					Expect(err).ToNot(HaveOccurred())
					return out == "No resources found.\n"
				}, time.Duration(60)*time.Second).Should(BeTrue(), "Timed out waiting for VMI to disappear")
			}
		}

		assertRemovedFile := func(file string) func() {
			return func() {
				if _, err := os.Stat(file); !os.IsNotExist(err) {
					err := os.Remove(file)
					Expect(err).ToNot(HaveOccurred())
				}
				Expect(file).NotTo(BeAnExistingFile())
			}
		}

		testGivenTemplate := func() {
			It("should succeed to generate a VM JSON file using oc-process command", assertGeneratedVMJson())

			Context("with given VM JSON from the Template", func() {<commit_msg>Changed Expect on assertion for ExpectWithOffset for more accurate logging<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 2018 Red Hat, Inc.
 *
 */

package tests_test

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime"

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

var _ = Describe("Templates", func() {
	flag.Parse()

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

	var (
		template         *tests.Template
		templateParams   TemplateParams
		templateJsonFile string
		vmJsonFile       string
	)

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

	Describe("Launching VMI from VM Template", func() {

		assertGeneratedVMJson := func() func() {
			return func() {
				By("Generating VM JSON from the Template via oc-process command")
				_, err := runOcProcessCommand(templateJsonFile, templateParams, vmJsonFile)
				ExpectWithOffset(1, err).ToNot(HaveOccurred())
				ExpectWithOffset(1, vmJsonFile).To(BeAnExistingFile())
			}
		}

		assertCreatedVM := func() func() {
			return func() {
				By("Creating VM via oc-create command")
				out, err := runOcCreateCommand(vmJsonFile)
				ExpectWithOffset(1, err).ToNot(HaveOccurred())
				message := fmt.Sprintf("virtualmachine.kubevirt.io \"%s\" created\n", templateParams.Name)
				ExpectWithOffset(1, out).To(Equal(message))

				By("Checking if the VM exists via oc-get command.")
				Eventually(func() bool {
					out, err := runOcGetCommand("vms")
					ExpectWithOffset(1, err).ToNot(HaveOccurred())
					return strings.Contains(out, templateParams.Name)
				}, time.Duration(60)*time.Second).Should(BeTrue(), "Timed out waiting for VM to apppear")
			}
		}

		assertDeletedVM := func() func() {
			return func() {
				By("Deleting the VM via oc-delete command")
				out, err := runOcDeleteCommand(templateParams.Name)
				ExpectWithOffset(1, err).ToNot(HaveOccurred())
				message := fmt.Sprintf("virtualmachine.kubevirt.io \"%s\" deleted\n", templateParams.Name)
				ExpectWithOffset(1, out).To(Equal(message))

				By("Checking if the VM does not exist anymore via oc-get command.")
				Eventually(func() bool {
					out, err := runOcGetCommand("vms")
					ExpectWithOffset(1, err).ToNot(HaveOccurred())
					return out == "No resources found.\n"
				}, time.Duration(60)*time.Second).Should(BeTrue(), "Timed out waiting for VM to disappear")
			}
		}

		assertLaunchedVMI := func() func() {
			return func() {
				By("Launching VMI via oc-patch command")
				out, err := runOcPatchCommand(templateParams.Name, "{\"spec\":{\"running\":true}}")
				ExpectWithOffset(1, err).ToNot(HaveOccurred())
				message := fmt.Sprintf("virtualmachine.kubevirt.io \"%s\" patched\n", templateParams.Name)
				ExpectWithOffset(1, out).To(Equal(message))

				By("Checking if the VMI does exist via oc-get command")
				Eventually(func() bool {
					out, err := runOcGetCommand("vmis")
					ExpectWithOffset(1, err).ToNot(HaveOccurred())
					return strings.Contains(out, templateParams.Name)
				}, time.Duration(60)*time.Second).Should(BeTrue(), "Timed out waiting for VMI to appear")
			}
		}

		assertTerminatedVMI := func() func() {
			return func() {
				By("Terminating the VMI via oc-patch command")
				out, err := runOcPatchCommand(templateParams.Name, "{\"spec\":{\"running\":false}}")
				ExpectWithOffset(1, err).ToNot(HaveOccurred())
				message := fmt.Sprintf("virtualmachine.kubevirt.io \"%s\" patched\n", templateParams.Name)
				ExpectWithOffset(1, out).To(Equal(message))

				By("Checking if the VMI does not exist anymore via oc-get command")
				Eventually(func() bool {
					out, err := runOcGetCommand("vmis")
					ExpectWithOffset(1, err).ToNot(HaveOccurred())
					return out == "No resources found.\n"
				}, time.Duration(60)*time.Second).Should(BeTrue(), "Timed out waiting for VMI to disappear")
			}
		}

		assertRemovedFile := func(file string) func() {
			return func() {
				if _, err := os.Stat(file); !os.IsNotExist(err) {
					err := os.Remove(file)
					ExpectWithOffset(1, err).ToNot(HaveOccurred())
				}
				ExpectWithOffset(1, file).NotTo(BeAnExistingFile())
			}
		}

		testGivenTemplate := func() {
			It("should succeed to generate a VM JSON file using oc-process command", assertGeneratedVMJson())

			Context("with given VM JSON from the Template", func() {
{'fraction_non_alphanumeric': 0.13985456718796693, 'fraction_numerical': 0.006175913935650961, 'mean_word_length': 3.878522837706511, 'pattern_counts': {'":': 8, '<': 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': '2722428', 'n_tokens_mistral': 3777, 'n_tokens_neox': 3253, 'n_words': 799}