2017-02-27 22:35:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bcicen/ctop/metrics"
|
|
|
|
"github.com/jgautheron/codename-generator"
|
|
|
|
"github.com/nu7hatch/gouuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockContainerSource struct {
|
|
|
|
containers Containers
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockContainerSource() *MockContainerSource {
|
2017-03-03 07:57:26 +00:00
|
|
|
cs := &MockContainerSource{}
|
2017-03-06 02:00:30 +00:00
|
|
|
go cs.Init()
|
2017-02-27 22:35:33 +00:00
|
|
|
go cs.Loop()
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Mock containers
|
|
|
|
func (cs *MockContainerSource) Init() {
|
2017-03-03 07:57:26 +00:00
|
|
|
total := 40
|
2017-02-27 22:35:33 +00:00
|
|
|
rand.Seed(int64(time.Now().Nanosecond()))
|
|
|
|
|
|
|
|
for i := 0; i < total; i++ {
|
2017-03-06 02:05:34 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2017-03-03 07:57:26 +00:00
|
|
|
collector := metrics.NewMock()
|
2017-03-05 06:46:41 +00:00
|
|
|
c := NewContainer(makeID(), collector)
|
2017-03-06 08:25:59 +00:00
|
|
|
c.SetMeta("name", makeName())
|
2017-02-27 22:35:33 +00:00
|
|
|
c.SetState(makeState())
|
2017-03-06 02:00:30 +00:00
|
|
|
cs.containers = append(cs.containers, c)
|
2017-02-27 22:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *MockContainerSource) Loop() {
|
|
|
|
iter := 0
|
|
|
|
for {
|
2017-03-03 07:57:26 +00:00
|
|
|
// Change state for random container
|
2017-03-06 02:00:30 +00:00
|
|
|
if iter%5 == 0 && len(cs.containers) > 0 {
|
2017-03-03 07:57:26 +00:00
|
|
|
randC := cs.containers[rand.Intn(len(cs.containers))]
|
|
|
|
randC.SetState(makeState())
|
2017-02-27 22:35:33 +00:00
|
|
|
}
|
|
|
|
iter++
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a single container, by ID
|
|
|
|
func (cs *MockContainerSource) Get(id string) (*Container, bool) {
|
|
|
|
for _, c := range cs.containers {
|
2017-03-03 07:57:26 +00:00
|
|
|
if c.Id == id {
|
2017-02-27 22:35:33 +00:00
|
|
|
return c, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-03-06 02:00:30 +00:00
|
|
|
// Return array of all containers, sorted by field
|
|
|
|
func (cs *MockContainerSource) All() Containers {
|
|
|
|
sort.Sort(cs.containers)
|
|
|
|
return cs.containers
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:35:33 +00:00
|
|
|
// Remove containers by ID
|
|
|
|
func (cs *MockContainerSource) delByID(id string) {
|
|
|
|
for n, c := range cs.containers {
|
2017-03-03 07:57:26 +00:00
|
|
|
if c.Id == id {
|
2017-02-27 22:35:33 +00:00
|
|
|
cs.del(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove one or more containers by index
|
|
|
|
func (cs *MockContainerSource) del(idx ...int) {
|
|
|
|
for _, i := range idx {
|
|
|
|
cs.containers = append(cs.containers[:i], cs.containers[i+1:]...)
|
|
|
|
}
|
|
|
|
log.Infof("removed %d dead containers", len(idx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeID() string {
|
|
|
|
u, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-03-03 07:57:26 +00:00
|
|
|
return strings.Replace(u.String(), "-", "", -1)[:12]
|
2017-02-27 22:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeName() string {
|
|
|
|
n, err := codename.Get(codename.Sanitized)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return strings.Replace(n, "-", "_", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeState() string {
|
|
|
|
switch rand.Intn(10) {
|
|
|
|
case 0, 1, 2:
|
|
|
|
return "exited"
|
|
|
|
case 3:
|
|
|
|
return "paused"
|
|
|
|
}
|
|
|
|
return "running"
|
|
|
|
}
|