Skip to content

Commit

Permalink
Adding test for archive_security_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
hush-hush committed Nov 24, 2022
1 parent 27410ea commit 57ee25b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
8 changes: 6 additions & 2 deletions comp/core/flare/helpers/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,12 @@ func (fb *builder) copyFileTo(shouldScrub bool, srcFile string, destDir string,
}
}

return os.WriteFile(path, content, os.ModePerm)
err = os.WriteFile(path, content, os.ModePerm)
if err != nil {
return fb.logError("error writing file '%s': %s", destFile, err)
}

return nil
}

func (fb *builder) CopyFileTo(srcFile string, destDir string) error {
Expand All @@ -234,7 +239,6 @@ func (fb *builder) copyDirTo(shouldScrub bool, srcDir string, destDir string, sh
}
fb.permsInfos.add(srcDir) //nolint:errcheck

fmt.Printf("walk: %s\n", srcDir)
err = filepath.Walk(srcDir, func(src string, f os.FileInfo, err error) error {
if f == nil {
return nil
Expand Down
14 changes: 9 additions & 5 deletions pkg/flare/archive_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ func CreateSecurityAgentArchive(local bool, logFilePath string, runtimeStatus, c
if err != nil {
return "", err
}
createSecurityAgentArchive(fb, local, logFilePath, runtimeStatus, complianceStatus)

return fb.Save()
}

// createSecurityAgentArchive packages up the files
func createSecurityAgentArchive(fb flarehelpers.FlareBuilder, local bool, logFilePath string, runtimeStatus, complianceStatus map[string]interface{}) {
// If the request against the API does not go through we don't collect the status log.
if local {
fb.AddFile("local", []byte(""))
Expand All @@ -32,7 +38,7 @@ func CreateSecurityAgentArchive(local bool, logFilePath string, runtimeStatus, c
})
if err != nil {
log.Infof("Error getting the status of the Security Agent, %q", err)
return "", err
return
}
}

Expand All @@ -48,8 +54,6 @@ func CreateSecurityAgentArchive(local bool, logFilePath string, runtimeStatus, c
getLinuxKprobeEvents(fb)
getLinuxTracingAvailableEvents(fb)
getLinuxTracingAvailableFilterFunctions(fb)

return fb.Save()
}

func getComplianceFiles(fb flarehelpers.FlareBuilder) error {
Expand All @@ -60,7 +64,7 @@ func getComplianceFiles(fb flarehelpers.FlareBuilder) error {
if err != nil {
return false
}
return f.Mode()&os.ModeSymlink != 0
return f.Mode()&os.ModeSymlink == 0
})
}

Expand All @@ -72,6 +76,6 @@ func getRuntimeFiles(fb flarehelpers.FlareBuilder) error {
if err != nil {
return false
}
return f.Mode()&os.ModeSymlink != 0
return f.Mode()&os.ModeSymlink == 0
})
}
26 changes: 4 additions & 22 deletions pkg/flare/archive_security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@
package flare

import (
"archive/zip"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/DataDog/datadog-agent/cmd/agent/common"
flarehelpers "github.com/DataDog/datadog-agent/comp/core/flare/helpers"
"github.com/DataDog/datadog-agent/pkg/config"
)

func TestCreateSecurityAgentArchive(t *testing.T) {
assert := assert.New(t)

common.SetupConfig("./test")
mockConfig := config.Mock(t)
mockConfig.Set("compliance_config.dir", "./test/compliance.d")
Expand Down Expand Up @@ -54,23 +48,11 @@ func TestCreateSecurityAgentArchive(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
zipFilePath, err := CreateSecurityAgentArchive(test.local, logFilePath, nil, nil)
defer os.Remove(zipFilePath)

assert.NoError(err)

// asserts that it as indeed created a permissions.log file
z, err := zip.OpenReader(zipFilePath)
assert.NoError(err, "opening the zip shouldn't pop an error")

var fileNames []string
for _, f := range z.File {
fileNames = append(fileNames, f.Name)
}
mock := flarehelpers.NewFlareBuilderMock(t)
createSecurityAgentArchive(mock.Fb, test.local, logFilePath, nil, nil)

dir := fileNames[0]
for _, f := range test.expectedFiles {
assert.Contains(fileNames, filepath.Join(dir, f))
mock.AssertFileExists(f)
}
})
}
Expand Down

0 comments on commit 57ee25b

Please sign in to comment.