-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
profile_test.go
117 lines (91 loc) · 3.08 KB
/
profile_test.go
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
/*
Copyright 2016-2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package profile_test
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/gravitational/trace"
"github.com/stretchr/testify/require"
"github.com/gravitational/teleport/api/profile"
)
// TestProfileBasics verifies basic profile operations such as
// load/store and setting current.
func TestProfileBasics(t *testing.T) {
t.Parallel()
dir := t.TempDir()
p := &profile.Profile{
WebProxyAddr: "proxy:3088",
SSHProxyAddr: "proxy:3023",
Username: "testuser",
DynamicForwardedPorts: []string{"localhost:8080"},
Dir: dir,
SiteName: "example.com",
AuthConnector: "passwordless",
MFAMode: "auto",
}
// verify that profile name is proxy host component
require.Equal(t, "proxy", p.Name())
// save to a file:
err := p.SaveToDir(dir, false)
require.NoError(t, err)
// verify that the resulting file exists and is of the form `<profile-dir>/<profile-name>.yaml`.
_, err = os.Stat(filepath.Join(dir, p.Name()+".yaml"))
require.NoError(t, err)
// try to save to non-existent dir, should get an error
err = p.SaveToDir("/bad/directory/", false)
require.Error(t, err)
// make sure current profile was not set
_, err = profile.GetCurrentProfileName(dir)
require.True(t, trace.IsNotFound(err))
// save again, this time also making current
err = p.SaveToDir(dir, true)
require.NoError(t, err)
// verify that current profile is set and matches this profile
name, err := profile.GetCurrentProfileName(dir)
require.NoError(t, err)
require.Equal(t, p.Name(), name)
// load and verify current profile
clone, err := profile.FromDir(dir, "")
require.NoError(t, err)
require.Equal(t, *p, *clone)
// load and verify directly
clone, err = profile.FromDir(dir, p.Name())
require.NoError(t, err)
require.Equal(t, *p, *clone)
}
func TestAppPath(t *testing.T) {
t.Parallel()
dir := t.TempDir()
p := &profile.Profile{
WebProxyAddr: "proxy:3088",
SSHProxyAddr: "proxy:3023",
Username: "testuser",
Dir: dir,
SiteName: "example.com",
}
expected := filepath.Join(dir, "keys", "proxy", "testuser-app", "example.com", "banana-x509.pem")
require.Equal(t, expected, p.AppCertPath("banana"))
}
func TestProfilePath(t *testing.T) {
switch runtime.GOOS {
case "darwin", "linux":
default:
t.Skip("this test only runs on Unix")
}
dir := t.TempDir()
t.Setenv("HOME", dir)
require.Equal(t, "/foo/bar", profile.FullProfilePath("/foo/bar"))
require.Equal(t, filepath.Join(dir, ".tsh"), profile.FullProfilePath(""))
}