@@ -2,95 +2,84 @@ package cmd
22
33import (
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
5626func 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
10190func 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 )
0 commit comments