chore: remove the extension sock file before start (#512)
Co-authored-by: rick <linuxsuren@users.noreply.github.com>
This commit is contained in:
parent
a8c52fa891
commit
33c89be15c
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with 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) {
|
func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderServer) (err error) {
|
||||||
protocol, address := ext.GetListenAddress()
|
protocol, address := ext.GetListenAddress()
|
||||||
|
// remove the exist socket file
|
||||||
|
if ext.Socket != "" {
|
||||||
|
_ = os.Remove(ext.Socket)
|
||||||
|
}
|
||||||
|
|
||||||
var lis net.Listener
|
var lis net.Listener
|
||||||
lis, err = net.Listen(protocol, address)
|
lis, err = net.Listen(protocol, address)
|
||||||
|
@ -85,6 +89,7 @@ func CreateRunner(ext *Extension, c *cobra.Command, remoteServer remote.LoaderSe
|
||||||
|
|
||||||
RegisterStopSignal(c.Context(), func() {
|
RegisterStopSignal(c.Context(), func() {
|
||||||
_ = os.Remove(ext.Socket)
|
_ = os.Remove(ext.Socket)
|
||||||
|
_ = lis.Close()
|
||||||
}, gRPCServer)
|
}, gRPCServer)
|
||||||
|
|
||||||
err = gRPCServer.Serve(lis)
|
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) {
|
func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.MonitorServer) (err error) {
|
||||||
protocol, address := ext.GetListenAddress()
|
protocol, address := ext.GetListenAddress()
|
||||||
|
// remove the exist socket file
|
||||||
|
if ext.Socket != "" {
|
||||||
|
_ = os.Remove(ext.Socket)
|
||||||
|
}
|
||||||
|
|
||||||
var lis net.Listener
|
var lis net.Listener
|
||||||
lis, err = net.Listen(protocol, address)
|
lis, err = net.Listen(protocol, address)
|
||||||
|
@ -106,6 +115,7 @@ func CreateMonitor(ext *Extension, c *cobra.Command, remoteServer monitor.Monito
|
||||||
|
|
||||||
RegisterStopSignal(c.Context(), func() {
|
RegisterStopSignal(c.Context(), func() {
|
||||||
_ = os.Remove(ext.Socket)
|
_ = os.Remove(ext.Socket)
|
||||||
|
_ = lis.Close()
|
||||||
}, gRPCServer)
|
}, gRPCServer)
|
||||||
|
|
||||||
err = gRPCServer.Serve(lis)
|
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) {
|
func CreateExtensionRunner(ext *Extension, c *cobra.Command, remoteServer server.RunnerExtensionServer) (err error) {
|
||||||
protocol, address := ext.GetListenAddress()
|
protocol, address := ext.GetListenAddress()
|
||||||
|
// remove the exist socket file
|
||||||
|
if ext.Socket != "" {
|
||||||
|
_ = os.Remove(ext.Socket)
|
||||||
|
}
|
||||||
|
|
||||||
var lis net.Listener
|
var lis net.Listener
|
||||||
lis, err = net.Listen(protocol, address)
|
lis, err = net.Listen(protocol, address)
|
||||||
|
@ -127,6 +141,7 @@ func CreateExtensionRunner(ext *Extension, c *cobra.Command, remoteServer server
|
||||||
|
|
||||||
RegisterStopSignal(c.Context(), func() {
|
RegisterStopSignal(c.Context(), func() {
|
||||||
_ = os.Remove(ext.Socket)
|
_ = os.Remove(ext.Socket)
|
||||||
|
_ = lis.Close()
|
||||||
}, gRPCServer)
|
}, gRPCServer)
|
||||||
|
|
||||||
err = gRPCServer.Serve(lis)
|
err = gRPCServer.Serve(lis)
|
||||||
|
|
|
@ -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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -16,8 +16,13 @@ limitations under the License.
|
||||||
package extension
|
package extension
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"github.com/stretchr/testify/assert"
|
"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("port"))
|
||||||
assert.NotNil(t, flags.Lookup("socket"))
|
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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue