Merge pull request #1536 from VaibhavMalik4187/printer/pdf-tests

Enhancement and Test Suite for Printer Functions
This commit is contained in:
Matthias Bertschy
2023-11-26 22:02:03 +01:00
committed by GitHub
8 changed files with 382 additions and 0 deletions

View File

@@ -44,6 +44,13 @@ func (jp *JsonPrinter) SetWriter(ctx context.Context, outputFile string) {
}
func (jp *JsonPrinter) Score(score float32) {
// Handle invalid scores
if score > 100 {
score = 100
} else if score < 0 {
score = 0
}
fmt.Fprintf(os.Stderr, "\nOverall compliance-score (100- Excellent, 0- All failed): %d\n", cautils.Float32ToInt(score))
}

View File

@@ -0,0 +1,85 @@
package printer
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewJsonPrinter(t *testing.T) {
pp := NewJsonPrinter()
assert.NotNil(t, pp)
assert.Empty(t, pp)
}
func TestScore_Json(t *testing.T) {
tests := []struct {
name string
score float32
want string
}{
{
name: "Score not an integer",
score: 20.7,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 21\n",
},
{
name: "Score less than 0",
score: -20.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 0\n",
},
{
name: "Score greater than 100",
score: 120.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 100\n",
},
{
name: "Score 50",
score: 50.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 50\n",
},
{
name: "Zero Score",
score: 0.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 0\n",
},
{
name: "Perfect Score",
score: 100,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 100\n",
},
}
jp := NewJsonPrinter()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary file to capture output
f, err := os.CreateTemp("", "pdfPrinter-score-output")
if err != nil {
panic(err)
}
defer f.Close()
// Redirect stderr to the temporary file
oldStderr := os.Stderr
defer func() {
os.Stderr = oldStderr
}()
os.Stderr = f
// Print the score using the `Score` function
jp.Score(tt.score)
// Read the contents of the temporary file
f.Seek(0, 0)
got, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
assert.Equal(t, tt.want, string(got))
})
}
}

View File

@@ -111,6 +111,13 @@ func (jp *JunitPrinter) SetWriter(ctx context.Context, outputFile string) {
}
func (jp *JunitPrinter) Score(score float32) {
// Handle invalid scores
if score > 100 {
score = 100
} else if score < 0 {
score = 0
}
fmt.Fprintf(os.Stderr, "\nOverall compliance-score (100- Excellent, 0- All failed): %d\n", cautils.Float32ToInt(score))
}

View File

@@ -0,0 +1,91 @@
package printer
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJunitPdfPrinter(t *testing.T) {
// Verbose mode off
jp := NewJunitPrinter(false)
assert.NotNil(t, jp)
assert.Equal(t, false, jp.verbose)
// Verbose mode on
jp = NewJunitPrinter(true)
assert.NotNil(t, jp)
assert.Equal(t, true, jp.verbose)
}
func TestScore_Junit(t *testing.T) {
tests := []struct {
name string
score float32
want string
}{
{
name: "Score not an integer",
score: 20.7,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 21\n",
},
{
name: "Score less than 0",
score: -20.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 0\n",
},
{
name: "Score greater than 100",
score: 120.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 100\n",
},
{
name: "Score 50",
score: 50.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 50\n",
},
{
name: "Zero Score",
score: 0.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 0\n",
},
{
name: "Perfect Score",
score: 100,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 100\n",
},
}
jp := NewJunitPrinter(false)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary file to capture output
f, err := os.CreateTemp("", "score-output")
if err != nil {
panic(err)
}
defer f.Close()
// Redirect stderr to the temporary file
oldStderr := os.Stderr
defer func() {
os.Stderr = oldStderr
}()
os.Stderr = f
// Print the score using the `Score` function
jp.Score(tt.score)
// Read the contents of the temporary file
f.Seek(0, 0)
got, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
assert.Equal(t, tt.want, string(got))
})
}
}

View File

@@ -57,6 +57,13 @@ func (pp *PdfPrinter) SetWriter(ctx context.Context, outputFile string) {
}
func (pp *PdfPrinter) Score(score float32) {
// Handle invalid scores
if score > 100 {
score = 100
} else if score < 0 {
score = 0
}
fmt.Fprintf(os.Stderr, "\nOverall compliance-score (100- Excellent, 0- All failed): %d\n", cautils.Float32ToInt(score))
}
func (pp *PdfPrinter) printInfo(m pdf.Maroto, summaryDetails *reportsummary.SummaryDetails, infoMap []infoStars) {

View File

@@ -0,0 +1,85 @@
package printer
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewPdfPrinter(t *testing.T) {
pp := NewPdfPrinter()
assert.NotNil(t, pp)
assert.Empty(t, pp)
}
func TestScore_Pdf(t *testing.T) {
tests := []struct {
name string
score float32
want string
}{
{
name: "Score not an integer",
score: 20.7,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 21\n",
},
{
name: "Score less than 0",
score: -20.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 0\n",
},
{
name: "Score greater than 100",
score: 120.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 100\n",
},
{
name: "Score 50",
score: 50.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 50\n",
},
{
name: "Zero Score",
score: 0.0,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 0\n",
},
{
name: "Perfect Score",
score: 100,
want: "\nOverall compliance-score (100- Excellent, 0- All failed): 100\n",
},
}
pp := NewPdfPrinter()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary file to capture output
f, err := os.CreateTemp("", "pdfPrinter-score-output")
if err != nil {
panic(err)
}
defer f.Close()
// Redirect stderr to the temporary file
oldStderr := os.Stderr
defer func() {
os.Stderr = oldStderr
}()
os.Stderr = f
// Print the score using the `Score` function
pp.Score(tt.score)
// Read the contents of the temporary file
f.Seek(0, 0)
got, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
assert.Equal(t, tt.want, string(got))
})
}
}

View File

@@ -37,6 +37,13 @@ func (pp *PrometheusPrinter) SetWriter(ctx context.Context, outputFile string) {
}
func (pp *PrometheusPrinter) Score(score float32) {
// Handle invalid scores
if score > 100 {
score = 100
} else if score < 0 {
score = 0
}
fmt.Printf("\n# Overall compliance-score (100- Excellent, 0- All failed)\nkubescape_score %d\n", cautils.Float32ToInt(score))
}

View File

@@ -0,0 +1,93 @@
package printer
import (
"context"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewPrometheusPrinter(t *testing.T) {
// For verbose mode false
verboseMode := false
promPrinter := NewPrometheusPrinter(verboseMode)
assert.NotNil(t, promPrinter)
assert.Equal(t, verboseMode, promPrinter.verboseMode)
// For verbose mode true
verboseMode = true
promPrinter = NewPrometheusPrinter(verboseMode)
assert.NotNil(t, promPrinter)
assert.Equal(t, verboseMode, promPrinter.verboseMode)
}
func TestSetWriter(t *testing.T) {
// Test case 1: Empty outputFile
outputFile := ""
promPrinter := &PrometheusPrinter{}
promPrinter.SetWriter(context.Background(), outputFile)
assert.Equal(t, os.Stdout, promPrinter.writer)
// Test case 2: Valid outputFile
outputFile = "/tmp/test.log"
promPrinter = &PrometheusPrinter{}
promPrinter.SetWriter(context.Background(), outputFile)
f, err := os.Open(outputFile)
assert.NoError(t, err)
defer f.Close()
assert.NotNil(t, promPrinter.writer)
}
func TestScore(t *testing.T) {
tests := []struct {
name string
score float32
want string
}{
{
name: "Score less than 0",
score: -20.0,
want: "\n# Overall compliance-score (100- Excellent, 0- All failed)\nkubescape_score 0\n",
},
{
name: "Score greater than 100",
score: 120.0,
want: "\n# Overall compliance-score (100- Excellent, 0- All failed)\nkubescape_score 100\n",
},
{
name: "Score 50",
score: 50.0,
want: "\n# Overall compliance-score (100- Excellent, 0- All failed)\nkubescape_score 50\n",
},
{
name: "Zero Score",
score: 0.0,
want: "\n# Overall compliance-score (100- Excellent, 0- All failed)\nkubescape_score 0\n",
},
{
name: "Perfect Score",
score: 100,
want: "\n# Overall compliance-score (100- Excellent, 0- All failed)\nkubescape_score 100\n",
},
}
promPrinter := NewPrometheusPrinter(false)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Redirect stdout to a buffer
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
promPrinter.Score(tt.score)
w.Close()
got, _ := io.ReadAll(r)
os.Stdout = rescueStdout
assert.Equal(t, tt.want, string(got))
})
}
}