An implementation of exec in TestUpdateCPUQUota had a few issues,
including resource leaking and calling both ContainerExecAttach and
ContainerExecRun. The last one makes the test flaky:
update_linux_test.go:136: expected cgroup value 20000, got: Error: Exec
command f923baf709525f6b38f6511126addc5d9bb88fb477eeca1c22440551090fa2bb
is already running
Fix by using the integration/internal/exec package.
While at it, use require/assert to further improve code readability.
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
| ... | ... |
@@ -88,55 +88,17 @@ func TestUpdateCPUQUota(t *testing.T) {
|
| 88 | 88 |
} |
| 89 | 89 |
|
| 90 | 90 |
inspect, err := client.ContainerInspect(ctx, cID) |
| 91 |
- if err != nil {
|
|
| 92 |
- t.Fatal(err) |
|
| 93 |
- } |
|
| 91 |
+ require.NoError(t, err) |
|
| 92 |
+ assert.Equal(t, test.update, inspect.HostConfig.CPUQuota) |
|
| 94 | 93 |
|
| 95 |
- if inspect.HostConfig.CPUQuota != test.update {
|
|
| 96 |
- t.Fatalf("quota not updated in the API, expected %d, got: %d", test.update, inspect.HostConfig.CPUQuota)
|
|
| 97 |
- } |
|
| 94 |
+ res, err := container.Exec(ctx, client, cID, |
|
| 95 |
+ []string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"})
|
|
| 96 |
+ require.NoError(t, err) |
|
| 97 |
+ require.Empty(t, res.Stderr()) |
|
| 98 |
+ require.Equal(t, 0, res.ExitCode) |
|
| 98 | 99 |
|
| 99 |
- execCreate, err := client.ContainerExecCreate(ctx, cID, types.ExecConfig{
|
|
| 100 |
- Cmd: []string{"/bin/cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"},
|
|
| 101 |
- AttachStdout: true, |
|
| 102 |
- AttachStderr: true, |
|
| 103 |
- }) |
|
| 104 |
- if err != nil {
|
|
| 105 |
- t.Fatal(err) |
|
| 106 |
- } |
|
| 107 |
- |
|
| 108 |
- attach, err := client.ContainerExecAttach(ctx, execCreate.ID, types.ExecStartCheck{})
|
|
| 109 |
- if err != nil {
|
|
| 110 |
- t.Fatal(err) |
|
| 111 |
- } |
|
| 112 |
- |
|
| 113 |
- if err := client.ContainerExecStart(ctx, execCreate.ID, types.ExecStartCheck{}); err != nil {
|
|
| 114 |
- t.Fatal(err) |
|
| 115 |
- } |
|
| 116 |
- |
|
| 117 |
- buf := bytes.NewBuffer(nil) |
|
| 118 |
- ready := make(chan error) |
|
| 119 |
- |
|
| 120 |
- go func() {
|
|
| 121 |
- _, err := stdcopy.StdCopy(buf, buf, attach.Reader) |
|
| 122 |
- ready <- err |
|
| 123 |
- }() |
|
| 124 |
- |
|
| 125 |
- select {
|
|
| 126 |
- case <-time.After(60 * time.Second): |
|
| 127 |
- t.Fatal("timeout waiting for exec to complete")
|
|
| 128 |
- case err := <-ready: |
|
| 129 |
- if err != nil {
|
|
| 130 |
- t.Fatal(err) |
|
| 131 |
- } |
|
| 132 |
- } |
|
| 133 |
- |
|
| 134 |
- actual := strings.TrimSpace(buf.String()) |
|
| 135 |
- if actual != strconv.Itoa(int(test.update)) {
|
|
| 136 |
- t.Fatalf("expected cgroup value %d, got: %s", test.update, actual)
|
|
| 137 |
- } |
|
| 100 |
+ assert.Equal(t, strconv.FormatInt(test.update, 10), strings.TrimSpace(res.Stdout())) |
|
| 138 | 101 |
} |
| 139 |
- |
|
| 140 | 102 |
} |
| 141 | 103 |
|
| 142 | 104 |
func getContainerSysFSValue(ctx context.Context, client client.APIClient, cID string, path string) (string, error) {
|