Skip to content

Commit

Permalink
Adding GetManifestFiles() and refactoring (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
manasachi committed Jul 30, 2024
1 parent 0108a19 commit e81cf9c
Show file tree
Hide file tree
Showing 16 changed files with 540 additions and 399 deletions.
24 changes: 6 additions & 18 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"context"
"fmt"
"path"

"github.com/Azure/draft/pkg/safeguards"
"github.com/Azure/draft/pkg/safeguards/preprocessing"
"github.com/Azure/draft/pkg/safeguards/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -55,24 +56,11 @@ func (vc *validateCmd) run(c *cobra.Command) error {
}

ctx := context.Background()
isDir, err := safeguards.IsDirectory(vc.manifestPath)
if err != nil {
return fmt.Errorf("not a valid file or directory: %w", err)
}

var manifestFiles []safeguards.ManifestFile
if isDir {
manifestFiles, err = safeguards.GetManifestFiles(vc.manifestPath)
if err != nil {
return err
}
} else if safeguards.IsYAML(vc.manifestPath) {
manifestFiles = append(manifestFiles, safeguards.ManifestFile{
Name: path.Base(vc.manifestPath),
Path: vc.manifestPath,
})
} else {
return fmt.Errorf("expected at least one .yaml or .yml file within given path")
var manifestFiles []types.ManifestFile
manifestFiles, err := preprocessing.GetManifestFiles(vc.manifestPath)
if err != nil {
return fmt.Errorf("error retrieving manifest files: %w", err)
}

log.Debugf("validating manifests")
Expand Down
78 changes: 30 additions & 48 deletions cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,77 @@ package cmd

import (
"context"
"os"
"path"
"path/filepath"

"testing"

"github.com/Azure/draft/pkg/safeguards"
"github.com/Azure/draft/pkg/safeguards/preprocessing"
"github.com/stretchr/testify/assert"
)

// TestIsDirectory tests the isDirectory function for proper returns
func TestIsDirectory(t *testing.T) {
testWd, _ := os.Getwd()
pathTrue := testWd
pathFalse := path.Join(testWd, "validate.go")
pathError := ""

isDir, err := safeguards.IsDirectory(pathTrue)
assert.True(t, isDir)
assert.Nil(t, err)

isDir, err = safeguards.IsDirectory(pathFalse)
assert.False(t, isDir)
assert.Nil(t, err)

isDir, err = safeguards.IsDirectory(pathError)
assert.False(t, isDir)
assert.NotNil(t, err)
}

// TestIsYAML tests the isYAML function for proper returns
func TestIsYAML(t *testing.T) {
dirNotYaml, _ := filepath.Abs("../pkg/safeguards/tests/not-yaml")
dirYaml, _ := filepath.Abs("../pkg/safeguards/tests/all/success")
fileNotYaml, _ := filepath.Abs("../pkg/safeguards/tests/not-yaml/readme.md")
fileYaml, _ := filepath.Abs("../pkg/safeguards/tests/all/success/all-success-manifest-1.yaml")

assert.False(t, safeguards.IsYAML(fileNotYaml))
assert.True(t, safeguards.IsYAML(fileYaml))

manifestFiles, err := safeguards.GetManifestFiles(dirNotYaml)
assert.Nil(t, manifestFiles)
assert.NotNil(t, err)

manifestFiles, err = safeguards.GetManifestFiles(dirYaml)
assert.NotNil(t, manifestFiles)
assert.Nil(t, err)
}
"github.com/Azure/draft/pkg/safeguards/types"
)

// TestRunValidate tests the run command for `draft validate` for proper returns
func TestRunValidate(t *testing.T) {
ctx := context.TODO()
manifestFilesEmpty := []safeguards.ManifestFile{}
manifestFilesEmpty := []types.ManifestFile{}
manifestPathDirectorySuccess, _ := filepath.Abs("../pkg/safeguards/tests/all/success")
manifestPathDirectoryError, _ := filepath.Abs("../pkg/safeguards/tests/all/error")
manifestPathFileSuccess, _ := filepath.Abs("../pkg/safeguards/tests/all/success/all-success-manifest-1.yaml")
manifestPathFileError, _ := filepath.Abs("../pkg/safeguards/tests/all/error/all-error-manifest-1.yaml")
var manifestFiles []safeguards.ManifestFile
var manifestFiles []types.ManifestFile

// Scenario 1: empty manifest path should error
_, err := safeguards.GetManifestResults(ctx, manifestFilesEmpty)
assert.NotNil(t, err)

// Scenario 2a: manifest path leads to a directory of manifestFiles - expect success
manifestFiles, err = safeguards.GetManifestFiles(manifestPathDirectorySuccess)
manifestFiles, err = preprocessing.GetManifestFiles(manifestPathDirectorySuccess)
assert.Nil(t, err)
v, err := safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
numViolations := countTestViolations(v)
assert.Equal(t, numViolations, 0)

// Scenario 2b: manifest path leads to a directory of manifestFiles - expect failure
manifestFiles, err = safeguards.GetManifestFiles(manifestPathDirectoryError)
manifestFiles, err = preprocessing.GetManifestFiles(manifestPathDirectoryError)
assert.Nil(t, err)
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
numViolations = countTestViolations(v)
assert.Greater(t, numViolations, 0)

// Scenario 3a: manifest path leads to one manifest file - expect success
manifestFiles, err = safeguards.GetManifestFiles(manifestPathFileSuccess)
manifestFiles, err = preprocessing.GetManifestFiles(manifestPathFileSuccess)
assert.Nil(t, err)
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
numViolations = countTestViolations(v)
assert.Equal(t, numViolations, 0)

// Scenario 3b: manifest path leads to one manifest file - expect failure
manifestFiles, err = safeguards.GetManifestFiles(manifestPathFileError)
manifestFiles, err = preprocessing.GetManifestFiles(manifestPathFileError)
assert.Nil(t, err)
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
numViolations = countTestViolations(v)
assert.Greater(t, numViolations, 0)

// Scenario 4: Test Helm
makeTempDir(t)
t.Cleanup(func() { cleanupDir(t, tempDir) })

manifestFiles, err = preprocessing.GetManifestFiles(chartPath)
assert.Nil(t, err)
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
numViolations = countTestViolations(v)
assert.Greater(t, numViolations, 0)

//Scenario 5: Test Kustomize
manifestFiles, err = preprocessing.GetManifestFiles(kustomizationPath)
assert.Nil(t, err)
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
numViolations = countTestViolations(v)
Expand All @@ -106,7 +88,7 @@ func TestRunValidate_Kustomize(t *testing.T) {
makeTempDir(t)
t.Cleanup(func() { cleanupDir(t, tempDir) })

var manifestFiles []safeguards.ManifestFile
var manifestFiles []types.ManifestFile
var err error

// Scenario 1a: kustomizationPath leads to a directory containing kustomization.yaml - expect success
Expand Down
9 changes: 7 additions & 2 deletions cmd/validate_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import (
"path/filepath"
"testing"

"github.com/Azure/draft/pkg/safeguards"
types "github.com/Azure/draft/pkg/safeguards/types"
)

var tempDir, _ = filepath.Abs("./testdata")

func countTestViolations(results []safeguards.ManifestResult) int {
const (
chartPath = "../pkg/safeguards/tests/testmanifests/validchart"
kustomizationPath = "../pkg/safeguards/tests/kustomize/overlays/production"
)

func countTestViolations(results []types.ManifestResult) int {
numViolations := 0
for _, r := range results {
numViolations += len(r.ObjectViolations)
Expand Down
72 changes: 0 additions & 72 deletions pkg/safeguards/constants.go

This file was deleted.

Loading

0 comments on commit e81cf9c

Please sign in to comment.