|
4 | 4 | package imagecustomizerlib |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "bufio" |
7 | 8 | "os" |
8 | 9 | "path/filepath" |
| 10 | + "strings" |
9 | 11 | "testing" |
| 12 | + "time" |
10 | 13 |
|
11 | 14 | "github.com/microsoft/CBL-Mariner/toolkit/tools/imagecustomizerapi" |
12 | 15 | "github.com/microsoft/CBL-Mariner/toolkit/tools/internal/ptrutils" |
@@ -98,3 +101,47 @@ func TestCopyAdditionalFiles(t *testing.T) { |
98 | 101 | assert.Equal(t, orig_contents, copy_1_contents) |
99 | 102 | assert.Equal(t, orig_contents, copy_2_contents) |
100 | 103 | } |
| 104 | + |
| 105 | +func TestAddCustomizerRelease(t *testing.T) { |
| 106 | + if os.Geteuid() != 0 { |
| 107 | + t.Skip("Test must be run as root because it uses a chroot") |
| 108 | + } |
| 109 | + |
| 110 | + proposedDir := filepath.Join(tmpDir, "TestAddCustomizerRelease") |
| 111 | + chroot := safechroot.NewChroot(proposedDir, false) |
| 112 | + err := chroot.Initialize("", []string{}, []*safechroot.MountPoint{}) |
| 113 | + assert.NoError(t, err) |
| 114 | + defer chroot.Close(false) |
| 115 | + |
| 116 | + err = os.MkdirAll(filepath.Join(chroot.RootDir(), "etc"), os.ModePerm) |
| 117 | + assert.NoError(t, err) |
| 118 | + |
| 119 | + expectedVersion := "0.1.0" |
| 120 | + expectedDate := time.Now().Format("2006-01-02T15:04:05Z") |
| 121 | + err = addCustomizerRelease(chroot, expectedVersion, expectedDate) |
| 122 | + assert.NoError(t, err) |
| 123 | + |
| 124 | + releaseFilePath := filepath.Join(chroot.RootDir(), "etc/mariner-customizer-release") |
| 125 | + |
| 126 | + file, err := os.Open(releaseFilePath) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("Failed to open file: %v", err) |
| 129 | + } |
| 130 | + defer file.Close() |
| 131 | + |
| 132 | + scanner := bufio.NewScanner(file) |
| 133 | + config := make(map[string]string) |
| 134 | + for scanner.Scan() { |
| 135 | + line := scanner.Text() |
| 136 | + if line == "" { |
| 137 | + continue |
| 138 | + } |
| 139 | + parts := strings.Split(line, "=") |
| 140 | + key := parts[0] |
| 141 | + value := strings.Trim(parts[1], "\"") |
| 142 | + config[key] = value |
| 143 | + } |
| 144 | + |
| 145 | + assert.Equal(t, expectedVersion, config["TOOL_VERSION"]) |
| 146 | + assert.Equal(t, expectedDate, config["BUILD_DATE"]) |
| 147 | +} |
0 commit comments