Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions bundle/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package bundle

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

"github.com/divolgin/archiver/extractor"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/replicatedcom/support-bundle/types"
)

// TestGenerate runs stub tasks to ensure results are being parsed and packed properly
func TestGenerate(t *testing.T) {

singleResults := taskStub{
elapse: time.Nanosecond,
results: []*types.Result{
{
Description: "Testing single",
Path: "/testSingle.txt",
},
},
}
mixedResults := taskStub{
elapse: time.Nanosecond,
results: []*types.Result{
{
Description: "Testing mixed results pass result",
Path: "/testPass.txt",
},
{
Description: "Testing mixed results fail result",
Error: errors.New("This was destined to fail"),
},
{
Description: "Testing mixed results other fail result",
Path: "/testFail.txt",
Error: errors.New("This was also meant to fail"),
},
},
}

tasks := []types.Task{singleResults, mixedResults}

got, _ := ioutil.TempFile("", "generate-test-bundle")
defer os.Remove(got.Name())

err := Generate(tasks, time.Duration(time.Second*2), got.Name())
require.NoError(t, err)

testDir, err := ioutil.TempDir("", "generate-test")
require.NoError(t, err)
defer os.RemoveAll(testDir)

//decompress to temp dir
extractor := extractor.NewTgz()
extractor.Extract(got.Name(), filepath.Join(testDir, "dir"))

//verify what we got
files, err := ioutil.ReadDir(filepath.Join(testDir, "dir"))
require.NoError(t, err)

require.Equal(t, 1, len(files))
require.True(t, files[0].IsDir())

uncompressedDir := files[0].Name()

//get index.json and error.json
indexReader, err := os.Open(filepath.Join(testDir, "dir", uncompressedDir, "index.json"))
require.NoError(t, err)
errorReader, err := os.Open(filepath.Join(testDir, "dir", uncompressedDir, "error.json"))
require.NoError(t, err)

//read into byte arrays
indexBytes, err := ioutil.ReadAll(indexReader)
require.NoError(t, err)
errorBytes, err := ioutil.ReadAll(errorReader)
require.NoError(t, err)

type testResult struct {
Description string `json:"description"`
Path string `json:"path"`
Error string `json:"error,omitempty"`
}

var indexAll []testResult
var errorAll []testResult

err = json.Unmarshal(indexBytes, &indexAll)
require.NoError(t, err)
err = json.Unmarshal(errorBytes, &errorAll)
require.NoError(t, err)

// everything that includes a path
require.Contains(t, indexAll, testResult{Description: "Testing single", Path: "/testSingle.txt"})
require.Contains(t, indexAll, testResult{Description: "Testing mixed results pass result", Path: "/testPass.txt"})
require.NotContains(t, indexAll, testResult{Description: "Testing mixed results fail result", Error: "This was destined to fail"})
require.Contains(t, indexAll, testResult{Description: "Testing mixed results other fail result", Path: "/testFail.txt", Error: "This was also meant to fail"})

// everything that includes an error
require.NotContains(t, errorAll, testResult{Description: "Testing single", Path: "/testSingle.txt"})
require.NotContains(t, errorAll, testResult{Description: "Testing mixed results pass result", Path: "/testPass.txt"})
require.Contains(t, errorAll, testResult{Description: "Testing mixed results fail result", Error: "This was destined to fail"})
require.Contains(t, errorAll, testResult{Description: "Testing mixed results other fail result", Path: "/testFail.txt", Error: "This was also meant to fail"})
}
5 changes: 3 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"github.com/replicatedcom/support-bundle/bundle"
"github.com/replicatedcom/support-bundle/types"

"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
Expand Down Expand Up @@ -50,9 +51,9 @@ func generate(cmd *cobra.Command, args []string) error {

jww.FEEDBACK.Println("Generating a new support bundle")

var tasks = []bundle.Task{}
var tasks = []types.Task{}

if _, err := bundle.Generate(tasks, time.Duration(time.Second*15)); err != nil {
if err := bundle.Generate(tasks, time.Duration(time.Second*15), "supportbundle.tar.gz"); err != nil {
jww.ERROR.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/core/planners/loadavg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestParseLoadAvg(t *testing.T) {
loadAvgValues, err := parseLoadAvg([]byte("0.02 0.01 0.00 4/229 5"))
require.NoError(t, err)
assert.Equal(t, loadAvgValues.minuteOne, float64(0.02))
assert.Equal(t, float64(0.02), loadAvgValues.(*LoadAverage).MinuteOne)

loadAvgValues, err = parseLoadAvg([]byte("0"))
require.NotNil(t, err)
Expand Down
5 changes: 3 additions & 2 deletions plugins/core/planners/uptime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
func TestParseUptime(t *testing.T) {
uptimeSeconds, err := parseUptime([]byte("33524.72 66785.42"))
require.NoError(t, err)
assert.Equal(t, uptimeSeconds[0], float64(33524.72))

assert.Equal(t, float64(33524.72), uptimeSeconds.(uptime).TotalSeconds)

uptimeSeconds, err = parseUptime([]byte("33524.72"))
require.NotNil(t, err)
assert.Nil(t, uptimeSeconds)

uptimeSeconds, err = parseUptime([]byte("0 0"))
require.NoError(t, err)
assert.Equal(t, uptimeSeconds[0], float64(0))
assert.Equal(t, float64(0), uptimeSeconds.(uptime).IdleSeconds)
}
3 changes: 1 addition & 2 deletions plugins/core/producers/read-command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package producers
import (
"context"
"os/exec"
"strings"

"github.com/replicatedcom/support-bundle/types"
)

func ReadCommand(command string, args ...string) types.BytesProducer {
return func(ctx context.Context) ([]byte, error) {
return exec.CommandContext(ctx, command, strings.Join(args, " ")).Output()
return exec.CommandContext(ctx, command, args...).Output()
}
}

Expand Down
48 changes: 48 additions & 0 deletions spec/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package spec

import (
"testing"

"github.com/replicatedcom/support-bundle/types"
"github.com/stretchr/testify/require"
)

// Test that yml files are parsed into spec lists properly
func TestParse(t *testing.T) {
yml := `
specs:
- builtin: core.loadavg
raw: /raw/metrics/loadavg
json: /json/metrics/loadavg.json
human: /human/metrics/loadavg
- builtin: docker.logs
raw: /raw/containers/testExample/logs.txt
config:
container_id: testExample
- builtin: docker.daemon
raw: /raw/docker/
json: /json/docker/
`

specs, err := Parse([]byte(yml))
require.NoError(t, err)

require.Contains(t, specs, types.Spec{
Builtin: "core.loadavg",
Raw: "/raw/metrics/loadavg",
JSON: "/json/metrics/loadavg.json",
Human: "/human/metrics/loadavg",
})

require.Contains(t, specs, types.Spec{
Builtin: "docker.logs",
Raw: "/raw/containers/testExample/logs.txt",
Config: map[interface{}]interface{}{"container_id": "testExample"},
})

require.Contains(t, specs, types.Spec{
Builtin: "docker.daemon",
Raw: "/raw/docker/",
JSON: "/json/docker/",
})
}
Loading