-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a72c7ef
commit 94947ba
Showing
4 changed files
with
679 additions
and
482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.