File: copier_test.go

package info (click to toggle)
golang-github-crc-org-crc 2.34.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,548 kB
  • sloc: sh: 398; makefile: 326; javascript: 40
file content (78 lines) | stat: -rw-r--r-- 1,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package bundle

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"testing"

	crcos "github.com/crc-org/crc/v2/pkg/os"
	"github.com/stretchr/testify/assert"
)

func TestGenerateBundle(t *testing.T) {
	var b CrcBundleInfo
	assert.NoError(t, json.Unmarshal([]byte(jsonForBundle("crc_4.7.1")), &b))

	tmpBundleDir := t.TempDir()
	b.cachedPath = filepath.Join(tmpBundleDir, b.Name)

	createDummyBundleFiles(t, &b)

	srcDir := t.TempDir()

	customBundleName := "custom_bundle"
	copier, err := NewCopier(&b, srcDir, customBundleName)
	assert.NoError(t, err)

	assert.NoError(t, copier.CopyKubeConfig())

	assert.NoError(t, copier.CopyPrivateSSHKey(copier.srcBundle.GetSSHKeyPath()))
	assert.NoError(t, copier.CopyFilesFromFileList())

	err = crcos.CopyFileContents(copier.srcBundle.GetDiskImagePath(), copier.copiedBundle.GetDiskImagePath(), 0644)
	assert.NoError(t, err)
	assert.NoError(t, copier.SetDiskImage(copier.copiedBundle.GetDiskImagePath(), "qcow2"))

	assert.NoError(t, copier.GenerateBundle(customBundleName))
	defer os.Remove(fmt.Sprintf("%s%s", customBundleName, bundleExtension))
}

func TestGetType(t *testing.T) {
	type data struct {
		value         string
		expectedValue string
	}
	testdata := []data{
		{value: "snc", expectedValue: "snc_custom"},
		{value: "podman", expectedValue: "podman_custom"},
		{value: "snc_custom", expectedValue: "snc_custom"},
		{value: "podman_custom", expectedValue: "podman_custom"},
	}
	for _, d := range testdata {
		assert.EqualValues(t, d.expectedValue, getType(d.value))
	}
}

func createDummyBundleFiles(t *testing.T, bundle *CrcBundleInfo) {
	assert.NoError(t, os.MkdirAll(bundle.cachedPath, 0750))

	files := []string{
		bundle.GetOcPath(),
		bundle.GetKubeConfigPath(),
		bundle.GetSSHKeyPath(),
		bundle.GetDiskImagePath(),
		bundle.GetKernelPath(),
		bundle.GetInitramfsPath(),
	}

	for _, file := range files {
		if file == "" {
			continue
		}
		fd, err := os.Create(file)
		fd.Close()
		assert.NoError(t, err)
	}
}