Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Commit

Permalink
Implement functional options
Browse files Browse the repository at this point in the history
Signed-off-by: Liam White <liam@tetrate.io>
  • Loading branch information
liamawhite committed Aug 2, 2019
1 parent f0e4de2 commit aff8c81
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 57 deletions.
58 changes: 29 additions & 29 deletions pkg/binary/getenvoy/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,12 @@ func TestRuntime_RunPath(t *testing.T) {
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
r, _ := New()
// This ensures functions are called in the correct order
r, preStartCalled, preTerminationCalled := newRuntimeWithMockFunctions(t)
tmpDir, _ := ioutil.TempDir("", "getenvoy-test-")
defer os.RemoveAll(tmpDir)
r.local = tmpDir

// These will t.Error if not called in correct order
preStartCalled := registerPreStart(t, r)
preTerminationCalled := registerPreTermination(t, r)

wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
Expand All @@ -88,29 +85,32 @@ func waitForProcessStart(r *Runtime) {
}
}

func registerPreStart(t *testing.T, r *Runtime) *bool {
called := false
r.registerPreStart(func() error {
if r.cmd != nil && r.cmd.Process != nil {
t.Error("preStart was called after process has started")
}
called = true
return nil
})
return &called
}
// This ensures functions are called in the correct order
func newRuntimeWithMockFunctions(t *testing.T) (*Runtime, *bool, *bool) {
preStartCalled := false
preStart := func(r *Runtime) {
r.registerPreStart(func() error {
if r.cmd != nil && r.cmd.Process != nil {
t.Error("preStart was called after process has started")
}
preStartCalled = true
return nil
})
}

func registerPreTermination(t *testing.T, r *Runtime) *bool {
called := false
r.registerPreTermination(func() error {
if r.cmd != nil && r.cmd.Process == nil {
t.Error("preTermination was called before process was started")
}
if r.cmd != nil && r.cmd.ProcessState != nil {
t.Error("preTermination was called after process was terminated")
}
called = true
return nil
})
return &called
preTerminationCalled := false
preTermination := func(r *Runtime) {
r.registerPreTermination(func() error {
if r.cmd != nil && r.cmd.Process == nil {
t.Error("preTermination was called before process was started")
}
if r.cmd != nil && r.cmd.ProcessState != nil {
t.Error("preTermination was called after process was terminated")
}
preTerminationCalled = true
return nil
})
}
runtime, _ := New(preStart, preTermination)
return runtime, &preStartCalled, &preTerminationCalled
}
10 changes: 7 additions & 3 deletions pkg/binary/getenvoy/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ import (
)

// New creates a new GetEnvoy binary.Runtime with the local file storage set to the home directory
func New() (*Runtime, error) {
func New(options ...func(*Runtime)) (*Runtime, error) {
usrDir, err := homedir.Dir()
local := filepath.Join(usrDir, ".getenvoy")
return &Runtime{
runtime := &Runtime{
local: local,
wg: &sync.WaitGroup{},
signals: make(chan os.Signal),
preStart: make([]preStartFunc, 0),
preTermination: make([]preTerminationFunc, 0),
}, err
}
for _, option := range options {
option(runtime)
}
return runtime, err
}

// Runtime implements the GetEnvoy binary.Runtime
Expand Down
43 changes: 20 additions & 23 deletions pkg/binary/getenvoy/termination.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,25 @@ var adminAPIPaths = map[string]string{
"runtime": "runtime.json",
}

// EnvoyAdminDataCollection registers collection of Envoy Admin API information
// TODO: Test this (Liam)
func (r *Runtime) EnvoyAdminDataCollection(enable bool) {
if enable {
r.registerPreTermination(func() error {
var multiErr *multierror.Error
for path, file := range adminAPIPaths {
resp, err := http.Get(fmt.Sprintf("http://0.0.0.0:15001/%v", path))
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
f, err := os.OpenFile(filepath.Join(r.debugDir, file), os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
defer func() { _ = f.Close() }()
defer func() { _ = resp.Body.Close() }()
if _, err := io.Copy(f, resp.Body); err != nil {
multiErr = multierror.Append(multiErr, err)
}
// EnableEnvoyAdminDataCollection registers collection of Envoy Admin API information
var EnableEnvoyAdminDataCollection = func(r *Runtime) {
r.registerPreTermination(func() error {
var multiErr *multierror.Error
for path, file := range adminAPIPaths {
resp, err := http.Get(fmt.Sprintf("http://0.0.0.0:15001/%v", path))
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
return multiErr.ErrorOrNil()
})
}
f, err := os.OpenFile(filepath.Join(r.debugDir, file), os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
defer func() { _ = f.Close() }()
defer func() { _ = resp.Body.Close() }()
if _, err := io.Copy(f, resp.Body); err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
return multiErr.ErrorOrNil()
})
}
5 changes: 3 additions & 2 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ getenvoy run standard:1.10.1 -- --help
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
runtime, err := getenvoy.New()
runtime, err := getenvoy.New(
getenvoy.EnableEnvoyAdminDataCollection,
)
if err != nil {
return err
}
runtime.EnvoyAdminDataCollection(true)

key, manifestErr := manifest.NewKey(args[0])
if manifestErr != nil {
Expand Down

0 comments on commit aff8c81

Please sign in to comment.