Skip to content

Commit

Permalink
Issue 241 generate also png for dot output (#274)
Browse files Browse the repository at this point in the history
* code update - when format is dot generating also png file

* new/overridden graphs from running 'make test-update'

* update comment on make test-update

* merge

* adding svg graphs too + warning if dot exec is not found

* warn if graphviz is not installed only
  • Loading branch information
shireenf-ibm authored Nov 20, 2023
1 parent 0df3561 commit c9e1d07
Show file tree
Hide file tree
Showing 27 changed files with 3,611 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test:
@echo -- $@ --
go test ./... -v -cover -coverprofile netpolicy.coverprofile

test-update: # overrides/ generates tests' expected output files for relevant tests
test-update: # overrides/ generates tests' expected output files for relevant tests
# if the format is dot - generates also png files
@echo -- $@ --
go test ./pkg/netpol/connlist/ ./pkg/netpol/diff/ --args --update
32 changes: 32 additions & 0 deletions pkg/internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/np-guard/netpol-analyzer/pkg/internal/output"
"github.com/np-guard/netpol-analyzer/pkg/logger"
)

// a flag for writing/overriding the golden result files for tests
Expand Down Expand Up @@ -53,6 +55,11 @@ func CheckActualVsExpectedOutputMatch(t *testing.T, testName, dirName, expectedO
if *update {
err := output.WriteToFile(actualOutput, expectedOutputFile)
require.Nil(t, err, testInfo)
// if format is dot - generate/ override also png graph file using graphviz program
if strings.HasSuffix(expectedOutputFile, dotSign+output.DOTFormat) {
err = generateGraphFilesIfPossible(expectedOutputFile)
require.Nil(t, err, testInfo)
}
return
}
// read expected output file
Expand Down Expand Up @@ -80,3 +87,28 @@ func CheckErrorContainment(t *testing.T, testInfo, expectedErrorMsg, actualErrMs
require.Contains(t, actualErrMsg, expectedErrorMsg, "err/warn message mismatch for test: %q, actual: %q, expected contains: %q",
testInfo, actualErrMsg, expectedErrorMsg)
}

const (
// the executable we need from graphviz is "dot"
executableNameForGraphviz = output.DOTFormat
dotSign = "."
)

var graphsSuffixes = []string{"png", "svg"}

// checks if "graphviz" executable exists, if yes runs a cmd to generates a png graph file from the dot output
func generateGraphFilesIfPossible(dotFilePath string) error {
// check if graphviz is installed to continue
if _, err := exec.LookPath(executableNameForGraphviz); err != nil {
logger.NewDefaultLogger().Warnf("dot executable of graphviz was not found. Output Graphs will not be generated")
return nil
}
for _, graphSuffix := range graphsSuffixes {
graphFilePath := dotFilePath + dotSign + graphSuffix
cmd := exec.Command("dot", dotFilePath, "-T"+graphSuffix, "-o", graphFilePath) //nolint:gosec // nosec
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
182 changes: 182 additions & 0 deletions tests/acs-security-demos/connlist_output.dot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/acs_security_frontend_demos/connlist_output.dot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c9e1d07

Please sign in to comment.