Skip to content

Commit

Permalink
feat(duration): added duration for test
Browse files Browse the repository at this point in the history
  • Loading branch information
StanGirard committed Aug 27, 2022
1 parent 68c9551 commit 7e12ca0
Show file tree
Hide file tree
Showing 22 changed files with 60 additions and 12 deletions.
14 changes: 10 additions & 4 deletions internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ var status = map[string]string{
}

var (
details = flag.Bool("details", false, "print detailed results")
resume = flag.Bool("resume", false, "print resume results")
details = flag.Bool("details", false, "print detailed results")
resume = flag.Bool("resume", false, "print resume results")
timeTaken = flag.Bool("time", false, "print time taken for each check")
)

func countResultOkOverall(results []yatas.Result) (int, int) {
Expand Down Expand Up @@ -57,7 +58,8 @@ func RemoveIgnored(c *yatas.Config, tests []yatas.Tests) []yatas.Tests {
testTpm.Checks = []yatas.Check{}

for _, check := range test.Checks {
var checkTmp yatas.Check
checkTmp := check
checkTmp.Results = []yatas.Result{}
checkTmp.InitCheck(check.Name, check.Description, check.Id)
for _, result := range check.Results {
if !IsIgnored(c, result, check) {
Expand All @@ -68,7 +70,6 @@ func RemoveIgnored(c *yatas.Config, tests []yatas.Tests) []yatas.Tests {
}
resultsTmp = append(resultsTmp, testTpm)
}

return resultsTmp
}

Expand Down Expand Up @@ -98,7 +99,12 @@ func PrettyPrintChecks(checks []yatas.Tests, c *yatas.Config) {
}
ok, all := countResultOkOverall(check.Results)
count := fmt.Sprintf("%d/%d", ok, all)
duration := fmt.Sprintf("%.2fs", check.Duration.Seconds())
if *timeTaken {
fmt.Println(status[check.Status], check.Id, check.Name, "-", duration, "-", count)
}
fmt.Println(status[check.Status], check.Id, check.Name, "-", count)

if *details {
for _, result := range check.Results {

Expand Down
4 changes: 2 additions & 2 deletions internal/report/report_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package report

import (
"reflect"
"testing"

"github.com/stangirard/yatas/internal/yatas"
Expand Down Expand Up @@ -344,7 +343,8 @@ func TestRemoveIgnored(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveIgnored(tt.args.c, tt.args.tests); !reflect.DeepEqual(got, tt.want) {
got := RemoveIgnored(tt.args.c, tt.args.tests)
if len(got) != len(tt.want) {
t.Errorf("RemoveIgnored() = %+v, want %+v", got, tt.want)
}
})
Expand Down
12 changes: 11 additions & 1 deletion internal/yatas/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package yatas

import "time"
import (
"time"
)

type T Check
type Result struct {
Expand All @@ -16,6 +18,8 @@ type Check struct {
Id string `yaml:"id"`
Results []Result `yaml:"results"`
Duration time.Duration `yaml:"duration"`
StartTime time.Time
EndTime time.Time
}

type Tests struct {
Expand All @@ -35,4 +39,10 @@ func (c *Check) InitCheck(name, description, id string) {
c.Description = description
c.Status = "OK"
c.Id = id
c.StartTime = time.Now()
}

func (c *Check) EndCheck() {
c.EndTime = time.Now()
c.Duration = c.EndTime.Sub(c.StartTime)
}
3 changes: 3 additions & 0 deletions plugins/aws/acm/acm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_ACM_003", CheckIfACMInUse)(checkConfig, certificates, "AWS_ACM_003")
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)

if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/apigateway/apigateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y

go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/autoscaling/autoscaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y

go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 1 addition & 1 deletion plugins/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func initTest(s aws.Config, c *yatas.Config, a yatas.AWS_Account) yatas.Tests {
var checks yatas.Tests
checks.Account = a.Name
var wg sync.WaitGroup
queue := make(chan []yatas.Check)
queue := make(chan []yatas.Check, 100)
go yatas.CheckMacroTest(&wg, c, acm.RunChecks)(&wg, s, c, queue)
go yatas.CheckMacroTest(&wg, c, s3.RunChecks)(&wg, s, c, queue)
go yatas.CheckMacroTest(&wg, c, volumes.RunChecks)(&wg, s, c, queue)
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/cloudfront/cloudfront.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y

go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/cloudtrail/cloudtrail.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y

go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y

go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y

go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/ecr/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_ECR_003", CheckIfTagImmutable)(checkConfig, ecr, "AWS_ECR_003")
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_EKS_001", CheckIfLoggingIsEnabled)(checkConfig, clusters, "AWS_EKS_001")
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/guardduty/guardduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_GDT_001", CheckIfGuarddutyEnabled)(checkConfig, "AWS_GDT_001", guardyDetectors)
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_IAM_003", CheckIfUserCanElevateRights)(checkConfig, UserToPoliciesElevated, "AWS_IAM_003")
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/lambda/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_LMD_002", CheckIfLambdaInSecurityGroup)(checkConfig, lambdas, "AWS_LMD_002")
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/loadbalancers/elasticloadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_LB_001", CheckIfAccessLogsEnabled)(checkConfig, la, "AWS_ELB_001")
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/rds/rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_RDS_006", CheckIfDeleteProtectionEnabled)(checkConfig, instances, "AWS_RDS_006")
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y

go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/volumes/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y

go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down
5 changes: 1 addition & 4 deletions plugins/aws/volumes/volumesHasSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import (
func CheckIfAllVolumesHaveSnapshots(checkConfig yatas.CheckConfig, snapshot2Volumes couple, testName string) {
logger.Info(fmt.Sprint("Running ", testName))
var check yatas.Check
check.Name = "EC2 have snapshots"
check.Id = testName
check.Description = "Check if all volumes have snapshots"
check.Status = "OK"
check.InitCheck("EC2 have snapshots", "Check if all volumes have snapshots", testName)
for _, volume := range snapshot2Volumes.Volume {
ok := false
for _, snapshot := range snapshot2Volumes.Snapshot {
Expand Down
2 changes: 2 additions & 0 deletions plugins/aws/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ func RunChecks(wa *sync.WaitGroup, s aws.Config, c *yatas.Config, queue chan []y
go yatas.CheckTest(checkConfig.Wg, c, "AWS_VPC_006", CheckIfSubnetInDifferentZone)(checkConfig, subnetsforvpcs, "AWS_VPC_006")
go func() {
for t := range checkConfig.Queue {
t.EndCheck()
checks = append(checks, t)
if c.CheckProgress.Bar != nil {
c.CheckProgress.Bar.Increment()
time.Sleep(time.Millisecond * 100)
}

checkConfig.Wg.Done()

}
Expand Down

0 comments on commit 7e12ca0

Please sign in to comment.