Skip to content

Commit

Permalink
Add a tail parameter to pod collector. (#220)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Owsiany <mowsiany@D2iQ.com>
  • Loading branch information
porridge committed Oct 9, 2020
1 parent a27076f commit ab76b76
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
13 changes: 8 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ You can find the full text of the DCO here: https://developercertificate.org/
### Build Instructions

- Get the KUTTL repo: `git clone https://github.com/kudobuilder/kuttl.git`
- `cd kudo`
- `make all` to build manager as well as CLI
- `cd kuttl`
- `make cli` to build the CLI

#### Testing new CLI
You can build CLI locally via `make cli`. After running that command, CLI will be available in `bin/kubectl-kuttl` and you can invoke the command for example like this `bin/kubectl-kuttl version` (no need to install it as kubectl plugin).
After running this command, CLI will be available in `bin/kubectl-kuttl` and you can invoke the command for example like this `bin/kubectl-kuttl version` (no need to install it as `kubectl` plugin).

### Testing

See the [contributor's testing guide](https://github.com/kudobuilder/kudo/blob/main/test/README.md).
Use `make all` to run all available tests.
Run just `make` to see what individual targets are available.

The project has settled on the [testify](https://github.com/stretchr/testify) library for testing purposes.
Please use it for new tests as well if possible.

## Community, Discussion, and Support

Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/testharness/v1beta1/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ func podCommand(tc *TestCollector) *Command {
} else {
b.WriteString(" --all-containers")
}
if tc.Tail == 0 {
if len(tc.Selector) > 0 {
tc.Tail = 10
} else {
tc.Tail = -1
}
}
fmt.Fprintf(&b, " --tail=%d", tc.Tail)
return &Command{
Command: b.String(),
IgnoreFailure: true,
Expand Down
38 changes: 38 additions & 0 deletions pkg/apis/testharness/v1beta1/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v1beta1
import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTestCollector_String(t *testing.T) {
Expand Down Expand Up @@ -119,3 +121,39 @@ func TestTestCollector_String(t *testing.T) {
})
}
}

func TestPodCommand(t *testing.T) {
tests := []struct {
name string
tc TestCollector
cmd string
}{
{
name: "selector with default tail",
tc: TestCollector{Type: pod, Selector: "x=y"},
cmd: "kubectl logs --prefix -l x=y -n $NAMESPACE --all-containers --tail=10",
},
{
name: "pod name with default tail",
tc: TestCollector{Type: pod, Pod: "foo"},
cmd: "kubectl logs --prefix foo -n $NAMESPACE --all-containers --tail=-1",
},
{
name: "selector with set tail",
tc: TestCollector{Type: pod, Selector: "x=y", Tail: 42},
cmd: "kubectl logs --prefix -l x=y -n $NAMESPACE --all-containers --tail=42",
},
{
name: "pod name with set tail",
tc: TestCollector{Type: pod, Pod: "foo", Tail: 42},
cmd: "kubectl logs --prefix foo -n $NAMESPACE --all-containers --tail=42",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
cmd := podCommand(&tt.tc)
assert.Equal(t, cmd.Command, tt.cmd)
})
}
}
4 changes: 4 additions & 0 deletions pkg/apis/testharness/v1beta1/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ type TestCollector struct {
Container string `json:"container,omitempty"`
// Selector is a label query to select pod.
Selector string `json:"selector,omitempty"`
// Tail is the number of last lines to collect from pods. If omitted or zero,
// then the default is 10 if you use a selector, or -1 (all) if you use a pod name.
// This matches default behavior of `kubectl logs`.
Tail int `json:"tail,omitempty"`
// Cmd is a command to run for collection. It requires an empty Type or Type=command
Cmd string `json:"command,omitempty"`
}
Expand Down

0 comments on commit ab76b76

Please sign in to comment.