Skip to content

Commit

Permalink
chore(lint): appease the linter (#746)
Browse files Browse the repository at this point in the history
Co-authored-by: Brandt Keller <[email protected]>
  • Loading branch information
mildwonkey and brandtkeller authored Oct 18, 2024
1 parent 481648f commit bcb2ab0
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 102 deletions.
2 changes: 1 addition & 1 deletion src/cmd/dev/get-resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var getResourcesCmd = &cobra.Command{
Example: getResourcesHelp,
Run: func(cmd *cobra.Command, args []string) {
spinnerMessage := fmt.Sprintf("Getting Resources from %s", getResourcesOpts.InputFile)
spinner := message.NewProgressSpinner(spinnerMessage)
spinner := message.NewProgressSpinner("%s", spinnerMessage)
defer spinner.Stop()

ctx := context.Background()
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/dev/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var validateCmd = &cobra.Command{
Example: validateHelp,
Run: func(cmd *cobra.Command, args []string) {
spinnerMessage := fmt.Sprintf("Validating %s", validateOpts.InputFile)
spinner := message.NewProgressSpinner(spinnerMessage)
spinner := message.NewProgressSpinner("%s", spinnerMessage)
defer spinner.Stop()

ctx := context.Background()
Expand All @@ -60,7 +60,7 @@ var validateCmd = &cobra.Command{
}

// Reset the spinner message
spinner.Updatef(spinnerMessage)
spinner.Updatef("%s", spinnerMessage)

// If a resources file is provided, read the resources file
if validateOpts.ResourcesFile != "" {
Expand Down
13 changes: 7 additions & 6 deletions src/internal/template/template.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package template

import (
"errors"
"fmt"
"os"
"regexp"
Expand Down Expand Up @@ -217,7 +218,7 @@ func CollectTemplatingData(constants map[string]interface{}, variables []Variabl
variablesMissing.WriteString(fmt.Sprintf("sensitive variable %s is missing a value;\n", k))
}
}
message.Debugf(variablesMissing.String())
message.Debug(variablesMissing.String())

return templateData, nil
}
Expand Down Expand Up @@ -346,14 +347,14 @@ func returnUniqueMatches(matches [][]string, captures int) map[string][]string {
// checkForInvalidKeys checks for invalid characters in keys for go text/template
// cannot contain '-' or '.'
func checkForInvalidKeys(constants map[string]interface{}, variables []VariableConfig) error {
var errors strings.Builder
var errs strings.Builder

containsInvalidChars := func(key string) {
if strings.Contains(key, "-") {
errors.WriteString(fmt.Sprintf("invalid key %s - cannot contain '-';", key))
errs.WriteString(fmt.Sprintf("invalid key %s - cannot contain '-';", key))
}
if strings.Contains(key, ".") {
errors.WriteString(fmt.Sprintf("invalid key %s - cannot contain '.';", key))
errs.WriteString(fmt.Sprintf("invalid key %s - cannot contain '.';", key))
}
}

Expand All @@ -375,8 +376,8 @@ func checkForInvalidKeys(constants map[string]interface{}, variables []VariableC
containsInvalidChars(variable.Key)
}

if errors.Len() > 0 {
return fmt.Errorf(errors.String()[:len(errors.String())-1])
if errs.Len() > 0 {
return errors.New(errs.String()[:len(errs.String())-1])
}

return nil
Expand Down
27 changes: 0 additions & 27 deletions src/pkg/common/oscal/assessment-results.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,33 +212,6 @@ func MakeAssessmentResultsDeterministic(assessment *oscalTypes_1_1_2.AssessmentR

}

// findAndSortResults takes a map of results and returns a list of thresholds and a sorted list of results in order of time
func findAndSortResults(resultMap map[string]*oscalTypes_1_1_2.AssessmentResults) ([]*oscalTypes_1_1_2.Result, []*oscalTypes_1_1_2.Result) {

thresholds := make([]*oscalTypes_1_1_2.Result, 0)
sortedResults := make([]*oscalTypes_1_1_2.Result, 0)

for _, assessment := range resultMap {
for _, result := range assessment.Results {
if result.Props != nil {
for _, prop := range *result.Props {
if prop.Name == "threshold" && prop.Value == "true" {
thresholds = append(thresholds, &result)
}
}
}
// Store all results in a non-sorted list
sortedResults = append(sortedResults, &result)
}
}

// Sort the results by start time
slices.SortFunc(sortedResults, func(a, b *oscalTypes_1_1_2.Result) int { return a.Start.Compare(b.Start) })
slices.SortFunc(thresholds, func(a, b *oscalTypes_1_1_2.Result) int { return a.Start.Compare(b.Start) })

return thresholds, sortedResults
}

// filterResults consumes many assessment-results objects and builds out a map of EvalResults filtered by target
// this function looks at the target prop as the key in the map
func FilterResults(resultMap map[string]*oscalTypes_1_1_2.AssessmentResults) map[string]EvalResult {
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/common/validation-store/validation-store.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (v *ValidationStore) RunValidations(ctx context.Context, confirmExecution,
for k, val := range v.validationMap {
completedText := "evaluated"
spinnerMessage := fmt.Sprintf("Running validation %s", k)
spinner := message.NewProgressSpinner(spinnerMessage)
spinner := message.NewProgressSpinner("%s", spinnerMessage)
defer spinner.Stop()
err := val.Validate(ctx, types.ExecutionAllowed(confirmExecution))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func Warnf(format string, a ...any) {
// WarnErr prints an error message as a warning.
func WarnErr(err any, message string) {
debugPrinter(2, err)
Warnf(message)
Warnf("%s", message)
}

// WarnErrf prints an error message as a warning with a given format.
Expand Down Expand Up @@ -261,7 +261,7 @@ func HeaderInfof(format string, a ...any) {
WithBackgroundStyle(pterm.NewStyle(pterm.BgDarkGray)).
WithTextStyle(pterm.NewStyle(pterm.FgLightWhite)).
WithMargin(2).
Printfln(message + strings.Repeat(" ", padding))
Printfln("%s", message+strings.Repeat(" ", padding))
}

// HorizontalRule prints a white horizontal rule to separate the terminal
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/message/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (p *Spinner) Stop() {

// Success prints a success message and stops the spinner.
func (p *Spinner) Success() {
p.Successf(p.startText)
p.Successf("%s", p.startText)
}

// Successf prints a success message with the spinner and stops it.
Expand Down Expand Up @@ -145,7 +145,7 @@ func (p *Spinner) Errorf(err error, format string, a ...any) {

// Fatal calls message.Fatalf with the given error.
func (p *Spinner) Fatal(err error) {
p.Fatalf(err, p.startText)
p.Fatalf(err, "%s", p.startText)
}

// Fatalf calls message.Fatalf with the given error and format.
Expand Down
28 changes: 14 additions & 14 deletions src/test/e2e/api_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package test

import (
"context"
"testing"
"time"

"github.com/defenseunicorns/lula/src/pkg/common/validation"
"github.com/defenseunicorns/lula/src/pkg/message"
"github.com/defenseunicorns/lula/src/test/util"
corev1 "k8s.io/api/core/v1"

// netv1 "k8s.io/api/networking/v1"
// "sigs.k8s.io/e2e-framework/klient/k8s"
"testing"
"time"

"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/klient/wait/conditions"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

func TestApiValidation(t *testing.T) {
const (
ckAPIFieldConfigMap contextKey = "api-field-configmap"
ckApiFieldPod contextKey = "api-field-pod"
)
featureTrueValidation := features.New("Check API Validation - Success").
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
// Create the configmap
Expand All @@ -30,7 +30,7 @@ func TestApiValidation(t *testing.T) {
if err = config.Client().Resources().Create(ctx, configMap); err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-configmap", configMap)
ctx = context.WithValue(ctx, ckAPIFieldConfigMap, configMap)

// Create the pod
pod, err := util.GetPod("./scenarios/api-field/pod.yaml")
Expand All @@ -47,7 +47,7 @@ func TestApiValidation(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-pod", pod)
ctx = context.WithValue(ctx, ckApiFieldPod, pod)

return ctx
}).
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestApiValidation(t *testing.T) {
return ctx
}).
Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
pod := ctx.Value("api-field-pod").(*corev1.Pod)
pod := ctx.Value(ckApiFieldPod).(*corev1.Pod)
if err := config.Client().Resources().Delete(ctx, pod); err != nil {
t.Fatal(err)
}
Expand All @@ -97,7 +97,7 @@ func TestApiValidation(t *testing.T) {
t.Fatal(err)
}

configMap := ctx.Value("api-field-configmap").(*corev1.ConfigMap)
configMap := ctx.Value(ckAPIFieldConfigMap).(*corev1.ConfigMap)
if err := config.Client().Resources().Delete(ctx, configMap); err != nil {
t.Fatal(err)
}
Expand All @@ -121,7 +121,7 @@ func TestApiValidation(t *testing.T) {
if err = config.Client().Resources().Create(ctx, configMap); err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-configmap", configMap)
ctx = context.WithValue(ctx, ckAPIFieldConfigMap, configMap)

pod, err := util.GetPod("./scenarios/api-field/pod.yaml")
if err != nil {
Expand All @@ -137,7 +137,7 @@ func TestApiValidation(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "api-field-pod", pod)
ctx = context.WithValue(ctx, ckApiFieldPod, pod)
return ctx
}).
Assess("Validate API response field", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestApiValidation(t *testing.T) {
return ctx
}).
Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
pod := ctx.Value("api-field-pod").(*corev1.Pod)
pod := ctx.Value(ckApiFieldPod).(*corev1.Pod)
if err := config.Client().Resources().Delete(ctx, pod); err != nil {
t.Fatal(err)
}
Expand All @@ -186,7 +186,7 @@ func TestApiValidation(t *testing.T) {
t.Fatal(err)
}

configMap := ctx.Value("api-field-configmap").(*corev1.ConfigMap)
configMap := ctx.Value(ckAPIFieldConfigMap).(*corev1.ConfigMap)
if err := config.Client().Resources().Delete(ctx, configMap); err != nil {
t.Fatal(err)
}
Expand Down
14 changes: 9 additions & 5 deletions src/test/e2e/dev_get_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
)

func TestGetResources(t *testing.T) {
const (
ckPodGetResources contextKey = "pod-get-resources"
ckCfgGetResources contextKey = "config-get-resources"
)
featureTrueGetResources := features.New("Check dev get-resources").
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
// Create the pod
Expand All @@ -31,7 +35,7 @@ func TestGetResources(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "pod-get-resources", pod)
ctx = context.WithValue(ctx, ckPodGetResources, pod)

// Create the configmap
configMap, err := util.GetConfigMap("./scenarios/dev-get-resources/configmap.yaml")
Expand All @@ -41,7 +45,7 @@ func TestGetResources(t *testing.T) {
if err = config.Client().Resources().Create(ctx, configMap); err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "configmap-get-resources", configMap)
ctx = context.WithValue(ctx, ckCfgGetResources, configMap)

return ctx
}).
Expand Down Expand Up @@ -75,13 +79,13 @@ func TestGetResources(t *testing.T) {
t.Fatal("The nginx-conf resource was not found in the collection")
}

message.Infof("Successfully validated dev get-resources command")
message.Info("Successfully validated dev get-resources command")

return ctx
}).
Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
// Delete the configmap
configMap := ctx.Value("configmap-get-resources").(*corev1.ConfigMap)
configMap := ctx.Value(ckCfgGetResources).(*corev1.ConfigMap)
if err := config.Client().Resources().Delete(ctx, configMap); err != nil {
t.Fatal(err)
}
Expand All @@ -94,7 +98,7 @@ func TestGetResources(t *testing.T) {
}

// Delete the pod
pod := ctx.Value("pod-get-resources").(*corev1.Pod)
pod := ctx.Value(ckPodGetResources).(*corev1.Pod)
if err := config.Client().Resources().Delete(ctx, pod); err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/e2e/dev_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
)

func TestDevValidation(t *testing.T) {
const ckPodDevValidate contextKey = "pod-dev-validate"

featureTrueDevValidate := features.New("Check dev validate").
Setup(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
// Create the pod
Expand All @@ -31,7 +33,7 @@ func TestDevValidation(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ctx = context.WithValue(ctx, "pod-dev-validate", pod)
ctx = context.WithValue(ctx, ckPodDevValidate, pod)

return ctx
}).
Expand Down Expand Up @@ -131,7 +133,7 @@ func TestDevValidation(t *testing.T) {
Teardown(func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {

// Delete the pod
pod := ctx.Value("pod-dev-validate").(*corev1.Pod)
pod := ctx.Value(ckPodDevValidate).(*corev1.Pod)
if err := config.Client().Resources().Delete(ctx, pod); err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestMain(m *testing.M) {
envfuncs.CreateClusterWithConfig(
kind.NewProvider(),
kindClusterName,
"kind-config.yaml"),
"./testdata/kind-config.yaml"),

envfuncs.CreateNamespace(namespace),
)
Expand Down
Loading

0 comments on commit bcb2ab0

Please sign in to comment.