Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diamonwiggins authored and nvanthao committed Oct 17, 2024
1 parent a72c7ef commit 94947ba
Show file tree
Hide file tree
Showing 4 changed files with 679 additions and 482 deletions.
138 changes: 138 additions & 0 deletions pkg/analyze/collected_contents_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package analyzer

import (
"encoding/json"
"testing"

"github.com/replicatedhq/troubleshoot/pkg/constants"
"github.com/replicatedhq/troubleshoot/pkg/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRetrieveCollectedContents(t *testing.T) {
tests := []struct {
name string
getCollectedFileContents func(string) ([]byte, error) // Mock function
localPath string
remoteNodeBaseDir string
remoteFileName string
expectedResult []CollectedContent
expectedError string
}{
{
name: "successfully retrieve local content",
getCollectedFileContents: func(path string) ([]byte, error) {
if path == "localPath" {
return []byte("localContent"), nil
}
return nil, &types.NotFoundError{Name: path}
},
localPath: "localPath",
remoteNodeBaseDir: "remoteBaseDir",
remoteFileName: "remoteFileName",
expectedResult: []CollectedContent{
{
NodeName: "",
Data: []byte("localContent"),
},
},
expectedError: "",
},
{
name: "local content not found, retrieve remote node content successfully",
getCollectedFileContents: func(path string) ([]byte, error) {
if path == constants.NODE_LIST_FILE {
nodeNames := NodeNames{Nodes: []string{"node1", "node2"}}
return json.Marshal(nodeNames)
}
if path == "remoteBaseDir/node1/remoteFileName" {
return []byte("remoteContent1"), nil
}
if path == "remoteBaseDir/node2/remoteFileName" {
return []byte("remoteContent2"), nil
}
return nil, &types.NotFoundError{Name: path}
},
localPath: "localPath",
remoteNodeBaseDir: "remoteBaseDir",
remoteFileName: "remoteFileName",
expectedResult: []CollectedContent{
{
NodeName: "node1",
Data: []byte("remoteContent1"),
},
{
NodeName: "node2",
Data: []byte("remoteContent2"),
},
},
expectedError: "",
},
{
name: "fail to retrieve local content and node list",
getCollectedFileContents: func(path string) ([]byte, error) {
return nil, &types.NotFoundError{Name: path}
},
localPath: "localPath",
remoteNodeBaseDir: "remoteBaseDir",
remoteFileName: "remoteFileName",
expectedResult: nil,
expectedError: "failed to get node list",
},
{
name: "fail to retrieve content for one of the nodes",
getCollectedFileContents: func(path string) ([]byte, error) {
if path == constants.NODE_LIST_FILE {
nodeNames := NodeNames{Nodes: []string{"node1", "node2"}}
return json.Marshal(nodeNames)
}
if path == "remoteBaseDir/node1/remoteFileName" {
return []byte("remoteContent1"), nil
}
if path == "remoteBaseDir/node2/remoteFileName" {
return nil, &types.NotFoundError{Name: path}
}
return nil, &types.NotFoundError{Name: path}
},
localPath: "localPath",
remoteNodeBaseDir: "remoteBaseDir",
remoteFileName: "remoteFileName",
expectedResult: nil,
expectedError: "failed to retrieve content for node node2",
},
{
name: "fail to unmarshal node list",
getCollectedFileContents: func(path string) ([]byte, error) {
if path == constants.NODE_LIST_FILE {
return []byte("invalidJSON"), nil
}
return nil, &types.NotFoundError{Name: path}
},
localPath: "localPath",
remoteNodeBaseDir: "remoteBaseDir",
remoteFileName: "remoteFileName",
expectedResult: nil,
expectedError: "failed to unmarshal node names",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := retrieveCollectedContents(
test.getCollectedFileContents,
test.localPath,
test.remoteNodeBaseDir,
test.remoteFileName,
)

if test.expectedError != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), test.expectedError)
} else {
require.NoError(t, err)
assert.Equal(t, test.expectedResult, result)
}
})
}
}
Loading

0 comments on commit 94947ba

Please sign in to comment.