talos/pkg/tail/tail_test.go
Andrey Smirnov 9379cf9ee1 refactor: expose provision as public package
This change is only moving packages and updating import paths.

Goal: expose `internal/pkg/provision` as `pkg/provision` to enable other
projects to import Talos provisioning library.

As cluster checks are almost always required as part of provisioning
process, package `internal/pkg/cluster` was also made public as
`pkg/cluster`.

Other changes were direct dependencies discovered by `importvet` which
were updated.

Public packages (useful, general purpose packages with stable API):

* `internal/pkg/conditions` -> `pkg/conditions`
* `internal/pkg/tail` -> `pkg/tail`

Private packages (used only on provisioning library internally):

* `internal/pkg/inmemhttp` -> `pkg/provision/internal/inmemhttp`
* `internal/pkg/kernel/vmlinuz` -> `pkg/provision/internal/vmlinuz`
* `internal/pkg/cniutils` -> `pkg/provision/internal/cniutils`

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2020-08-12 05:12:05 -07:00

97 lines
2.5 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 tail_test
import (
"bufio"
"bytes"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/talos-systems/talos/pkg/tail"
)
func TestSkipLines(t *testing.T) {
for _, test := range []struct {
desc string
input []byte
tailLines []int
expectLines []int
}{
{
desc: "empty",
input: nil,
tailLines: []int{0, 1, 2, 10},
expectLines: []int{0, 0, 0, 0},
},
{
desc: "enormous line",
input: bytes.Repeat([]byte{0xde, 0xad, 0xbe, 0xef}, 8000),
tailLines: []int{0, 1, 10},
expectLines: []int{0, 1, 1},
},
{
desc: "enormous line with \\n",
input: append(bytes.Repeat([]byte{0xde, 0xad, 0xbe, 0xef}, 8000), '\n'),
tailLines: []int{0, 1, 10},
expectLines: []int{0, 1, 1},
},
{
desc: "many small lines",
input: bytes.Repeat([]byte{0xde, 0xad, 0xbe, 0xef, '\n'}, 1024),
tailLines: []int{0, 1, 3, 10, 100, 1000},
expectLines: []int{0, 1, 3, 10, 100, 1000},
},
{
desc: "many small aligned lines",
input: bytes.Repeat([]byte{0xde, 0xad, 0xbe, '\n'}, 1024),
tailLines: []int{0, 1, 3, 10, 100, 1000},
expectLines: []int{0, 1, 3, 10, 100, 1000},
},
{
desc: "empty lines",
input: bytes.Repeat([]byte{'\n'}, 65536),
tailLines: []int{0, 1, 3, 10, 100, 1000, 10000},
expectLines: []int{0, 1, 3, 10, 100, 1000, 10000},
},
{
desc: "window-sized lines",
input: bytes.Repeat(append(bytes.Repeat([]byte{'a'}, tail.Window-1), '\n'), 24),
tailLines: []int{0, 1, 3, 10, 100, 1000},
expectLines: []int{0, 1, 3, 10, 24, 24},
},
{
desc: "long lines",
input: bytes.Repeat(append(bytes.Repeat([]byte{'a'}, 356), '\n'), 24),
tailLines: []int{0, 1, 3, 10, 15, 24, 100, 1000},
expectLines: []int{0, 1, 3, 10, 15, 24, 24, 24},
},
} {
for i, lines := range test.tailLines {
r := bytes.NewReader(test.input)
err := tail.SeekLines(r, lines)
assert.NoError(t, err, "test %q", test.desc)
tailOffset, _ := r.Seek(0, io.SeekCurrent) //nolint: errcheck
expected := test.expectLines[i]
actual := 0
scanner := bufio.NewScanner(r)
for scanner.Scan() {
actual++
}
assert.NoError(t, scanner.Err(), "test %q", test.desc)
assert.Equal(t, expected, actual, "test %q, tailOffset %d", test.desc, tailOffset)
}
}
}