-
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.
Refactor Multi Node Analyzers (#1646)
* initial refactor of host os analyzer * refactor remote collect analysis --------- Signed-off-by: Evans Mungai <[email protected]> Co-authored-by: Gerard Nguyen <[email protected]> Co-authored-by: Evans Mungai <[email protected]>
- Loading branch information
1 parent
9c24ab6
commit b88bc8d
Showing
11 changed files
with
1,128 additions
and
765 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,58 @@ | ||
package analyzer | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/troubleshoot/pkg/constants" | ||
) | ||
|
||
type collectedContent struct { | ||
NodeName string | ||
Data collectorData | ||
} | ||
|
||
type collectorData interface{} | ||
|
||
type nodeNames struct { | ||
Nodes []string `json:"nodes"` | ||
} | ||
|
||
func retrieveCollectedContents( | ||
getCollectedFileContents func(string) ([]byte, error), | ||
localPath string, remoteNodeBaseDir string, remoteFileName string, | ||
) ([]collectedContent, error) { | ||
var collectedContents []collectedContent | ||
|
||
// Try to retrieve local data first | ||
if contents, err := getCollectedFileContents(localPath); err == nil { | ||
collectedContents = append(collectedContents, collectedContent{NodeName: "", Data: contents}) | ||
// Return immediately if local content is available | ||
return collectedContents, nil | ||
} | ||
|
||
// Local data not available, move to remote collection | ||
nodeListContents, err := getCollectedFileContents(constants.NODE_LIST_FILE) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get node list") | ||
} | ||
|
||
var nodeNames nodeNames | ||
if err := json.Unmarshal(nodeListContents, &nodeNames); err != nil { | ||
return nil, errors.Wrap(err, "failed to unmarshal node names") | ||
} | ||
|
||
// Collect data for each node | ||
for _, node := range nodeNames.Nodes { | ||
nodeFilePath := fmt.Sprintf("%s/%s/%s", remoteNodeBaseDir, node, remoteFileName) | ||
nodeContents, err := getCollectedFileContents(nodeFilePath) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to retrieve content for node %s", node) | ||
} | ||
|
||
collectedContents = append(collectedContents, collectedContent{NodeName: node, Data: nodeContents}) | ||
} | ||
|
||
return collectedContents, nil | ||
} |
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) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.