Skip to content

Commit

Permalink
PT-2281 - provide container name for copying files in the dump
Browse files Browse the repository at this point in the history
- Implemented error handling when container name is not specified
- Added test case for error handling
  • Loading branch information
svetasmirnova committed Nov 29, 2023
1 parent b862945 commit 43ae487
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/go/pt-k8s-debug-collector/dumper/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (d *Dumper) DumpCluster() error {
// get individual Logs
location = filepath.Join(d.location, ns.Name, pod.Name)
for _, path := range d.filePaths {
err = d.getIndividualFiles(resourceType(d.crType), ns.Name, pod.Name, path, location, tw)
err = d.getIndividualFiles(ns.Name, pod.Name, path, location, tw)
if err != nil {
d.logError(err.Error(), "get file "+path+" for pod "+pod.Name)
log.Printf("Error: get %s file: %v", path, err)
Expand Down Expand Up @@ -372,13 +372,15 @@ type crSecrets struct {
} `json:"spec"`
}

// TODO: check if resource parameter is really needed
func (d *Dumper) getIndividualFiles(resource, namespace string, podName, path, location string, tw *tar.Writer) error {
func (d *Dumper) getIndividualFiles(namespace string, podName, path, location string, tw *tar.Writer) error {
if len(d.fileContainer) == 0 {
return errors.Errorf("Logs container name is not specified for resource %s in namespace %s", resourceType(d.crType), d.namespace)
}
args := []string{"-n", namespace, "-c", d.fileContainer, "cp", podName + ":" + path, "/dev/stdout"}
output, err := d.runCmd(args...)
if err != nil {
d.logError(err.Error(), args...)
log.Printf("Error: get path %s for resource %s in namespace %s: %v", path, resource, d.namespace, err)
log.Printf("Error: get path %s for resource %s in namespace %s: %v", path, resourceType(d.crType), d.namespace, err)
return addToArchive(location, d.mode, []byte(err.Error()), tw)
}

Expand Down
20 changes: 20 additions & 0 deletions src/go/pt-k8s-debug-collector/dumper/dumper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dumper

import (
"testing"

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

/*
Unit test for non-existing logs container name error handling
*/

func TestGetIndividualFilesError(t *testing.T) {
d := New("", "", "psmdb", "", "")

err := d.getIndividualFiles("", "", "", "", nil)

assert.Error(t, err)
assert.ErrorContains(t, err, "Logs container name is not specified")
}

0 comments on commit 43ae487

Please sign in to comment.