-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathartifactsinputoutput_test.go
More file actions
193 lines (164 loc) · 6.37 KB
/
Copy pathartifactsinputoutput_test.go
File metadata and controls
193 lines (164 loc) · 6.37 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package imagecustomizerlib
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/microsoft/azurelinux/toolkit/tools/imagecustomizerapi"
"github.com/microsoft/azurelinux/toolkit/tools/internal/file"
"github.com/stretchr/testify/assert"
)
func TestOutputAndInjectArtifacts(t *testing.T) {
baseImage, _ := checkSkipForCustomizeDefaultImage(t)
ukifyExists, err := file.CommandExists("ukify")
assert.NoError(t, err)
if !ukifyExists {
t.Skip("The 'ukify' command is not available")
}
testTempDir := filepath.Join(tmpDir, "TestOutputAndInjectArtifacts")
buildDir := filepath.Join(testTempDir, "build")
outImageFilePath := filepath.Join(testTempDir, "image.raw")
originalConfigFile := filepath.Join(testDir, "artifacts-output.yaml")
configFile := filepath.Join(testTempDir, "artifacts-output.yaml")
outputArtifactsDir := filepath.Join(testTempDir, "output")
// Copy test config to the temp dir so it's isolated
err = file.Copy(originalConfigFile, configFile)
assert.NoError(t, err)
// Customize image
err = CustomizeImageWithConfigFile(buildDir, configFile, baseImage, nil, outImageFilePath, "raw",
true /*useBaseImageRpmRepos*/, "" /*packageSnapshotTime*/)
if !assert.NoError(t, err) {
return
}
// Confirm inject-files.yaml was generated
injectConfigPath := filepath.Join(outputArtifactsDir, "inject-files.yaml")
exists, err := file.PathExists(injectConfigPath)
assert.NoError(t, err)
assert.True(t, exists, "Expected inject-files.yaml to be generated")
var injectConfig imagecustomizerapi.InjectFilesConfig
err = imagecustomizerapi.UnmarshalYamlFile(injectConfigPath, &injectConfig)
assert.NoError(t, err)
// Check previewFeatures
assert.Contains(t, injectConfig.PreviewFeatures, imagecustomizerapi.PreviewFeatureInjectFiles, "Expected previewFeatures to include 'inject-files'")
// Check artifacts
hasShim := false
hasSystemdBoot := false
hasUKI := false
for _, entry := range injectConfig.InjectFiles {
switch {
case strings.HasPrefix(entry.Destination, "/EFI/BOOT/boot") &&
strings.HasSuffix(entry.Destination, ".efi") &&
strings.HasPrefix(entry.Source, "./boot") &&
strings.HasSuffix(entry.Source, ".signed.efi"):
hasShim = true
case strings.HasPrefix(entry.Destination, "/EFI/systemd/systemd-boot") &&
strings.HasSuffix(entry.Destination, ".efi") &&
strings.HasPrefix(entry.Source, "./systemd-boot") &&
strings.HasSuffix(entry.Source, ".signed.efi"):
hasSystemdBoot = true
case strings.HasPrefix(entry.Destination, "/EFI/Linux/vmlinuz") &&
strings.HasSuffix(entry.Destination, ".efi") &&
strings.HasPrefix(entry.Source, "./vmlinuz") &&
strings.HasSuffix(entry.Source, ".signed.efi"):
hasUKI = true
}
}
assert.True(t, hasShim, "Expected an inject entry for shim")
assert.True(t, hasSystemdBoot, "Expected an inject entry for systemd-boot")
assert.True(t, hasUKI, "Expected at least one inject entry for UKI")
// Confirm artifacts were outputted
// Detect boot binary
bootBinaries, err := filepath.Glob(filepath.Join(outputArtifactsDir, "boot*.efi"))
assert.NoError(t, err)
assert.Equal(t, 1, len(bootBinaries), "Expected exactly one boot<arch>.efi binary")
bootBinary := bootBinaries[0]
// Detect systemd-boot binary
systemdBootBinaries, err := filepath.Glob(filepath.Join(outputArtifactsDir, "systemd-boot*.efi"))
assert.NoError(t, err)
assert.Equal(t, 1, len(systemdBootBinaries), "Expected exactly one systemd-boot<arch>.efi binary")
systemdBootBinary := systemdBootBinaries[0]
// Detect unsigned UKIs
ukiUnsignedFiles, err := filepath.Glob(filepath.Join(outputArtifactsDir, "vmlinuz-*.unsigned.efi"))
assert.NoError(t, err)
assert.GreaterOrEqual(t, len(ukiUnsignedFiles), 1, "Expected at least one unsigned UKI")
// Simulate signed boot & systemd-boot
marker := "##TEST_MARKER_INJECTED##"
for _, src := range []string{bootBinary, systemdBootBinary} {
dst := replaceSuffix(src, ".efi", ".signed.efi")
err := file.Copy(src, dst)
assert.NoError(t, err)
err = appendMarker(dst, marker)
assert.NoError(t, err)
}
// Simulate signed UKIs
for _, src := range ukiUnsignedFiles {
dst := replaceSuffix(src, ".unsigned.efi", ".signed.efi")
err := file.Copy(src, dst)
assert.NoError(t, err)
}
// Inject artifacts into a fresh copy of the raw image
err = InjectFilesWithConfigFile(buildDir, injectConfigPath, outImageFilePath, "", "")
if !assert.NoError(t, err) {
return
}
// Mount injected image and verify one file was injected
// Connect to customized image.
mountPoints := []mountPoint{
{
PartitionNum: 3,
Path: "/",
FileSystemType: "ext4",
},
{
PartitionNum: 2,
Path: "/boot",
FileSystemType: "ext4",
},
{
PartitionNum: 1,
Path: "/boot/efi",
FileSystemType: "vfat",
},
}
// Connect to customized image.
imageConnection, err := connectToImage(buildDir, outImageFilePath, false /*includeDefaultMounts*/, mountPoints)
if !assert.NoError(t, err) {
return
}
defer imageConnection.Close()
// Check the injected files
// shim
expectedInjectedShim := filepath.Join(imageConnection.chroot.RootDir(), "EFI", "BOOT", filepath.Base(bootBinary))
contains, err := fileContainsMarker(expectedInjectedShim, marker)
assert.NoError(t, err)
assert.True(t, contains, "Expected injected shim to exist:\n%s", expectedInjectedShim)
// systemd-boot
expectedInjectedSystemdBoot := filepath.Join(imageConnection.chroot.RootDir(), "EFI", "systemd", filepath.Base(systemdBootBinary))
contains, err = fileContainsMarker(expectedInjectedSystemdBoot, marker)
assert.NoError(t, err)
assert.True(t, contains, "Expected injected systemd-boot to exist:\n%s", expectedInjectedSystemdBoot)
// UKI(s)
for _, src := range ukiUnsignedFiles {
signedName := filepath.Base(replaceSuffix(src, ".unsigned.efi", ".efi"))
expectedInjectedUKI := filepath.Join(imageConnection.chroot.RootDir(), "EFI", "Linux", signedName)
exists, err = file.PathExists(expectedInjectedUKI)
assert.NoError(t, err)
assert.True(t, exists, "Expected injected UKI to exist:\n%s", expectedInjectedUKI)
}
}
func appendMarker(path string, marker string) error {
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(marker)
return err
}
func fileContainsMarker(path string, marker string) (bool, error) {
content, err := os.ReadFile(path)
if err != nil {
return false, err
}
return strings.Contains(string(content), marker), nil
}