File: configuration_test.go

package info (click to toggle)
golang-github-ovh-go-ovh 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: makefile: 5
file content (265 lines) | stat: -rw-r--r-- 6,417 bytes parent folder | download | duplicates (2)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package ovh

import (
	"io/ioutil"
	"os"
	"testing"
)

//
// Utils
//

var home string

func setup() {
	systemConfigPath = "./ovh.unittest.global.conf"
	userConfigPath = "/.ovh.unittest.user.conf"
	localConfigPath = "./ovh.unittest.local.conf"
	home, _ = currentUserHome()
}

func teardown() {
	os.Remove(systemConfigPath)
	os.Remove(home + userConfigPath)
	os.Remove(localConfigPath)
}

//
// Tests
//

func TestConfigFromFiles(t *testing.T) {
t.Skip("DM-skipped")
	// Write each parameter to one different configuration file
	// This is a simple way to test precedence

	// Prepare
	ioutil.WriteFile(systemConfigPath, []byte(`
[ovh-eu]
application_key=system
application_secret=system
consumer_key=system
`), 0660)

	ioutil.WriteFile(home+userConfigPath, []byte(`
[ovh-eu]
application_secret=user
consumer_key=user
`), 0660)

	ioutil.WriteFile(localConfigPath, []byte(`
[ovh-eu]
consumer_key=local
`), 0660)

	// Clear
	defer ioutil.WriteFile(systemConfigPath, []byte(``), 0660)
	defer ioutil.WriteFile(home+userConfigPath, []byte(``), 0660)
	defer ioutil.WriteFile(localConfigPath, []byte(``), 0660)

	// Test
	client := Client{}
	err := client.loadConfig("ovh-eu")

	// Validate
	if err != nil {
		t.Fatalf("loadConfig failed with: '%v'", err)
	}
	if client.AppKey != "system" {
		t.Fatalf("client.AppKey should be 'system'. Got '%s'", client.AppKey)
	}
	if client.AppSecret != "user" {
		t.Fatalf("client.AppSecret should be 'user'. Got '%s'", client.AppSecret)
	}
	if client.ConsumerKey != "local" {
		t.Fatalf("client.ConsumerKey should be 'local'. Got '%s'", client.ConsumerKey)
	}
}

func TestConfigFromOnlyOneFile(t *testing.T) {
t.Skip("DM-skipped")
	// ini package has a bug causing it to ignore all subsequent configuration
	// files if any could not be loaded. Make sure that workaround... works.

	// Prepare
	os.Remove(systemConfigPath)
	ioutil.WriteFile(home+userConfigPath, []byte(`
[ovh-eu]
application_key=user
application_secret=user
consumer_key=user
`), 0660)

	// Clear
	defer ioutil.WriteFile(home+userConfigPath, []byte(``), 0660)

	// Test
	client := Client{}
	err := client.loadConfig("ovh-eu")

	// Validate
	if err != nil {
		t.Fatalf("loadConfig failed with: '%v'", err)
	}
	if client.AppKey != "user" {
		t.Fatalf("client.AppKey should be 'user'. Got '%s'", client.AppKey)
	}
	if client.AppSecret != "user" {
		t.Fatalf("client.AppSecret should be 'user'. Got '%s'", client.AppSecret)
	}
	if client.ConsumerKey != "user" {
		t.Fatalf("client.ConsumerKey should be 'user'. Got '%s'", client.ConsumerKey)
	}
}

func TestConfigFromEnv(t *testing.T) {
	// Prepare
	ioutil.WriteFile(systemConfigPath, []byte(`
[ovh-eu]
application_key=fail
application_secret=fail
consumer_key=fail
`), 0660)

	defer ioutil.WriteFile(systemConfigPath, []byte(``), 0660)
	os.Setenv("OVH_ENDPOINT", "ovh-eu")
	os.Setenv("OVH_APPLICATION_KEY", "env")
	os.Setenv("OVH_APPLICATION_SECRET", "env")
	os.Setenv("OVH_CONSUMER_KEY", "env")

	// Clear
	defer os.Unsetenv("OVH_ENDPOINT")
	defer os.Unsetenv("OVH_APPLICATION_KEY")
	defer os.Unsetenv("OVH_APPLICATION_SECRET")
	defer os.Unsetenv("OVH_CONSUMER_KEY")

	// Test
	client := Client{}
	err := client.loadConfig("")

	// Validate
	if err != nil {
		t.Fatalf("loadConfig failed with: '%v'", err)
	}
	if client.endpoint != OvhEU {
		t.Fatalf("client.AppKey should be 'env'. Got '%s'", client.AppKey)
	}
	if client.AppKey != "env" {
		t.Fatalf("client.AppKey should be 'env'. Got '%s'", client.AppKey)
	}
	if client.AppSecret != "env" {
		t.Fatalf("client.AppSecret should be 'env'. Got '%s'", client.AppSecret)
	}
	if client.ConsumerKey != "env" {
		t.Fatalf("client.ConsumerKey should be 'env'. Got '%s'", client.ConsumerKey)
	}
}

func TestConfigFromArgs(t *testing.T) {
	// Test
	client := Client{
		AppKey:      "param",
		AppSecret:   "param",
		ConsumerKey: "param",
	}
	err := client.loadConfig("ovh-eu")

	// Validate
	if err != nil {
		t.Fatalf("loadConfig failed with: '%v'", err)
	}
	if client.endpoint != OvhEU {
		t.Fatalf("client.AppKey should be 'param'. Got '%s'", client.AppKey)
	}
	if client.AppKey != "param" {
		t.Fatalf("client.AppKey should be 'param'. Got '%s'", client.AppKey)
	}
	if client.AppSecret != "param" {
		t.Fatalf("client.AppSecret should be 'param'. Got '%s'", client.AppSecret)
	}
	if client.ConsumerKey != "param" {
		t.Fatalf("client.ConsumerKey should be 'param'. Got '%s'", client.ConsumerKey)
	}
}

func TestEndpoint(t *testing.T) {
	// Prepare
	ioutil.WriteFile(systemConfigPath, []byte(`
[ovh-eu]
application_key=ovh
application_secret=ovh
consumer_key=ovh

[https://api.example.com:4242]
application_key=example.com
application_secret=example.com
consumer_key=example.com
`), 0660)

	// Clear
	defer ioutil.WriteFile(systemConfigPath, []byte(``), 0660)

	// Test: by name
	client := Client{}
	err := client.loadConfig("ovh-eu")
	if err != nil {
		t.Fatalf("loadConfig should not fail for endpoint 'ovh-eu'. Got '%v'", err)
	}
	if client.AppKey != "ovh" {
		t.Fatalf("configured value should be 'ovh' for endpoint 'ovh-eu'. Got '%s'", client.AppKey)
	}

	// Test: by URL
	client = Client{}
	err = client.loadConfig("https://api.example.com:4242")
	if err != nil {
		t.Fatalf("loadConfig should not fail for endpoint 'https://api.example.com:4242'. Got '%v'", err)
	}
	if client.AppKey != "example.com" {
		t.Fatalf("configured value should be 'example.com' for endpoint 'https://api.example.com:4242'. Got '%s'", client.AppKey)
	}

}

func TestMissingParam(t *testing.T) {
	// Setup
	var err error
	client := Client{
		AppKey:      "param",
		AppSecret:   "param",
		ConsumerKey: "param",
	}

	// Test
	client.endpoint = ""
	if err = client.loadConfig(""); err == nil {
		t.Fatalf("loadConfig should fail when client.endpoint is missing. Got '%s'", client.endpoint)
	}

	client.AppKey = ""
	if err = client.loadConfig("ovh-eu"); err == nil {
		t.Fatalf("loadConfig should fail when client.AppKey is missing. Got '%s'", client.AppKey)
	}
	client.AppKey = "param"

	client.AppSecret = ""
	if err = client.loadConfig("ovh-eu"); err == nil {
		t.Fatalf("loadConfig should fail when client.AppSecret is missing. Got '%s'", client.AppSecret)
	}
	client.AppSecret = "param"
}

//
// Main
//

// TestMain changes the location of configuration files. We need
// this to avoid any interference with existing configuration
// and non-root users
func TestMain(m *testing.M) {
	setup()
	code := m.Run()
	teardown()
	os.Exit(code)
}