Skip to content

Commit

Permalink
test: invalid character 'i' in literal in runc State func. Do not com…
Browse files Browse the repository at this point in the history
…bine stdout and stderr for json parsing
  • Loading branch information
ansgarschulte committed Sep 21, 2023
1 parent 49b60df commit 0fe7cc2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
29 changes: 20 additions & 9 deletions pkg/container/runc/runc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
package runc

import (
"bytes"
"context"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -64,21 +64,32 @@ func NewRunc(runtime types.Runtime) Runc {

func (r *defaultRunc) State(ctx context.Context, id string) (*Container, error) {
cmd := r.command(ctx, "state", id)
var outputBuffer, errorBuffer bytes.Buffer
cmd.Stdout = &outputBuffer
cmd.Stderr = &errorBuffer
err := cmd.Run()
output := outputBuffer.Bytes()
stderr := errorBuffer.Bytes()
var outputBuffer, errorBuffer bytes.Buffer
cmd.Stdout = &outputBuffer
cmd.Stderr = &errorBuffer
err := cmd.Run()
output := outputBuffer.Bytes()
stderr := errorBuffer.Bytes()
if err != nil {
return nil, fmt.Errorf("%s (%s): %s", err, stderr, output)
}

log.Trace().Str("output", string(output)).Str("stderr", string(stderr)).Msg("get container state")

return parseRunCStateToContainer(output)
}

func parseRunCStateToContainer(output []byte) (*Container, error) {
var c Container
if err := json.Unmarshal(output, &c); err != nil {
return nil, err
if output[0] != '{' && bytes.Contains(output, []byte("{")) && bytes.Contains(output, []byte("}")) {
outputTruncated := output[bytes.IndexByte(output, '{'):]
if err2 := json.Unmarshal(outputTruncated, &c); err2 != nil {
return nil, err2
}
} else {
return nil, err
}
}
return &c, nil
}
Expand Down
70 changes: 70 additions & 0 deletions pkg/container/runc/runc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package runc

import (
"reflect"
"testing"
"time"
)


func Test_parseRunCStateToContainer(t *testing.T) {
type args struct {
output []byte
}

timeVal, _ := time.Parse(time.RFC3339, "2023-09-20T05:35:15.520959889Z")
container := &Container{
ID: "7d51145a4959742f7185563dc72f7fd9b08c6c375db406696ae0c94eac7f787e",
Status: "running",
Bundle: "/run/containerd/io.containerd.runtime.v2.task/moby/7d51145a4959742f7185563dc72f7fd9b08c6c375db406696ae0c94eac7f787e",
Rootfs: "/var/lib/docker/overlay2/88d42eefb3b59ff1055efa14e6ac07bffd30e3321242bc546bcf1e69b607f0b0/merged",
Pid: 14907,
Created: timeVal,
}

warning := "time=\"2023-09-20T19:36:27Z\" level=debug msg=\"openat2 not available, falling back to securejoin\" func=\"libcontainer/cgroups.prepareOpenat2.func1()\" file=\"libcontainer/cgroups/file.go:95\"\n"
payload := "{\n \"ociVersion\": \"1.0.2-dev\",\n \"id\": \"7d51145a4959742f7185563dc72f7fd9b08c6c375db406696ae0c94eac7f787e\",\n \"pid\": 14907,\n \"status\": \"running\",\n \"bundle\": \"/run/containerd/io.containerd.runtime.v2.task/moby/7d51145a4959742f7185563dc72f7fd9b08c6c375db406696ae0c94eac7f787e\",\n \"rootfs\": \"/var/lib/docker/overlay2/88d42eefb3b59ff1055efa14e6ac07bffd30e3321242bc546bcf1e69b607f0b0/merged\",\n \"created\": \"2023-09-20T05:35:15.520959889Z\",\n \"owner\": \"\"\n}"
tests := []struct {
name string
args args
want *Container
wantErr bool
}{
{
name: "parseRunCStateToContainer",
args: args{
output: []byte(payload),
},
want: container,
wantErr: false,
},
{
name: "include warning",
args: args{
output: []byte(warning+ payload),
},
want: container,
wantErr: false,
},
{
name: "error",
args: args{
output: []byte(warning),
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseRunCStateToContainer(tt.args.output)
if (err != nil) != tt.wantErr {
t.Errorf("parseRunCStateToContainer() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseRunCStateToContainer() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 0fe7cc2

Please sign in to comment.