From 899deb33f77a0b7d4d9face1d1a1b7c2f5c736e7 Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Fri, 14 Apr 2023 14:31:12 +0200 Subject: [PATCH] feat(refacto): added examples --- plugins/commons/yatas.go | 44 +++++++++++++++++++ plugins/commons/yatas_test.go | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/plugins/commons/yatas.go b/plugins/commons/yatas.go index 9d057b6..9ee7e3e 100644 --- a/plugins/commons/yatas.go +++ b/plugins/commons/yatas.go @@ -24,3 +24,47 @@ func CheckMacroTest[A, B, C, D any](wg *sync.WaitGroup, config *Config, test fun return test } + +type CheckFunc func(interface{}) Result + +type Resource interface { + GetID() string +} + +type CheckDefinition struct { + Title string + Description string + Categories []string + ConditionFn func(Resource) bool + SuccessMessage string + FailureMessage string +} + +func CheckResources(checkConfig CheckConfig, resources []Resource, checkDefinitions []CheckDefinition) { + for _, checkDefinition := range checkDefinitions { + if !checkConfig.ConfigYatas.CheckExclude(checkDefinition.Title) && checkConfig.ConfigYatas.CheckInclude(checkDefinition.Title) { + check := createCheck(checkDefinition) + for _, resource := range resources { + result := checkResource(resource, checkDefinition.ConditionFn, checkDefinition.SuccessMessage, checkDefinition.FailureMessage) + check.AddResult(result) + } + checkConfig.Queue <- check + } + } +} + +func createCheck(checkDefinition CheckDefinition) Check { + var check Check + check.InitCheck(checkDefinition.Description, checkDefinition.Description, checkDefinition.Title, checkDefinition.Categories) + return check +} + +func checkResource(resource Resource, conditionFn func(Resource) bool, successMessage, failureMessage string) Result { + if conditionFn(resource) { + message := successMessage + " - Resource " + resource.GetID() + return Result{Status: "OK", Message: message, ResourceID: resource.GetID()} + } else { + message := failureMessage + " - Resource " + resource.GetID() + return Result{Status: "FAIL", Message: message, ResourceID: resource.GetID()} + } +} diff --git a/plugins/commons/yatas_test.go b/plugins/commons/yatas_test.go index 91aa528..81aef82 100644 --- a/plugins/commons/yatas_test.go +++ b/plugins/commons/yatas_test.go @@ -224,3 +224,83 @@ func TestCheckMacroTest(t *testing.T) { wrappedTest(1, 2, 3, 4) wg.Wait() } + +// MockResource to be used for testing +type MockResource struct { + ID string +} + +func (m *MockResource) GetID() string { + return m.ID +} + +func TestCreateCheck(t *testing.T) { + checkDefinition := CheckDefinition{ + Title: "AWS_TEST_001", + Description: "Test check", + Categories: []string{"Test"}, + ConditionFn: nil, + SuccessMessage: "Test check succeeded", + FailureMessage: "Test check failed", + } + + check := createCheck(checkDefinition) + + if check.Description != checkDefinition.Description { + t.Errorf("Expected Description: %v, got: %v", checkDefinition.Description, check.Description) + } + + if check.Id != checkDefinition.Title { + t.Errorf("Expected Title: %v, got: %v", checkDefinition.Title, check.Id) + } + + for i, tag := range check.Categories { + if tag != checkDefinition.Categories[i] { + t.Errorf("Expected Tag: %v, got: %v", checkDefinition.Categories[i], tag) + } + } +} + +func TestCheckResource(t *testing.T) { + resource := &MockResource{ID: "test-resource-id"} + conditionFn := func(r Resource) bool { + return r.GetID() == "test-resource-id" + } + successMessage := "Resource check succeeded" + failureMessage := "Resource check failed" + + result := checkResource(resource, conditionFn, successMessage, failureMessage) + + if result.Status != "OK" { + t.Errorf("Expected Status: OK, got: %v", result.Status) + } + + expectedMessage := successMessage + " - Resource " + resource.GetID() + if result.Message != expectedMessage { + t.Errorf("Expected Message: %v, got: %v", expectedMessage, result.Message) + } + + if result.ResourceID != resource.GetID() { + t.Errorf("Expected ResourceID: %v, got: %v", resource.GetID(), result.ResourceID) + } + + // Test failing condition + conditionFn = func(r Resource) bool { + return r.GetID() != "test-resource-id" + } + + result = checkResource(resource, conditionFn, successMessage, failureMessage) + + if result.Status != "FAIL" { + t.Errorf("Expected Status: FAIL, got: %v", result.Status) + } + + expectedMessage = failureMessage + " - Resource " + resource.GetID() + if result.Message != expectedMessage { + t.Errorf("Expected Message: %v, got: %v", expectedMessage, result.Message) + } + + if result.ResourceID != resource.GetID() { + t.Errorf("Expected ResourceID: %v, got: %v", resource.GetID(), result.ResourceID) + } +}