talos/internal/pkg/archiver/walker_test.go
Andrey Smirnov 6d5ee0ca80
feat(init): unify filesystem walkers for ls/cp APIs (#779)
This unifies low-level filesystem walker code for `ls` and `cp`.

New features:

* `ls` now reports relative filenames
* `ls` now prints symlink destination for symlinks
* `cp` now properly always reports errors from the API
* `cp` now reports all the errors back to the client

Example for `ls`:

```
osctl-linux-amd64 --talosconfig talosconfig ls -l /var
MODE          SIZE(B)   LASTMOD       NAME
drwxr-xr-x    4096      Jun 26 2019   .
Lrwxrwxrwx    4         Jun 25 2019   etc -> /etc
drwxr-xr-x    4096      Jun 26 2019   lib
drwxr-xr-x    4096      Jun 21 2019   libexec
drwxr-xr-x    4096      Jun 26 2019   log
drwxr-xr-x    4096      Jun 21 2019   mail
drwxr-xr-x    4096      Jun 26 2019   opt
Lrwxrwxrwx    6         Jun 21 2019   run -> ../run
drwxr-xr-x    4096      Jun 21 2019   spool
dtrwxrwxrwx   4096      Jun 21 2019   tmp
-rw-------    14979     Jun 26 2019   userdata.yaml
```

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2019-06-26 17:43:09 +03:00

83 lines
2.1 KiB
Go

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Package archiver provides a service to archive part of the filesystem into tar archive
package archiver_test
import (
"context"
"path/filepath"
"testing"
"github.com/stretchr/testify/suite"
"github.com/talos-systems/talos/internal/pkg/archiver"
)
type WalkerSuite struct {
CommonSuite
}
func (suite *WalkerSuite) TestIterationDir() {
ch, err := archiver.Walker(context.Background(), suite.tmpDir, archiver.WithSkipRoot())
suite.Require().NoError(err)
relPaths := []string(nil)
for fi := range ch {
suite.Require().NoError(fi.Error)
relPaths = append(relPaths, fi.RelPath)
if fi.RelPath == "usr/bin/mv" {
suite.Assert().Equal("/usr/bin/cp", fi.Link)
}
}
suite.Assert().Equal([]string{
"dev", "dev/random",
"etc", "etc/certs", "etc/certs/ca.crt", "etc/hostname",
"lib", "lib/dynalib.so",
"usr", "usr/bin", "usr/bin/cp", "usr/bin/mv"},
relPaths)
}
func (suite *WalkerSuite) TestIterationMaxRecurseDepth() {
ch, err := archiver.Walker(context.Background(), suite.tmpDir, archiver.WithMaxRecurseDepth(1))
suite.Require().NoError(err)
relPaths := []string(nil)
for fi := range ch {
suite.Require().NoError(fi.Error)
relPaths = append(relPaths, fi.RelPath)
}
suite.Assert().Equal([]string{
".", "dev", "etc", "lib", "usr"},
relPaths)
}
func (suite *WalkerSuite) TestIterationFile() {
ch, err := archiver.Walker(context.Background(), filepath.Join(suite.tmpDir, "usr/bin/cp"))
suite.Require().NoError(err)
relPaths := []string(nil)
for fi := range ch {
suite.Require().NoError(fi.Error)
relPaths = append(relPaths, fi.RelPath)
}
suite.Assert().Equal([]string{"cp"},
relPaths)
}
func (suite *WalkerSuite) TestIterationNotFound() {
_, err := archiver.Walker(context.Background(), filepath.Join(suite.tmpDir, "doesntlivehere"))
suite.Require().Error(err)
}
func TestWalkerSuite(t *testing.T) {
suite.Run(t, new(WalkerSuite))
}