Skip to content

Commit

Permalink
Adding unit tests (#1217)
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Sharma <[email protected]>
  • Loading branch information
abhishek44sharma authored Nov 12, 2024
1 parent 2fc37cb commit 10ea8e5
Show file tree
Hide file tree
Showing 9 changed files with 721 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/keygen/internal/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@
*/

package internal

import "testing"

func TestPrintGeneratedKeys(t *testing.T) {
tests := []struct {
name string
}{
{
name: "sample_test",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
PrintGeneratedKeys()
})
}
}
41 changes: 41 additions & 0 deletions app/safe/internal/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,44 @@
*/

package bootstrap

import (
"testing"
)

var id string = "test_id"

func TestChannelsToMonitor_Size(t *testing.T) {
type fields struct {
AcquiredSvid <-chan bool
UpdatedSecret <-chan bool
ServerStarted <-chan bool
}
tests := []struct {
name string
fields fields
want int
}{
{
name: "test-1",
fields: fields{
AcquiredSvid: make(<-chan bool),
UpdatedSecret: make(<-chan bool),
ServerStarted: make(<-chan bool),
},
want: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := ChannelsToMonitor{
AcquiredSvid: tt.fields.AcquiredSvid,
UpdatedSecret: tt.fields.UpdatedSecret,
ServerStarted: tt.fields.ServerStarted,
}
if got := c.Size(); got != tt.want {
t.Errorf("ChannelsToMonitor.Size() = %v, want %v", got, tt.want)
}
})
}
}
25 changes: 25 additions & 0 deletions app/safe/internal/bootstrap/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,28 @@
*/

package bootstrap

import "testing"

func Test_completeInitialization(t *testing.T) {
id := "test_id"
type args struct {
correlationId *string
}
tests := []struct {
name string
args args
}{
{
name: "test-1",
args: args{
correlationId: &id,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
completeInitialization(tt.args.correlationId)
})
}
}
79 changes: 79 additions & 0 deletions app/scout/internal/filter/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
| Protect your secrets, protect your sensitive data.
: Explore VMware Secrets Manager docs at https://vsecm.com/
</
<>/ keep your secrets... secret
>/
<>/' Copyright 2023-present VMware Secrets Manager contributors.
>/' SPDX-License-Identifier: BSD-2-Clause
*/

package filter

import (
"errors"
"reflect"
"testing"
)

func TestValueFromPath(t *testing.T) {
type args struct {
data any
path string
}
tests := []struct {
name string
args args
want any
wantErr error
}{
{
name: "empty path",
args: args{path: "", data: ""},
want: "",
wantErr: nil,
},
{
name: "path without dotted notation",
args: args{path: "samplePath", data: "sampleData"},
want: "sampleData",
wantErr: nil,
},
{
name: "path with dotted notation, valid data",
args: args{path: "path1.path2", data: map[string]interface{}{"path1": map[string]interface{}{"path2": "data"}}},
want: "data",
wantErr: nil,
},
{
name: "path with dotted notation, invalid key",
args: args{path: "invalidPath.path2", data: map[string]interface{}{"path1": map[string]interface{}{"path2": "data"}}},
want: nil,
wantErr: errors.New("key not found: invalidPath"),
},
{
name: "path with dotted notation, array type data",
args: args{path: "path1.path2", data: map[string]interface{}{"path1": []interface{}{"data"}}},
want: nil,
wantErr: errors.New("arrays are not supported in path queries"),
},
{
name: "path with dotted notation, invalid data",
args: args{path: "path1.path2", data: map[string]interface{}{"path1": "data"}},
want: nil,
wantErr: errors.New("cannot navigate further from data"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ValueFromPath(tt.args.data, tt.args.path)
if !reflect.DeepEqual(err, tt.wantErr) {
t.Errorf("ValueFromPath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ValueFromPath() = %v, want %v", got, tt.want)
}
})
}
}
120 changes: 120 additions & 0 deletions app/sentinel/internal/cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,123 @@ func TestParseNotBefore(t *testing.T) {
assert.Equal(t, "2023-12-31", *notBefore)
})
}

func TestParseWorkload(t *testing.T) {
t.Run("test short flag", func(t *testing.T) {
parser := newParser()
workload := ParseWorkload(parser)

assert.NotNil(t, workload)
assert.Equal(t, []string{}, *workload)

os.Args = []string{"sentinel", "-w", "short workload"}

err := parser.Parse(os.Args)
assert.NoError(t, err)
assert.Equal(t, []string{"short workload"}, *workload)
})

t.Run("test long flag", func(t *testing.T) {
parser := newParser()
workload := ParseWorkload(parser)

assert.NotNil(t, workload)
assert.Equal(t, []string{}, *workload)

os.Args = []string{"sentinel", "--workload", "long workload"}

err := parser.Parse(os.Args)
assert.NoError(t, err)
assert.Equal(t, []string{"long workload"}, *workload)
})
}

func TestParseSecret(t *testing.T) {
t.Run("test short flag", func(t *testing.T) {
parser := newParser()
secret := ParseSecret(parser)

assert.NotNil(t, secret)
assert.Equal(t, "", *secret)

os.Args = []string{"sentinel", "-s", "short secret option"}

err := parser.Parse(os.Args)
assert.NoError(t, err)
assert.Equal(t, "short secret option", *secret)
})

t.Run("test long flag", func(t *testing.T) {
parser := newParser()
secret := ParseSecret(parser)

assert.NotNil(t, secret)
assert.Equal(t, "", *secret)

os.Args = []string{"sentinel", "--secret", "long secret option"}

err := parser.Parse(os.Args)
assert.NoError(t, err)
assert.Equal(t, "long secret option", *secret)
})
}

func TestParseTemplate(t *testing.T) {
t.Run("test short flag", func(t *testing.T) {
parser := newParser()
template := ParseTemplate(parser)

assert.NotNil(t, template)
assert.Equal(t, "", *template)

os.Args = []string{"sentinel", "-t", "short template"}

err := parser.Parse(os.Args)
assert.NoError(t, err)
assert.Equal(t, "short template", *template)
})

t.Run("test long flag", func(t *testing.T) {
parser := newParser()
template := ParseTemplate(parser)

assert.NotNil(t, template)
assert.Equal(t, "", *template)

os.Args = []string{"sentinel", "--template", "long template"}

err := parser.Parse(os.Args)
assert.NoError(t, err)
assert.Equal(t, "long template", *template)
})
}

func TestParseFormat(t *testing.T) {
t.Run("test short flag", func(t *testing.T) {
parser := newParser()
format := ParseFormat(parser)

assert.NotNil(t, format)
assert.Equal(t, "", *format)

os.Args = []string{"sentinel", "-f", "short format"}

err := parser.Parse(os.Args)
assert.NoError(t, err)
assert.Equal(t, "short format", *format)
})

t.Run("test long flag", func(t *testing.T) {
parser := newParser()
format := ParseFormat(parser)

assert.NotNil(t, format)
assert.Equal(t, "", *format)

os.Args = []string{"sentinel", "--format", "long format"}

err := parser.Parse(os.Args)
assert.NoError(t, err)
assert.Equal(t, "long format", *format)
})
}
Loading

0 comments on commit 10ea8e5

Please sign in to comment.