-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
51207b9
commit d72f227
Showing
4 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apiVersion: chainsaw.kyverno.io/v1alpha1 | ||
kind: Test | ||
metadata: | ||
name: &name yaml-anchors | ||
spec: | ||
namespace: &namespace default | ||
steps: | ||
- name: *name | ||
try: | ||
- apply: | ||
resource: &resource | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: *name | ||
namespace: *namespace | ||
spec: | ||
containers: | ||
- name: main | ||
image: alpine | ||
- assert: | ||
resource: | ||
<<: *resource | ||
spec: | ||
restartPolicy: Always | ||
timeout: 1s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
apiVersion: chainsaw.kyverno.io/v1alpha1 | ||
kind: Test | ||
metadata: | ||
name: yaml-anchors | ||
spec: | ||
namespace: default | ||
steps: | ||
- name: yaml-anchors | ||
try: | ||
- apply: | ||
resource: | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: yaml-anchors | ||
namespace: default | ||
spec: | ||
containers: | ||
- image: alpine | ||
name: main | ||
- assert: | ||
resource: | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: yaml-anchors | ||
namespace: default | ||
spec: | ||
restartPolicy: Always | ||
timeout: 1s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package yaml | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_remarshal(t *testing.T) { | ||
in, err := os.ReadFile("with-anchors.yaml") | ||
assert.NoError(t, err) | ||
out, err := os.ReadFile("without-anchors.yaml") | ||
assert.NoError(t, err) | ||
tests := []struct { | ||
name string | ||
document string | ||
unmarshal func(in []byte, out interface{}) (err error) | ||
want string | ||
wantErr bool | ||
}{{ | ||
name: "ok", | ||
document: string(in), | ||
want: string(out), | ||
wantErr: false, | ||
}, { | ||
name: "error", | ||
unmarshal: func(in []byte, out interface{}) (err error) { | ||
return errors.New("dummy") | ||
}, | ||
wantErr: true, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := remarshal([]byte(tt.document), tt.unmarshal) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tt.want, string(got)) | ||
}) | ||
} | ||
} | ||
|
||
func TestRemarshal(t *testing.T) { | ||
in, err := os.ReadFile("with-anchors.yaml") | ||
assert.NoError(t, err) | ||
out, err := os.ReadFile("without-anchors.yaml") | ||
assert.NoError(t, err) | ||
tests := []struct { | ||
name string | ||
document string | ||
want string | ||
wantErr bool | ||
}{{ | ||
name: "ok", | ||
document: string(in), | ||
want: string(out), | ||
wantErr: false, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := Remarshal([]byte(tt.document)) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, tt.want, string(got)) | ||
}) | ||
} | ||
} |