Skip to content

Commit

Permalink
WIP3
Browse files Browse the repository at this point in the history
  • Loading branch information
rtorrero committed Sep 25, 2023
1 parent fba3c06 commit 32b521c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 64 deletions.
65 changes: 34 additions & 31 deletions internal/factsengine/gatherers/saptune.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gatherers

import (
"encoding/json"
"strings"

log "github.com/sirupsen/logrus"
"github.com/trento-project/agent/internal/core/saptune"
Expand All @@ -15,12 +14,12 @@ const (
)

// nolint:gochecknoglobals
var whitelistedArguments = map[string]string{
"status": "status --non-compliance-check",
"solution-verify": "solution verify",
"solution-list": "solution list",
"note-verify": "note verify",
"note-list": "note list",
var whitelistedArguments = map[string][]string{
"status": {"status", "--non-compliance-check"},
"solution-verify": {"solution", "verify"},
"solution-list": {"solution", "list"},
"note-verify": {"note", "verify"},
"note-list": {"note", "list"},
}

// nolint:gochecknoglobals
Expand All @@ -44,7 +43,6 @@ var (
Type: "saptune-cmd-error",
Message: "error executing saptune command",
}

)

type SaptuneGatherer struct {
Expand All @@ -61,7 +59,7 @@ func NewSaptuneGatherer(executor utils.CommandExecutor) *SaptuneGatherer {
}
}

func parseJSONToFactValue(jsonStr string) (entities.FactValue, error) {
func parseJSONToFactValue(jsonStr json.RawMessage) (entities.FactValue, error) {
// Unmarshal the JSON into an interface{} type.
var jsonData interface{}
if err := json.Unmarshal([]byte(jsonStr), &jsonData); err != nil {
Expand All @@ -77,14 +75,31 @@ func (s *SaptuneGatherer) Gather(factsRequests []entities.FactRequest) ([]entiti
log.Infof("Starting %s facts gathering process", SaptuneGathererName)
saptuneRetriever, _ := saptune.NewSaptune(s.executor)
for _, factReq := range factsRequests {
var fact entities.Fact
if len(factReq.Argument) == 0 {
var fact entities.Fact

internalArguments, ok := whitelistedArguments[factReq.Argument]

switch {
case len(internalArguments) > 0 && !ok:
gatheringError := SaptuneUnknownArgument.Wrap(factReq.Argument)
log.Error(gatheringError)
fact = entities.NewFactGatheredWithError(factReq, gatheringError)

case !saptuneRetriever.IsJSONSupported:
log.Error(SaptuneVersionUnsupported.Message)
fact = entities.NewFactGatheredWithError(factReq, &SaptuneVersionUnsupported)

case len(internalArguments) == 0:
log.Error(SaptuneMissingArgument.Message)
fact = entities.NewFactGatheredWithError(factReq, &SaptuneMissingArgument)
} else if factValue, err := handleArgument(&saptuneRetriever, factReq.Argument); err != nil {
fact = entities.NewFactGatheredWithError(factReq, err)
} else {
fact = entities.NewFactGatheredWithRequest(factReq, factValue)

default:
factValue, err := handleArgument(&saptuneRetriever, internalArguments)
if err != nil {
fact = entities.NewFactGatheredWithError(factReq, err)
} else {
fact = entities.NewFactGatheredWithRequest(factReq, factValue)
}
}
facts = append(facts, fact)
}
Expand All @@ -95,29 +110,17 @@ func (s *SaptuneGatherer) Gather(factsRequests []entities.FactRequest) ([]entiti

func handleArgument(
saptuneRetriever *saptune.Saptune,
argument string,
) (entities.FactValue, *entities.FactGatheringError) {
internalArguments, ok := whitelistedArguments[argument]
arguments []string,
) (entities.FactValue, *entities.FactGatheringError) {

if !ok {
gatheringError := SaptuneUnknownArgument.Wrap(internalArguments)
log.Error(gatheringError)
return nil, gatheringError
}

argList := strings.Split(internalArguments, " ")
saptuneOutput, commandError := saptuneRetriever.RunCommandJSON(argList...)
saptuneOutput, commandError := saptuneRetriever.RunCommandJSON(arguments...)
if commandError != nil {
gatheringError := SaptuneCommandError.Wrap(commandError.Error())
log.Error(gatheringError)
return nil, gatheringError
}

return gatherFactsFromOutput(saptuneOutput)
}

func gatherFactsFromOutput(commandOutput []byte) (entities.FactValue, *entities.FactGatheringError) {
status, err := parseJSONToFactValue(string(commandOutput))
status, err := parseJSONToFactValue(saptuneOutput)
if err != nil {
gatheringError := SaptuneCommandError.Wrap(err.Error())
log.Error(gatheringError)
Expand Down
69 changes: 36 additions & 33 deletions internal/factsengine/gatherers/saptune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererStatus() {

expectedResults := []entities.Fact{
{
Name: "saptune_status",
Name: "saptune_status",
Value: &entities.FactValueMap{
Value: map[string]entities.FactValue{
"$schema": &entities.FactValueString{Value: "file:///usr/share/saptune/schemas/1.0/saptune_status.schema.json"},
"$schema": &entities.FactValueString{Value: "file:///usr/share/saptune/schemas/1.0/saptune_status.schema.json"},
"publish time": &entities.FactValueString{Value: "2023-09-15 15:15:14.599"},
"argv": &entities.FactValueString{Value: "saptune --format json status"},
"pid": &entities.FactValueInt{Value: 6593},
"command": &entities.FactValueString{Value: "status"},
"exit code": &entities.FactValueInt{Value: 1},
"argv": &entities.FactValueString{Value: "saptune --format json status"},
"pid": &entities.FactValueInt{Value: 6593},
"command": &entities.FactValueString{Value: "status"},
"exit code": &entities.FactValueInt{Value: 1},
"result": &entities.FactValueMap{
Value: map[string]entities.FactValue{
"services": &entities.FactValueMap{
Expand All @@ -66,17 +66,17 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererStatus() {
},
},
"sapconf": &entities.FactValueList{Value: []entities.FactValue{}},
"tuned": &entities.FactValueList{Value: []entities.FactValue{}},
"tuned": &entities.FactValueList{Value: []entities.FactValue{}},
},
},
"systemd system state": &entities.FactValueString{Value: "degraded"},
"tuning state": &entities.FactValueString{Value: "compliant"},
"virtualization": &entities.FactValueString{Value: "kvm"},
"configured version": &entities.FactValueString{Value: "3"},
"package version": &entities.FactValueString{Value: "3.1.0"},
"Solution enabled": &entities.FactValueList{Value: []entities.FactValue{}},
"systemd system state": &entities.FactValueString{Value: "degraded"},
"tuning state": &entities.FactValueString{Value: "compliant"},
"virtualization": &entities.FactValueString{Value: "kvm"},
"configured version": &entities.FactValueString{Value: "3"},
"package version": &entities.FactValueString{Value: "3.1.0"},
"Solution enabled": &entities.FactValueList{Value: []entities.FactValue{}},
"Notes enabled by Solution": &entities.FactValueList{Value: []entities.FactValue{}},
"Solution applied": &entities.FactValueList{Value: []entities.FactValue{}},
"Solution applied": &entities.FactValueList{Value: []entities.FactValue{}},
"Notes applied by Solution": &entities.FactValueList{Value: []entities.FactValue{}},
"Notes enabled additionally": &entities.FactValueList{
Value: []entities.FactValue{
Expand All @@ -95,8 +95,8 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererStatus() {
},
"staging": &entities.FactValueMap{
Value: map[string]entities.FactValue{
"staging enabled": &entities.FactValueBool{Value: false},
"Notes staged": &entities.FactValueList{Value: []entities.FactValue{}},
"staging enabled": &entities.FactValueBool{Value: false},
"Notes staged": &entities.FactValueList{Value: []entities.FactValue{}},
"Solutions staged": &entities.FactValueList{Value: []entities.FactValue{}},
},
},
Expand All @@ -108,7 +108,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererStatus() {
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"priority": &entities.FactValueString{Value: "NOTICE"},
"message": &entities.FactValueString{Value: "actions.go:85: ATTENTION: You are running a test version (3.1.0 from 2023/08/03) of saptune which is not supported for production use\n"},
"message": &entities.FactValueString{Value: "actions.go:85: ATTENTION: You are running a test version (3.1.0 from 2023/08/03) of saptune which is not supported for production use\n"},
},
},
},
Expand Down Expand Up @@ -143,7 +143,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() {

expectedResults := []entities.Fact{
{
Name: "saptune_note_verify",
Name: "saptune_note_verify",
Value: &entities.FactValueMap{
Value: map[string]entities.FactValue{
"$schema": &entities.FactValueString{
Expand Down Expand Up @@ -282,7 +282,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() {
Value: []entities.FactValue{
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"index": &entities.FactValueInt{Value: 3},
"index": &entities.FactValueInt{Value: 3},
"amendment": &entities.FactValueString{Value: " [3] value is only checked, but NOT set"},
},
},
Expand All @@ -302,12 +302,12 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() {
Value: []entities.FactValue{
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"index": &entities.FactValueInt{Value: 3},
"index": &entities.FactValueInt{Value: 3},
"amendment": &entities.FactValueString{Value: " [3] value is only checked, but NOT set"},
},
},
},
},
},
},
},
&entities.FactValueMap{
Expand Down Expand Up @@ -401,12 +401,12 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() {
Value: []entities.FactValue{
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"index": &entities.FactValueInt{Value: 15},
"index": &entities.FactValueInt{Value: 15},
"amendment": &entities.FactValueString{Value: "[15] the parameter is only used to calculate the size of tmpfs (/dev/shm)"},
},
},
},
},
},
},
},
&entities.FactValueMap{
Expand All @@ -421,12 +421,12 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() {
Value: []entities.FactValue{
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"index": &entities.FactValueInt{Value: 11},
"index": &entities.FactValueInt{Value: 11},
"amendment": &entities.FactValueString{Value: "[11] parameter is additional defined in sysctl config file /boot/sysctl.conf-5.3.18-150300.59.90-default(0x0fffffffffffff00), /boot/sysctl.conf-5.3.18-150300.59.93-default(0x0fffffffffffff00)"},
},
},
},
},
},
},
},
&entities.FactValueMap{
Expand All @@ -441,12 +441,12 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() {
Value: []entities.FactValue{
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"index": &entities.FactValueInt{Value: 11},
"index": &entities.FactValueInt{Value: 11},
"amendment": &entities.FactValueString{Value: "[11] parameter is additional defined in sysctl config file /boot/sysctl.conf-5.3.18-150300.59.90-default(0x0fffffffffffff00), /boot/sysctl.conf-5.3.18-150300.59.93-default(0x0fffffffffffff00)\n [11] parameter is additional defined in sysctl config file /boot/sysctl.conf-5.3.18-150300.59.90-default(0xffffffffffffffff), /boot/sysctl.conf-5.3.18-150300.59.93-default(0xffffffffffffffff)"},
},
},
},
},
},
},
},
// ... represent other verifications similarly.
Expand Down Expand Up @@ -475,33 +475,33 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() {
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"priority": &entities.FactValueString{Value: "NOTICE"},
"message": &entities.FactValueString{Value: "actions.go:85: ATTENTION: You are running a test version (3.1.0 from 2022/11/28) of saptune which is not supported for production use\n"},
"message": &entities.FactValueString{Value: "actions.go:85: ATTENTION: You are running a test version (3.1.0 from 2022/11/28) of saptune which is not supported for production use\n"},
},
},
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"priority": &entities.FactValueString{Value: "WARNING"},
"message": &entities.FactValueString{Value: "sysctl.go:73: Parameter 'kernel.shmmax' additional defined in the following sysctl config file /boot/sysctl.conf-5.3.18-150300.59.90-default(0xffffffffffffffff), /boot/sysctl.conf-5.3.18-150300.59.93-default(0xffffffffffffffff).\n"},
"message": &entities.FactValueString{Value: "sysctl.go:73: Parameter 'kernel.shmmax' additional defined in the following sysctl config file /boot/sysctl.conf-5.3.18-150300.59.90-default(0xffffffffffffffff), /boot/sysctl.conf-5.3.18-150300.59.93-default(0xffffffffffffffff).\n"},
},
},
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"priority": &entities.FactValueString{Value: "WARNING"},
"message": &entities.FactValueString{Value: "sysctl.go:73: Parameter 'kernel.shmall' additional defined in the following sysctl config file /boot/sysctl.conf-5.3.18-150300.59.90-default(0x0fffffffffffff00), /boot/sysctl.conf-5.3.18-150300.59.93-default(0x0fffffffffffff00).\n"},
"message": &entities.FactValueString{Value: "sysctl.go:73: Parameter 'kernel.shmall' additional defined in the following sysctl config file /boot/sysctl.conf-5.3.18-150300.59.90-default(0x0fffffffffffff00), /boot/sysctl.conf-5.3.18-150300.59.93-default(0x0fffffffffffff00).\n"},
},
},
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"priority": &entities.FactValueString{Value: "NOTICE"},
"message": &entities.FactValueString{Value: "ini.go:308: block device related section settings detected: Traversing all block devices can take a considerable amount of time.\n"},
"message": &entities.FactValueString{Value: "ini.go:308: block device related section settings detected: Traversing all block devices can take a considerable amount of time.\n"},
},
},
&entities.FactValueMap{
Value: map[string]entities.FactValue{
"priority": &entities.FactValueString{Value: "ERROR"},
"message": &entities.FactValueString{Value: "system.go:148: The parameters listed above have deviated from SAP/SUSE recommendations.\n\n"},
"message": &entities.FactValueString{Value: "system.go:148: The parameters listed above have deviated from SAP/SUSE recommendations.\n\n"},
},
},
},
},
},
},
Expand All @@ -514,6 +514,9 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() {
}

func (suite *SaptuneTestSuite) TestSaptuneNoArgumentProvided() {
suite.mockExecutor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "saptune").Return(
[]byte("3.1.0"), nil,
)
c := gatherers.NewSaptuneGatherer(suite.mockExecutor)

factRequests := []entities.FactRequest{
Expand Down

0 comments on commit 32b521c

Please sign in to comment.