File: target_test.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (111 lines) | stat: -rw-r--r-- 3,117 bytes parent folder | download | duplicates (3)
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
package core

import (
	"testing"

	"github.com/hashicorp/vagrant-plugin-sdk/component"
	"github.com/hashicorp/vagrant-plugin-sdk/core"
	"github.com/hashicorp/vagrant/internal/plugin"
	"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
	"github.com/stretchr/testify/require"
)

func TestTargetSpecializeMachine(t *testing.T) {
	tt := TestMinimalTarget(t)
	specialized, err := tt.Specialize((*core.Machine)(nil))
	if err != nil {
		t.Errorf("Specialize function returned an error")
	}
	if _, ok := specialized.(core.Machine); !ok {
		t.Errorf("Unable to specialize a target to a machine")
	}

	// Get machine from the cache, should be the same machine
	reSpecialized, err := tt.Specialize((*core.Machine)(nil))
	if err != nil {
		t.Errorf("Specialize function returned an error")
	}
	require.Equal(t, reSpecialized, specialized)
}

func TestTargetSpecializeMultiMachine(t *testing.T) {
	p := TestMinimalProject(t)
	tt1 := TestTarget(t, p, &vagrant_server.Target{Name: "tt1"})
	tt2 := TestTarget(t, p, &vagrant_server.Target{Name: "tt2"})

	specialized, err := tt1.Specialize((*core.Machine)(nil))
	if err != nil {
		t.Errorf("Specialize function returned an error")
	}
	if _, ok := specialized.(core.Machine); !ok {
		t.Errorf("Unable to specialize a target to a machine")
	}
	specializedName, _ := specialized.(core.Machine).Name()

	specialized2, err := tt2.Specialize((*core.Machine)(nil))
	if err != nil {
		t.Errorf("Specialize function returned an error")
	}
	if _, ok := specialized2.(core.Machine); !ok {
		t.Errorf("Unable to specialize a target to a machine")
	}
	specialized2Name, _ := specialized2.(core.Machine).Name()

	require.NotEqual(t, specializedName, specialized2Name)
}

func TestTargetSpecializeBad(t *testing.T) {
	tt := TestMinimalTarget(t)
	specialized, err := tt.Specialize((*core.Project)(nil))

	if err != nil {
		t.Errorf("Specialize function returned an error")
	}

	if specialized != nil {
		t.Errorf("Should not specialize to an unsupported type")
	}
}

func TestTargetConfigedCommunicator(t *testing.T) {
	type test struct {
		config *component.ConfigData
		errors bool
	}

	tests := []test{
		{config: testCommunicatorConfig("winrm"), errors: false},
		{config: testSyncedFolderConfig([]*testSyncedFolder{}), errors: false},
		{config: testCommunicatorConfig("idontexist"), errors: true},
	}
	communicatorMockSSH := BuildTestCommunicatorPlugin("ssh")
	communicatorMockWinRM := BuildTestCommunicatorPlugin("winrm")

	pluginManager := plugin.TestManager(t,
		plugin.TestPlugin(t,
			communicatorMockSSH,
			plugin.WithPluginName("ssh"),
			plugin.WithPluginTypes(component.CommunicatorType),
		),
		plugin.TestPlugin(t,
			communicatorMockWinRM,
			plugin.WithPluginName("winrm"),
			plugin.WithPluginTypes(component.CommunicatorType),
		),
	)

	for _, tc := range tests {
		tp := TestProject(t, WithPluginManager(pluginManager))
		tm := TestMachine(t, tp,
			WithTestTargetConfig(tc.config),
		)
		comm, err := tm.Communicate()
		if tc.errors {
			require.Error(t, err)
			require.Nil(t, comm)
		} else {
			require.NoError(t, err)
			require.NotNil(t, comm)
		}
	}
}