Skip to content

Commit

Permalink
Merge branch 'main' into network-namespace-collector-and-analyser
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardomaraschini authored Nov 4, 2024
2 parents d4e4822 + f0b8de6 commit c5e660e
Show file tree
Hide file tree
Showing 29 changed files with 853 additions and 1,130 deletions.
4 changes: 1 addition & 3 deletions pkg/analyze/collected_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (

type collectedContent struct {
NodeName string
Data collectorData
Data []byte
}

type collectorData interface{}

type nodeNames struct {
Nodes []string `json:"nodes"`
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyze/host_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (c *resultCollector) get(title string) []*AnalyzeResult {
return []*AnalyzeResult{{Title: title, IsWarn: true, Message: "no results"}}
}

func analyzeHostCollectorResults(collectedContent []collectedContent, outcomes []*troubleshootv1beta2.Outcome, checkCondition func(string, collectorData) (bool, error), title string) ([]*AnalyzeResult, error) {
func analyzeHostCollectorResults(collectedContent []collectedContent, outcomes []*troubleshootv1beta2.Outcome, checkCondition func(string, []byte) (bool, error), title string) ([]*AnalyzeResult, error) {
var results []*AnalyzeResult
for _, content := range collectedContent {
currentTitle := title
Expand All @@ -110,7 +110,7 @@ func analyzeHostCollectorResults(collectedContent []collectedContent, outcomes [
return results, nil
}

func evaluateOutcomes(outcomes []*troubleshootv1beta2.Outcome, checkCondition func(string, collectorData) (bool, error), data collectorData, title string) ([]*AnalyzeResult, error) {
func evaluateOutcomes(outcomes []*troubleshootv1beta2.Outcome, checkCondition func(string, []byte) (bool, error), data []byte, title string) ([]*AnalyzeResult, error) {
var results []*AnalyzeResult

for _, outcome := range outcomes {
Expand Down
63 changes: 29 additions & 34 deletions pkg/analyze/host_analyzer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package analyzer

import (
"encoding/json"
"testing"

"github.com/pkg/errors"
Expand All @@ -10,25 +11,30 @@ import (
"github.com/stretchr/testify/require"
)

func collectorToBytes(t *testing.T, collector any) []byte {
jsonData, err := json.Marshal(collector)
require.NoError(t, err)
return jsonData
}

func TestAnalyzeHostCollectorResults(t *testing.T) {
tests := []struct {
name string
outcomes []*troubleshootv1beta2.Outcome
collectedContent []collectedContent
checkCondition func(string, collectorData) (bool, error)
expectResult []*AnalyzeResult
}{
{
name: "pass if ubuntu >= 00.1.2",
collectedContent: []collectedContent{
{
NodeName: "node1",
Data: collect.HostOSInfo{
Data: collectorToBytes(t, collect.HostOSInfo{
Name: "myhost",
KernelVersion: "5.4.0-1034-gcp",
PlatformVersion: "00.1.2",
Platform: "ubuntu",
},
}),
},
},
outcomes: []*troubleshootv1beta2.Outcome{
Expand All @@ -44,10 +50,6 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
},
},
},
checkCondition: func(when string, data collectorData) (bool, error) {
osInfo := data.(collect.HostOSInfo)
return osInfo.Platform == "ubuntu" && osInfo.PlatformVersion >= "00.1.2", nil
},
expectResult: []*AnalyzeResult{
{
Title: "Host OS Info - Node node1",
Expand All @@ -61,21 +63,21 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
collectedContent: []collectedContent{
{
NodeName: "node1",
Data: collect.HostOSInfo{
Data: collectorToBytes(t, collect.HostOSInfo{
Name: "myhost",
KernelVersion: "5.4.0-1034-gcp",
PlatformVersion: "11.04",
Platform: "ubuntu",
},
}),
},
{
NodeName: "node2",
Data: collect.HostOSInfo{
Data: collectorToBytes(t, collect.HostOSInfo{
Name: "myhost",
KernelVersion: "5.4.0-1034-gcp",
PlatformVersion: "11.04",
Platform: "ubuntu",
},
}),
},
},
outcomes: []*troubleshootv1beta2.Outcome{
Expand All @@ -91,10 +93,6 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
},
},
},
checkCondition: func(when string, data collectorData) (bool, error) {
osInfo := data.(collect.HostOSInfo)
return osInfo.Platform == "ubuntu" && osInfo.PlatformVersion <= "11.04", nil
},
expectResult: []*AnalyzeResult{
{
Title: "Host OS Info - Node node1",
Expand All @@ -113,12 +111,12 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
collectedContent: []collectedContent{
{
NodeName: "",
Data: collect.HostOSInfo{
Data: collectorToBytes(t, collect.HostOSInfo{
Name: "myhost",
KernelVersion: "5.4.0-1034-gcp",
PlatformVersion: "20.04",
Platform: "ubuntu",
},
}),
},
},
outcomes: []*troubleshootv1beta2.Outcome{
Expand All @@ -134,10 +132,6 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {
},
},
},
checkCondition: func(when string, data collectorData) (bool, error) {
osInfo := data.(collect.HostOSInfo)
return osInfo.Platform == "ubuntu" && osInfo.PlatformVersion >= "20.04", nil
},
expectResult: []*AnalyzeResult{
{
Title: "Host OS Info", // Ensuring the title does not include node name if it's empty
Expand All @@ -150,8 +144,9 @@ func TestAnalyzeHostCollectorResults(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
a := AnalyzeHostOS{}
// Call the new analyzeHostCollectorResults function with the test data
result, err := analyzeHostCollectorResults(test.collectedContent, test.outcomes, test.checkCondition, "Host OS Info")
result, err := analyzeHostCollectorResults(test.collectedContent, test.outcomes, a.CheckCondition, "Host OS Info")
require.NoError(t, err)
assert.Equal(t, test.expectResult, result)
})
Expand All @@ -162,8 +157,8 @@ func TestEvaluateOutcomes(t *testing.T) {
tests := []struct {
name string
outcomes []*troubleshootv1beta2.Outcome
checkCondition func(string, collectorData) (bool, error)
data collectorData
checkCondition func(string, []byte) (bool, error)
data []byte
expectedResult []*AnalyzeResult
}{
{
Expand All @@ -176,11 +171,11 @@ func TestEvaluateOutcomes(t *testing.T) {
},
},
},
checkCondition: func(when string, data collectorData) (bool, error) {
checkCondition: func(when string, data []byte) (bool, error) {
// Return true if the condition being checked matches "failCondition"
return when == "failCondition", nil
},
data: "someData",
data: []byte("someData"),
expectedResult: []*AnalyzeResult{
{
Title: "Test Title",
Expand All @@ -199,11 +194,11 @@ func TestEvaluateOutcomes(t *testing.T) {
},
},
},
checkCondition: func(when string, data collectorData) (bool, error) {
checkCondition: func(when string, data []byte) (bool, error) {
// Return true if the condition being checked matches "warnCondition"
return when == "warnCondition", nil
},
data: "someData",
data: []byte("someData"),
expectedResult: []*AnalyzeResult{
{
Title: "Test Title",
Expand All @@ -222,11 +217,11 @@ func TestEvaluateOutcomes(t *testing.T) {
},
},
},
checkCondition: func(when string, data collectorData) (bool, error) {
checkCondition: func(when string, data []byte) (bool, error) {
// Return true if the condition being checked matches "passCondition"
return when == "passCondition", nil
},
data: "someData",
data: []byte("someData"),
expectedResult: []*AnalyzeResult{
{
Title: "Test Title",
Expand All @@ -253,11 +248,11 @@ func TestEvaluateOutcomes(t *testing.T) {
},
},
},
checkCondition: func(when string, data collectorData) (bool, error) {
checkCondition: func(when string, data []byte) (bool, error) {
// Always return false to simulate no condition matching
return false, nil
},
data: "someData",
data: []byte("someData"),
expectedResult: nil, // No condition matches, so we expect no results
},
{
Expand All @@ -270,11 +265,11 @@ func TestEvaluateOutcomes(t *testing.T) {
},
},
},
checkCondition: func(when string, data collectorData) (bool, error) {
checkCondition: func(when string, data []byte) (bool, error) {
// Simulate an error occurring during condition evaluation
return false, errors.New("mock error")
},
data: "someData",
data: []byte("someData"),
expectedResult: []*AnalyzeResult{
{
Title: "Test Title",
Expand Down
103 changes: 24 additions & 79 deletions pkg/analyze/host_block_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,90 +27,24 @@ func (a *AnalyzeHostBlockDevices) IsExcluded() (bool, error) {
func (a *AnalyzeHostBlockDevices) Analyze(
getCollectedFileContents func(string) ([]byte, error), findFiles getChildCollectedFileContents,
) ([]*AnalyzeResult, error) {
hostAnalyzer := a.hostAnalyzer

contents, err := getCollectedFileContents(collect.HostBlockDevicesPath)
result := AnalyzeResult{Title: a.Title()}

collectedContents, err := retrieveCollectedContents(
getCollectedFileContents,
collect.HostBlockDevicesPath,
collect.NodeInfoBaseDir,
collect.HostBlockDevicesFileName,
)
if err != nil {
return nil, errors.Wrap(err, "failed to get collected file")
return []*AnalyzeResult{&result}, err
}

var devices []collect.BlockDeviceInfo
if err := json.Unmarshal(contents, &devices); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal block devices info")
}

result := AnalyzeResult{}

result.Title = a.Title()

for _, outcome := range hostAnalyzer.Outcomes {
if outcome.Fail != nil {
if outcome.Fail.When == "" {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return []*AnalyzeResult{&result}, nil
}

isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Fail.When, hostAnalyzer.MinimumAcceptableSize, hostAnalyzer.IncludeUnmountedPartitions, devices)
if err != nil {
return nil, errors.Wrapf(err, "failed to compare %s", outcome.Fail.When)
}

if isMatch {
result.IsFail = true
result.Message = outcome.Fail.Message
result.URI = outcome.Fail.URI

return []*AnalyzeResult{&result}, nil
}
} else if outcome.Warn != nil {
if outcome.Warn.When == "" {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return []*AnalyzeResult{&result}, nil
}

isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Warn.When, hostAnalyzer.MinimumAcceptableSize, hostAnalyzer.IncludeUnmountedPartitions, devices)
if err != nil {
return nil, errors.Wrapf(err, "failed to compare %s", outcome.Warn.When)
}

if isMatch {
result.IsWarn = true
result.Message = outcome.Warn.Message
result.URI = outcome.Warn.URI

return []*AnalyzeResult{&result}, nil
}
} else if outcome.Pass != nil {
if outcome.Pass.When == "" {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI

return []*AnalyzeResult{&result}, nil
}

isMatch, err := compareHostBlockDevicesConditionalToActual(outcome.Pass.When, hostAnalyzer.MinimumAcceptableSize, hostAnalyzer.IncludeUnmountedPartitions, devices)
if err != nil {
return nil, errors.Wrapf(err, "failed to compare %s", outcome.Pass.When)
}

if isMatch {
result.IsPass = true
result.Message = outcome.Pass.Message
result.URI = outcome.Pass.URI

return []*AnalyzeResult{&result}, nil
}
}
results, err := analyzeHostCollectorResults(collectedContents, a.hostAnalyzer.Outcomes, a.CheckCondition, a.Title())
if err != nil {
return nil, errors.Wrap(err, "failed to analyze block devices")
}

return []*AnalyzeResult{&result}, nil
return results, nil
}

// <regexp> <op> <count>
Expand Down Expand Up @@ -205,3 +139,14 @@ func isEligibleBlockDevice(rx *regexp.Regexp, minimumAcceptableSize uint64, incl

return true
}

func (a *AnalyzeHostBlockDevices) CheckCondition(when string, data []byte) (bool, error) {

var devices []collect.BlockDeviceInfo
if err := json.Unmarshal(data, &devices); err != nil {
return false, errors.Wrap(err, "failed to unmarshal block devices info")
}

return compareHostBlockDevicesConditionalToActual(when, a.hostAnalyzer.MinimumAcceptableSize, a.hostAnalyzer.IncludeUnmountedPartitions, devices)

}
Loading

0 comments on commit c5e660e

Please sign in to comment.