<commit_before>package importer_test

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"kubevirt.io/containerized-data-importer/pkg/image"
	imagesize "kubevirt.io/containerized-data-importer/pkg/lib/size"
	"kubevirt.io/containerized-data-importer/tests/utils"
	"kubevirt.io/containerized-data-importer/pkg/common"
	"kubevirt.io/containerized-data-importer/pkg/util"
	"syscall"
	"kubevirt.io/containerized-data-importer/pkg/importer"
)

var _ = Describe("Streaming Data Conversion", func() {

	Context("when data is in a supported file format", func() {

		tinyCoreIso := "tinyCore.iso"
		baseTestImage, err := filepath.Abs(filepath.Join(importer.TestImagesDir, tinyCoreIso))
		if err != nil {
			Fail(fmt.Sprintf("Error getting abs path: %v\n", err))
		}

		var tmpTestDir string
		BeforeEach(func() {
			tmpTestDir = testDir(os.TempDir())
			if err != nil {
				Fail(fmt.Sprintf("[BeforeEach] Failed created test dir: %v\n", err))
			}
			By(fmt.Sprintf("[BeforeEach] Creating temporary dir %s", tmpTestDir))
			syscall.Umask(0000)
			err = os.Mkdir(tmpTestDir, 0777)
			if err != nil {
				Fail(fmt.Sprintf("Could not create tmp file: %v\n ", err))
			}
		})

		AfterEach(func() {
			By(fmt.Sprintf("[AfterEach] Cleaning up temporary dir %s", tmpTestDir))
			os.RemoveAll(tmpTestDir)
		})

		// Test Table
		tests := []struct {
			testDesc      string
			originalFile  string
			useVirtSize   bool
			expectFormats []string
		}{
			{
				testDesc:      "should decompress gzip",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtGz},
			},
			{
				testDesc:      "should decompress xz",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtXz},
			},
			{
				testDesc:      "should unarchive tar",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtTar},
			},
			{
				testDesc:      "should unpack .tar.gz",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtTar, image.ExtGz},
			},
			{
				testDesc:      "should unpack .tar.xz",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtTar, image.ExtXz},
			},
			{
				testDesc:      "should convert .qcow2",
				originalFile:  baseTestImage,
				useVirtSize:   true,
				expectFormats: []string{image.ExtQcow2},
			},
			{
				testDesc:      "should convert and unpack .qcow2.gz",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtQcow2, image.ExtGz},
			},<commit_msg>Disable .tar.xz until issue 335 is resolved (#361)<commit_after>package importer_test

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"kubevirt.io/containerized-data-importer/pkg/image"
	imagesize "kubevirt.io/containerized-data-importer/pkg/lib/size"
	"kubevirt.io/containerized-data-importer/tests/utils"
	"kubevirt.io/containerized-data-importer/pkg/common"
	"kubevirt.io/containerized-data-importer/pkg/util"
	"syscall"
	"kubevirt.io/containerized-data-importer/pkg/importer"
)

var _ = Describe("Streaming Data Conversion", func() {

	Context("when data is in a supported file format", func() {

		tinyCoreIso := "tinyCore.iso"
		baseTestImage, err := filepath.Abs(filepath.Join(importer.TestImagesDir, tinyCoreIso))
		if err != nil {
			Fail(fmt.Sprintf("Error getting abs path: %v\n", err))
		}

		var tmpTestDir string
		BeforeEach(func() {
			tmpTestDir = testDir(os.TempDir())
			if err != nil {
				Fail(fmt.Sprintf("[BeforeEach] Failed created test dir: %v\n", err))
			}
			By(fmt.Sprintf("[BeforeEach] Creating temporary dir %s", tmpTestDir))
			syscall.Umask(0000)
			err = os.Mkdir(tmpTestDir, 0777)
			if err != nil {
				Fail(fmt.Sprintf("Could not create tmp file: %v\n ", err))
			}
		})

		AfterEach(func() {
			By(fmt.Sprintf("[AfterEach] Cleaning up temporary dir %s", tmpTestDir))
			os.RemoveAll(tmpTestDir)
		})

		// Test Table
		tests := []struct {
			testDesc      string
			originalFile  string
			useVirtSize   bool
			expectFormats []string
		}{
			{
				testDesc:      "should decompress gzip",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtGz},
			},
			{
				testDesc:      "should decompress xz",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtXz},
			},
			{
				testDesc:      "should unarchive tar",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtTar},
			},
			{
				testDesc:      "should unpack .tar.gz",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtTar, image.ExtGz},
			},
			// Disabled until issue 335 is resolved
			// https://github.com/kubevirt/containerized-data-importer/issues/335
			//{
			//	testDesc:      "should unpack .tar.xz",
			//	originalFile:  baseTestImage,
			//	useVirtSize:   false,
			//	expectFormats: []string{image.ExtTar, image.ExtXz},
			//},
			{
				testDesc:      "should convert .qcow2",
				originalFile:  baseTestImage,
				useVirtSize:   true,
				expectFormats: []string{image.ExtQcow2},
			},
			{
				testDesc:      "should convert and unpack .qcow2.gz",
				originalFile:  baseTestImage,
				useVirtSize:   false,
				expectFormats: []string{image.ExtQcow2, image.ExtGz},
			},
{'fraction_non_alphanumeric': 0.13619099294628323, 'fraction_numerical': 0.0065111231687466084, 'mean_word_length': 3.2604006163328196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '6186788', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2000, 'n_words': 374}