Skip to content

Commit

Permalink
Removing Manifest Path property, replacing with []byte content (#355)
Browse files Browse the repository at this point in the history
Signed-off-by: Manasa Chinta <manasachinta7@gmail.com>
  • Loading branch information
manasachi committed Oct 7, 2024
1 parent 39bc803 commit 2394a6f
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 513 deletions.
3 changes: 1 addition & 2 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"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 @@ -71,7 +70,7 @@ func (vc *validateCmd) run(c *cobra.Command) error {
ctx := context.Background()

var manifestFiles []types.ManifestFile
manifestFiles, err := preprocessing.GetManifestFiles(vc.manifestPath, opt)
manifestFiles, err := safeguards.GetManifestFiles(vc.manifestPath, opt)
if err != nil {
return fmt.Errorf("error retrieving manifest files: %w", err)
}
Expand Down
43 changes: 19 additions & 24 deletions cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"path/filepath"
"testing"

"github.com/Azure/draft/pkg/safeguards"
Expand All @@ -13,14 +12,21 @@ import (
"helm.sh/helm/v3/pkg/chartutil"
)

const (
manifestPathDirectorySuccess = "../pkg/safeguards/tests/all/success"
manifestPathDirectoryError = "../pkg/safeguards/tests/all/error"
manifestPathFileSuccess = "../pkg/safeguards/tests/all/success/all-success-manifest-1.yaml"
manifestPathFileError = "../pkg/safeguards/tests/all/error/all-error-manifest-1.yaml"
kustomizationPath = "../pkg/safeguards/tests/kustomize/overlays/production"
chartPath = "../pkg/safeguards/tests/testmanifests/validchart"
kustomizationFilePath = "../pkg/safeguards/tests/kustomize/overlays/production/kustomization.yaml"
)

// TestRunValidate tests the run command for `draft validate` for proper returns
func TestRunValidate(t *testing.T) {
ctx := context.TODO()
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 []types.ManifestFile
var opt chartutil.ReleaseOptions

Expand All @@ -29,42 +35,39 @@ func TestRunValidate(t *testing.T) {
assert.NotNil(t, err)

// Scenario 2a: manifest path leads to a directory of manifestFiles - expect success
manifestFiles, err = preprocessing.GetManifestFiles(manifestPathDirectorySuccess, opt)
manifestFiles, err = safeguards.GetManifestFiles(manifestPathDirectorySuccess, opt)
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 = preprocessing.GetManifestFiles(manifestPathDirectoryError, opt)
manifestFiles, err = safeguards.GetManifestFiles(manifestPathDirectoryError, opt)
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 = preprocessing.GetManifestFiles(manifestPathFileSuccess, opt)
manifestFiles, err = safeguards.GetManifestFiles(manifestPathFileSuccess, opt)
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 = preprocessing.GetManifestFiles(manifestPathFileError, opt)
manifestFiles, err = safeguards.GetManifestFiles(manifestPathFileError, opt)
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 Kustomize
makeTempDir(t)
t.Cleanup(func() { cleanupDir(t, tempDir) })

manifestFiles, err = preprocessing.GetManifestFiles(kustomizationPath, opt)
manifestFiles, err = safeguards.GetManifestFiles(kustomizationPath, opt)
assert.Nil(t, err)
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
Expand All @@ -74,10 +77,8 @@ func TestRunValidate(t *testing.T) {
// Scenario 5: Test Helm
opt.Name = "test-release-name"
opt.Namespace = "test-release-namespace"
makeTempDir(t)
t.Cleanup(func() { cleanupDir(t, tempDir) })

manifestFiles, err = preprocessing.GetManifestFiles(chartPath, opt)
manifestFiles, err = safeguards.GetManifestFiles(chartPath, opt)
assert.Nil(t, err)
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
Expand All @@ -88,25 +89,19 @@ func TestRunValidate(t *testing.T) {
// TestRunValidate_Kustomize tests the run command for `draft validate` for proper returns when given a kustomize project
func TestRunValidate_Kustomize(t *testing.T) {
ctx := context.TODO()
kustomizationPath, _ := filepath.Abs("../pkg/safeguards/tests/kustomize/overlays/production")
kustomizationFilePath, _ := filepath.Abs("../pkg/safeguards/tests/kustomize/overlays/production/kustomization.yaml")

makeTempDir(t)
t.Cleanup(func() { cleanupDir(t, tempDir) })

var manifestFiles []types.ManifestFile
var err error

// Scenario 1a: kustomizationPath leads to a directory containing kustomization.yaml - expect success
manifestFiles, err = preprocessing.RenderKustomizeManifest(kustomizationPath, tempDir)
manifestFiles, err = preprocessing.RenderKustomizeManifest(kustomizationPath)
assert.Nil(t, err)
v, err := safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
numViolations := countTestViolations(v)
assert.Equal(t, numViolations, 1)

// Scenario 1b: kustomizationFilePath path leads to a specific kustomization.yaml - expect success
manifestFiles, err = preprocessing.RenderKustomizeManifest(kustomizationFilePath, tempDir)
manifestFiles, err = preprocessing.RenderKustomizeManifest(kustomizationFilePath)
assert.Nil(t, err)
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
assert.Nil(t, err)
Expand Down
24 changes: 0 additions & 24 deletions cmd/validate_test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
package cmd

import (
"os"
"path/filepath"
"testing"

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

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

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 {
Expand All @@ -23,16 +12,3 @@ func countTestViolations(results []types.ManifestResult) int {

return numViolations
}

func makeTempDir(t *testing.T) {
if err := os.MkdirAll(tempDir, 0755); err != nil {
t.Fatalf("failed to create temporary output directory: %s", err)
}
}

func cleanupDir(t *testing.T, dir string) {
err := os.RemoveAll(dir)
if err != nil {
t.Fatalf("Failed to clean directory: %s", err)
}
}
Loading

0 comments on commit 2394a6f

Please sign in to comment.