Skip to content

Commit

Permalink
Added a few unit tests and scenarios (#579)
Browse files Browse the repository at this point in the history
Signed-off-by: muhammet.arikan <[email protected]>
  • Loading branch information
marikann authored Mar 2, 2024
1 parent bc8872c commit 906f272
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/safe/internal/backoff/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func TestRetry(t *testing.T) {
strategy: Strategy{},
expectError: false,
},
{
name: "TestSetMaxDurationIfExponentialEnabledAndMaxDurationZero",
failuresBeforeSuccess: 3,
strategy: Strategy{Exponential: true, MaxDuration: 0},
expectError: false,
},
}

for _, tt := range tests {
Expand Down
40 changes: 40 additions & 0 deletions core/entity/data/v1/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ func TestJsonTime_MarshalJSON(t *testing.T) {
}
}

func TestJsonTimeUnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
jsonData []byte
wantErr bool
}{
{
name: "invalid_json_data",
jsonData: []byte(`"invalid_date"`),
wantErr: true,
},
{
name: "valid_json_data",
jsonData: []byte(`"2024-03-01T12:00:00Z"`),
wantErr: false,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
var jsonTime JsonTime
err := jsonTime.UnmarshalJSON(tt.jsonData)
if (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestJsonTimeString(t *testing.T) {
jsonTime := JsonTime(time.Now())

expectedTimeStr := time.Now().Format(time.RFC3339)

result := jsonTime.String()

if result != expectedTimeStr {
t.Errorf("Expected: %s, Got: %s", expectedTimeStr, result)
}
}

func Test_parseForK8sSecret(t *testing.T) {
type args struct {
secret SecretStored
Expand Down
53 changes: 53 additions & 0 deletions core/env/safe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,59 @@ func TestSafeRootKeyPath(t *testing.T) {
}
}

func TestSourceAcquisitionTimeout(t *testing.T) {
tests := []struct {
name string
setup func() error
cleanup func() error
want time.Duration
}{
{
name: "default_safe_source_acquisition_timeout",
want: 10000 * time.Millisecond,
},
{
name: "safe_source_acquisition_timeout_from_env",
setup: func() error {
return os.Setenv("VSECM_SAFE_SOURCE_ACQUISITION_TIMEOUT", "500")
},
cleanup: func() error {
return os.Unsetenv("VSECM_SAFE_SOURCE_ACQUISITION_TIMEOUT")
},
want: 500 * time.Millisecond,
},
{
name: "invalid_safe_source_acquisition_timeout_from_env",
setup: func() error {
return os.Setenv("VSECM_SAFE_SOURCE_ACQUISITION_TIMEOUT", "test")
},
cleanup: func() error {
return os.Unsetenv("VSECM_SAFE_SOURCE_ACQUISITION_TIMEOUT")
},
want: 10000 * time.Millisecond,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setup != nil {
if err := tt.setup(); err != nil {
t.Errorf("SourceAcquisitionTimeoutForSafe() = failed to setup, with error: %+v", err)
}
}
defer func() {
if tt.cleanup != nil {
if err := tt.cleanup(); err != nil {
t.Errorf("SourceAcquisitionTimeoutForSafe() = failed to cleanup, with error: %+v", err)
}
}
}()
if got := SourceAcquisitionTimeoutForSafe(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("SourceAcquisitionTimeoutForSafe() = %v, want %v", got, tt.want)
}
})
}
}

func TestSafeBootstrapTimeout(t *testing.T) {
tests := []struct {
name string
Expand Down
8 changes: 8 additions & 0 deletions core/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ func TestTryParse(t *testing.T) {
},
want: "{username}",
},
{
name: "invalid_template_valid_json",
args: args{
tmpStr: "{\"USER\":\"{{}}\", \"PASS\":\"{{.password}}\"}",
jason: "{\"username\":\"admin\",\"password\":\"VSecMRocks\"}",
},
want: "{\"username\":\"admin\",\"password\":\"VSecMRocks\"}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 906f272

Please sign in to comment.