diff --git a/pkg/extension/option.go b/pkg/extension/option.go index 29329c7..1c0e456 100644 --- a/pkg/extension/option.go +++ b/pkg/extension/option.go @@ -1,5 +1,5 @@ /* -Copyright 2023 API Testing Authors. +Copyright 2023-2024 API Testing Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -71,6 +71,10 @@ func (o *Extension) GetFullName() string { func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderServer) (err error) { protocol, address := ext.GetListenAddress() + // remove the exist socket file + if ext.Socket != "" { + _ = os.Remove(ext.Socket) + } var lis net.Listener lis, err = net.Listen(protocol, address) @@ -85,6 +89,7 @@ func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderSe RegisterStopSignal(c.Context(), func() { _ = os.Remove(ext.Socket) + _ = lis.Close() }, gRPCServer) err = gRPCServer.Serve(lis) @@ -93,6 +98,10 @@ func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderSe func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.MonitorServer) (err error) { protocol, address := ext.GetListenAddress() + // remove the exist socket file + if ext.Socket != "" { + _ = os.Remove(ext.Socket) + } var lis net.Listener lis, err = net.Listen(protocol, address) @@ -106,6 +115,7 @@ func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.Monito RegisterStopSignal(c.Context(), func() { _ = os.Remove(ext.Socket) + _ = lis.Close() }, gRPCServer) err = gRPCServer.Serve(lis) @@ -114,6 +124,10 @@ func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.Monito func CreateExtensionRunner(ext *Extension, c *cobra.Command, remoteServer server.RunnerExtensionServer) (err error) { protocol, address := ext.GetListenAddress() + // remove the exist socket file + if ext.Socket != "" { + _ = os.Remove(ext.Socket) + } var lis net.Listener lis, err = net.Listen(protocol, address) @@ -127,6 +141,7 @@ func CreateExtensionRunner(ext *Extension, c *cobra.Command, remoteServer server RegisterStopSignal(c.Context(), func() { _ = os.Remove(ext.Socket) + _ = lis.Close() }, gRPCServer) err = gRPCServer.Serve(lis) diff --git a/pkg/extension/option_test.go b/pkg/extension/option_test.go index ad5bb10..69c82eb 100644 --- a/pkg/extension/option_test.go +++ b/pkg/extension/option_test.go @@ -1,5 +1,5 @@ /* -Copyright 2023 API Testing Authors. +Copyright 2023-2024 API Testing Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -16,8 +16,13 @@ limitations under the License. package extension import ( + "context" + "os" + "path/filepath" "testing" + "time" + "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/stretchr/testify/assert" ) @@ -54,3 +59,78 @@ func TestExtension(t *testing.T) { assert.NotNil(t, flags.Lookup("port")) assert.NotNil(t, flags.Lookup("socket")) } + +func TestCreateRunner(t *testing.T) { + + t.Run("invalid port", func(t *testing.T) { + extMgr := NewExtension("git", "store", 75530) + extMgr.Port = 75530 + assert.NotNil(t, extMgr) + assert.Error(t, CreateRunner(extMgr, nil, nil)) + assert.Error(t, CreateMonitor(extMgr, nil, nil)) + assert.Error(t, CreateExtensionRunner(extMgr, nil, nil)) + }) + + t.Run("random port", func(t *testing.T) { + extMgr := NewExtension("git", "store", -1) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + command := &cobra.Command{} + command.SetContext(ctx) + assert.Error(t, CreateRunner(extMgr, command, nil)) + }) + + t.Run("random port, CreateMonitor", func(t *testing.T) { + extMgr := NewExtension("git", "store", -1) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + command := &cobra.Command{} + command.SetContext(ctx) + assert.Error(t, CreateMonitor(extMgr, command, nil)) + }) + + t.Run("random port, CreateExtensionRunner", func(t *testing.T) { + extMgr := NewExtension("git", "store", -1) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + command := &cobra.Command{} + command.SetContext(ctx) + assert.Error(t, CreateExtensionRunner(extMgr, command, nil)) + }) + + t.Run("socket", func(t *testing.T) { + extMgr := NewExtension("git", "store", -1) + extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String()) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + command := &cobra.Command{} + command.SetContext(ctx) + assert.Error(t, CreateRunner(extMgr, command, nil)) + }) + + t.Run("socket, CreateMonitor", func(t *testing.T) { + extMgr := NewExtension("git", "store", -1) + extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String()) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + command := &cobra.Command{} + command.SetContext(ctx) + assert.Error(t, CreateMonitor(extMgr, command, nil)) + }) + + t.Run("socket, CreateExtensionRunner", func(t *testing.T) { + extMgr := NewExtension("git", "store", -1) + extMgr.Socket = filepath.Join(os.TempDir(), time.Microsecond.String()) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + command := &cobra.Command{} + command.SetContext(ctx) + assert.Error(t, CreateExtensionRunner(extMgr, command, nil)) + }) +}