Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ test(VSecM): increase test coverage from core/env #970

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions core/env/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,213 @@
*/

package env

import (
"os"
"testing"
"time"
)

func TestBackoffMaxRetries(t *testing.T) {
tests := []struct {
name string
envValue string
expected int64
}{
{
name: "Environment variable not set",
envValue: "",
expected: 10,
},
{
name: "Environment variable set to valid value",
envValue: "5",
expected: 5,
},
{
name: "Environment variable set to invalid value",
envValue: "invalid",
expected: 10,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
err := os.Setenv("VSECM_BACKOFF_MAX_RETRIES", tt.envValue)
if err != nil {
t.Errorf("Error setting environment variable: %v", err)
return
}
} else {
err := os.Unsetenv("VSECM_BACKOFF_MAX_RETRIES")
if err != nil {
t.Errorf("Error unsetting environment variable: %v", err)
return
}
}

result := BackoffMaxRetries()

if result != tt.expected {
t.Errorf("Expected %d, but got %d", tt.expected, result)
}

os.Unsetenv("VSECM_BACKOFF_MAX_RETRIES")
})
}
}

func TestBackoffDelay(t *testing.T) {
tests := []struct {
name string
envValue string
expected time.Duration
}{
{
name: "Environment variable not set",
envValue: "",
expected: 1000 * time.Millisecond,
},
{
name: "Environment variable set to valid value",
envValue: "500",
expected: 500 * time.Millisecond,
},
{
name: "Environment variable set to invalid value",
envValue: "invalid",
expected: 1000 * time.Millisecond,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
err := os.Setenv("VSECM_BACKOFF_DELAY", tt.envValue)
if err != nil {
return
}
} else {
err := os.Unsetenv("VSECM_BACKOFF_DELAY")
if err != nil {
t.Errorf("Error unsetting environment variable: %v", err)
return
}
}

result := BackoffDelay()

if result != tt.expected {
t.Errorf("Expected %v, but got %v", tt.expected, result)
}

os.Unsetenv("VSECM_BACKOFF_DELAY")
})
}
}

func TestBackoffMode(t *testing.T) {
tests := []struct {
name string
envValue string
expected string
}{
{
name: "Environment variable not set",
envValue: "",
expected: "exponential",
},
{
name: "Environment variable set to exponential",
envValue: "exponential",
expected: "exponential",
},
{
name: "Environment variable set to linear",
envValue: "linear",
expected: "linear",
},
{
name: "Environment variable set to invalid value",
envValue: "invalid",
expected: "linear",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
err := os.Setenv("VSECM_BACKOFF_MODE", tt.envValue)
if err != nil {
t.Errorf("Error setting environment variable: %v", err)
return
}
} else {
err := os.Unsetenv("VSECM_BACKOFF_MODE")
if err != nil {
t.Errorf("Error unsetting environment variable: %v", err)
return
}
}

result := BackoffMode()

if result != tt.expected {
t.Errorf("Expected %s, but got %s", tt.expected, result)
}

os.Unsetenv("VSECM_BACKOFF_MODE")
})
}
}

func TestBackoffMaxDuration(t *testing.T) {
tests := []struct {
name string
envValue string
expected time.Duration
}{
{
name: "Environment variable not set",
envValue: "",
expected: 30000 * time.Millisecond,
},
{
name: "Environment variable set to valid value",
envValue: "15000",
expected: 15000 * time.Millisecond,
},
{
name: "Environment variable set to invalid value",
envValue: "invalid",
expected: 30000 * time.Millisecond,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
err := os.Setenv("VSECM_BACKOFF_MAX_DURATION", tt.envValue)
if err != nil {
t.Errorf("Error setting environment variable: %v", err)
return
}
} else {
err := os.Unsetenv("VSECM_BACKOFF_MAX_DURATION")
if err != nil {
t.Errorf("Error unsetting environment variable: %v", err)
return
}
}

result := BackoffMaxDuration()

if result != tt.expected {
t.Errorf("Expected %v, but got %v", tt.expected, result)
}

os.Unsetenv("VSECM_BACKOFF_MAX_DURATION")
})
}
}
146 changes: 146 additions & 0 deletions core/env/keygen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,149 @@
*/

package env

import (
"os"
"testing"
)

func TestRootKeyPathForKeyGen(t *testing.T) {
tests := []struct {
name string
envValue string
expected string
}{
{
name: "Environment variable not set",
envValue: "",
expected: "/opt/vsecm/keys.txt",
},
{
name: "Environment variable set to non-empty value",
envValue: "/custom/key/path",
expected: "/custom/key/path",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
err := os.Setenv("VSECM_KEYGEN_ROOT_KEY_PATH", tt.envValue)
if err != nil {
t.Errorf("Error setting environment variable: %v", err)
return
}
} else {
err := os.Unsetenv("VSECM_KEYGEN_ROOT_KEY_PATH")
if err != nil {
t.Errorf("Error unsetting environment variable: %v", err)
return
}
}

result := RootKeyPathForKeyGen()

if result != tt.expected {
t.Errorf("Expected %s, but got %s", tt.expected, result)
}

os.Unsetenv("VSECM_KEYGEN_ROOT_KEY_PATH")
})
}
}

func TestExportedSecretPathForKeyGen(t *testing.T) {
tests := []struct {
name string
envValue string
expected string
}{
{
name: "Environment variable not set",
envValue: "",
expected: "/opt/vsecm/secrets.json",
},
{
name: "Environment variable set to non-empty value",
envValue: "/custom/secrets/path",
expected: "/custom/secrets/path",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
err := os.Setenv("VSECM_KEYGEN_EXPORTED_SECRET_PATH", tt.envValue)
if err != nil {
t.Errorf("Error setting environment variable: %v", err)
return
}
} else {
err := os.Unsetenv("VSECM_KEYGEN_EXPORTED_SECRET_PATH")
if err != nil {
t.Errorf("Error unsetting environment variable: %v", err)
return
}
}

result := ExportedSecretPathForKeyGen()

if result != tt.expected {
t.Errorf("Expected %s, but got %s", tt.expected, result)
}

os.Unsetenv("VSECM_KEYGEN_EXPORTED_SECRET_PATH")
})
}
}

func TestKeyGenDecrypt(t *testing.T) {
tests := []struct {
name string
envValue string
expected bool
}{
{
name: "Environment variable not set",
envValue: "",
expected: false,
},
{
name: "Environment variable set to true",
envValue: "true",
expected: true,
},
{
name: "Environment variable set to false",
envValue: "false",
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envValue != "" {
err := os.Setenv("VSECM_KEYGEN_DECRYPT", tt.envValue)
if err != nil {
t.Errorf("Error setting environment variable: %v", err)
return
}
} else {
err := os.Unsetenv("VSECM_KEYGEN_DECRYPT")
if err != nil {
t.Errorf("Error unsetting environment variable: %v", err)
return
}
}

result := KeyGenDecrypt()

if result != tt.expected {
t.Errorf("Expected %v, but got %v", tt.expected, result)
}

// Clean up
os.Unsetenv("VSECM_KEYGEN_DECRYPT")
})
}
}
Loading