Skip to content

Commit

Permalink
tests: fix panic for unpack test
Browse files Browse the repository at this point in the history
Throws a panic like below when run TestUnpack test case:

```
converter_test.go:415:
Error Trace:	converter_test.go:415
Error:      	Not equal:
              expected: "sha256:b4679f7afa21a5513d84ba7d9a6faf2621f101421c830648512cd63475118fa1"
              actual  : "sha256:92ad2fb1075eabd299daa2dbb5e822031c198551fa9cebf29046ed79efa27ce3"

              Diff:
              --- Expected
              +++ Actual
              @@ -1,2 +1,2 @@
              -(digest.Digest) (len=71) "sha256:b4679f7afa21a5513d84ba7d9a6faf2621f101421c830648512cd63475118fa1"
              +(digest.Digest) (len=71) "sha256:92ad2fb1075eabd299daa2dbb5e822031c198551fa9cebf29046ed79efa27ce3"

Test:       	TestUnpack
```

Related: dragonflyoss/nydus#630

To make the unpacked tar consistent with the OCI-formatted tar before the pack,
we need to backfill the username and groupname in the tar header, which may
break the repeatable build when unpacking in different hosts, but actually has
little effect.

Signed-off-by: Yan Song <[email protected]>
  • Loading branch information
imeoer committed Aug 3, 2022
1 parent a6576de commit ca15ba1
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions tests/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -73,25 +74,39 @@ func ensureNoFile(t *testing.T, name string) {
}

func writeFileToTar(t *testing.T, tw *tar.Writer, name string, data string) {
u, err := user.Current()
require.NoError(t, err)
g, err := user.LookupGroupId(u.Uid)
require.NoError(t, err)

hdr := &tar.Header{
Name: name,
Mode: 0444,
Size: int64(len(data)),
Name: name,
Mode: 0444,
Size: int64(len(data)),
Uname: u.Name,
Gname: g.Name,
}
err := tw.WriteHeader(hdr)
err = tw.WriteHeader(hdr)
require.NoError(t, err)

io.Copy(tw, bytes.NewReader([]byte(data)))
require.NoError(t, err)
}

func writeDirToTar(t *testing.T, tw *tar.Writer, name string) {
u, err := user.Current()
require.NoError(t, err)
g, err := user.LookupGroupId(u.Uid)
require.NoError(t, err)

hdr := &tar.Header{
Name: name,
Mode: 0444,
Typeflag: tar.TypeDir,
Uname: u.Name,
Gname: g.Name,
}
err := tw.WriteHeader(hdr)
err = tw.WriteHeader(hdr)
require.NoError(t, err)
}

Expand Down

0 comments on commit ca15ba1

Please sign in to comment.