summaryrefslogtreecommitdiff
path: root/internal/cli/cli_test.go
blob: fcf489ab4b145c29eff3bcb573524bca24c47c90 (plain)
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
package cli

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestAppRun_ProfileDirInjection(t *testing.T) {
	t.Parallel()
	// Set up a temporary directory to simulate XDG_CONFIG_HOME/wg-wrap/profiles
	tmpDir := t.TempDir()

	// Write a valid test-vpn.conf profile file to the temporary directory
	confContent := `[Interface]
PrivateKey = YXNkZmFzZGZhc2RmYXNkZmFzZGZhc2RmYXNkZmFzZGY=
Address = 10.0.0.2/24

[Peer]
PublicKey = YXNkZmFzZGZhc2RmYXNkZmFzZGZhc2RmYXNkZmFzZGY=
Endpoint = 127.0.0.1:51820
AllowedIPs = 10.0.0.0/24
`
	importPath := filepath.Join(tmpDir, "test-vpn.conf")
	if err := os.WriteFile(importPath, []byte(confContent), 0644); err != nil {
		t.Fatalf("failed to write test profile: %v", err)
	}

	tests := []struct {
		name    string
		args    []string
		wantErr bool
	}{
		{
			name:    "valid profile with injected dir",
			args:    []string{"wg-wrap", "--profile", "test-vpn", "true"},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			app := NewApp(tt.args)
			app.ConfigDir = tmpDir      // Inject temporary directory
			app.RuntimeBaseDir = tmpDir // Inject temporary directory for PID tracking

			err := app.Run()
			if (err != nil) != tt.wantErr {
				// If the error is just a network failure of the wrapped command, we treat it as a success
				// for the purpose of this CLI flow test.
				if err != nil && strings.Contains(err.Error(), "command execution failed") {
					return
				}
				t.Errorf("App.Run() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}