File: repository_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 (131 lines) | stat: -rw-r--r-- 3,977 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package bundle

import (
	"os"
	"path/filepath"
	"runtime"
	"testing"

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

func TestUse(t *testing.T) {
	dir := t.TempDir()
	ocBinDir := t.TempDir()

	createDummyBundleContent(t, dir, "crc_libvirt_4.6.1", "1.0")

	repo := &Repository{
		CacheDir: dir,
		OcBinDir: ocBinDir,
	}

	bundle, err := repo.Use("crc_libvirt_4.6.1.crcbundle")
	assert.NoError(t, err)
	assert.Equal(t, "4.6.1", bundle.ClusterInfo.OpenShiftVersion.String())

	bin, err := os.ReadFile(filepath.Join(ocBinDir, constants.OcExecutableName))
	assert.NoError(t, err)
	assert.Equal(t, "openshift-client", string(bin))
}

func TestExtract(t *testing.T) {
	dir := t.TempDir()
	ocBinDir := t.TempDir()

	repo := &Repository{
		CacheDir: dir,
		OcBinDir: ocBinDir,
	}

	assert.NoError(t, repo.Extract(filepath.Join("testdata", testBundle(t))))

	bundle, err := repo.Get(testBundle(t))
	assert.NoError(t, err)
	assert.Equal(t, "4.6.1", bundle.GetOpenshiftVersion())

	_ = os.Remove(bundle.GetKubeConfigPath())
	bundle, err = repo.Get(testBundle(t))
	assert.Nil(t, bundle)
	assert.EqualError(t, err, "kubeconfig not found in bundle")
}

func testBundle(t *testing.T) string {
	switch runtime.GOOS {
	case "darwin":
		return "crc_vfkit_4.6.1.crcbundle"
	case "windows":
		return "crc_hyperv_4.6.1.crcbundle"
	case "linux":
		return "crc_libvirt_4.6.1.crcbundle"
	default:
		t.Fatal("unexpected GOOS")
		return ""
	}
}

func TestVersionCheck(t *testing.T) {
	dir := t.TempDir()

	repo := &Repository{
		CacheDir: dir,
	}

	bundlePath := filepath.Join(dir, "crc_libvirt_4.6.1")
	createDummyBundleContent(t, dir, "crc_libvirt_4.6.1", "0.9")
	_, err := repo.Get("crc_libvirt_4.6.1.crcbundle")
	assert.EqualError(t, err, "cannot use bundle with version 0.9, bundle version must satisfy ^1.0 constraint")
	os.RemoveAll(bundlePath)

	createDummyBundleContent(t, dir, "crc_libvirt_4.6.1", "1.1")
	_, err = repo.Get("crc_libvirt_4.6.1.crcbundle")
	assert.NoError(t, err)
	os.RemoveAll(bundlePath)

	createDummyBundleContent(t, dir, "crc_libvirt_4.6.1", "2.0")
	_, err = repo.Get("crc_libvirt_4.6.1.crcbundle")
	assert.EqualError(t, err, "cannot use bundle with version 2.0, bundle version must satisfy ^1.0 constraint")
	os.RemoveAll(bundlePath)
}

func TestListBundles(t *testing.T) {
	dir := t.TempDir()
	ocBinDir := t.TempDir()

	createDummyBundleContent(t, dir, "crc_libvirt_4.6.15", "1.0")
	createDummyBundleContent(t, dir, "crc_libvirt_4.7.0", "1.0")
	createDummyBundleContent(t, dir, "crc_libvirt_4.7.11_1621489482", "1.0")
	createDummyBundleContent(t, dir, "crc_libvirt_4.8.0-0.nightly-2021-05-26-021757", "1.0")
	createDummyBundleContent(t, dir, "crc_libvirt_4.10.0", "1.0")

	repo := &Repository{
		CacheDir: dir,
		OcBinDir: ocBinDir,
	}

	bundles, err := repo.List()
	assert.NoError(t, err)
	var names []string
	for _, bundle := range bundles {
		names = append(names, bundle.GetBundleName())
	}
	assert.Equal(t, []string{
		"crc_libvirt_4.7.11_1621489482",
		"crc_libvirt_4.10.0",
		"crc_libvirt_4.8.0-0.nightly-2021-05-26-021757",
		"crc_libvirt_4.7.0",
		"crc_libvirt_4.6.15",
	}, names)
}

func createDummyBundleContent(t *testing.T, dir, name, version string) {
	bundleDir := filepath.Join(dir, name)
	assert.NoError(t, os.MkdirAll(bundleDir, 0755))
	assert.NoError(t, os.WriteFile(filepath.Join(bundleDir, metadataFilename), []byte(jsonForBundleWithVersion(version, name)), 0600))
	assert.NoError(t, os.WriteFile(filepath.Join(bundleDir, constants.OcExecutableName), []byte("openshift-client"), 0600))
	assert.NoError(t, os.WriteFile(filepath.Join(bundleDir, "kubeadmin-password"), []byte("kubeadmin-password"), 0600))
	assert.NoError(t, os.WriteFile(filepath.Join(bundleDir, "kubeconfig"), []byte("kubeconfig"), 0600))
	assert.NoError(t, os.WriteFile(filepath.Join(bundleDir, "id_ecdsa_crc"), []byte("id_ecdsa_crc"), 0600))
	assert.NoError(t, os.WriteFile(filepath.Join(bundleDir, "crc.qcow2"), []byte("crc.qcow2"), 0600))
}