Skip to content

Commit 10ea8e5

Browse files
Adding unit tests (#1217)
Signed-off-by: Abhishek Sharma <[email protected]>
1 parent 2fc37cb commit 10ea8e5

File tree

9 files changed

+721
-0
lines changed

9 files changed

+721
-0
lines changed

app/keygen/internal/generate_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,20 @@
99
*/
1010

1111
package internal
12+
13+
import "testing"
14+
15+
func TestPrintGeneratedKeys(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
}{
19+
{
20+
name: "sample_test",
21+
},
22+
}
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
PrintGeneratedKeys()
26+
})
27+
}
28+
}

app/safe/internal/bootstrap/bootstrap_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,44 @@
99
*/
1010

1111
package bootstrap
12+
13+
import (
14+
"testing"
15+
)
16+
17+
var id string = "test_id"
18+
19+
func TestChannelsToMonitor_Size(t *testing.T) {
20+
type fields struct {
21+
AcquiredSvid <-chan bool
22+
UpdatedSecret <-chan bool
23+
ServerStarted <-chan bool
24+
}
25+
tests := []struct {
26+
name string
27+
fields fields
28+
want int
29+
}{
30+
{
31+
name: "test-1",
32+
fields: fields{
33+
AcquiredSvid: make(<-chan bool),
34+
UpdatedSecret: make(<-chan bool),
35+
ServerStarted: make(<-chan bool),
36+
},
37+
want: 3,
38+
},
39+
}
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
c := ChannelsToMonitor{
43+
AcquiredSvid: tt.fields.AcquiredSvid,
44+
UpdatedSecret: tt.fields.UpdatedSecret,
45+
ServerStarted: tt.fields.ServerStarted,
46+
}
47+
if got := c.Size(); got != tt.want {
48+
t.Errorf("ChannelsToMonitor.Size() = %v, want %v", got, tt.want)
49+
}
50+
})
51+
}
52+
}

app/safe/internal/bootstrap/init_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,28 @@
99
*/
1010

1111
package bootstrap
12+
13+
import "testing"
14+
15+
func Test_completeInitialization(t *testing.T) {
16+
id := "test_id"
17+
type args struct {
18+
correlationId *string
19+
}
20+
tests := []struct {
21+
name string
22+
args args
23+
}{
24+
{
25+
name: "test-1",
26+
args: args{
27+
correlationId: &id,
28+
},
29+
},
30+
}
31+
for _, tt := range tests {
32+
t.Run(tt.name, func(t *testing.T) {
33+
completeInitialization(tt.args.correlationId)
34+
})
35+
}
36+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
| Protect your secrets, protect your sensitive data.
3+
: Explore VMware Secrets Manager docs at https://vsecm.com/
4+
</
5+
<>/ keep your secrets... secret
6+
>/
7+
<>/' Copyright 2023-present VMware Secrets Manager contributors.
8+
>/' SPDX-License-Identifier: BSD-2-Clause
9+
*/
10+
11+
package filter
12+
13+
import (
14+
"errors"
15+
"reflect"
16+
"testing"
17+
)
18+
19+
func TestValueFromPath(t *testing.T) {
20+
type args struct {
21+
data any
22+
path string
23+
}
24+
tests := []struct {
25+
name string
26+
args args
27+
want any
28+
wantErr error
29+
}{
30+
{
31+
name: "empty path",
32+
args: args{path: "", data: ""},
33+
want: "",
34+
wantErr: nil,
35+
},
36+
{
37+
name: "path without dotted notation",
38+
args: args{path: "samplePath", data: "sampleData"},
39+
want: "sampleData",
40+
wantErr: nil,
41+
},
42+
{
43+
name: "path with dotted notation, valid data",
44+
args: args{path: "path1.path2", data: map[string]interface{}{"path1": map[string]interface{}{"path2": "data"}}},
45+
want: "data",
46+
wantErr: nil,
47+
},
48+
{
49+
name: "path with dotted notation, invalid key",
50+
args: args{path: "invalidPath.path2", data: map[string]interface{}{"path1": map[string]interface{}{"path2": "data"}}},
51+
want: nil,
52+
wantErr: errors.New("key not found: invalidPath"),
53+
},
54+
{
55+
name: "path with dotted notation, array type data",
56+
args: args{path: "path1.path2", data: map[string]interface{}{"path1": []interface{}{"data"}}},
57+
want: nil,
58+
wantErr: errors.New("arrays are not supported in path queries"),
59+
},
60+
{
61+
name: "path with dotted notation, invalid data",
62+
args: args{path: "path1.path2", data: map[string]interface{}{"path1": "data"}},
63+
want: nil,
64+
wantErr: errors.New("cannot navigate further from data"),
65+
},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
got, err := ValueFromPath(tt.args.data, tt.args.path)
70+
if !reflect.DeepEqual(err, tt.wantErr) {
71+
t.Errorf("ValueFromPath() error = %v, wantErr %v", err, tt.wantErr)
72+
return
73+
}
74+
if !reflect.DeepEqual(got, tt.want) {
75+
t.Errorf("ValueFromPath() = %v, want %v", got, tt.want)
76+
}
77+
})
78+
}
79+
}

app/sentinel/internal/cli/parse_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,123 @@ func TestParseNotBefore(t *testing.T) {
260260
assert.Equal(t, "2023-12-31", *notBefore)
261261
})
262262
}
263+
264+
func TestParseWorkload(t *testing.T) {
265+
t.Run("test short flag", func(t *testing.T) {
266+
parser := newParser()
267+
workload := ParseWorkload(parser)
268+
269+
assert.NotNil(t, workload)
270+
assert.Equal(t, []string{}, *workload)
271+
272+
os.Args = []string{"sentinel", "-w", "short workload"}
273+
274+
err := parser.Parse(os.Args)
275+
assert.NoError(t, err)
276+
assert.Equal(t, []string{"short workload"}, *workload)
277+
})
278+
279+
t.Run("test long flag", func(t *testing.T) {
280+
parser := newParser()
281+
workload := ParseWorkload(parser)
282+
283+
assert.NotNil(t, workload)
284+
assert.Equal(t, []string{}, *workload)
285+
286+
os.Args = []string{"sentinel", "--workload", "long workload"}
287+
288+
err := parser.Parse(os.Args)
289+
assert.NoError(t, err)
290+
assert.Equal(t, []string{"long workload"}, *workload)
291+
})
292+
}
293+
294+
func TestParseSecret(t *testing.T) {
295+
t.Run("test short flag", func(t *testing.T) {
296+
parser := newParser()
297+
secret := ParseSecret(parser)
298+
299+
assert.NotNil(t, secret)
300+
assert.Equal(t, "", *secret)
301+
302+
os.Args = []string{"sentinel", "-s", "short secret option"}
303+
304+
err := parser.Parse(os.Args)
305+
assert.NoError(t, err)
306+
assert.Equal(t, "short secret option", *secret)
307+
})
308+
309+
t.Run("test long flag", func(t *testing.T) {
310+
parser := newParser()
311+
secret := ParseSecret(parser)
312+
313+
assert.NotNil(t, secret)
314+
assert.Equal(t, "", *secret)
315+
316+
os.Args = []string{"sentinel", "--secret", "long secret option"}
317+
318+
err := parser.Parse(os.Args)
319+
assert.NoError(t, err)
320+
assert.Equal(t, "long secret option", *secret)
321+
})
322+
}
323+
324+
func TestParseTemplate(t *testing.T) {
325+
t.Run("test short flag", func(t *testing.T) {
326+
parser := newParser()
327+
template := ParseTemplate(parser)
328+
329+
assert.NotNil(t, template)
330+
assert.Equal(t, "", *template)
331+
332+
os.Args = []string{"sentinel", "-t", "short template"}
333+
334+
err := parser.Parse(os.Args)
335+
assert.NoError(t, err)
336+
assert.Equal(t, "short template", *template)
337+
})
338+
339+
t.Run("test long flag", func(t *testing.T) {
340+
parser := newParser()
341+
template := ParseTemplate(parser)
342+
343+
assert.NotNil(t, template)
344+
assert.Equal(t, "", *template)
345+
346+
os.Args = []string{"sentinel", "--template", "long template"}
347+
348+
err := parser.Parse(os.Args)
349+
assert.NoError(t, err)
350+
assert.Equal(t, "long template", *template)
351+
})
352+
}
353+
354+
func TestParseFormat(t *testing.T) {
355+
t.Run("test short flag", func(t *testing.T) {
356+
parser := newParser()
357+
format := ParseFormat(parser)
358+
359+
assert.NotNil(t, format)
360+
assert.Equal(t, "", *format)
361+
362+
os.Args = []string{"sentinel", "-f", "short format"}
363+
364+
err := parser.Parse(os.Args)
365+
assert.NoError(t, err)
366+
assert.Equal(t, "short format", *format)
367+
})
368+
369+
t.Run("test long flag", func(t *testing.T) {
370+
parser := newParser()
371+
format := ParseFormat(parser)
372+
373+
assert.NotNil(t, format)
374+
assert.Equal(t, "", *format)
375+
376+
os.Args = []string{"sentinel", "--format", "long format"}
377+
378+
err := parser.Parse(os.Args)
379+
assert.NoError(t, err)
380+
assert.Equal(t, "long format", *format)
381+
})
382+
}

0 commit comments

Comments
 (0)