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
142
143
144
145
146
147
148
149
150
151
152
153
154
|
//go:build linux || darwin
// +build linux darwin
package tvmtool_test
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
"a.yandex-team.ru/library/go/yandex/tvm/tvmtool"
)
func TestNewClients(t *testing.T) {
type TestCase struct {
env map[string]string
willFail bool
expectedErr string
expectedBaseURI string
expectedAuthToken string
}
cases := map[string]struct {
constructor func(opts ...tvmtool.Option) (*tvmtool.Client, error)
cases map[string]TestCase
}{
"qloud": {
constructor: tvmtool.NewQloudClient,
cases: map[string]TestCase{
"no-auth": {
willFail: true,
expectedErr: "empty auth token (looked at ENV[QLOUD_TVM_TOKEN])",
},
"ok-default-origin": {
env: map[string]string{
"QLOUD_TVM_TOKEN": "ok-default-origin-token",
},
willFail: false,
expectedBaseURI: "http://localhost:1/tvm",
expectedAuthToken: "ok-default-origin-token",
},
"ok-custom-origin": {
env: map[string]string{
"QLOUD_TVM_INTERFACE_ORIGIN": "http://localhost:9000",
"QLOUD_TVM_TOKEN": "ok-custom-origin-token",
},
willFail: false,
expectedBaseURI: "http://localhost:9000/tvm",
expectedAuthToken: "ok-custom-origin-token",
},
},
},
"deploy": {
constructor: tvmtool.NewDeployClient,
cases: map[string]TestCase{
"no-url": {
willFail: true,
expectedErr: "empty tvmtool url (looked at ENV[DEPLOY_TVM_TOOL_URL])",
},
"no-auth": {
env: map[string]string{
"DEPLOY_TVM_TOOL_URL": "http://localhost:2",
},
willFail: true,
expectedErr: "empty auth token (looked at ENV[TVMTOOL_LOCAL_AUTHTOKEN])",
},
"ok": {
env: map[string]string{
"DEPLOY_TVM_TOOL_URL": "http://localhost:1337",
"TVMTOOL_LOCAL_AUTHTOKEN": "ok-token",
},
willFail: false,
expectedBaseURI: "http://localhost:1337/tvm",
expectedAuthToken: "ok-token",
},
},
},
"any": {
constructor: tvmtool.NewAnyClient,
cases: map[string]TestCase{
"empty": {
willFail: true,
expectedErr: "unknown tvmtool environment",
},
"ok-qloud": {
env: map[string]string{
"QLOUD_TVM_INTERFACE_ORIGIN": "http://qloud:9000",
"QLOUD_TVM_TOKEN": "ok-qloud",
},
expectedBaseURI: "http://qloud:9000/tvm",
expectedAuthToken: "ok-qloud",
},
"ok-deploy": {
env: map[string]string{
"DEPLOY_TVM_TOOL_URL": "http://deploy:1337",
"TVMTOOL_LOCAL_AUTHTOKEN": "ok-deploy",
},
expectedBaseURI: "http://deploy:1337/tvm",
expectedAuthToken: "ok-deploy",
},
"ok-local": {
env: map[string]string{
"TVMTOOL_URL": "http://local:1338",
"TVMTOOL_LOCAL_AUTHTOKEN": "ok-local",
},
willFail: false,
expectedBaseURI: "http://local:1338/tvm",
expectedAuthToken: "ok-local",
},
},
},
}
// NB! this checks are not thread safe, never use t.Parallel() and so on
for clientName, client := range cases {
t.Run(clientName, func(t *testing.T) {
for name, tc := range client.cases {
t.Run(name, func(t *testing.T) {
savedEnv := os.Environ()
defer func() {
os.Clearenv()
for _, env := range savedEnv {
parts := strings.SplitN(env, "=", 2)
err := os.Setenv(parts[0], parts[1])
require.NoError(t, err)
}
}()
os.Clearenv()
for key, val := range tc.env {
_ = os.Setenv(key, val)
}
tvmClient, err := client.constructor()
if tc.willFail {
require.Error(t, err)
if tc.expectedErr != "" {
require.EqualError(t, err, tc.expectedErr)
}
require.Nil(t, tvmClient)
} else {
require.NoError(t, err)
require.NotNil(t, tvmClient)
require.Equal(t, tc.expectedBaseURI, tvmClient.BaseURI())
require.Equal(t, tc.expectedAuthToken, tvmClient.AuthToken())
}
})
}
})
}
}
|