BUG/MINOR: runtime: use context for canceling jobs

single client did not use context and exited when other parts of runtime did
v3.0
Zlatko Bratkovic 2 years ago
parent 114fe7102c
commit b15a346a25
  1. 4
      e2e/test_client.go
  2. 26
      runtime/acls_test.go
  3. 33
      runtime/certs_test.go
  4. 24
      runtime/crt-lists_test.go
  5. 16
      runtime/runtime_client.go
  6. 15
      runtime/runtime_single_client.go

@ -115,7 +115,9 @@ func GetClient(t *testing.T) (*ClientResponse, error) {
end := time.Now()
t.Logf("%s done", end.Format("15:04:05.000"))
runtimeClient, err := runtime.New(context.Background(), options.Socket(socketPath))
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
runtimeClient, err := runtime.New(ctx, options.Socket(socketPath))
if err != nil {
return nil, err
}

@ -1,8 +1,10 @@
package runtime
import (
"context"
"reflect"
"testing"
"time"
"github.com/haproxytech/client-native/v3/models"
)
@ -51,7 +53,9 @@ func TestSingleRuntime_ShowACLS(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -157,7 +161,9 @@ func TestSingleRuntime_GetACL(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -250,7 +256,9 @@ func TestSingleRuntime_ShowACLFileEntries(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -325,7 +333,9 @@ func TestSingleRuntime_GetACLFileEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -431,7 +441,9 @@ func TestSingleRuntime_AddACLFileEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -515,7 +527,9 @@ func TestSingleRuntime_DeleteACLFileEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return

@ -1,6 +1,7 @@
package runtime
import (
"context"
"reflect"
"testing"
"time"
@ -56,7 +57,9 @@ func TestSingleRuntime_ShowCerts(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -145,7 +148,9 @@ func TestSingleRuntime_GetCert(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -229,7 +234,9 @@ func TestSingleRuntime_ShowCertEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -305,7 +312,9 @@ func TestSingleRuntime_NewCertEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -392,7 +401,9 @@ func TestSingleRuntime_SetCertEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -465,7 +476,9 @@ func TestSingleRuntime_CommitCertEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -536,7 +549,9 @@ func TestSingleRuntime_AbortCertEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -607,7 +622,9 @@ func TestSingleRuntime_DeleteCertEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return

@ -1,8 +1,10 @@
package runtime
import (
"context"
"reflect"
"testing"
"time"
)
func TestSingleRuntime_ShowCrtLists(t *testing.T) {
@ -49,7 +51,9 @@ func TestSingleRuntime_ShowCrtLists(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -134,7 +138,9 @@ func TestSingleRuntime_GetCrtList(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -210,7 +216,7 @@ func TestSingleRuntime_ShowCrtListEntries(t *testing.T) {
socketResponse: map[string]string{
"show ssl crt-list -n /etc/haproxy/crt-list\n": ` # /etc/ssl/crt-list
/etc/ssl/cert-0.pem:1 !*.crt-test.platform.domain.com !connectivitynotification.platform.domain.com !connectivitytunnel.platform.domain.com !authentication.cert.another.domain.com !*.authentication.cert.another.domain.com
/etc/ssl/cert-1.pem:2 [verify optional ca-file /etc/ssl/ca-file-1.pem] *.crt-test.platform.domain.com !connectivitynotification.platform.domain.com
/etc/ssl/cert-1.pem:2 [verify optional ca-file /etc/ssl/ca-file-1.pem] *.crt-test.platform.domain.com !connectivitynotification.platform.domain.com
/etc/ssl/cert-2.pem:4 [verify required ca-file /etc/ssl/ca-file-2.pem]
`,
},
@ -233,7 +239,9 @@ func TestSingleRuntime_ShowCrtListEntries(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -334,7 +342,9 @@ func TestSingleRuntime_AddCrtListEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return
@ -402,7 +412,9 @@ func TestSingleRuntime_DeleteCrtListEntry(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
haProxy.SetResponses(&tt.socketResponse)
s := &SingleRuntime{}
err := s.Init(tt.fields.socketPath, tt.fields.process, tt.fields.worker)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second))
defer cancel()
err := s.Init(ctx, tt.fields.socketPath, tt.fields.process, tt.fields.worker)
if err != nil {
t.Errorf("SingleRuntime.Init() error = %v", err)
return

@ -53,7 +53,7 @@ func (c *client) initWithSockets(ctx context.Context, socketPath map[int]string)
c.runtimes = make([]SingleRuntime, 0)
for process, path := range socketPath {
runtime := SingleRuntime{}
err := runtime.InitWithContext(ctx, path, 0, process)
err := runtime.Init(ctx, path, 0, process)
if err != nil {
return err
}
@ -73,7 +73,7 @@ func (c *client) initWithMasterSocket(ctx context.Context, masterSocketPath stri
c.runtimes = make([]SingleRuntime, nbproc)
for i := 1; i <= nbproc; i++ {
runtime := SingleRuntime{}
err := runtime.InitWithContext(ctx, masterSocketPath, i, i)
err := runtime.Init(ctx, masterSocketPath, i, i)
if err != nil {
return err
}
@ -1111,15 +1111,3 @@ func (c *client) CommitACL(version, name string) error {
}
return nil
}
func isValidHaproxySocket(path string) bool {
runtime := SingleRuntime{}
err := runtime.Init(path, 0, 0)
if err != nil {
return false
}
if _, err := runtime.ExecuteRaw("help"); err != nil {
return false
}
return true
}

@ -51,21 +51,16 @@ type SingleRuntime struct {
// give the path to the master socket path, and non 0 number for workers. Process is for
// nbproc > 1. In master-worker mode it's the same as the worker number, but when having
// multiple stats socket lines bound to processes then use the correct process number
func (s *SingleRuntime) Init(socketPath string, worker int, process int) error {
s.socketPath = socketPath
s.jobs = make(chan Task)
s.worker = worker
s.process = process
go s.handleIncomingJobs(context.Background())
return nil
}
func (s *SingleRuntime) InitWithContext(ctx context.Context, socketPath string, worker int, process int) error {
func (s *SingleRuntime) Init(ctx context.Context, socketPath string, worker int, process int) error {
s.socketPath = socketPath
s.jobs = make(chan Task)
s.worker = worker
s.process = process
go s.handleIncomingJobs(ctx)
// check if we have a valid scket
if _, err := s.ExecuteRaw("help"); err != nil {
return err
}
return nil
}

Loading…
Cancel
Save