| package validators_test |
| |
| import ( |
| "testing" |
| |
| "github.com/lrills/helm-unittest/unittest/common" |
| "github.com/lrills/helm-unittest/unittest/snapshot" |
| . "github.com/lrills/helm-unittest/unittest/validators" |
| "github.com/stretchr/testify/assert" |
| ) |
| |
| func TestSnapshotValidatorWhenOk(t *testing.T) { |
| data := common.K8sManifest{"a": "b"} |
| validator := MatchSnapshotValidator{Path: "a"} |
| |
| mockComparer := new(mockSnapshotComparer) |
| mockComparer.On("CompareToSnapshot", "b").Return(&snapshot.CompareResult{ |
| Passed: true, |
| }) |
| |
| pass, diff := validator.Validate(&ValidateContext{ |
| Docs: []common.K8sManifest{data}, |
| SnapshotComparer: mockComparer, |
| }) |
| |
| assert.True(t, pass) |
| assert.Equal(t, []string{}, diff) |
| |
| mockComparer.AssertExpectations(t) |
| } |
| |
| func TestSnapshotValidatorWhenNegativeAndOk(t *testing.T) { |
| data := common.K8sManifest{"a": "b"} |
| validator := MatchSnapshotValidator{Path: "a"} |
| |
| mockComparer := new(mockSnapshotComparer) |
| mockComparer.On("CompareToSnapshot", "b").Return(&snapshot.CompareResult{ |
| Passed: false, |
| CachedSnapshot: "a:\n b: c\n", |
| NewSnapshot: "x:\n y: x\n", |
| }) |
| |
| pass, diff := validator.Validate(&ValidateContext{ |
| Negative: true, |
| Docs: []common.K8sManifest{data}, |
| SnapshotComparer: mockComparer, |
| }) |
| |
| assert.True(t, pass) |
| assert.Equal(t, []string{}, diff) |
| |
| mockComparer.AssertExpectations(t) |
| } |
| |
| func TestSnapshotValidatorWhenFail(t *testing.T) { |
| data := common.K8sManifest{"a": "b"} |
| validator := MatchSnapshotValidator{Path: "a"} |
| |
| mockComparer := new(mockSnapshotComparer) |
| mockComparer.On("CompareToSnapshot", "b").Return(&snapshot.CompareResult{ |
| Passed: false, |
| CachedSnapshot: "a:\n b: c\n", |
| NewSnapshot: "x:\n y: x\n", |
| }) |
| |
| pass, diff := validator.Validate(&ValidateContext{ |
| Docs: []common.K8sManifest{data}, |
| SnapshotComparer: mockComparer, |
| }) |
| |
| assert.False(t, pass) |
| assert.Equal(t, []string{ |
| "DocumentIndex: 0", |
| "Path: a", |
| "Expected to match snapshot 0:", |
| " --- Expected", |
| " +++ Actual", |
| " @@ -1,3 +1,3 @@", |
| " -a:", |
| " - b: c", |
| " +x:", |
| " + y: x", |
| }, diff) |
| |
| mockComparer.AssertExpectations(t) |
| } |
| |
| func TestSnapshotValidatorWhenNegativeAndFail(t *testing.T) { |
| data := common.K8sManifest{"a": "b"} |
| validator := MatchSnapshotValidator{Path: "a"} |
| |
| cached := "a:\n b: c\n" |
| mockComparer := new(mockSnapshotComparer) |
| mockComparer.On("CompareToSnapshot", "b").Return(&snapshot.CompareResult{ |
| Passed: true, |
| CachedSnapshot: cached, |
| NewSnapshot: cached, |
| }) |
| |
| pass, diff := validator.Validate(&ValidateContext{ |
| Negative: true, |
| Docs: []common.K8sManifest{data}, |
| SnapshotComparer: mockComparer, |
| }) |
| |
| assert.False(t, pass) |
| assert.Equal(t, []string{ |
| "DocumentIndex: 0", |
| "Path: a", |
| "Expected NOT to match snapshot 0:", |
| " a:", |
| " b: c", |
| }, diff) |
| |
| mockComparer.AssertExpectations(t) |
| } |
| |
| func TestSnapshotValidatorWhenInvalidIndex(t *testing.T) { |
| manifest := makeManifest("a:b") |
| |
| validator := MatchSnapshotValidator{Path: "a"} |
| pass, diff := validator.Validate(&ValidateContext{ |
| Docs: []common.K8sManifest{manifest}, |
| Index: 2, |
| }) |
| |
| assert.False(t, pass) |
| assert.Equal(t, []string{ |
| "Error:", |
| " documentIndex 2 out of range", |
| }, diff) |
| } |
| |
| func TestSnapshotValidatorWhenInvalidPath(t *testing.T) { |
| manifest := makeManifest("a:b") |
| |
| cached := "a:\n b: c\n" |
| mockComparer := new(mockSnapshotComparer) |
| mockComparer.On("CompareToSnapshot", "b").Return(&snapshot.CompareResult{ |
| Passed: true, |
| CachedSnapshot: cached, |
| NewSnapshot: cached, |
| }) |
| |
| validator := MatchSnapshotValidator{Path: "x.b"} |
| pass, diff := validator.Validate(&ValidateContext{ |
| Docs: []common.K8sManifest{manifest}, |
| SnapshotComparer: mockComparer, |
| }) |
| |
| assert.False(t, pass) |
| assert.Equal(t, []string{ |
| "DocumentIndex: 0", |
| "Error:", |
| " can't get [\"b\"] from a non map type:", |
| " null", |
| }, diff) |
| } |