summaryrefslogtreecommitdiff
path: root/pkg/machine/qemu/config_test.go
blob: 3f92881fa93fb0e0021f079c8a9428fe7435b47e (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
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
package qemu

import (
	"os"
	"path/filepath"
	"reflect"
	"testing"

	"github.com/containers/podman/v4/pkg/machine"
	"github.com/containers/podman/v4/test/utils"
)

func TestMachineFile_GetPath(t *testing.T) {
	path := "/var/tmp/podman/my.sock"
	sym := "/tmp/podman/my.sock"
	type fields struct {
		Path    string
		Symlink *string
	}
	tests := []struct {
		name   string
		fields fields
		want   string
	}{
		{
			name:   "Original path",
			fields: fields{path, nil},
			want:   path,
		},
		{
			name: "Symlink over path",
			fields: fields{
				Path:    path,
				Symlink: &sym,
			},
			want: sym,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			m := &machine.VMFile{
				Path:    tt.fields.Path,    //nolint: scopelint
				Symlink: tt.fields.Symlink, //nolint: scopelint
			}
			if got := m.GetPath(); got != tt.want { //nolint: scopelint
				t.Errorf("GetPath() = %v, want %v", got, tt.want) //nolint: scopelint
			}
		})
	}
}

func TestNewMachineFile(t *testing.T) {
	empty := ""

	homedir, err := os.MkdirTemp("/tmp", "homedir")
	if err != nil {
		panic(err)
	}
	defer os.RemoveAll(homedir)
	longTemp, err := os.MkdirTemp("/tmp", "tmpdir")
	if err != nil {
		panic(err)
	}
	defer os.RemoveAll(longTemp)
	oldhome := os.Getenv("HOME")
	os.Setenv("HOME", homedir) //nolint: tenv
	defer os.Setenv("HOME", oldhome)

	p := "/var/tmp/podman/my.sock"
	longp := filepath.Join(longTemp, utils.RandomString(100), "my.sock")
	err = os.MkdirAll(filepath.Dir(longp), 0755)
	if err != nil {
		panic(err)
	}
	f, err := os.Create(longp)
	if err != nil {
		panic(err)
	}
	_ = f.Close()
	sym := "my.sock"
	longSym := filepath.Join(homedir, ".podman", sym)

	m := machine.VMFile{
		Path:    p,
		Symlink: nil,
	}
	type args struct {
		path    string
		symlink *string
	}
	tests := []struct {
		name    string
		args    args
		want    *machine.VMFile
		wantErr bool
	}{
		{
			name:    "Good",
			args:    args{path: p},
			want:    &m,
			wantErr: false,
		},
		{
			name:    "Good with short symlink",
			args:    args{p, &sym},
			want:    &machine.VMFile{Path: p},
			wantErr: false,
		},
		{
			name:    "Bad path name",
			args:    args{empty, nil},
			want:    nil,
			wantErr: true,
		},
		{
			name:    "Bad symlink name",
			args:    args{p, &empty},
			want:    nil,
			wantErr: true,
		},
		{
			name:    "Good with long symlink",
			args:    args{longp, &sym},
			want:    &machine.VMFile{Path: longp, Symlink: &longSym},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			got, err := machine.NewMachineFile(tt.args.path, tt.args.symlink)
			if (err != nil) != tt.wantErr {
				t.Errorf("NewMachineFile() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("NewMachineFile() got = %v, want %v", got, tt.want)
			}
		})
	}
}