Skip to content

Commit

Permalink
Removes utility code formerly used by Docker
Browse files Browse the repository at this point in the history
We had a lot of redudant code that was deleted with the extension
subcommand. However, code analysis failed to notice other parts that are
only referenced in their own tests. This completes removal of the extra
code made for the extension experiment.

Fixes #172 #173

Signed-off-by: Adrian Cole <adrian@tetrate.io>
  • Loading branch information
Adrian Cole committed May 11, 2021
1 parent d5b16dc commit a52aa00
Show file tree
Hide file tree
Showing 19 changed files with 32 additions and 779 deletions.
4 changes: 2 additions & 2 deletions pkg/binary/envoy/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (r *Runtime) Run(ctx context.Context, args []string) error {
// handlers, and they expect the process to still be running. For example, this allows admin API hooks.
cmd := exec.Command(r.opts.EnvoyPath, args...) // #nosec -> users can run whatever binary they like!
cmd.Dir = r.opts.WorkingDir
cmd.Stdout = r.IO.Out
cmd.Stderr = r.IO.Err
cmd.Stdout = r.Out
cmd.Stderr = r.Err
cmd.SysProcAttr = sysProcAttr()
r.cmd = cmd

Expand Down
5 changes: 3 additions & 2 deletions pkg/binary/envoy/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"

"github.com/tetratelabs/getenvoy/pkg/globals"
ioutil "github.com/tetratelabs/getenvoy/pkg/util/io"
)

// NewRuntime creates a new Runtime that runs envoy in globals.RunOpts WorkingDir
Expand All @@ -37,7 +37,8 @@ type Runtime struct {
opts *globals.RunOpts

cmd *exec.Cmd
IO ioutil.StdStreams
Out io.Writer
Err io.Writer

adminAddress, adminAddressPath string

Expand Down
14 changes: 4 additions & 10 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/spf13/cobra"

"github.com/tetratelabs/getenvoy/pkg/globals"
"github.com/tetratelabs/getenvoy/pkg/util/exec"
"github.com/tetratelabs/getenvoy/pkg/version"
)

Expand Down Expand Up @@ -106,15 +105,10 @@ func handleFlagOverrides(o *globals.GlobalOpts, homeDirFlag, manifestURLFlag str
func Execute(cmd *cobra.Command) error {
actualCmd, err := cmd.ExecuteC()
if actualCmd != nil && err != nil { // both are always true on error
var serr exec.ShutdownError
if errors.As(err, &serr) { // in case of ShutdownError, we want to avoid any wrapper messages
cmd.PrintErrln("NOTE:", serr.Error())
} else {
cmd.PrintErrln("Error:", err.Error())
// actualCmd ensures command path includes the subcommand (ex "extension run")
cmd.PrintErrf("\nRun '%v --help' for usage.\n", actualCmd.CommandPath())
return err
}
cmd.PrintErrln("Error:", err.Error())
// actualCmd ensures command path includes the subcommand (ex "extension run")
cmd.PrintErrf("\nRun '%v --help' for usage.\n", actualCmd.CommandPath())
return err
}
return nil
}
19 changes: 15 additions & 4 deletions pkg/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
package cmd_test

import (
"bytes"
"fmt"
"os"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

rootcmd "github.com/tetratelabs/getenvoy/pkg/cmd"
"github.com/tetratelabs/getenvoy/pkg/globals"
cmdtest "github.com/tetratelabs/getenvoy/pkg/test/cmd"
)

func TestGetEnvoyValidateArgs(t *testing.T) {
Expand Down Expand Up @@ -55,7 +56,7 @@ func TestGetEnvoyValidateArgs(t *testing.T) {
test := test // pin! see https://github.com/kyoh86/scopelint for why

t.Run(test.name, func(t *testing.T) {
c, stdout, stderr := cmdtest.NewRootCommand(o)
c, stdout, stderr := newRootCommand(o)
c.SetArgs(test.args)
err := rootcmd.Execute(c)

Expand Down Expand Up @@ -116,7 +117,7 @@ func TestGetEnvoyHomeDir(t *testing.T) {
}

o := &globals.GlobalOpts{}
c, stdout, stderr := cmdtest.NewRootCommand(o)
c, stdout, stderr := newRootCommand(o)
c.SetArgs(test.args)
err := rootcmd.Execute(c)

Expand Down Expand Up @@ -175,7 +176,7 @@ func TestGetEnvoyManifest(t *testing.T) {
}

o := &globals.GlobalOpts{}
c, stdout, stderr := cmdtest.NewRootCommand(o)
c, stdout, stderr := newRootCommand(o)
c.SetArgs(append(test.args, "help"))
err := rootcmd.Execute(c)

Expand All @@ -198,3 +199,13 @@ func requireSetenv(t *testing.T, key, value string) func() {
require.NoError(t, e, `error reverting env variable %s=%s`, key, previous)
}
}

// newRootCommand initializes a command with buffers for stdout and stderr.
func newRootCommand(o *globals.GlobalOpts) (c *cobra.Command, stdout, stderr *bytes.Buffer) {
stdout = new(bytes.Buffer)
stderr = new(bytes.Buffer)
c = rootcmd.NewRoot(o)
c.SetOut(stdout)
c.SetErr(stderr)
return c, stdout, stderr
}
8 changes: 2 additions & 6 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/tetratelabs/getenvoy/pkg/binary/envoy"
"github.com/tetratelabs/getenvoy/pkg/binary/envoy/debug"
"github.com/tetratelabs/getenvoy/pkg/globals"
ioutil "github.com/tetratelabs/getenvoy/pkg/util/io"
)

// NewRunCmd create a command responsible for starting an Envoy process
Expand Down Expand Up @@ -95,11 +94,8 @@ func InitializeRunOpts(o *globals.GlobalOpts, reference string) error {
// This is exposed for re-use in "getenvoy extension run"
func Run(o *globals.GlobalOpts, cmd *cobra.Command, args []string) error {
r := envoy.NewRuntime(&o.RunOpts)
r.IO = ioutil.StdStreams{
In: cmd.InOrStdin(),
Out: cmd.OutOrStdout(),
Err: cmd.ErrOrStderr(),
}
r.Out = cmd.OutOrStdout()
r.Err = cmd.ErrOrStderr()
debug.EnableAll(r)
return r.Run(cmd.Context(), args)
}
7 changes: 3 additions & 4 deletions pkg/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
rootcmd "github.com/tetratelabs/getenvoy/pkg/cmd"
"github.com/tetratelabs/getenvoy/pkg/globals"
"github.com/tetratelabs/getenvoy/pkg/manifest"
"github.com/tetratelabs/getenvoy/pkg/test/cmd"
manifesttest "github.com/tetratelabs/getenvoy/pkg/test/manifest"
"github.com/tetratelabs/getenvoy/pkg/test/morerequire"
)
Expand Down Expand Up @@ -54,7 +53,7 @@ func TestGetEnvoyRunValidateFlag(t *testing.T) {

t.Run(test.name, func(t *testing.T) {
// Run "getenvoy run"
c, stdout, stderr := cmd.NewRootCommand(&globals.GlobalOpts{})
c, stdout, stderr := newRootCommand(&globals.GlobalOpts{})
c.SetArgs(test.args)
err := rootcmd.Execute(c)

Expand All @@ -72,7 +71,7 @@ func TestGetEnvoyRun(t *testing.T) {
defer cleanup()

// Run "getenvoy run standard:1.17.1 -- -c envoy.yaml"
c, stdout, stderr := cmd.NewRootCommand(&o.GlobalOpts)
c, stdout, stderr := newRootCommand(&o.GlobalOpts)
c.SetArgs([]string{"run", reference.Latest, "--", "-c", "envoy.yaml"})
err := rootcmd.Execute(c)

Expand All @@ -92,7 +91,7 @@ func TestGetEnvoyRunFailWithUnknownVersion(t *testing.T) {
defer cleanup()

o.EnvoyPath = "" // force lookup of version flag
c, _, stderr := cmd.NewRootCommand(&o.GlobalOpts)
c, _, stderr := newRootCommand(&o.GlobalOpts)

// Run "getenvoy run unknown"
version := "unknown"
Expand Down
34 changes: 0 additions & 34 deletions pkg/test/cmd/command.go

This file was deleted.

7 changes: 0 additions & 7 deletions pkg/test/morerequire/morerequire.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,3 @@ func RequireCaptureScript(t *testing.T, name string) (string, func()) {
}
return path, cleanup
}

// RequireAbs runs filepath.Abs and ensures there are no errors.
func RequireAbs(t *testing.T, f string) string {
f, err := filepath.Abs(f)
require.NoError(t, err, `error determining absolute path: %v`, f)
return f
}
167 changes: 0 additions & 167 deletions pkg/util/exec/exec.go

This file was deleted.

Loading

0 comments on commit a52aa00

Please sign in to comment.