Skip to content

Commit

Permalink
Add json annotations for Suites
Browse files Browse the repository at this point in the history
This lets us read Suites from the disk.

Per open-policy-agent#1446, we aren't doing Assertions for pre-alpha so I haven't added
those annotations yet.

Signed-off-by: Will Beason <willbeason@google.com>
  • Loading branch information
Will Beason committed Jul 22, 2021
1 parent f9bad1c commit 954f5d2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
54 changes: 54 additions & 0 deletions pkg/gktest/read_suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,60 @@ apiVersion: test.gatekeeper.sh/v1alpha1
want: nil,
wantErr: ErrNotADirectory,
},
{
name: "suite with test and cases",
target: "test.yaml",
recursive: false,
fileSystem: fstest.MapFS{
"test.yaml": &fstest.MapFile{
Data: []byte(`
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
tests:
- template: template.yaml
constraint: constraint.yaml
cases:
- allow: allow.yaml
- deny: deny.yaml
`),
},
},
want: []Suite{{
Tests: []Test{{
Template: "template.yaml",
Constraint: "constraint.yaml",
Cases: []Case{{
Allow: "allow.yaml",
}, {
Deny: "deny.yaml",
}},
}},
}},
wantErr: nil,
},
{
name: "suite with tests and no cases",
target: "test.yaml",
recursive: false,
fileSystem: fstest.MapFS{
"test.yaml": &fstest.MapFile{
Data: []byte(`
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
tests:
- template: template.yaml
constraint: constraint.yaml
`),
},
},
want: []Suite{{
Tests: []Test{{
Template: "template.yaml",
Constraint: "constraint.yaml",
}},
}},
wantErr: nil,
},
}

for _, tc := range testCases {
Expand Down
12 changes: 6 additions & 6 deletions pkg/gktest/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ type Suite struct {

// Tests is a list of Template&Constraint pairs, with tests to run on
// each.
Tests []Test
Tests []Test `json:"spec"`
}

// Test defines a Template&Constraint pair to instantiate, and Cases to
// run on the instantiated Constraint.
type Test struct {
// Template is the path to the ConstraintTemplate, relative to the file
// defining the Suite.
Template string
Template string `json:"template"`

// Constraint is the path to the Constraint, relative to the file defining
// the Suite. Must be an instance of Template.
Constraint string
Constraint string `json:"constraint"`

// Cases are the test cases to run on the instantiated Constraint.
Cases []Case
Cases []Case `json:"cases"`
}

// Case runs Constraint against a YAML object.
type Case struct {
Allow string
Deny string
Allow string `json:"allow"`
Deny string `json:"deny"`
Assertions []Assertion
}

Expand Down

0 comments on commit 954f5d2

Please sign in to comment.