| 1 | |
| 2 | |
| 3 | @@ -186,6 +186,10 @@ |
| 4 | } |
| 5 | |
| 6 | func setTimezone(ctx context.Context, timezone string) error { |
| 7 | + // systemsetup lists GMT as its UTC zone on macOS. |
| 8 | + if timezone == "Etc/UTC" || timezone == "UTC" { |
| 9 | + timezone = "GMT" |
| 10 | + } |
| 11 | cmd := exec.CommandContext(ctx, "systemsetup", "-settimezone", timezone) |
| 12 | logrus.Infof("Executing command: %v", cmd.Args) |
| 13 | if output, err := cmd.CombinedOutput(); err != nil { |
| 14 | |
| 15 | |
| 16 | @@ -0,0 +1,34 @@ |
| 17 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 18 | +// SPDX-License-Identifier: Apache-2.0 |
| 19 | + |
| 20 | +package fakecloudinit |
| 21 | + |
| 22 | +import ( |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | +) |
| 28 | + |
| 29 | +func TestSystemSetupUTCZone(t *testing.T) { |
| 30 | + dir := t.TempDir() |
| 31 | + log := filepath.Join(dir, "args") |
| 32 | + cmd := filepath.Join(dir, "systemsetup") |
| 33 | + if err := os.WriteFile(cmd, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$CAGE_TEST_ARGS\"\n"), 0o700); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + t.Setenv("PATH", dir) |
| 37 | + t.Setenv("CAGE_TEST_ARGS", log) |
| 38 | + for input, want := range map[string]string{"Etc/UTC": "GMT", "UTC": "GMT", "GMT": "GMT", "Asia/Shanghai": "Asia/Shanghai", "Unlisted/Zone": "Unlisted/Zone"} { |
| 39 | + if err := setTimezone(t.Context(), input); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + data, err := os.ReadFile(log) |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + if got := strings.TrimSpace(string(data)); got != "-settimezone\n"+want { |
| 47 | + t.Errorf("%s: got %q", input, got) |
| 48 | + } |
| 49 | + } |
| 50 | +} |