Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: encapsulate codes into function FormatStatus #542

Merged
merged 2 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,33 +233,27 @@ func (s *Server) getContainers(ctx context.Context, rw http.ResponseWriter, req
containerList := make([]types.Container, 0, len(metas))

for _, m := range metas {
t, _ := time.Parse(utils.TimeLayout, m.Created)
status, err := m.FormatStatus()
if err != nil {
return err
}

t, err := time.Parse(utils.TimeLayout, m.Created)
if err != nil {
return err
}

container := types.Container{
ID: m.ID,
Names: []string{m.Name},
Image: m.Config.Image,
Command: strings.Join(m.Config.Cmd, " "),
Status: status,
Created: t.UnixNano(),
Labels: m.Config.Labels,
HostConfig: m.HostConfig,
}

// TODO encapsulate this into a single function
if m.State.Status == types.StatusRunning || m.State.Status == types.StatusPaused {
start, _ := time.Parse(utils.TimeLayout, m.State.StartedAt)
startAt, err := utils.FormatTimeInterval(start.UnixNano())
if err != nil {
return err
}

container.Status = "Up " + startAt
if m.State.Status == types.StatusPaused {
container.Status += "(paused)"
}
} else {
container.Status = string(m.State.Status)
}

containerList = append(containerList, container)
}
return EncodeResponse(rw, http.StatusOK, containerList)
Expand Down
29 changes: 29 additions & 0 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package mgr
import (
"net/http"
"sync"
"time"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/daemon/meta"
"github.com/alibaba/pouch/pkg/utils"

"github.com/opencontainers/image-spec/specs-go/v1"
)
Expand Down Expand Up @@ -154,6 +156,33 @@ func (meta *ContainerMeta) merge(getconfig func() (v1.ImageConfig, error)) error
return nil
}

// FormatStatus format container status
func (meta *ContainerMeta) FormatStatus() (string, error) {
var status string

// return status if container is not running
if meta.State.Status != types.StatusRunning && meta.State.Status != types.StatusPaused {
return string(meta.State.Status), nil
}

// format container status if container is running
start, err := time.Parse(utils.TimeLayout, meta.State.StartedAt)
if err != nil {
return "", err
}

startAt, err := utils.FormatTimeInterval(start.UnixNano())
if err != nil {
return "", err
}

status = "Up " + startAt
if meta.State.Status == types.StatusPaused {
status += "(paused)"
}
return status, nil
}

// Container represents the container instance in runtime.
type Container struct {
sync.Mutex
Expand Down
70 changes: 70 additions & 0 deletions daemon/mgr/container_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package mgr

import (
"testing"
"time"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/utils"

"github.com/stretchr/testify/assert"
)

type tCase struct {
name string
input *ContainerMeta
expected string
err error
}

func TestContainerMeta_FormatStatus(t *testing.T) {
// TODO: add more cases
for _, tc := range []tCase{
{
name: "Created",
input: &ContainerMeta{
State: &types.ContainerState{
Status: types.StatusCreated,
},
},
expected: string(types.StatusCreated),
err: nil,
},
{
name: "Stopped",
input: &ContainerMeta{
State: &types.ContainerState{
Status: types.StatusStopped,
},
},
expected: string(types.StatusStopped),
err: nil,
},
{
name: "Running",
input: &ContainerMeta{
State: &types.ContainerState{
Status: types.StatusRunning,
StartedAt: time.Now().Add(0 - utils.Minute).UTC().Format(utils.TimeLayout),
},
},
expected: "Up 1 minute",
err: nil,
},
{
name: "Paused",
input: &ContainerMeta{
State: &types.ContainerState{
Status: types.StatusPaused,
StartedAt: time.Now().Add(0 - utils.Minute*2).UTC().Format(utils.TimeLayout),
},
},
expected: "Up 2 minutes(paused)",
err: nil,
},
} {
output, err := tc.input.FormatStatus()
assert.Equal(t, output, tc.expected)
assert.Equal(t, err, tc.err)
}
}