Skip to content

Commit 65143c7

Browse files
committed
test: ParseConfigAtLine
1 parent b1d3c79 commit 65143c7

File tree

1 file changed

+334
-0
lines changed

1 file changed

+334
-0
lines changed

v3/integrations/nroci/nrocinosql_test.go

Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package nroci
44

55
import (
6+
"strings"
67
"testing"
78

89
"github.com/oracle/nosql-go-sdk/nosqldb"
@@ -183,3 +184,336 @@ func Test_extractHostPort(t *testing.T) {
183184
})
184185
}
185186
}
187+
188+
// Test_parseConfigAtLine tests parseConfigAtLine(start int, splitContent []string)
189+
// It will check for keys in the oci config file and assign them accordingly to the
190+
// *nrConfig. Currently, the only key we pull in is tenancyOCID so that is the only
191+
// field we are checking for in this test
192+
func Test_parseConfigAtLine(t *testing.T) {
193+
splitContent := strings.Split(`
194+
[DEFAULT]
195+
user=ocid1.user
196+
fingerprint=fingerprint
197+
key_file=~/.oci/oci_api_key.pem
198+
tenancy=ocid1.tenancy
199+
region=us-ashburn-1
200+
201+
[ADMIN_USER]
202+
user=ocid1.user.admin
203+
fingerprint=fingerprint
204+
key_file=keys/admin_key.pem
205+
tenancy=ocid1.tenancy
206+
pass_phrase=funwords
207+
208+
[NO_TENANCY]
209+
user=ocid1.user.admin
210+
fingerprint=fingerprint
211+
key_file=keys/admin_key.pem
212+
pass_phrase=funwords
213+
214+
[NO_EQUALS_SIGN]
215+
user=ocid1.user.admin
216+
fingerprint=fingerprint
217+
key_file=keys/admin_key.pem
218+
tenancy:ocid1.tenancy
219+
pass_phrase=funwords`, "\n")
220+
tests := []struct {
221+
name string // description of this test case
222+
// Named input parameters for target function.
223+
start int
224+
splitContent []string
225+
want *nrConfig
226+
wantErr bool
227+
}{
228+
{
229+
name: "Parse DEFAULT USER (line 2) with TenancyOCID",
230+
start: 2,
231+
splitContent: splitContent,
232+
want: &nrConfig{
233+
tenancyOCID: "ocid1.tenancy",
234+
},
235+
wantErr: false,
236+
},
237+
{
238+
name: "Parse ADMIN_USER (line 9) with TenancyOCID",
239+
start: 9,
240+
splitContent: splitContent,
241+
want: &nrConfig{
242+
tenancyOCID: "ocid1.tenancy",
243+
},
244+
wantErr: false,
245+
},
246+
{
247+
name: "Parse NO_TENANCY USER (line 15) with TenancyOCID",
248+
start: 15,
249+
splitContent: splitContent,
250+
want: &nrConfig{
251+
tenancyOCID: "",
252+
},
253+
wantErr: false,
254+
},
255+
{
256+
name: "Parse [DEFAULT] (line 1) and immediately break",
257+
start: 1,
258+
splitContent: splitContent,
259+
want: &nrConfig{
260+
tenancyOCID: "",
261+
},
262+
wantErr: false,
263+
},
264+
{
265+
name: "Parse [ADMIN_USER] (line 8) and immediately break",
266+
start: 8,
267+
splitContent: splitContent,
268+
want: &nrConfig{
269+
tenancyOCID: "",
270+
},
271+
wantErr: false,
272+
},
273+
{
274+
name: "Parse [NO_TENANCY] (line 14) and immediately break",
275+
start: 14,
276+
splitContent: splitContent,
277+
want: &nrConfig{
278+
tenancyOCID: "",
279+
},
280+
wantErr: false,
281+
},
282+
{
283+
name: "Parse NO_EQUALS USER (line 14) and return empty string",
284+
start: 14,
285+
splitContent: splitContent,
286+
want: &nrConfig{
287+
tenancyOCID: "",
288+
},
289+
wantErr: false,
290+
},
291+
}
292+
for _, tt := range tests {
293+
t.Run(tt.name, func(t *testing.T) {
294+
got, gotErr := parseConfigAtLine(tt.start, tt.splitContent)
295+
if gotErr != nil {
296+
if !tt.wantErr {
297+
t.Errorf("parseConfigAtLine() failed: %v", gotErr)
298+
}
299+
return
300+
}
301+
if tt.wantErr {
302+
t.Fatal("parseConfigAtLine() succeeded unexpectedly")
303+
}
304+
if tt.want.tenancyOCID != got.tenancyOCID {
305+
t.Errorf("parseConfigAtLine().tenancyOCID = %v, want %v", got, tt.want)
306+
}
307+
})
308+
}
309+
}
310+
311+
func Test_parseOCIConfigFile(t *testing.T) {
312+
tests := []struct {
313+
name string // description of this test case
314+
// Named input parameters for target function.
315+
data []byte
316+
profile string
317+
want *nrConfig
318+
wantErr bool
319+
}{
320+
{
321+
name: "No data is passed in",
322+
data: []byte{},
323+
profile: "",
324+
want: &nrConfig{},
325+
wantErr: true,
326+
},
327+
{
328+
name: "Data is unable to be parsed data is passed in with profile",
329+
data: []byte(`
330+
user=ocid1.user
331+
fingerprint=fingerprint
332+
key_file=~/.oci/oci_api_key.pem
333+
tenancy=ocid1.tenancy
334+
region=us-ashburn-1`),
335+
profile: "",
336+
want: &nrConfig{},
337+
wantErr: true,
338+
},
339+
{
340+
name: "Data is unable to be parsed data is passed in with no matching profile",
341+
data: []byte(`
342+
[DEFAULT]
343+
user=ocid1.user
344+
fingerprint=fingerprint
345+
key_file=~/.oci/oci_api_key.pem
346+
tenancy=ocid1.tenancy
347+
region=us-ashburn-1`),
348+
profile: "NOT_DEFAULT",
349+
want: &nrConfig{},
350+
wantErr: true,
351+
},
352+
{
353+
name: "Data is unable to be parsed data is passed in with incorrect profile format",
354+
data: []byte(`
355+
DEFAULT
356+
user=ocid1.user
357+
fingerprint=fingerprint
358+
key_file=~/.oci/oci_api_key.pem
359+
tenancy=ocid1.tenancy
360+
region=us-ashburn-1`),
361+
profile: "DEFAULT",
362+
want: &nrConfig{},
363+
wantErr: true,
364+
},
365+
{
366+
name: "Data is unable to be parsed data is passed in with incorrect spacing",
367+
data: []byte(`
368+
\t[DEFAULT]
369+
user=ocid1.user
370+
fingerprint=fingerprint
371+
key_file=~/.oci/oci_api_key.pem
372+
tenancy=ocid1.tenancy
373+
region=us-ashburn-1`),
374+
profile: "DEFAULT",
375+
want: &nrConfig{},
376+
wantErr: true,
377+
},
378+
{
379+
name: "DEFAULT profile is parsed correctly",
380+
data: []byte(`
381+
[DEFAULT]
382+
user=ocid1.user
383+
fingerprint=fingerprint
384+
key_file=~/.oci/oci_api_key.pem
385+
tenancy=ocid1.tenancy
386+
region=us-ashburn-1`),
387+
profile: "DEFAULT",
388+
want: &nrConfig{
389+
profile: "DEFAULT",
390+
tenancyOCID: "ocid1.tenancy",
391+
},
392+
wantErr: false,
393+
},
394+
{
395+
name: "DEFAULT profile is parsed correctly with no tenancy",
396+
data: []byte(`
397+
[DEFAULT]
398+
user=ocid1.user
399+
fingerprint=fingerprint
400+
key_file=~/.oci/oci_api_key.pem
401+
region=us-ashburn-1`),
402+
profile: "DEFAULT",
403+
want: &nrConfig{
404+
profile: "DEFAULT",
405+
tenancyOCID: "",
406+
},
407+
wantErr: false,
408+
},
409+
{
410+
name: "NOT_DEFAULT profile is parsed correctly",
411+
data: []byte(`
412+
[DEFAULT]
413+
user=ocid1.user
414+
fingerprint=fingerprint
415+
key_file=~/.oci/oci_api_key.pem
416+
tenancy=ocid1.tenancy
417+
region=us-ashburn-1
418+
419+
[NOT_DEFAULT]
420+
user=ocid1.user
421+
fingerprint=fingerprint
422+
key_file=~/.oci/oci_api_key.pem
423+
tenancy=ocid1.tenancy
424+
region=us-ashburn-1`),
425+
profile: "NOT_DEFAULT",
426+
want: &nrConfig{
427+
profile: "NOT_DEFAULT",
428+
tenancyOCID: "ocid1.tenancy",
429+
},
430+
wantErr: false,
431+
},
432+
{
433+
name: "NOT_DEFAULT profile is parsed correctly and DEFAULT has no tenancyOCID",
434+
data: []byte(`
435+
[DEFAULT]
436+
user=ocid1.user
437+
fingerprint=fingerprint
438+
key_file=~/.oci/oci_api_key.pem
439+
region=us-ashburn-1
440+
441+
[NOT_DEFAULT]
442+
user=ocid1.user
443+
fingerprint=fingerprint
444+
key_file=~/.oci/oci_api_key.pem
445+
tenancy=ocid1.tenancy
446+
region=us-ashburn-1`),
447+
profile: "NOT_DEFAULT",
448+
want: &nrConfig{
449+
profile: "NOT_DEFAULT",
450+
tenancyOCID: "ocid1.tenancy",
451+
},
452+
wantErr: false,
453+
},
454+
{
455+
name: "NOT_DEFAULT profile is parsed correctly but contains no tenancyOCID",
456+
data: []byte(`
457+
[DEFAULT]
458+
user=ocid1.user
459+
fingerprint=fingerprint
460+
key_file=~/.oci/oci_api_key.pem
461+
tenancy=ocid1.tenancy
462+
region=us-ashburn-1
463+
464+
[NOT_DEFAULT]
465+
user=ocid1.user
466+
fingerprint=fingerprint
467+
key_file=~/.oci/oci_api_key.pem
468+
region=us-ashburn-1`),
469+
profile: "NOT_DEFAULT",
470+
want: &nrConfig{
471+
profile: "NOT_DEFAULT",
472+
tenancyOCID: "",
473+
},
474+
wantErr: false,
475+
},
476+
{
477+
name: "NOT_DEFAULT profile is parsed correctly and no other profile exists",
478+
data: []byte(`
479+
user=ocid1.user
480+
fingerprint=fingerprint
481+
key_file=~/.oci/oci_api_key.pem
482+
tenancy=ocid1.tenancy
483+
region=us-ashburn-1
484+
485+
[NOT_DEFAULT]
486+
user=ocid1.user
487+
fingerprint=fingerprint
488+
key_file=~/.oci/oci_api_key.pem
489+
tenancy=ocid1.tenancy
490+
region=us-ashburn-1`),
491+
profile: "NOT_DEFAULT",
492+
want: &nrConfig{
493+
profile: "NOT_DEFAULT",
494+
tenancyOCID: "ocid1.tenancy",
495+
},
496+
wantErr: false,
497+
},
498+
}
499+
for _, tt := range tests {
500+
t.Run(tt.name, func(t *testing.T) {
501+
got, gotErr := parseOCIConfigFile(tt.data, tt.profile)
502+
if gotErr != nil {
503+
if !tt.wantErr {
504+
t.Errorf("parseOCIConfigFile() failed: %v", gotErr)
505+
}
506+
return
507+
}
508+
if tt.wantErr {
509+
t.Fatal("parseOCIConfigFile() succeeded unexpectedly")
510+
}
511+
if tt.want.profile != got.profile {
512+
t.Errorf("parseOCIConfigFile() = %v, want %v", got, tt.want)
513+
}
514+
if tt.want.tenancyOCID != got.tenancyOCID {
515+
t.Errorf("parseOCIConfigFile() = %v, want %v", got, tt.want)
516+
}
517+
})
518+
}
519+
}

0 commit comments

Comments
 (0)