Skip to content

Commit

Permalink
update 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 94947ba commit c05d677
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 69 deletions.
108 changes: 86 additions & 22 deletions pkg/analyze/host_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package analyzer

import (
"encoding/json"
"fmt"
"testing"

"github.com/pkg/errors"
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"
)
Expand Down Expand Up @@ -102,7 +105,7 @@ func TestAnalyzeHostMemory(t *testing.T) {
expectedError string
}{
{
name: "Pass on memory available",
name: "Pass on memory available (local)",
hostAnalyzer: &troubleshootv1beta2.MemoryAnalyze{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Expand All @@ -113,11 +116,15 @@ func TestAnalyzeHostMemory(t *testing.T) {
},
},
},
getCollectedFileContents: func(filename string) ([]byte, error) {
memoryInfo := collect.MemoryInfo{
Total: 8 * 1024 * 1024 * 1024, // 8GiB
getCollectedFileContents: func(path string) ([]byte, error) {
// Simulate local memory content retrieval
if path == collect.HostMemoryPath {
memoryInfo := collect.MemoryInfo{
Total: 8 * 1024 * 1024 * 1024, // 8GiB
}
return json.Marshal(memoryInfo)
}
return json.Marshal(memoryInfo)
return nil, errors.New("file not found")
},
expectedResults: []*AnalyzeResult{
{
Expand All @@ -129,7 +136,7 @@ func TestAnalyzeHostMemory(t *testing.T) {
expectedError: "",
},
{
name: "Fail on memory available",
name: "Fail on memory available (remote node)",
hostAnalyzer: &troubleshootv1beta2.MemoryAnalyze{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Expand All @@ -140,23 +147,31 @@ func TestAnalyzeHostMemory(t *testing.T) {
},
},
},
getCollectedFileContents: func(filename string) ([]byte, error) {
memoryInfo := collect.MemoryInfo{
Total: 8 * 1024 * 1024 * 1024, // 8GiB
getCollectedFileContents: func(path string) ([]byte, error) {
// Simulate remote node list and memory content retrieval
if path == constants.NODE_LIST_FILE {
nodeNames := NodeNames{Nodes: []string{"node1"}}
return json.Marshal(nodeNames)
}
return json.Marshal(memoryInfo)
if path == fmt.Sprintf("%s/node1/%s", collect.NodeInfoBaseDir, collect.HostMemoryFileName) {
memoryInfo := collect.MemoryInfo{
Total: 8 * 1024 * 1024 * 1024, // 8GiB for remote node
}
return json.Marshal(memoryInfo)
}
return nil, errors.New("file not found")
},
expectedResults: []*AnalyzeResult{
{
Title: "Amount of Memory",
Title: "Amount of Memory - Node node1",
IsFail: true,
Message: "System requires at least 16Gi of memory",
},
},
expectedError: "",
},
{
name: "Warn on memory available",
name: "Warn on memory available (remote node)",
hostAnalyzer: &troubleshootv1beta2.MemoryAnalyze{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Expand All @@ -167,23 +182,31 @@ func TestAnalyzeHostMemory(t *testing.T) {
},
},
},
getCollectedFileContents: func(filename string) ([]byte, error) {
memoryInfo := collect.MemoryInfo{
Total: 8 * 1024 * 1024 * 1024, // 8GiB
getCollectedFileContents: func(path string) ([]byte, error) {
// Simulate remote node list and memory content retrieval
if path == constants.NODE_LIST_FILE {
nodeNames := NodeNames{Nodes: []string{"node1"}}
return json.Marshal(nodeNames)
}
return json.Marshal(memoryInfo)
if path == fmt.Sprintf("%s/node1/%s", collect.NodeInfoBaseDir, collect.HostMemoryFileName) {
memoryInfo := collect.MemoryInfo{
Total: 8 * 1024 * 1024 * 1024, // 8GiB for remote node
}
return json.Marshal(memoryInfo)
}
return nil, errors.New("file not found")
},
expectedResults: []*AnalyzeResult{
{
Title: "Amount of Memory",
Title: "Amount of Memory - Node node1",
IsWarn: true,
Message: "System performs best with more than 8Gi of memory",
},
},
expectedError: "",
},
{
name: "Pass on empty pass predicate",
name: "Pass on empty pass predicate (local)",
hostAnalyzer: &troubleshootv1beta2.MemoryAnalyze{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Expand All @@ -200,11 +223,15 @@ func TestAnalyzeHostMemory(t *testing.T) {
},
},
},
getCollectedFileContents: func(filename string) ([]byte, error) {
memoryInfo := collect.MemoryInfo{
Total: 16 * 1024 * 1024 * 1024, // 16GiB
getCollectedFileContents: func(path string) ([]byte, error) {
// Simulate local memory content retrieval
if path == collect.HostMemoryPath {
memoryInfo := collect.MemoryInfo{
Total: 16 * 1024 * 1024 * 1024, // 16GiB
}
return json.Marshal(memoryInfo)
}
return json.Marshal(memoryInfo)
return nil, errors.New("file not found")
},
expectedResults: []*AnalyzeResult{
{
Expand All @@ -215,6 +242,43 @@ func TestAnalyzeHostMemory(t *testing.T) {
},
expectedError: "",
},
{
name: "Fix for empty pass predicate",
hostAnalyzer: &troubleshootv1beta2.MemoryAnalyze{
Outcomes: []*troubleshootv1beta2.Outcome{
{
Fail: &troubleshootv1beta2.SingleOutcome{
When: "< 8Gi",
Message: "oops",
},
},
{
Pass: &troubleshootv1beta2.SingleOutcome{
When: "",
Message: "it passed",
},
},
},
},
getCollectedFileContents: func(path string) ([]byte, error) {
// Simulate local memory content retrieval
if path == collect.HostMemoryPath {
memoryInfo := collect.MemoryInfo{
Total: 16 * 1024 * 1024 * 1024, // 16GiB
}
return json.Marshal(memoryInfo)
}
return nil, errors.New("file not found")
},
expectedResults: []*AnalyzeResult{
{
Title: "Amount of Memory",
IsPass: true,
Message: "it passed",
},
},
expectedError: "",
},
}

for _, test := range tests {
Expand Down
6 changes: 1 addition & 5 deletions pkg/analyze/host_os_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (a *AnalyzeHostOS) CheckCondition(when string, data CollectorData) (bool, e
}

// Match the kernel version regardless of the platform
// e.g "kernelVersion == 4.15"
if parts[0] == "kernelVersion" {
fixedKernelVer := fixVersion(osInfo.KernelVersion)
toleratedKernelVer, err := semver.ParseTolerant(fixedKernelVer)
Expand All @@ -89,8 +88,7 @@ func (a *AnalyzeHostOS) CheckCondition(when string, data CollectorData) (bool, e
}
}

// Match the platform version and and kernel version passed in as
// "<platform>-<kernelVersion>-kernel" e.g "centos-8.2-kernel == 8.2"
// Match platform version and kernel version, such as "centos-8.2-kernel == 8.2"
platform := parts[0]
kernelInfo := fmt.Sprintf("%s-%s-kernel", osInfo.Platform, osInfo.PlatformVersion)
if len(strings.Split(platform, "-")) == 3 && strings.Split(platform, "-")[2] == "kernel" {
Expand All @@ -104,8 +102,6 @@ func (a *AnalyzeHostOS) CheckCondition(when string, data CollectorData) (bool, e
return true, nil
}
}
// Match the platform version
// e.g "centos == 8.2"
} else if platform == osInfo.Platform {
fixedDistVer := fixVersion(osInfo.PlatformVersion)
toleratedDistVer, err := semver.ParseTolerant(fixedDistVer)
Expand Down
Loading

0 comments on commit c05d677

Please sign in to comment.