Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanthao committed Dec 31, 2024
1 parent 9a16001 commit 8d3d641
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/supportbundle/supportbundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ func saveAndRedactFinalSpec(spec *troubleshootv1beta2.SupportBundleSpec, result
constants.SPEC_FILENAME: []byte(yamlContent),
}

err = collect.RedactResult(bundlePath, singleResult, additionalRedactors.Spec.Redactors)
var redactors []*troubleshootv1beta2.Redact
if additionalRedactors != nil {
redactors = additionalRedactors.Spec.Redactors
}
err = collect.RedactResult(bundlePath, singleResult, redactors)
if err != nil {
return errors.Wrap(err, "failed to redact final support bundle yaml spec")
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/supportbundle/supportbundle_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package supportbundle

import (
"os"
"path/filepath"
"reflect"
"testing"

troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/replicatedhq/troubleshoot/pkg/constants"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -114,3 +121,47 @@ func Test_getNodeList(t *testing.T) {
})
}
}

func Test_saveAndRedactFinalSpec(t *testing.T) {
spec := &troubleshootv1beta2.SupportBundleSpec{
Collectors: []*troubleshootv1beta2.Collect{
{
ClusterInfo: &troubleshootv1beta2.ClusterInfo{},
ClusterResources: &troubleshootv1beta2.ClusterResources{},
Postgres: &troubleshootv1beta2.Database{
URI: "postgresql://user:password@hostname:5432/defaultdb?sslmode=require",
},
},
},
}

result := make(collect.CollectorResult)
bundlePath := t.TempDir()
expectedYAML := `
apiVersion: troubleshoot.sh/v1beta2
kind: SupportBundle
metadata:
creationTimestamp: null
spec:
collectors:
- clusterInfo: {}
clusterResources: {}
postgres:
uri: postgresql://***HIDDEN***:***HIDDEN***@***HIDDEN***:5432/***HIDDEN***
status: {}`

err := saveAndRedactFinalSpec(spec, &result, bundlePath, nil)
if err != nil {
t.Fatal(err)
}

actualYAML, err := os.ReadFile(filepath.Join(bundlePath, constants.SPEC_FILENAME))
require.NoError(t, err)

var expectedData, actualData interface{}
err = yaml.Unmarshal([]byte(expectedYAML), &expectedData)
require.NoError(t, err)
err = yaml.Unmarshal(actualYAML, &actualData)
require.NoError(t, err)
assert.Equal(t, expectedData, actualData)
}

0 comments on commit 8d3d641

Please sign in to comment.