Fresh dep ensure

This commit is contained in:
Mike Cronce
2018-11-26 13:23:56 -05:00
parent 93cb8a04d7
commit 407478ab9a
9016 changed files with 551394 additions and 279685 deletions

37
vendor/k8s.io/utils/exec/exec.go generated vendored
View File

@ -60,6 +60,17 @@ type Cmd interface {
SetStdin(in io.Reader)
SetStdout(out io.Writer)
SetStderr(out io.Writer)
SetEnv(env []string)
// StdoutPipe and StderrPipe for getting the process' Stdout and Stderr as
// Readers
StdoutPipe() (io.ReadCloser, error)
StderrPipe() (io.ReadCloser, error)
// Start and Wait are for running a process non-blocking
Start() error
Wait() error
// Stops the command by sending SIGTERM. It is not guaranteed the
// process will stop before this function returns. If the process is not
// responding, an internal timer function will send a SIGKILL to force
@ -121,6 +132,30 @@ func (cmd *cmdWrapper) SetStderr(out io.Writer) {
cmd.Stderr = out
}
func (cmd *cmdWrapper) SetEnv(env []string) {
cmd.Env = env
}
func (cmd *cmdWrapper) StdoutPipe() (io.ReadCloser, error) {
r, err := (*osexec.Cmd)(cmd).StdoutPipe()
return r, handleError(err)
}
func (cmd *cmdWrapper) StderrPipe() (io.ReadCloser, error) {
r, err := (*osexec.Cmd)(cmd).StderrPipe()
return r, handleError(err)
}
func (cmd *cmdWrapper) Start() error {
err := (*osexec.Cmd)(cmd).Start()
return handleError(err)
}
func (cmd *cmdWrapper) Wait() error {
err := (*osexec.Cmd)(cmd).Wait()
return handleError(err)
}
// Run is part of the Cmd interface.
func (cmd *cmdWrapper) Run() error {
err := (*osexec.Cmd)(cmd).Run()
@ -206,10 +241,12 @@ func (e CodeExitError) String() string {
return e.Err.Error()
}
// Exited is to check if the process has finished
func (e CodeExitError) Exited() bool {
return true
}
// ExitStatus is for checking the error code
func (e CodeExitError) ExitStatus() int {
return e.Code
}

View File

@ -18,6 +18,8 @@ package exec
import (
"context"
"io"
"io/ioutil"
osexec "os/exec"
"testing"
"time"
@ -139,3 +141,75 @@ func TestTimeout(t *testing.T) {
t.Errorf("expected %v but got %v", context.DeadlineExceeded, err)
}
}
func TestSetEnv(t *testing.T) {
ex := New()
out, err := ex.Command("/bin/sh", "-c", "echo $FOOBAR").CombinedOutput()
if err != nil {
t.Errorf("expected success, got %+v", err)
}
if string(out) != "\n" {
t.Errorf("unexpected output: %q", string(out))
}
cmd := ex.Command("/bin/sh", "-c", "echo $FOOBAR")
cmd.SetEnv([]string{"FOOBAR=baz"})
out, err = cmd.CombinedOutput()
if err != nil {
t.Errorf("expected success, got %+v", err)
}
if string(out) != "baz\n" {
t.Errorf("unexpected output: %q", string(out))
}
}
func TestStdIOPipes(t *testing.T) {
cmd := New().Command("/bin/sh", "-c", "echo 'OUT'>&1; echo 'ERR'>&2")
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
t.Fatalf("expected StdoutPipe() not to error, got: %v", err)
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
t.Fatalf("expected StderrPipe() not to error, got: %v", err)
}
stdout := make(chan string)
stderr := make(chan string)
go func() {
stdout <- readAll(t, stdoutPipe, "StdOut")
}()
go func() {
stderr <- readAll(t, stderrPipe, "StdErr")
}()
if err := cmd.Start(); err != nil {
t.Errorf("expected Start() not to error, got: %v", err)
}
if e, a := "OUT\n", <-stdout; e != a {
t.Errorf("expected StdOut to be '%s', got: '%v'", e, a)
}
if e, a := "ERR\n", <-stderr; e != a {
t.Errorf("expected StdErr to be '%s', got: '%v'", e, a)
}
if err := cmd.Wait(); err != nil {
t.Errorf("expected Wait() not to error, got: %v", err)
}
}
func readAll(t *testing.T, r io.Reader, n string) string {
t.Helper()
b, err := ioutil.ReadAll(r)
if err != nil {
t.Fatalf("unexpected error when reading from %s: %v", n, err)
}
return string(b)
}

55
vendor/k8s.io/utils/exec/stdiopipe_test.go generated vendored Normal file
View File

@ -0,0 +1,55 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package exec_test
import (
"fmt"
"io/ioutil"
"k8s.io/utils/exec"
)
func ExampleNew_stderrPipe() {
cmd := exec.New().Command("/bin/sh", "-c", "echo 'We can read from stderr via pipe!' >&2")
stderrPipe, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
stderr := make(chan []byte)
go func() {
b, err := ioutil.ReadAll(stderrPipe)
if err != nil {
panic(err)
}
stderr <- b
}()
if err := cmd.Start(); err != nil {
panic(err)
}
received := <-stderr
if err := cmd.Wait(); err != nil {
panic(err)
}
fmt.Println(string(received))
// Output: We can read from stderr via pipe!
}

View File

@ -24,7 +24,7 @@ import (
"k8s.io/utils/exec"
)
// A simple scripted Interface type.
// FakeExec is a simple scripted Interface type.
type FakeExec struct {
CommandScript []FakeCommandAction
CommandCalls int
@ -33,8 +33,10 @@ type FakeExec struct {
var _ exec.Interface = &FakeExec{}
// FakeCommandAction is the function to be executed
type FakeCommandAction func(cmd string, args ...string) exec.Cmd
// Command is to track the commands that are executed
func (fake *FakeExec) Command(cmd string, args ...string) exec.Cmd {
if fake.CommandCalls > len(fake.CommandScript)-1 {
panic(fmt.Sprintf("ran out of Command() actions. Could not handle command [%d]: %s args: %v", fake.CommandCalls, cmd, args))
@ -44,15 +46,17 @@ func (fake *FakeExec) Command(cmd string, args ...string) exec.Cmd {
return fake.CommandScript[i](cmd, args...)
}
// CommandContext wraps arguments into exec.Cmd
func (fake *FakeExec) CommandContext(ctx context.Context, cmd string, args ...string) exec.Cmd {
return fake.Command(cmd, args...)
}
// LookPath is for finding the path of a file
func (fake *FakeExec) LookPath(file string) (string, error) {
return fake.LookPathFunc(file)
}
// A simple scripted Cmd type.
// FakeCmd is a simple scripted Cmd type.
type FakeCmd struct {
Argv []string
CombinedOutputScript []FakeCombinedOutputAction
@ -65,34 +69,84 @@ type FakeCmd struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Env []string
StdoutPipeResponse FakeStdIOPipeResponse
StderrPipeResponse FakeStdIOPipeResponse
WaitResponse error
StartResponse error
}
var _ exec.Cmd = &FakeCmd{}
// InitFakeCmd is for creating a fake exec.Cmd
func InitFakeCmd(fake *FakeCmd, cmd string, args ...string) exec.Cmd {
fake.Argv = append([]string{cmd}, args...)
return fake
}
// FakeStdIOPipeResponse holds responses to use as fakes for the StdoutPipe and
// StderrPipe method calls
type FakeStdIOPipeResponse struct {
ReadCloser io.ReadCloser
Error error
}
// FakeCombinedOutputAction is a function type
type FakeCombinedOutputAction func() ([]byte, error)
// FakeRunAction is a function type
type FakeRunAction func() ([]byte, []byte, error)
// SetDir sets the directory
func (fake *FakeCmd) SetDir(dir string) {
fake.Dirs = append(fake.Dirs, dir)
}
// SetStdin sets the stdin
func (fake *FakeCmd) SetStdin(in io.Reader) {
fake.Stdin = in
}
// SetStdout sets the stdout
func (fake *FakeCmd) SetStdout(out io.Writer) {
fake.Stdout = out
}
// SetStderr sets the stderr
func (fake *FakeCmd) SetStderr(out io.Writer) {
fake.Stderr = out
}
// SetEnv sets the environment variables
func (fake *FakeCmd) SetEnv(env []string) {
fake.Env = env
}
// StdoutPipe returns an injected ReadCloser & error (via StdoutPipeResponse)
// to be able to inject an output stream on Stdout
func (fake *FakeCmd) StdoutPipe() (io.ReadCloser, error) {
return fake.StdoutPipeResponse.ReadCloser, fake.StdoutPipeResponse.Error
}
// StderrPipe returns an injected ReadCloser & error (via StderrPipeResponse)
// to be able to inject an output stream on Stderr
func (fake *FakeCmd) StderrPipe() (io.ReadCloser, error) {
return fake.StderrPipeResponse.ReadCloser, fake.StderrPipeResponse.Error
}
// Start mimicks starting the process (in the background) and returns the
// injected StartResponse
func (fake *FakeCmd) Start() error {
return fake.StartResponse
}
// Wait mimicks waiting for the process to exit returns the
// injected WaitResponse
func (fake *FakeCmd) Wait() error {
return fake.WaitResponse
}
// Run sets runs the command
func (fake *FakeCmd) Run() error {
if fake.RunCalls > len(fake.RunScript)-1 {
panic("ran out of Run() actions")
@ -113,6 +167,7 @@ func (fake *FakeCmd) Run() error {
return err
}
// CombinedOutput returns the output from the command
func (fake *FakeCmd) CombinedOutput() ([]byte, error) {
if fake.CombinedOutputCalls > len(fake.CombinedOutputScript)-1 {
panic("ran out of CombinedOutput() actions")
@ -126,15 +181,17 @@ func (fake *FakeCmd) CombinedOutput() ([]byte, error) {
return fake.CombinedOutputScript[i]()
}
// Output is the response from the command
func (fake *FakeCmd) Output() ([]byte, error) {
return nil, fmt.Errorf("unimplemented")
}
// Stop is to stop the process
func (fake *FakeCmd) Stop() {
// no-op
}
// A simple fake ExitError type.
// FakeExitError is a simple fake ExitError type.
type FakeExitError struct {
Status int
}
@ -149,10 +206,12 @@ func (fake FakeExitError) Error() string {
return fake.String()
}
// Exited always returns true
func (fake FakeExitError) Exited() bool {
return true
}
// ExitStatus returns the fake status
func (fake FakeExitError) ExitStatus() int {
return fake.Status
}