1
+ // SPDX-FileCopyrightText: Copyright The Lima Authors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ package usrlocalsharelima
5
+
6
+ import (
7
+ "fmt"
8
+ "os"
9
+ "path/filepath"
10
+ "runtime"
11
+ "testing"
12
+ )
13
+
14
+ // TestDirInTestEnvironment tests that usrlocalsharelima.Dir() works correctly
15
+ // when called from tests, where os.Executable is located in a temp directory.
16
+ // This test verifies that the guestagent binary can be found relative to the
17
+ // test executable's location.
18
+ func TestDirInTestEnvironment (t * testing.T ) {
19
+ // Get the test executable path (will be in a temp directory)
20
+ testExe , err := os .Executable ()
21
+ if err != nil {
22
+ t .Fatalf ("Failed to get test executable path: %v" , err )
23
+ }
24
+
25
+ // Determine architecture for binary name
26
+ arch := "aarch64"
27
+ if runtime .GOARCH == "amd64" {
28
+ arch = "x86_64"
29
+ }
30
+
31
+ // Create guestagent binary in the first expected location
32
+ // (same directory as test executable)
33
+ gaBinary1 := filepath .Join (filepath .Dir (testExe ), fmt .Sprintf ("lima-guestagent.Linux-%s" , arch ))
34
+ err = os .WriteFile (gaBinary1 , []byte ("dummy" ), 0755 )
35
+ if err != nil {
36
+ t .Fatalf ("Failed to create guestagent binary: %v" , err )
37
+ }
38
+ defer os .Remove (gaBinary1 )
39
+
40
+ // Create guestagent binary in the second expected location
41
+ // (share/lima directory relative to test executable)
42
+ shareDir := filepath .Join (filepath .Dir (filepath .Dir (testExe )), "share" , "lima" )
43
+ err = os .MkdirAll (shareDir , 0755 )
44
+ if err != nil {
45
+ t .Fatalf ("Failed to create share directory: %v" , err )
46
+ }
47
+ defer os .RemoveAll (shareDir )
48
+
49
+ gaBinary2 := filepath .Join (shareDir , fmt .Sprintf ("lima-guestagent.Linux-%s" , arch ))
50
+ err = os .WriteFile (gaBinary2 , []byte ("dummy" ), 0755 )
51
+ if err != nil {
52
+ t .Fatalf ("Failed to create guestagent binary: %v" , err )
53
+ }
54
+ defer os .Remove (gaBinary2 )
55
+
56
+ // Test that Dir() can find the binary
57
+ dir , err := Dir ()
58
+ if err != nil {
59
+ t .Fatalf ("Dir() failed: %v" , err )
60
+ }
61
+
62
+ // Verify that Dir() returns the correct directory
63
+ expectedDir := filepath .Dir (gaBinary1 )
64
+ if dir != expectedDir {
65
+ t .Errorf ("Expected directory %q, got %q" , expectedDir , dir )
66
+ }
67
+ }
0 commit comments