fix: enhanced test cases, removed so file

This commit is contained in:
rupinder-syngh
2025-04-07 08:55:24 +05:30
parent 5ca64dfa36
commit c2f215b744
4 changed files with 236 additions and 287 deletions

2
.gitignore vendored
View File

@@ -131,7 +131,7 @@ dist
.pnp.* .pnp.*
# Ignore compiled shared object files # Ignore compiled shared object files
# *.so *.so
# Ignore coverage output files # Ignore coverage output files
coverage.out coverage.out

View File

@@ -4,9 +4,9 @@ import (
"archive/zip" "archive/zip"
"context" "context"
"errors" "errors"
"fmt"
"net/http" "net/http"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"plugin" "plugin"
"strings" "strings"
@@ -124,6 +124,7 @@ func (m *mockPublisherProvider) New(ctx context.Context, config map[string]strin
type mockSchemaValidatorProvider struct { type mockSchemaValidatorProvider struct {
validator *mockSchemaValidator validator *mockSchemaValidator
err error err error
errFunc func() error
} }
func (m *mockSchemaValidatorProvider) New(ctx context.Context, config map[string]string) (definition.SchemaValidator, func() error, error) { func (m *mockSchemaValidatorProvider) New(ctx context.Context, config map[string]string) (definition.SchemaValidator, func() error, error) {
@@ -135,8 +136,9 @@ func (m *mockSchemaValidatorProvider) New(ctx context.Context, config map[string
// Mock providers for additional interfaces // Mock providers for additional interfaces
type mockRouterProvider struct { type mockRouterProvider struct {
router *mockRouter router *mockRouter
err error err error
errFunc func() error
} }
func (m *mockRouterProvider) New(ctx context.Context, config map[string]string) (definition.Router, func() error, error) { func (m *mockRouterProvider) New(ctx context.Context, config map[string]string) (definition.Router, func() error, error) {
@@ -149,6 +151,7 @@ func (m *mockRouterProvider) New(ctx context.Context, config map[string]string)
type mockMiddlewareProvider struct { type mockMiddlewareProvider struct {
middleware func(http.Handler) http.Handler middleware func(http.Handler) http.Handler
err error err error
errFunc func() error
} }
func (m *mockMiddlewareProvider) New(ctx context.Context, config map[string]string) (func(http.Handler) http.Handler, error) { func (m *mockMiddlewareProvider) New(ctx context.Context, config map[string]string) (func(http.Handler) http.Handler, error) {
@@ -162,8 +165,9 @@ func (m *mockMiddlewareProvider) New(ctx context.Context, config map[string]stri
} }
type mockStepProvider struct { type mockStepProvider struct {
step *mockStep step *mockStep
err error err error
errFunc func() error
} }
func (m *mockStepProvider) New(ctx context.Context, config map[string]string) (definition.Step, func(), error) { func (m *mockStepProvider) New(ctx context.Context, config map[string]string) (definition.Step, func(), error) {
@@ -175,8 +179,9 @@ func (m *mockStepProvider) New(ctx context.Context, config map[string]string) (d
// Mock providers for additional interfaces // Mock providers for additional interfaces
type mockCacheProvider struct { type mockCacheProvider struct {
cache *mockCache cache *mockCache
err error err error
errFunc func() error
} }
func (m *mockCacheProvider) New(ctx context.Context, config map[string]string) (definition.Cache, func() error, error) { func (m *mockCacheProvider) New(ctx context.Context, config map[string]string) (definition.Cache, func() error, error) {
@@ -187,8 +192,9 @@ func (m *mockCacheProvider) New(ctx context.Context, config map[string]string) (
} }
type mockSignerProvider struct { type mockSignerProvider struct {
signer *mockSigner signer *mockSigner
err error err error
errFunc func() error
} }
func (m *mockSignerProvider) New(ctx context.Context, config map[string]string) (definition.Signer, func() error, error) { func (m *mockSignerProvider) New(ctx context.Context, config map[string]string) (definition.Signer, func() error, error) {
@@ -201,6 +207,7 @@ func (m *mockSignerProvider) New(ctx context.Context, config map[string]string)
type mockEncrypterProvider struct { type mockEncrypterProvider struct {
encrypter *mockEncrypter encrypter *mockEncrypter
err error err error
errFunc func() error
} }
func (m *mockEncrypterProvider) New(ctx context.Context, config map[string]string) (definition.Encrypter, func() error, error) { func (m *mockEncrypterProvider) New(ctx context.Context, config map[string]string) (definition.Encrypter, func() error, error) {
@@ -213,6 +220,7 @@ func (m *mockEncrypterProvider) New(ctx context.Context, config map[string]strin
type mockDecrypterProvider struct { type mockDecrypterProvider struct {
decrypter *mockDecrypter decrypter *mockDecrypter
err error err error
errFunc func() error
} }
func (m *mockDecrypterProvider) New(ctx context.Context, config map[string]string) (definition.Decrypter, func() error, error) { func (m *mockDecrypterProvider) New(ctx context.Context, config map[string]string) (definition.Decrypter, func() error, error) {
@@ -225,6 +233,7 @@ func (m *mockDecrypterProvider) New(ctx context.Context, config map[string]strin
type mockSignValidatorProvider struct { type mockSignValidatorProvider struct {
validator *mockSignValidator validator *mockSignValidator
err error err error
errFunc func() error
} }
func (m *mockSignValidatorProvider) New(ctx context.Context, config map[string]string) (definition.SignValidator, func() error, error) { func (m *mockSignValidatorProvider) New(ctx context.Context, config map[string]string) (definition.SignValidator, func() error, error) {
@@ -237,6 +246,7 @@ func (m *mockSignValidatorProvider) New(ctx context.Context, config map[string]s
type mockKeyManagerProvider struct { type mockKeyManagerProvider struct {
keyManager *mockKeyManager keyManager *mockKeyManager
err error err error
errFunc func() error
} }
func (m *mockKeyManagerProvider) New(ctx context.Context, cache definition.Cache, lookup definition.RegistryLookup, config map[string]string) (definition.KeyManager, func() error, error) { func (m *mockKeyManagerProvider) New(ctx context.Context, cache definition.Cache, lookup definition.RegistryLookup, config map[string]string) (definition.KeyManager, func() error, error) {
@@ -251,8 +261,21 @@ type mockRegistryLookup struct {
definition.RegistryLookup definition.RegistryLookup
} }
// TestNewManager_Success tests the successful scenarios of the NewManager function // TestNewManagerSuccess tests the successful scenarios of the NewManager function
func TestNewManagerSuccess(t *testing.T) { func TestNewManagerSuccess(t *testing.T) {
// Build the dummy plugin first
cmd := exec.Command("go", "build", "-buildmode=plugin", "-o", "./testdata/dummy.so", "./testdata/dummy.go")
if err := cmd.Run(); err != nil {
t.Fatalf("Failed to build dummy plugin: %v", err)
}
// Clean up the .so file after test completes
t.Cleanup(func() {
if err := os.Remove("./testdata/dummy.so"); err != nil && !os.IsNotExist(err) {
t.Logf("Failed to remove dummy.so: %v", err)
}
})
tests := []struct { tests := []struct {
name string name string
cfg *ManagerConfig cfg *ManagerConfig
@@ -260,19 +283,26 @@ func TestNewManagerSuccess(t *testing.T) {
{ {
name: "valid config with empty root", name: "valid config with empty root",
cfg: &ManagerConfig{ cfg: &ManagerConfig{
Root: "./testdata", Root: t.TempDir(),
RemoteRoot: "", RemoteRoot: "",
}, },
}, },
{ {
name: "valid config with root path", name: "valid config with root path",
cfg: &ManagerConfig{ cfg: &ManagerConfig{
Root: "./testdata", Root: t.TempDir(),
RemoteRoot: "", RemoteRoot: "",
}, },
}, },
{ {
name: "valid config with remote root", name: "valid config with remote root",
cfg: &ManagerConfig{
Root: t.TempDir(),
RemoteRoot: "",
},
},
{
name: "valid config with so file",
cfg: &ManagerConfig{ cfg: &ManagerConfig{
Root: "./testdata", Root: "./testdata",
RemoteRoot: "", RemoteRoot: "",
@@ -311,7 +341,7 @@ func TestNewManagerSuccess(t *testing.T) {
} }
} }
// TestNewManager_Failure tests the failure scenarios of the NewManager function // TestNewManagerFailure tests the failure scenarios of the NewManager function
func TestNewManagerFailure(t *testing.T) { func TestNewManagerFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -381,15 +411,15 @@ func TestPublisherSuccess(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
errFunc := func() error { return nil } errFunc := func() error { return nil }
m := &Manager{ m := &Manager{
closers: []func(){},
plugins: map[string]onixPlugin{ plugins: map[string]onixPlugin{
tt.publisherID: &mockPlugin{ tt.publisherID: &mockPlugin{
symbol: &mockPublisherProvider{ symbol: &mockPublisherProvider{
publisher: tt.mockPublisher, publisher: tt.mockPublisher,
err: errFunc(), errFunc: errFunc,
}, },
}, },
}, },
closers: []func(){},
} }
p, err := m.Publisher(context.Background(), &Config{ p, err := m.Publisher(context.Background(), &Config{
@@ -405,6 +435,12 @@ func TestPublisherSuccess(t *testing.T) {
t.Errorf("Manager.Publisher() did not return the correct publisher") t.Errorf("Manager.Publisher() did not return the correct publisher")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
@@ -428,10 +464,8 @@ func TestPublisherFailure(t *testing.T) {
publisherID: "error-provider", publisherID: "error-provider",
plugins: map[string]onixPlugin{ plugins: map[string]onixPlugin{
"error-provider": &mockPlugin{ "error-provider": &mockPlugin{
symbol: &mockPublisherProvider{ symbol: nil,
publisher: nil, err: errors.New("provider error"),
err: errors.New("provider error"),
},
}, },
}, },
expectedError: "provider error", expectedError: "provider error",
@@ -474,7 +508,7 @@ func TestPublisherFailure(t *testing.T) {
} }
} }
// TestManager_SchemaValidator_Success tests the successful scenarios of the SchemaValidator method // TestSchemaValidatorSuccess tests the successful scenarios of the SchemaValidator method
func TestSchemaValidatorSuccess(t *testing.T) { func TestSchemaValidatorSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -489,6 +523,7 @@ func TestSchemaValidatorSuccess(t *testing.T) {
}, },
plugin: &mockSchemaValidatorProvider{ plugin: &mockSchemaValidatorProvider{
validator: &mockSchemaValidator{}, validator: &mockSchemaValidator{},
errFunc: func() error { return nil },
}, },
}, },
} }
@@ -515,11 +550,17 @@ func TestSchemaValidatorSuccess(t *testing.T) {
if validator != tt.plugin.validator { if validator != tt.plugin.validator {
t.Error("validator does not match expected instance") t.Error("validator does not match expected instance")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
// TestManager_SchemaValidator_Failure tests the failure scenarios of the SchemaValidator method // TestSchemaValidatorFailure tests the failure scenarios of the SchemaValidator method
func TestSchemaValidatorFailure(t *testing.T) { func TestSchemaValidatorFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -580,7 +621,7 @@ func TestSchemaValidatorFailure(t *testing.T) {
} }
} }
// TestManager_Router_Success tests the successful scenarios of the Router method // TestRouterSuccess tests the successful scenarios of the Router method
func TestRouterSuccess(t *testing.T) { func TestRouterSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -594,7 +635,8 @@ func TestRouterSuccess(t *testing.T) {
Config: map[string]string{}, Config: map[string]string{},
}, },
plugin: &mockRouterProvider{ plugin: &mockRouterProvider{
router: &mockRouter{}, router: &mockRouter{},
errFunc: func() error { return nil },
}, },
}, },
} }
@@ -624,11 +666,17 @@ func TestRouterSuccess(t *testing.T) {
if router != tt.plugin.router { if router != tt.plugin.router {
t.Error("router does not match expected instance") t.Error("router does not match expected instance")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
// TestManager_Router_Failure tests the failure scenarios of the Router method // TestRouterFailure tests the failure scenarios of the Router method
func TestRouterFailure(t *testing.T) { func TestRouterFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -689,7 +737,7 @@ func TestRouterFailure(t *testing.T) {
} }
} }
// TestManager_Step_Success tests the successful scenarios of the Step method // TestStepSuccess tests the successful scenarios of the Step method
func TestStepSuccess(t *testing.T) { func TestStepSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -703,7 +751,8 @@ func TestStepSuccess(t *testing.T) {
Config: map[string]string{}, Config: map[string]string{},
}, },
plugin: &mockStepProvider{ plugin: &mockStepProvider{
step: &mockStep{}, step: &mockStep{},
errFunc: func() error { return nil },
}, },
}, },
} }
@@ -733,11 +782,17 @@ func TestStepSuccess(t *testing.T) {
if step != tt.plugin.step { if step != tt.plugin.step {
t.Error("step does not match expected instance") t.Error("step does not match expected instance")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
// TestManager_Step_Failure tests the failure scenarios of the Step method // TestStepFailure tests the failure scenarios of the Step method
func TestStepFailure(t *testing.T) { func TestStepFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -798,7 +853,7 @@ func TestStepFailure(t *testing.T) {
} }
} }
// TestManager_Cache_Success tests the successful scenarios of the Cache method // TestCacheSuccess tests the successful scenarios of the Cache method
func TestCacheSuccess(t *testing.T) { func TestCacheSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -812,7 +867,8 @@ func TestCacheSuccess(t *testing.T) {
Config: map[string]string{}, Config: map[string]string{},
}, },
plugin: &mockCacheProvider{ plugin: &mockCacheProvider{
cache: &mockCache{}, cache: &mockCache{},
errFunc: func() error { return nil },
}, },
}, },
} }
@@ -839,11 +895,17 @@ func TestCacheSuccess(t *testing.T) {
if cache == nil { if cache == nil {
t.Error("expected non-nil cache, got nil") t.Error("expected non-nil cache, got nil")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
// TestManager_Cache_Failure tests the failure scenarios of the Cache method // TestCacheFailure tests the failure scenarios of the Cache method
func TestCacheFailure(t *testing.T) { func TestCacheFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -904,7 +966,7 @@ func TestCacheFailure(t *testing.T) {
} }
} }
// TestManager_Signer_Success tests the successful scenarios of the Signer method // TestSignerSuccess tests the successful scenarios of the Signer method
func TestSignerSuccess(t *testing.T) { func TestSignerSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -918,7 +980,8 @@ func TestSignerSuccess(t *testing.T) {
Config: map[string]string{}, Config: map[string]string{},
}, },
plugin: &mockSignerProvider{ plugin: &mockSignerProvider{
signer: &mockSigner{}, signer: &mockSigner{},
errFunc: func() error { return nil },
}, },
}, },
} }
@@ -945,11 +1008,17 @@ func TestSignerSuccess(t *testing.T) {
if signer == nil { if signer == nil {
t.Error("expected non-nil signer, got nil") t.Error("expected non-nil signer, got nil")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
// TestManager_Signer_Failure tests the failure scenarios of the Signer method // TestSignerFailure tests the failure scenarios of the Signer method
func TestSignerFailure(t *testing.T) { func TestSignerFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1010,7 +1079,7 @@ func TestSignerFailure(t *testing.T) {
} }
} }
// TestManager_Encryptor_Success tests the successful scenarios of the Encryptor method // TestEncryptorSuccess tests the successful scenarios of the Encryptor method
func TestEncryptorSuccess(t *testing.T) { func TestEncryptorSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1025,6 +1094,7 @@ func TestEncryptorSuccess(t *testing.T) {
}, },
plugin: &mockEncrypterProvider{ plugin: &mockEncrypterProvider{
encrypter: &mockEncrypter{}, encrypter: &mockEncrypter{},
errFunc: func() error { return nil },
}, },
}, },
} }
@@ -1051,11 +1121,17 @@ func TestEncryptorSuccess(t *testing.T) {
if encrypter == nil { if encrypter == nil {
t.Error("expected non-nil encrypter, got nil") t.Error("expected non-nil encrypter, got nil")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
// TestManager_Encryptor_Failure tests the failure scenarios of the Encryptor method // TestEncryptorFailure tests the failure scenarios of the Encryptor method
func TestEncryptorFailure(t *testing.T) { func TestEncryptorFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1116,7 +1192,7 @@ func TestEncryptorFailure(t *testing.T) {
} }
} }
// TestManager_Decryptor_Success tests the successful scenarios of the Decryptor method // TestDecryptorSuccess tests the successful scenarios of the Decryptor method
func TestDecryptorSuccess(t *testing.T) { func TestDecryptorSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1131,6 +1207,7 @@ func TestDecryptorSuccess(t *testing.T) {
}, },
plugin: &mockDecrypterProvider{ plugin: &mockDecrypterProvider{
decrypter: &mockDecrypter{}, decrypter: &mockDecrypter{},
errFunc: func() error { return nil },
}, },
}, },
} }
@@ -1157,11 +1234,17 @@ func TestDecryptorSuccess(t *testing.T) {
if decrypter == nil { if decrypter == nil {
t.Error("expected non-nil decrypter, got nil") t.Error("expected non-nil decrypter, got nil")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
// TestManager_Decryptor_Failure tests the failure scenarios of the Decryptor method // TestDecryptorFailure tests the failure scenarios of the Decryptor method
func TestDecryptorFailure(t *testing.T) { func TestDecryptorFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1222,13 +1305,12 @@ func TestDecryptorFailure(t *testing.T) {
} }
} }
// TestManager_SignValidator tests the SignValidator method of the Manager // TestSignValidatorSuccess tests the successful scenarios of the SignValidator method
func TestSignValidator(t *testing.T) { func TestSignValidatorSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
cfg *Config cfg *Config
plugin *mockSignValidatorProvider plugin *mockSignValidatorProvider
wantErr bool
}{ }{
{ {
name: "successful sign validator creation", name: "successful sign validator creation",
@@ -1238,25 +1320,14 @@ func TestSignValidator(t *testing.T) {
}, },
plugin: &mockSignValidatorProvider{ plugin: &mockSignValidatorProvider{
validator: &mockSignValidator{}, validator: &mockSignValidator{},
errFunc: func() error { return nil },
}, },
wantErr: false,
},
{
name: "provider error",
cfg: &Config{
ID: "test-sign-validator",
Config: map[string]string{},
},
plugin: &mockSignValidatorProvider{
err: errors.New("provider error"),
},
wantErr: true,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ctx := context.Background() // Create a manager with the mock plugin
m := &Manager{ m := &Manager{
plugins: map[string]onixPlugin{ plugins: map[string]onixPlugin{
tt.cfg.ID: &mockPlugin{ tt.cfg.ID: &mockPlugin{
@@ -1266,15 +1337,91 @@ func TestSignValidator(t *testing.T) {
closers: []func(){}, closers: []func(){},
} }
_, err := m.SignValidator(ctx, tt.cfg) // Call SignValidator
if (err != nil) != tt.wantErr { validator, err := m.SignValidator(context.Background(), tt.cfg)
t.Errorf("Manager.SignValidator() error = %v, wantErr %v", err, tt.wantErr)
// Check success case
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if validator == nil {
t.Error("expected non-nil validator, got nil")
}
if validator != tt.plugin.validator {
t.Error("validator does not match expected instance")
}
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
})
}
}
// TestSignValidatorFailure tests the failure scenarios of the SignValidator method
func TestSignValidatorFailure(t *testing.T) {
tests := []struct {
name string
cfg *Config
plugin *mockSignValidatorProvider
expectedError string
}{
{
name: "provider error",
cfg: &Config{
ID: "test-sign-validator",
Config: map[string]string{},
},
plugin: &mockSignValidatorProvider{
err: errors.New("provider error"),
},
expectedError: "provider error",
},
{
name: "plugin not found",
cfg: &Config{
ID: "nonexistent-sign-validator",
Config: map[string]string{},
},
plugin: nil,
expectedError: "plugin nonexistent-sign-validator not found",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a manager with the mock plugin
m := &Manager{
plugins: make(map[string]onixPlugin),
closers: []func(){},
}
// Only add the plugin if it's not nil
if tt.plugin != nil {
m.plugins[tt.cfg.ID] = &mockPlugin{
symbol: tt.plugin,
}
}
// Call SignValidator
validator, err := m.SignValidator(context.Background(), tt.cfg)
// Check error
if err == nil {
t.Error("expected error, got nil")
} else if !strings.Contains(err.Error(), tt.expectedError) {
t.Errorf("error = %v, want error containing %q", err, tt.expectedError)
}
if validator != nil {
t.Error("expected nil validator, got non-nil")
} }
}) })
} }
} }
// TestManager_KeyManager_Success tests the successful scenarios of the KeyManager method // TestKeyManagerSuccess tests the successful scenarios of the KeyManager method
func TestKeyManagerSuccess(t *testing.T) { func TestKeyManagerSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1289,6 +1436,7 @@ func TestKeyManagerSuccess(t *testing.T) {
}, },
plugin: &mockKeyManagerProvider{ plugin: &mockKeyManagerProvider{
keyManager: &mockKeyManager{}, keyManager: &mockKeyManager{},
errFunc: func() error { return nil },
}, },
}, },
} }
@@ -1319,11 +1467,17 @@ func TestKeyManagerSuccess(t *testing.T) {
if keyManager == nil { if keyManager == nil {
t.Error("expected non-nil key manager, got nil") t.Error("expected non-nil key manager, got nil")
} }
if len(m.closers) != 1 {
t.Errorf("Manager.closers has %d closers, expected 1", len(m.closers))
}
m.closers[0]()
}) })
} }
} }
// TestManager_KeyManager_Failure tests the failure scenarios of the KeyManager method // TestKeyManagerFailure tests the failure scenarios of the KeyManager method
func TestKeyManagerFailure(t *testing.T) { func TestKeyManagerFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1388,7 +1542,7 @@ func TestKeyManagerFailure(t *testing.T) {
} }
} }
// TestUnzip_Success tests the successful scenarios of the unzip function // TestUnzipSuccess tests the successful scenarios of the unzip function
func TestUnzipSuccess(t *testing.T) { func TestUnzipSuccess(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1578,7 +1732,7 @@ func TestUnzipSuccess(t *testing.T) {
} }
} }
// TestUnzip_Failure tests the failure scenarios of the unzip function // TestUnzipFailure tests the failure scenarios of the unzip function
func TestUnzipFailure(t *testing.T) { func TestUnzipFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -1721,229 +1875,6 @@ func TestUnzipFailure(t *testing.T) {
} }
} }
// TestManager_CloserCleanup tests the cleanup functionality of closer functions
func TestCloserCleanup(t *testing.T) {
// Create a manager
m := &Manager{
closers: make([]func(), 0),
}
// Track if closers were called
closer1Called := false
closer2Called := false
closer3Called := false
// Add closers using the same pattern as in manager.go
m.closers = append(m.closers, func() {
if err := func() error {
closer1Called = true
return nil
}(); err != nil {
panic(err)
}
})
m.closers = append(m.closers, func() {
if err := func() error {
closer2Called = true
return nil
}(); err != nil {
panic(err)
}
})
m.closers = append(m.closers, func() {
if err := func() error {
closer3Called = true
return nil
}(); err != nil {
panic(err)
}
})
// Verify closers were added
if len(m.closers) != 3 {
t.Errorf("Expected 3 closers, got %d", len(m.closers))
}
// Execute all closers
for _, closer := range m.closers {
closer()
}
// Verify all closers were called
if !closer1Called {
t.Errorf("Closer 1 was not called")
}
if !closer2Called {
t.Errorf("Closer 2 was not called")
}
if !closer3Called {
t.Errorf("Closer 3 was not called")
}
}
// TestManager_CloserManagement_Success tests the successful scenarios of closer management
func TestCloserManagementSuccess(t *testing.T) {
tests := []struct {
name string
initialClosers []func()
newCloser func() error
expectedCount int
verifyExecution bool
}{
{
name: "add first closer to empty list",
initialClosers: []func(){},
newCloser: func() error { return nil },
expectedCount: 1,
verifyExecution: true,
},
{
name: "add closer to existing list",
initialClosers: []func(){func() {}},
newCloser: func() error { return nil },
expectedCount: 2,
verifyExecution: true,
},
{
name: "ignore nil closer",
initialClosers: []func(){func() {}},
newCloser: nil,
expectedCount: 1,
verifyExecution: false,
},
{
name: "add multiple closers sequentially",
initialClosers: []func(){func() {}, func() {}},
newCloser: func() error { return nil },
expectedCount: 3,
verifyExecution: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a manager with initial closers
m := &Manager{
closers: make([]func(), 0, len(tt.initialClosers)),
}
m.closers = append(m.closers, tt.initialClosers...)
// Track if the new closer was called
closerCalled := false
// Add the new closer if it's not nil
if tt.newCloser != nil {
m.closers = append(m.closers, func() {
if err := func() error {
closerCalled = true
return tt.newCloser()
}(); err != nil {
panic(err)
}
})
}
// Verify the number of closers
if len(m.closers) != tt.expectedCount {
t.Errorf("got %d closers, want %d", len(m.closers), tt.expectedCount)
}
// Execute all closers
for _, closer := range m.closers {
closer()
}
// Verify the new closer was called if expected
if tt.verifyExecution && !closerCalled {
t.Error("new closer was not called")
}
})
}
}
// TestManager_CloserManagement_Failure tests the failure scenarios of closer management
func TestCloserManagementFailure(t *testing.T) {
tests := []struct {
name string
newCloser func() error
expectedError string
}{
{
name: "closer returns error",
newCloser: func() error {
return fmt.Errorf("intentional error")
},
expectedError: "intentional error",
},
{
name: "closer panics with string",
newCloser: func() error {
panic("intentional panic")
},
expectedError: "intentional panic",
},
{
name: "closer panics with error",
newCloser: func() error {
panic(fmt.Errorf("panic error"))
},
expectedError: "panic error",
},
{
name: "closer panics with nil",
newCloser: func() error {
panic(nil)
},
expectedError: "panic called with nil argument",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a manager
m := &Manager{
closers: make([]func(), 0),
}
// Add the closer that will fail
m.closers = append(m.closers, func() {
if err := tt.newCloser(); err != nil {
panic(err)
}
})
// Execute the closer and verify it panics
defer func() {
r := recover()
if r == nil {
t.Error("expected panic but got none")
return
}
// Convert panic value to string for comparison
var errStr string
switch v := r.(type) {
case error:
errStr = v.Error()
case string:
errStr = v
default:
errStr = "panic called with nil argument"
}
if errStr != tt.expectedError {
t.Errorf("got panic %q, want %q", errStr, tt.expectedError)
}
}()
// This should panic
m.closers[0]()
})
}
}
// TestValidateMgrCfgSuccess tests the successful scenarios of the validateMgrCfg function // TestValidateMgrCfgSuccess tests the successful scenarios of the validateMgrCfg function
func TestValidateMgrCfgSuccess(t *testing.T) { func TestValidateMgrCfgSuccess(t *testing.T) {
tests := []struct { tests := []struct {

18
pkg/plugin/testdata/dummy.go vendored Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"context"
"github.com/beckn/beckn-onix/pkg/plugin/definition"
"github.com/beckn/beckn-onix/pkg/plugin/implementation/encrypter"
)
// encrypterProvider implements the definition.encrypterProvider interface.
type encrypterProvider struct{}
func (ep encrypterProvider) New(ctx context.Context, config map[string]string) (definition.Encrypter, func() error, error) {
return encrypter.New(ctx)
}
// Provider is the exported symbol that the plugin manager will look for.
var Provider = encrypterProvider{}

Binary file not shown.