Skip to content
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
17 changes: 16 additions & 1 deletion pkg/extension/option.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
82 changes: 81 additions & 1 deletion pkg/extension/option_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"
)
Expand Down Expand Up @@ -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))
})
}