Skip to content

Commit

Permalink
feat: use provided terminal size when executing inside container. (#546)
Browse files Browse the repository at this point in the history
Co-authored-by: JACQUES Francois <Francois.JACQUES@murex.com>
  • Loading branch information
hypnoce and JACQUES Francois authored Jun 5, 2023
1 parent 0f59ade commit 15eb100
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/provider/aci.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,22 @@ func (p *ACIProvider) RunInContainer(ctx context.Context, namespace, name, conta
return err
}

termSize := api.TermSize{
Width: 60,
Height: 120,
}
if attach.TTY() {
resize := attach.Resize()
select {
case termSize = <-resize:
break
case <-time.After(5 * time.Second):
break
}
}
// Set default terminal size
cols := int32(60)
rows := int32(120)
cols := int32(termSize.Width)
rows := int32(termSize.Height)
cmdParam := strings.Join(cmd, " ")
req := azaciv2.ContainerExecRequest{
Command: &cmdParam,
Expand Down
46 changes: 46 additions & 0 deletions pkg/provider/aci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1996,3 +1996,49 @@ func TestCreatePodWithLifecycleHooks(t *testing.T) {
err = provider.CreatePod(context.Background(), pod)
assert.Error(t, err, "ACI does not support lifecycle hooks")
}

func TestRunInContainer(t *testing.T) {
podName := "pod-" + uuid.New().String()
podNamespace := "ns-" + uuid.New().String()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cols := 42
rows := 43
aciMocks := createNewACIMock()
aciMocks.MockExecuteContainerCommand = func(ctx context.Context, resourceGroup, cgName, containerName string, containerReq azaciv2.ContainerExecRequest) (*azaciv2.ContainerExecResponse, error) {
assert.Equal(t, int32(cols), *containerReq.TerminalSize.Cols, "terminal cols size mismatch")
assert.Equal(t, int32(rows), *containerReq.TerminalSize.Rows, "terminal rows size mismatch")
return nil, fmt.Errorf("this error workarounds the websocket connection")
}

aciMocks.MockGetContainerGroupInfo = func(ctx context.Context, resourceGroup, namespace, name, nodeName string) (*azaciv2.ContainerGroup, error) {
cg := testsutil.CreateContainerGroupObj(podName, podNamespace, "Succeeded",
testsutil.CreateACIContainersListObj(runningState, "Initializing", testsutil.CgCreationTime.Add(time.Second*2), testsutil.CgCreationTime.Add(time.Second*3), true, true, true), "Succeeded")
return cg, nil
}

pod := testsutil.CreatePodObj(podName, podNamespace)
pod.Spec.Containers[0].StartupProbe = &corev1.Probe{}

provider, err := createTestProvider(aciMocks, NewMockConfigMapLister(mockCtrl),
NewMockSecretLister(mockCtrl), NewMockPodLister(mockCtrl), nil)
if err != nil {
t.Fatal("failed to create the test provider", err)
}

termSize := make(chan api.TermSize)
defer close(termSize)
go func() {
termSize <- api.TermSize{
Width: uint16(cols),
Height: uint16(rows),
}
}()
attachIO := NewMockAttachIO(mockCtrl)
attachIO.EXPECT().TTY().Return(true)
attachIO.EXPECT().Resize().Return(termSize)
attachIO.EXPECT().Stdout().Return(nil)

provider.RunInContainer(context.Background(), podNamespace, podName, "", nil, attachIO)
}
106 changes: 106 additions & 0 deletions pkg/provider/mock_vk_exec_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 15eb100

Please sign in to comment.