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

Commit

Permalink
CI: Fix lint errors.
Browse files Browse the repository at this point in the history
Fix errors shown by `gometalinter`.

Fixes #30.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
  • Loading branch information
jodh-intel committed Jan 18, 2018
1 parent c70b88e commit 617aa3a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 64 deletions.
16 changes: 8 additions & 8 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,18 @@ func newTestSpec() *pb.Spec {
}
}

func (t *testAgent) addContainer(containerId, execId string) error {
func (t *testAgent) addContainer(containerID, execID string) error {
_, err := t.client.CreateContainer(t.ctx, &pb.CreateContainerRequest{
ContainerId: containerId,
ExecId: execId,
ContainerId: containerID,
ExecId: execID,
StringUser: &pb.StringUser{Uid: "root", Gid: "root"},
OCI: newTestSpec(),
})
if err != nil {
return fmt.Errorf("failed to create new container: %s", err)
}

_, err = t.client.StartContainer(t.ctx, &pb.StartContainerRequest{ContainerId: containerId})
_, err = t.client.StartContainer(t.ctx, &pb.StartContainerRequest{ContainerId: containerID})
if err != nil {
return fmt.Errorf("failed to create new container: %s", err)
}
Expand Down Expand Up @@ -153,10 +153,10 @@ func TestAddContainer(t *testing.T) {
agent := testSetup(t)
defer testTearDown(agent)

contId := "testContainer"
execId := "testExec"
err := agent.addContainer(contId, execId)
contID := "testContainer"
execID := "testExec"
err := agent.addContainer(contID, execID)
assert.Nil(t, err, "%s", err)
err = agent.addContainer(contId, execId)
err = agent.addContainer(contID, execID)
assert.NotNil(t, err, "unexpected success when adding duplicated container")
}
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {
logLevel string
agentAddr string
container string
execId string
execID string
proxyExitCode bool
showVersion bool
)
Expand All @@ -71,7 +71,7 @@ func main() {
flag.StringVar(&agentAddr, "agent", "", "agent gRPC socket endpoint")

flag.StringVar(&container, "container", "", "container id for the shim")
flag.StringVar(&execId, "exec-id", "", "process id for the shim")
flag.StringVar(&execID, "exec-id", "", "process id for the shim")
flag.BoolVar(&proxyExitCode, "proxy-exit-code", true, "proxy exit code of the process")

flag.Parse()
Expand All @@ -81,8 +81,8 @@ func main() {
os.Exit(0)
}

if agentAddr == "" || container == "" || execId == "" {
logger().WithField("agentAddr", agentAddr).WithField("container", container).WithField("exec-id", execId).Error("container ID, exec ID and agent socket endpoint must be set")
if agentAddr == "" || container == "" || execID == "" {
logger().WithField("agentAddr", agentAddr).WithField("container", container).WithField("exec-id", execID).Error("container ID, exec ID and agent socket endpoint must be set")
os.Exit(exitFailure)
}

Expand All @@ -92,7 +92,7 @@ func main() {
os.Exit(exitFailure)
}

shim, err := newShim(agentAddr, container, execId)
shim, err := newShim(agentAddr, container, execID)
if err != nil {
logger().WithError(err).Error("failed to create new shim")
os.Exit(exitFailure)
Expand All @@ -119,7 +119,7 @@ func main() {
// wait until exit
exitcode, err := shim.wait()
if err != nil {
logger().WithError(err).WithField("exec-id", execId).Error("failed waiting for process")
logger().WithError(err).WithField("exec-id", execID).Error("failed waiting for process")
os.Exit(exitFailure)
} else if proxyExitCode {
logger().WithField("exitcode", exitcode).Info("using shim to proxy exit code")
Expand Down
36 changes: 18 additions & 18 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
type inPipe struct {
ctx context.Context
agent *shimAgent
containerId string
execId string
containerID string
execID string
}

func (p *inPipe) Write(data []byte) (n int, err error) {
resp, err := p.agent.WriteStdin(p.ctx, &pb.WriteStreamRequest{
ContainerId: p.containerId,
ExecId: p.execId,
ContainerId: p.containerID,
ExecId: p.execID,
Data: data})
if err != nil {
return 0, err
Expand All @@ -36,18 +36,18 @@ func (p *inPipe) Write(data []byte) (n int, err error) {

func (p *inPipe) Close() error {
_, err := p.agent.CloseStdin(p.ctx, &pb.CloseStdinRequest{
ContainerId: p.containerId,
ExecId: p.execId})
ContainerId: p.containerID,
ExecId: p.execID})

return err
}

type readFn func(context.Context, *pb.ReadStreamRequest, ...grpc.CallOption) (*pb.ReadStreamResponse, error)

func pipeRead(ctx context.Context, containerId, execId string, data []byte, read readFn) (n int, err error) {
func pipeRead(ctx context.Context, containerID, execID string, data []byte, read readFn) (n int, err error) {
resp, err := read(ctx, &pb.ReadStreamRequest{
ContainerId: containerId,
ExecId: execId,
ContainerId: containerID,
ExecId: execID,
Len: uint32(len(data))})
if err == nil {
copy(data, resp.Data)
Expand All @@ -64,26 +64,26 @@ func pipeRead(ctx context.Context, containerId, execId string, data []byte, read
type outPipe struct {
ctx context.Context
agent *shimAgent
containerId string
execId string
containerID string
execID string
}

func (p *outPipe) Read(data []byte) (n int, err error) {
return pipeRead(p.ctx, p.containerId, p.execId, data, p.agent.ReadStdout)
return pipeRead(p.ctx, p.containerID, p.execID, data, p.agent.ReadStdout)
}

type errPipe struct {
ctx context.Context
agent *shimAgent
containerId string
execId string
containerID string
execID string
}

func (p *errPipe) Read(data []byte) (n int, err error) {
return pipeRead(p.ctx, p.containerId, p.execId, data, p.agent.ReadStderr)
return pipeRead(p.ctx, p.containerID, p.execID, data, p.agent.ReadStderr)
}

func shimStdioPipe(ctx context.Context, agent *shimAgent, containerId, execId string) (io.WriteCloser, io.Reader, io.Reader) {
return &inPipe{ctx: ctx, agent: agent, containerId: containerId, execId: execId},
&outPipe{ctx: ctx, agent: agent, containerId: containerId, execId: execId}, &errPipe{ctx: ctx, agent: agent, containerId: containerId, execId: execId}
func shimStdioPipe(ctx context.Context, agent *shimAgent, containerID, execID string) (io.WriteCloser, io.Reader, io.Reader) {
return &inPipe{ctx: ctx, agent: agent, containerID: containerID, execID: execID},
&outPipe{ctx: ctx, agent: agent, containerID: containerID, execID: execID}, &errPipe{ctx: ctx, agent: agent, containerID: containerID, execID: execID}
}
10 changes: 5 additions & 5 deletions pipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func TestPipe(t *testing.T) {
agent := testSetup(t)
defer testTearDown(agent)

containerId := "testContainer"
execId := "testExec"
err := agent.addContainer(containerId, execId)
containerID := "testContainer"
execID := "testExec"
err := agent.addContainer(containerID, execID)
assert.Nil(t, err, "failed to add new container: %s", err)

inPipe, outPipe, errPipe := shimStdioPipe(agent.ctx, agent.client, containerId, execId)
inPipe, outPipe, errPipe := shimStdioPipe(agent.ctx, agent.client, containerID, execID)

buf := []byte("foobar")
size, err := inPipe.Write(buf[:])
Expand All @@ -36,7 +36,7 @@ func TestPipe(t *testing.T) {
assert.Equal(t, size, 0, "unmatched write stdin pipe len %d:%d", 0, size)

// wrong process
inPipe, outPipe, errPipe = shimStdioPipe(agent.ctx, agent.client, containerId, execId+"foobar")
inPipe, outPipe, errPipe = shimStdioPipe(agent.ctx, agent.client, containerID, execID+"foobar")
_, err = inPipe.Write(buf[:])
assert.NotNil(t, err, "Unexpected success writing stdin pipe")

Expand Down
37 changes: 19 additions & 18 deletions shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,35 @@ var sigIgnored = map[syscall.Signal]bool{
}

type shim struct {
containerId string
execId string
containerID string
execID string

ctx context.Context
agent *shimAgent
}

func newShim(addr, containerId, execId string) (*shim, error) {
if agent, err := newShimAgent(addr); err != nil {
func newShim(addr, containerID, execID string) (*shim, error) {
agent, err := newShimAgent(addr)
if err != nil {
return nil, err
} else {
return &shim{containerId: containerId,
execId: execId,
ctx: context.Background(),
agent: agent}, nil
}

return &shim{containerID: containerID,
execID: execID,
ctx: context.Background(),
agent: agent}, nil
}

func (s *shim) proxyStdio(wg *sync.WaitGroup) {
// don't wait the copying of the stdin, because `io.Copy(inPipe, os.Stdin)`
// can't terminate when no input. todo: find a better way.
wg.Add(2)
inPipe, outPipe, errPipe := shimStdioPipe(s.ctx, s.agent, s.containerId, s.execId)
inPipe, outPipe, errPipe := shimStdioPipe(s.ctx, s.agent, s.containerID, s.execID)
go func() {
_, err1 := io.Copy(inPipe, os.Stdin)
_, err2 := s.agent.CloseStdin(s.ctx, &pb.CloseStdinRequest{
ContainerId: s.containerId,
ExecId: s.execId})
ContainerId: s.containerID,
ExecId: s.execID})
if err1 != nil {
shimLog.WithError(err1).Warn("copy stdin failed")
}
Expand Down Expand Up @@ -100,8 +101,8 @@ func (s *shim) forwardAllSignals() chan os.Signal {
}
// forward this signal to container
_, err := s.agent.SignalProcess(s.ctx, &pb.SignalProcessRequest{
ContainerId: s.containerId,
ExecId: s.execId,
ContainerId: s.containerID,
ExecId: s.execID,
Signal: uint32(sysSig)})
if err != nil {
shimLog.WithError(err).WithField("signal", sig.String()).Error("forward signal failed")
Expand All @@ -119,8 +120,8 @@ func (s *shim) resizeTty(fromTty *os.File) error {
}

_, err = s.agent.TtyWinResize(s.ctx, &pb.TtyWinResizeRequest{
ContainerId: s.containerId,
ExecId: s.execId,
ContainerId: s.containerID,
ExecId: s.execID,
Row: uint32(ws.Height),
Column: uint32(ws.Width)})
if err != nil {
Expand All @@ -143,8 +144,8 @@ func (s *shim) monitorTtySize(tty *os.File) {

func (s *shim) wait() (int32, error) {
resp, err := s.agent.WaitProcess(s.ctx, &pb.WaitProcessRequest{
ContainerId: s.containerId,
ExecId: s.execId})
ContainerId: s.containerID,
ExecId: s.execID})
if err != nil {
return 0, err
}
Expand Down
18 changes: 9 additions & 9 deletions shim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ func TestNewShim(t *testing.T) {
agent := testSetup(t)
defer testTearDown(agent)

contId := "foobarContainer"
execId := "testExec"
err := agent.addContainer(contId, execId)
contID := "foobarContainer"
execID := "testExec"
err := agent.addContainer(contID, execID)
assert.Nil(t, err, "%s", err)

shim, err := newShim(mockSockAddr, contId, execId)
shim, err := newShim(mockSockAddr, contID, execID)
assert.Nil(t, err, "%s", err)
defer shim.agent.Close()

_, err = newShim(badMockAddr, contId, execId)
_, err = newShim(badMockAddr, contID, execID)
assert.NotNil(t, err, "New shim with wrong socket address should fail")
}

func TestShimOps(t *testing.T) {
agent := testSetup(t)
defer testTearDown(agent)

contId := "foobarContainer"
execId := "testExec"
err := agent.addContainer(contId, execId)
contID := "foobarContainer"
execID := "testExec"
err := agent.addContainer(contID, execID)
assert.Nil(t, err, "%s", err)

shim, err := newShim(mockSockAddr, contId, execId)
shim, err := newShim(mockSockAddr, contID, execID)
assert.Nil(t, err, "%s", err)
defer shim.agent.Close()

Expand Down

0 comments on commit 617aa3a

Please sign in to comment.