Skip to content

Commit 90f534e

Browse files
manasachitbarnes94bfoley13davidgamero
authored
Validate with Helm and Kustomize Support (#392)
Signed-off-by: Manasa Chinta <[email protected]> Co-authored-by: Tommy Barnes <[email protected]> Co-authored-by: Brandon Foley <[email protected]> Co-authored-by: David Gamero <[email protected]>
1 parent f6c230c commit 90f534e

20 files changed

+827
-697
lines changed

cmd/validate.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"path"
76

87
"github.com/Azure/draft/pkg/safeguards"
8+
"github.com/Azure/draft/pkg/safeguards/types"
99
log "github.com/sirupsen/logrus"
1010
"github.com/spf13/cobra"
11+
12+
"helm.sh/helm/v3/pkg/chartutil"
1113
)
1214

1315
type validateCmd struct {
14-
manifestPath string
15-
imagePullSecret bool
16+
manifestPath string
17+
imagePullSecret bool
18+
releaseName string
19+
releaseNamespace string
1620
}
1721

1822
func init() {
@@ -38,6 +42,8 @@ func newValidateCmd() *cobra.Command {
3842

3943
f.StringVarP(&vc.manifestPath, "manifest", "m", "", "'manifest' asks for the path to the manifest")
4044
f.BoolVarP(&vc.imagePullSecret, "imagePullSecret", "s", false, "'imagePullSecret' enables the Safeguard that checks for usage of an image pull secret within the manifest(s)")
45+
f.StringVarP(&vc.releaseName, "releaseName", "n", "", "'releaseName' asks for a user-defined release name for the Helm package to use when rendering Helm projects in Draft")
46+
f.StringVarP(&vc.releaseNamespace, "releaseNamespace", "e", "", "'releaseNamespace' asks for a user-defined release namespace for the Helm package to use when rendering Helm projects in Draft")
4147

4248
return cmd
4349
}
@@ -54,25 +60,19 @@ func (vc *validateCmd) run(c *cobra.Command) error {
5460
safeguards.AddSafeguardCRIP()
5561
}
5662

57-
ctx := context.Background()
58-
isDir, err := safeguards.IsDirectory(vc.manifestPath)
59-
if err != nil {
60-
return fmt.Errorf("not a valid file or directory: %w", err)
63+
var opt chartutil.ReleaseOptions
64+
if vc.releaseName != "" {
65+
opt.Name = vc.releaseName
66+
}
67+
if vc.releaseNamespace != "" {
68+
opt.Namespace = vc.releaseNamespace
6169
}
70+
ctx := context.Background()
6271

63-
var manifestFiles []safeguards.ManifestFile
64-
if isDir {
65-
manifestFiles, err = safeguards.GetManifestFiles(vc.manifestPath)
66-
if err != nil {
67-
return err
68-
}
69-
} else if safeguards.IsYAML(vc.manifestPath) {
70-
manifestFiles = append(manifestFiles, safeguards.ManifestFile{
71-
Name: path.Base(vc.manifestPath),
72-
Path: vc.manifestPath,
73-
})
74-
} else {
75-
return fmt.Errorf("expected at least one .yaml or .yml file within given path")
72+
var manifestFiles []types.ManifestFile
73+
manifestFiles, err := safeguards.GetManifestFiles(vc.manifestPath, opt)
74+
if err != nil {
75+
return fmt.Errorf("error retrieving manifest files: %w", err)
7676
}
7777

7878
log.Debugf("validating manifests")

cmd/validate_test.go

Lines changed: 44 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,95 +2,84 @@ package cmd
22

33
import (
44
"context"
5-
"os"
6-
"path"
7-
"path/filepath"
8-
95
"testing"
106

117
"github.com/Azure/draft/pkg/safeguards"
128
"github.com/Azure/draft/pkg/safeguards/preprocessing"
9+
"github.com/Azure/draft/pkg/safeguards/types"
1310
"github.com/stretchr/testify/assert"
14-
)
15-
16-
// TestIsDirectory tests the isDirectory function for proper returns
17-
func TestIsDirectory(t *testing.T) {
18-
testWd, _ := os.Getwd()
19-
pathTrue := testWd
20-
pathFalse := path.Join(testWd, "validate.go")
21-
pathError := ""
22-
23-
isDir, err := safeguards.IsDirectory(pathTrue)
24-
assert.True(t, isDir)
25-
assert.Nil(t, err)
26-
27-
isDir, err = safeguards.IsDirectory(pathFalse)
28-
assert.False(t, isDir)
29-
assert.Nil(t, err)
30-
31-
isDir, err = safeguards.IsDirectory(pathError)
32-
assert.False(t, isDir)
33-
assert.NotNil(t, err)
34-
}
35-
36-
// TestIsYAML tests the isYAML function for proper returns
37-
func TestIsYAML(t *testing.T) {
38-
dirNotYaml, _ := filepath.Abs("../pkg/safeguards/tests/not-yaml")
39-
dirYaml, _ := filepath.Abs("../pkg/safeguards/tests/all/success")
40-
fileNotYaml, _ := filepath.Abs("../pkg/safeguards/tests/not-yaml/readme.md")
41-
fileYaml, _ := filepath.Abs("../pkg/safeguards/tests/all/success/all-success-manifest-1.yaml")
42-
43-
assert.False(t, safeguards.IsYAML(fileNotYaml))
44-
assert.True(t, safeguards.IsYAML(fileYaml))
4511

46-
manifestFiles, err := safeguards.GetManifestFiles(dirNotYaml)
47-
assert.Nil(t, manifestFiles)
48-
assert.NotNil(t, err)
12+
"helm.sh/helm/v3/pkg/chartutil"
13+
)
4914

50-
manifestFiles, err = safeguards.GetManifestFiles(dirYaml)
51-
assert.NotNil(t, manifestFiles)
52-
assert.Nil(t, err)
53-
}
15+
const (
16+
manifestPathDirectorySuccess = "../pkg/safeguards/tests/all/success"
17+
manifestPathDirectoryError = "../pkg/safeguards/tests/all/error"
18+
manifestPathFileSuccess = "../pkg/safeguards/tests/all/success/all-success-manifest-1.yaml"
19+
manifestPathFileError = "../pkg/safeguards/tests/all/error/all-error-manifest-1.yaml"
20+
kustomizationPath = "../pkg/safeguards/tests/kustomize/overlays/production"
21+
chartPath = "../pkg/safeguards/tests/testmanifests/validchart"
22+
kustomizationFilePath = "../pkg/safeguards/tests/kustomize/overlays/production/kustomization.yaml"
23+
)
5424

5525
// TestRunValidate tests the run command for `draft validate` for proper returns
5626
func TestRunValidate(t *testing.T) {
5727
ctx := context.TODO()
58-
manifestFilesEmpty := []safeguards.ManifestFile{}
59-
manifestPathDirectorySuccess, _ := filepath.Abs("../pkg/safeguards/tests/all/success")
60-
manifestPathDirectoryError, _ := filepath.Abs("../pkg/safeguards/tests/all/error")
61-
manifestPathFileSuccess, _ := filepath.Abs("../pkg/safeguards/tests/all/success/all-success-manifest-1.yaml")
62-
manifestPathFileError, _ := filepath.Abs("../pkg/safeguards/tests/all/error/all-error-manifest-1.yaml")
63-
var manifestFiles []safeguards.ManifestFile
28+
manifestFilesEmpty := []types.ManifestFile{}
29+
30+
var manifestFiles []types.ManifestFile
31+
var opt chartutil.ReleaseOptions
6432

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

6937
// Scenario 2a: manifest path leads to a directory of manifestFiles - expect success
70-
manifestFiles, err = safeguards.GetManifestFiles(manifestPathDirectorySuccess)
38+
manifestFiles, err = safeguards.GetManifestFiles(manifestPathDirectorySuccess, opt)
7139
assert.Nil(t, err)
7240
v, err := safeguards.GetManifestResults(ctx, manifestFiles)
7341
assert.Nil(t, err)
7442
numViolations := countTestViolations(v)
7543
assert.Equal(t, numViolations, 0)
7644

7745
// Scenario 2b: manifest path leads to a directory of manifestFiles - expect failure
78-
manifestFiles, err = safeguards.GetManifestFiles(manifestPathDirectoryError)
46+
manifestFiles, err = safeguards.GetManifestFiles(manifestPathDirectoryError, opt)
7947
assert.Nil(t, err)
8048
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
8149
assert.Nil(t, err)
8250
numViolations = countTestViolations(v)
8351
assert.Greater(t, numViolations, 0)
8452

8553
// Scenario 3a: manifest path leads to one manifest file - expect success
86-
manifestFiles, err = safeguards.GetManifestFiles(manifestPathFileSuccess)
54+
manifestFiles, err = safeguards.GetManifestFiles(manifestPathFileSuccess, opt)
55+
assert.Nil(t, err)
8756
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
8857
assert.Nil(t, err)
8958
numViolations = countTestViolations(v)
9059
assert.Equal(t, numViolations, 0)
9160

9261
// Scenario 3b: manifest path leads to one manifest file - expect failure
93-
manifestFiles, err = safeguards.GetManifestFiles(manifestPathFileError)
62+
manifestFiles, err = safeguards.GetManifestFiles(manifestPathFileError, opt)
63+
assert.Nil(t, err)
64+
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
65+
assert.Nil(t, err)
66+
numViolations = countTestViolations(v)
67+
assert.Greater(t, numViolations, 0)
68+
69+
//Scenario 4: Test Kustomize
70+
manifestFiles, err = safeguards.GetManifestFiles(kustomizationPath, opt)
71+
assert.Nil(t, err)
72+
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
73+
assert.Nil(t, err)
74+
numViolations = countTestViolations(v)
75+
assert.Greater(t, numViolations, 0)
76+
77+
// Scenario 5: Test Helm
78+
opt.Name = "test-release-name"
79+
opt.Namespace = "test-release-namespace"
80+
81+
manifestFiles, err = safeguards.GetManifestFiles(chartPath, opt)
82+
assert.Nil(t, err)
9483
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
9584
assert.Nil(t, err)
9685
numViolations = countTestViolations(v)
@@ -100,25 +89,19 @@ func TestRunValidate(t *testing.T) {
10089
// TestRunValidate_Kustomize tests the run command for `draft validate` for proper returns when given a kustomize project
10190
func TestRunValidate_Kustomize(t *testing.T) {
10291
ctx := context.TODO()
103-
kustomizationPath, _ := filepath.Abs("../pkg/safeguards/tests/kustomize/overlays/production")
104-
kustomizationFilePath, _ := filepath.Abs("../pkg/safeguards/tests/kustomize/overlays/production/kustomization.yaml")
105-
106-
makeTempDir(t)
107-
t.Cleanup(func() { cleanupDir(t, tempDir) })
108-
109-
var manifestFiles []safeguards.ManifestFile
92+
var manifestFiles []types.ManifestFile
11093
var err error
11194

11295
// Scenario 1a: kustomizationPath leads to a directory containing kustomization.yaml - expect success
113-
manifestFiles, err = preprocessing.RenderKustomizeManifest(kustomizationPath, tempDir)
96+
manifestFiles, err = preprocessing.RenderKustomizeManifest(kustomizationPath)
11497
assert.Nil(t, err)
11598
v, err := safeguards.GetManifestResults(ctx, manifestFiles)
11699
assert.Nil(t, err)
117100
numViolations := countTestViolations(v)
118101
assert.Equal(t, numViolations, 1)
119102

120103
// Scenario 1b: kustomizationFilePath path leads to a specific kustomization.yaml - expect success
121-
manifestFiles, err = preprocessing.RenderKustomizeManifest(kustomizationFilePath, tempDir)
104+
manifestFiles, err = preprocessing.RenderKustomizeManifest(kustomizationFilePath)
122105
assert.Nil(t, err)
123106
v, err = safeguards.GetManifestResults(ctx, manifestFiles)
124107
assert.Nil(t, err)

cmd/validate_test_helpers.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
11
package cmd
22

33
import (
4-
"os"
5-
"path/filepath"
6-
"testing"
7-
8-
"github.com/Azure/draft/pkg/safeguards"
4+
types "github.com/Azure/draft/pkg/safeguards/types"
95
)
106

11-
var tempDir, _ = filepath.Abs("./testdata")
12-
13-
func countTestViolations(results []safeguards.ManifestResult) int {
7+
func countTestViolations(results []types.ManifestResult) int {
148
numViolations := 0
159
for _, r := range results {
1610
numViolations += len(r.ObjectViolations)
1711
}
1812

1913
return numViolations
2014
}
21-
22-
func makeTempDir(t *testing.T) {
23-
if err := os.MkdirAll(tempDir, 0755); err != nil {
24-
t.Fatalf("failed to create temporary output directory: %s", err)
25-
}
26-
}
27-
28-
func cleanupDir(t *testing.T, dir string) {
29-
err := os.RemoveAll(dir)
30-
if err != nil {
31-
t.Fatalf("Failed to clean directory: %s", err)
32-
}
33-
}

pkg/safeguards/constants.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)