github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/integration/container/update_linux_test.go (about) 1 package container 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "strconv" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/docker/docker/api/types" 13 "github.com/docker/docker/api/types/container" 14 "github.com/docker/docker/integration/util/request" 15 "github.com/docker/docker/pkg/stdcopy" 16 ) 17 18 func TestUpdateCPUQUota(t *testing.T) { 19 t.Parallel() 20 21 client := request.NewAPIClient(t) 22 ctx := context.Background() 23 24 c, err := client.ContainerCreate(ctx, &container.Config{ 25 Image: "busybox", 26 Cmd: []string{"top"}, 27 }, nil, nil, "") 28 if err != nil { 29 t.Fatal(err) 30 } 31 defer func() { 32 if err := client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true}); err != nil { 33 panic(fmt.Sprintf("failed to clean up after test: %v", err)) 34 } 35 }() 36 37 if err := client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{}); err != nil { 38 t.Fatal(err) 39 } 40 41 for _, test := range []struct { 42 desc string 43 update int64 44 }{ 45 {desc: "some random value", update: 15000}, 46 {desc: "a higher value", update: 20000}, 47 {desc: "a lower value", update: 10000}, 48 {desc: "unset value", update: -1}, 49 } { 50 if _, err := client.ContainerUpdate(ctx, c.ID, container.UpdateConfig{ 51 Resources: container.Resources{ 52 CPUQuota: test.update, 53 }, 54 }); err != nil { 55 t.Fatal(err) 56 } 57 58 inspect, err := client.ContainerInspect(ctx, c.ID) 59 if err != nil { 60 t.Fatal(err) 61 } 62 63 if inspect.HostConfig.CPUQuota != test.update { 64 t.Fatalf("quota not updated in the API, expected %d, got: %d", test.update, inspect.HostConfig.CPUQuota) 65 } 66 67 execCreate, err := client.ContainerExecCreate(ctx, c.ID, types.ExecConfig{ 68 Cmd: []string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"}, 69 AttachStdout: true, 70 AttachStderr: true, 71 }) 72 if err != nil { 73 t.Fatal(err) 74 } 75 76 attach, err := client.ContainerExecAttach(ctx, execCreate.ID, types.ExecStartCheck{}) 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 if err := client.ContainerExecStart(ctx, execCreate.ID, types.ExecStartCheck{}); err != nil { 82 t.Fatal(err) 83 } 84 85 buf := bytes.NewBuffer(nil) 86 ready := make(chan error) 87 88 go func() { 89 _, err := stdcopy.StdCopy(buf, buf, attach.Reader) 90 ready <- err 91 }() 92 93 select { 94 case <-time.After(60 * time.Second): 95 t.Fatal("timeout waiting for exec to complete") 96 case err := <-ready: 97 if err != nil { 98 t.Fatal(err) 99 } 100 } 101 102 actual := strings.TrimSpace(buf.String()) 103 if actual != strconv.Itoa(int(test.update)) { 104 t.Fatalf("expected cgroup value %d, got: %s", test.update, actual) 105 } 106 } 107 108 }