-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors_test.go
97 lines (85 loc) · 2.31 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package v1alpha
import (
"embed"
"encoding/json"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/nobl9/govy/pkg/govy"
"github.com/nobl9/nobl9-go/manifest"
)
//go:embed test_data/errors
var errorsTestData embed.FS
func TestObjectError(t *testing.T) {
errs := govy.PropertyErrors{
&govy.PropertyError{
PropertyName: "metadata.name",
PropertyValue: "default",
Errors: []*govy.RuleError{{Message: "here's an error"}},
},
&govy.PropertyError{
PropertyName: "spec.description",
PropertyValue: "some long description",
Errors: []*govy.RuleError{{Message: "here's another error"}},
},
}
t.Run("non project scoped object", func(t *testing.T) {
err := &ObjectError{
Object: ObjectMetadata{
Kind: manifest.KindProject,
Name: "default",
Source: "/home/me/project.yaml",
},
Errors: errs,
}
assert.EqualError(t, err, expectedErrorOutput(t, "object_error.txt"))
})
t.Run("project scoped object", func(t *testing.T) {
err := &ObjectError{
Object: ObjectMetadata{
IsProjectScoped: true,
Kind: manifest.KindService,
Name: "my-service",
Project: "default",
Source: "/home/me/service.yaml",
},
Errors: errs,
}
assert.EqualError(t, err, expectedErrorOutput(t, "object_error_project_scoped.txt"))
})
}
func TestObjectError_UnmarshalJSON(t *testing.T) {
expected := ObjectError{
Object: ObjectMetadata{
Kind: manifest.KindService,
Name: "test-service",
Source: "/home/me/service.yaml",
IsProjectScoped: true,
Project: "default",
},
Errors: govy.PropertyErrors{
{
PropertyName: "metadata.project",
PropertyValue: "default",
Errors: []*govy.RuleError{{Message: "nested"}},
},
{
PropertyName: "metadata.name",
PropertyValue: "my-project",
},
},
}
data, err := json.MarshalIndent(expected, "", " ")
require.NoError(t, err)
var actual ObjectError
err = json.Unmarshal(data, &actual)
require.NoError(t, err)
assert.Equal(t, expected, actual)
}
func expectedErrorOutput(t *testing.T, name string) string {
t.Helper()
data, err := errorsTestData.ReadFile(filepath.Join("test_data", "errors", name))
require.NoError(t, err)
return string(data)
}