|
15 | 15 | package docker
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "fmt" |
18 | 19 | "io/ioutil"
|
19 | 20 | "strings"
|
20 | 21 | "testing"
|
21 | 22 |
|
22 | 23 | mclog "github.com/chryscloud/go-microkit-plugins/log"
|
| 24 | + "github.com/docker/docker/api/types" |
23 | 25 | )
|
24 | 26 |
|
25 | 27 | var (
|
@@ -52,6 +54,70 @@ func TestContainerReplace(t *testing.T) {
|
52 | 54 | }
|
53 | 55 | }
|
54 | 56 |
|
| 57 | +func TestSystemWideInfo(t *testing.T) { |
| 58 | + cl := NewSocketClient(Log(zl), Host("unix:///var/run/docker.sock")) |
| 59 | + systemInfo, diskUsage, err := cl.SystemWideInfo() |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + fmt.Printf("Container running: %v\n", systemInfo.ContainersRunning) |
| 65 | + fmt.Printf("Container paused: %v\n", systemInfo.ContainersPaused) |
| 66 | + fmt.Printf("Container stopped: %v\n", systemInfo.ContainersStopped) |
| 67 | + fmt.Printf("Containers total: %v\n", systemInfo.Containers) |
| 68 | + |
| 69 | + imgNum := len(diskUsage.Images) |
| 70 | + totalImgSize := int64(0) |
| 71 | + activeImages := int64(0) |
| 72 | + for _, im := range diskUsage.Images { |
| 73 | + activeImages += im.Containers |
| 74 | + totalImgSize += im.SharedSize |
| 75 | + } |
| 76 | + containerTotalSize := int64(0) |
| 77 | + for _, c := range diskUsage.Containers { |
| 78 | + containerTotalSize += c.SizeRw |
| 79 | + } |
| 80 | + totalVolumeSize := int64(0) |
| 81 | + activeVolumes := int64(0) |
| 82 | + for _, v := range diskUsage.Volumes { |
| 83 | + activeVolumes += v.UsageData.RefCount |
| 84 | + totalVolumeSize += v.UsageData.Size |
| 85 | + } |
| 86 | + fmt.Printf("disk size images: %v, size: %v, active images: %v\n", imgNum, totalImgSize, activeImages) |
| 87 | + fmt.Printf("Containers total disk size: %v, volume size: %v, Active volumes: %v\n", containerTotalSize, totalVolumeSize, activeVolumes) |
| 88 | + |
| 89 | + opts := types.ContainerListOptions{ |
| 90 | + All: true, |
| 91 | + Size: true, |
| 92 | + Quiet: false, |
| 93 | + } |
| 94 | + containers, err := cl.ContainersListWithOptions(opts) |
| 95 | + if err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + } |
| 98 | + for _, c := range containers { |
| 99 | + if len(c.Names) > 0 { |
| 100 | + skip := false |
| 101 | + for _, n := range c.Names { |
| 102 | + if strings.Contains(n, "chrysedgeportal") || strings.Contains(n, "chrysedgeserver") || strings.Contains(n, "redis") { |
| 103 | + skip = true |
| 104 | + } |
| 105 | + } |
| 106 | + if skip { |
| 107 | + continue |
| 108 | + } |
| 109 | + stats, err := cl.ContainerStats(c.ID) |
| 110 | + if err != nil { |
| 111 | + t.Fatal(err) |
| 112 | + } |
| 113 | + calculated := cl.CalculateStats(stats) |
| 114 | + calculated.Status = c.Status |
| 115 | + calculated.State = c.State |
| 116 | + fmt.Printf("Stats: %v\n", calculated) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
55 | 121 | //TODO: tests need to be modified to run without actual docker config
|
56 | 122 | // func TestSocketClient(t *testing.T) {
|
57 | 123 | // cl := NewSocketClient(Log(zl), Host("unix:///var/run/docker.sock"))
|
|
0 commit comments