File: workload_test.go

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (62 lines) | stat: -rw-r--r-- 1,876 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
package mkcw

import (
	"crypto/rand"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestReadWriteWorkloadConfig(t *testing.T) {
	// Create a temporary file to stand in for a disk image.
	temp := filepath.Join(t.TempDir(), "disk.img")
	f, err := os.OpenFile(temp, os.O_CREATE|os.O_RDWR, 0o600)
	require.NoError(t, err)
	err = f.Truncate(0x1000000)
	require.NoError(t, err)
	defer f.Close()

	// Generate a random "encoded workload config".
	workloadConfig := make([]byte, 0x100)
	n, err := rand.Read(workloadConfig)
	require.NoError(t, err)
	require.Equal(t, len(workloadConfig), n)

	// Read the size of our temporary file.
	st, err := f.Stat()
	require.NoError(t, err)
	originalSize := st.Size()

	// Should get an error, since there's no workloadConfig in there to read.
	_, err = ReadWorkloadConfigFromImage(f.Name())
	require.Error(t, err)

	// File should grow, even though we looked for an old config to overwrite.
	err = WriteWorkloadConfigToImage(f, workloadConfig, true)
	require.NoError(t, err)
	st, err = f.Stat()
	require.NoError(t, err)
	require.Greater(t, st.Size(), originalSize)
	originalSize = st.Size()

	// File shouldn't grow, even overwriting the config with a slightly larger one.
	err = WriteWorkloadConfigToImage(f, append([]byte("slightly longer"), workloadConfig...), true)
	require.NoError(t, err)
	st, err = f.Stat()
	require.NoError(t, err)
	require.Equal(t, originalSize, st.Size())
	originalSize = st.Size()

	// File should grow if we're not trying to replace an old one config with a new one.
	err = WriteWorkloadConfigToImage(f, []byte("{\"comment\":\"quite a bit shorter\"}"), false)
	require.NoError(t, err)
	st, err = f.Stat()
	require.NoError(t, err)
	require.Greater(t, st.Size(), originalSize)

	// Should read successfully.
	_, err = ReadWorkloadConfigFromImage(f.Name())
	require.NoError(t, err)
}