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

Commit

Permalink
Merge pull request #1 from bergwolf/shim
Browse files Browse the repository at this point in the history
initial shim implementation
  • Loading branch information
laijs authored Dec 5, 2017
2 parents a0fdf3c + 4e52208 commit 58ac96f
Show file tree
Hide file tree
Showing 895 changed files with 318,005 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kata-shim
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Copyright 2017 HyperHQ Inc.
#
# SPDX-License-Identifier: Apache-2.0
#

sudo: required
dist: trusty

language: go
go_import_path: github.com/kata-containers/shim

go:
- 1.8

before_install:
- sudo apt-get update -qq
- sudo apt-get install -y -qq automake

install:
- cd ${TRAVIS_BUILD_DIR} && make

script:
- cd ${TRAVIS_BUILD_DIR} && make test
110 changes: 110 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"

[prune]
non-go = true
go-tests = true

[[constraint]]
revision = "505229f166923b2ec15b7ef52edfc9c6b9f40676"
name = "github.com/kata-containers/agent"

[[constraint]]
name = "github.com/moby/moby"
version = "1.13.1"

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.0.3"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.1.4"

[[constraint]]
revision = "d379faa25cbdc04d653984913a2ceb43b0bc46d7"
name = "golang.org/x/net"

[[constraint]]
name = "google.golang.org/grpc"
version = "1.8.0"
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all:
go build -o kata-shim

test: all
go test -v -race

clean:
rm -f kata-shim
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Shim

This project implements the shim for kata project.

The goal for this component is to handle stdio and signals of the
container process.
21 changes: 21 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//

package main

import "github.com/kata-containers/agent/protocols/client"

type shimAgent struct {
*client.AgentClient
}

func newShimAgent(addr string) (*shimAgent, error) {
client, err := client.NewAgentClient(addr)
if err != nil {
return nil, err
}

return &shimAgent{AgentClient: client}, nil
}
160 changes: 160 additions & 0 deletions agent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright 2017 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//

package main

import (
"context"
"fmt"
"net"
"os"
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/grpc"

pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/kata-containers/agent/protocols/mockserver"
)

const mockSockAddr = "/tmp/agentserver.sock"
const unixMockAddr = "unix:///" + mockSockAddr
const badMockAddr = "vsock://" + mockSockAddr

type testAgent struct {
t *testing.T

server *grpc.Server
waitCh chan error

ctx context.Context
client *shimAgent
}

func startMockServer(t *testing.T) (*grpc.Server, chan error) {
os.Remove(mockSockAddr)

l, err := net.Listen("unix", mockSockAddr)
assert.Nil(t, err, "Listen on %s failed: %s", mockSockAddr, err)

mock := mockserver.NewMockServer()

stopWait := make(chan error, 1)
go func() {
mock.Serve(l)
stopWait <- nil
}()

return mock, stopWait
}

func testSetup(t *testing.T) *testAgent {
mock, waitCh := startMockServer(t)

agent, err := newShimAgent(mockSockAddr)
if !assert.Nil(t, err, "Failed to create new agent client: %s", err) {
t.FailNow()
}

ctx := context.Background()
_, err = agent.CreateSandbox(ctx, &pb.CreateSandboxRequest{
Hostname: "testSandbox",
Dns: []string{},
Storages: []*pb.Storage{},
SandboxPidns: true,
})
if !assert.Nil(t, err, "Failed to create sandbox: %s", err) {
agent.Close()
t.FailNow()
}

return &testAgent{
t: t,
server: mock,
waitCh: waitCh,
ctx: ctx,
client: agent,
}
}

func testTearDown(t *testAgent) {
t.client.Close()
t.server.Stop()
<-t.waitCh
}

var defaultSpec = &pb.Spec{
Process: &pb.Process{},
Root: &pb.Root{Path: "rootpath", Readonly: true},
Hostname: "testGuest",
}

func newTestSpec() *pb.Spec {
return &pb.Spec{
Version: "testGrpcVersion",
Process: &pb.Process{
Terminal: true,
ConsoleSize: &pb.Box{10, 10},
User: pb.User{UID: 0, GID: 0, Username: "root:root"},
Capabilities: &pb.LinuxCapabilities{},
Rlimits: []pb.POSIXRlimit{},
},
Root: &pb.Root{Path: "rootpath", Readonly: true},
Hostname: "testGuest",
}
}

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

resp, err := t.client.StartContainer(t.ctx, &pb.StartContainerRequest{ContainerId: containerId})
if err != nil {
return 0, fmt.Errorf("failed to create new container: %s", err)
}

return resp.PID, nil
}

func TestNewShimAgent(t *testing.T) {
mock, waitCh := startMockServer(t)

cliFunc := func(sock string, success bool) {
agent, err := newShimAgent(sock)
if success {
assert.Nil(t, err, "Failed to create new agent client: %s", err)
} else if !success {
assert.NotNil(t, err, "Unexpected success with sock address: %s", sock)
}
if err == nil {
agent.Close()
}
}

cliFunc(mockSockAddr, true)
cliFunc(unixMockAddr, true)
cliFunc(badMockAddr, false)

// wait mock server to stop
mock.Stop()
<-waitCh
}

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

id := "foobar"
_, err := agent.addContainer(id)
assert.Nil(t, err, "%s", err)
_, err = agent.addContainer(id)
assert.NotNil(t, err, "unexpected success when adding duplicated container")
}
Loading

0 comments on commit 58ac96f

Please sign in to comment.