1
1
package fetchers
2
2
3
3
import (
4
- "embed"
5
- "encoding/json"
6
- "errors"
7
- "fmt"
8
4
"os"
9
- "reflect"
10
5
"strings"
11
6
"testing"
12
7
13
8
"github.com/grafana/grafana-operator/v5/controllers/config"
9
+ "github.com/stretchr/testify/assert"
14
10
"github.com/stretchr/testify/require"
11
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
12
16
13
"github.com/grafana/grafana-operator/v5/api/v1beta1"
17
14
"github.com/grafana/grafana-operator/v5/embeds"
18
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
15
)
20
16
21
17
func setup (t * testing.T ) {
@@ -28,162 +24,101 @@ func teardown(t *testing.T) {
28
24
require .NoError (t , os .RemoveAll (config .GrafanaDashboardsRuntimeBuild ))
29
25
}
30
26
31
- func normalizeAndCompareJSON (json1 , json2 []byte ) bool {
32
- var data1 , data2 map [string ]any
33
-
34
- if err := json .Unmarshal (json1 , & data1 ); err != nil {
35
- return false
36
- }
37
-
38
- if err := json .Unmarshal (json2 , & data2 ); err != nil {
39
- return false
40
- }
41
-
42
- normalized1 , err := json .Marshal (data1 )
43
- if err != nil {
44
- return false
45
- }
46
-
47
- normalized2 , err := json .Marshal (data2 )
48
- if err != nil {
49
- return false
50
- }
51
-
52
- return reflect .DeepEqual (normalized1 , normalized2 )
53
- }
54
-
55
27
func TestFetchJsonnet (t * testing.T ) {
56
28
tests := []struct {
57
- name string
58
- dashboard * v1beta1.GrafanaDashboard
59
- libsonnet embed.FS
60
- expected []byte
61
- envs map [string ]string
62
- expectedError error
29
+ name string
30
+ jsonnet string
31
+ envs map [string ]string
32
+ want []byte
63
33
}{
64
34
{
65
- name : "Successful Jsonnet Evaluation" ,
66
- dashboard : & v1beta1.GrafanaDashboard {
67
- ObjectMeta : metav1.ObjectMeta {
68
- Name : "grafanadashboard-jsonnet" ,
69
- Namespace : "grafana" ,
70
- },
71
- Spec : v1beta1.GrafanaDashboardSpec {
72
- GrafanaContentSpec : v1beta1.GrafanaContentSpec {
73
- Jsonnet : string (embeds .TestDashboardEmbed ),
74
- },
75
- },
76
- Status : v1beta1.GrafanaDashboardStatus {},
77
- },
78
- libsonnet : embeds .GrafonnetEmbed ,
79
- expected : embeds .TestDashboardEmbedExpectedJSON ,
80
- envs : map [string ]string {},
81
- expectedError : nil ,
35
+ name : "Successful Jsonnet Evaluation" ,
36
+ jsonnet : string (embeds .TestDashboardEmbed ),
37
+ envs : map [string ]string {},
38
+ want : embeds .TestDashboardEmbedExpectedJSON ,
82
39
},
83
40
{
84
- name : "Empty Jsonnet Content" ,
85
- dashboard : & v1beta1.GrafanaDashboard {
86
- ObjectMeta : metav1.ObjectMeta {Name : "dashboard-1" },
87
- Spec : v1beta1.GrafanaDashboardSpec {
88
- GrafanaContentSpec : v1beta1.GrafanaContentSpec {
89
- Jsonnet : "" ,
90
- },
91
- },
92
- Status : v1beta1.GrafanaDashboardStatus {},
41
+ name : "Successful Jsonnet Evaluation with non-empty envs" ,
42
+ jsonnet : string (embeds .TestDashboardEmbedWithEnv ),
43
+ want : embeds .TestDashboardEmbedWithEnvExpectedJSON ,
44
+ envs : map [string ]string {
45
+ "TEST_ENV" : "123" ,
93
46
},
94
- libsonnet : embeds .GrafonnetEmbed ,
95
- expected : nil ,
96
- envs : map [string ]string {},
97
- expectedError : errors .New ("no jsonnet Content Found, nil or empty string" ),
98
47
},
99
- {
100
- name : "Successful Jsonnet Evaluation with non-empty envs" ,
101
- dashboard : & v1beta1.GrafanaDashboard {
48
+ }
49
+
50
+ for _ , tt := range tests {
51
+ t .Run (tt .name , func (t * testing.T ) {
52
+ cr := & v1beta1.GrafanaDashboard {
102
53
ObjectMeta : metav1.ObjectMeta {
103
54
Name : "grafanadashboard-jsonnet" ,
104
55
Namespace : "grafana" ,
105
56
},
106
57
Spec : v1beta1.GrafanaDashboardSpec {
107
58
GrafanaContentSpec : v1beta1.GrafanaContentSpec {
108
- Jsonnet : string ( embeds . TestDashboardEmbedWithEnv ) ,
59
+ Jsonnet : tt . jsonnet ,
109
60
},
110
61
},
111
- Status : v1beta1.GrafanaDashboardStatus {},
112
- },
113
- libsonnet : embeds .GrafonnetEmbed ,
114
- expected : embeds .TestDashboardEmbedWithEnvExpectedJSON ,
115
- envs : map [string ]string {
116
- "TEST_ENV" : "123" ,
117
- },
118
- expectedError : nil ,
119
- },
120
- }
121
-
122
- for _ , test := range tests {
123
- t .Run (test .name , func (t * testing.T ) {
124
- result , err := FetchJsonnet (test .dashboard , test .envs , test .libsonnet )
125
- if fmt .Sprintf ("%v" , err ) != fmt .Sprintf ("%v" , test .expectedError ) {
126
- t .Errorf ("expected error %v, but got %v" , test .expectedError , err )
127
62
}
128
63
129
- if test .expected != nil && ! normalizeAndCompareJSON (test .expected , result ) {
130
- t .Errorf ("expected string %s, but got %s" , string (test .expected ), string (result ))
131
- }
64
+ got , err := FetchJsonnet (cr , tt .envs , embeds .GrafonnetEmbed )
65
+ require .NoError (t , err )
66
+
67
+ assert .JSONEq (t , string (tt .want ), string (got ))
132
68
})
133
69
}
70
+
71
+ t .Run ("Empty Jsonnet Content" , func (t * testing.T ) {
72
+ cr := & v1beta1.GrafanaDashboard {
73
+ ObjectMeta : metav1.ObjectMeta {
74
+ Name : "grafanadashboard-jsonnet" ,
75
+ Namespace : "grafana" ,
76
+ },
77
+ Spec : v1beta1.GrafanaDashboardSpec {
78
+ GrafanaContentSpec : v1beta1.GrafanaContentSpec {
79
+ Jsonnet : "" ,
80
+ },
81
+ },
82
+ }
83
+
84
+ got , err := FetchJsonnet (cr , map [string ]string {}, embeds .GrafonnetEmbed )
85
+ assert .Nil (t , got )
86
+ require .ErrorIs (t , err , errJsonnetNoContent )
87
+ })
134
88
}
135
89
136
90
func TestBuildProjectAndFetchJsonnetFrom (t * testing.T ) {
137
91
setup (t )
138
92
defer teardown (t )
139
93
140
- tests := []struct {
141
- name string
142
- dashboard * v1beta1.GrafanaDashboard
143
- libsonnet embed.FS
144
- expected []byte
145
- envs map [string ]string
146
- expectedError error
147
- }{
148
- {
149
- name : "Successful Jsonnet Evaluation with jsonnet build" ,
150
- dashboard : & v1beta1.GrafanaDashboard {
151
- ObjectMeta : metav1.ObjectMeta {
152
- Name : "grafanadashboard-jsonnet" ,
153
- Namespace : "grafana" ,
154
- },
155
- Spec : v1beta1.GrafanaDashboardSpec {
156
- GrafanaContentSpec : v1beta1.GrafanaContentSpec {
157
- JsonnetProjectBuild : & v1beta1.JsonnetProjectBuild {
158
- JPath : []string {"/testing/jsonnetProjectWithRuntimeRaw" },
159
- FileName : "testing/jsonnetProjectWithRuntimeRaw/dashboard_with_envs.jsonnet" ,
160
- GzipJsonnetProject : embeds .TestJsonnetProjectBuildFolderGzip ,
161
- },
94
+ t .Run ("Successful Jsonnet Evaluation with jsonnet build" , func (t * testing.T ) {
95
+ cr := & v1beta1.GrafanaDashboard {
96
+ ObjectMeta : metav1.ObjectMeta {
97
+ Name : "grafanadashboard-jsonnet" ,
98
+ Namespace : "grafana" ,
99
+ },
100
+ Spec : v1beta1.GrafanaDashboardSpec {
101
+ GrafanaContentSpec : v1beta1.GrafanaContentSpec {
102
+ JsonnetProjectBuild : & v1beta1.JsonnetProjectBuild {
103
+ JPath : []string {"/testing/jsonnetProjectWithRuntimeRaw" },
104
+ FileName : "testing/jsonnetProjectWithRuntimeRaw/dashboard_with_envs.jsonnet" ,
105
+ GzipJsonnetProject : embeds .TestJsonnetProjectBuildFolderGzip ,
162
106
},
163
107
},
164
- Status : v1beta1.GrafanaDashboardStatus {},
165
- },
166
- libsonnet : embeds .GrafonnetEmbed ,
167
- expected : []byte ("{\n \" env\" : \" 123\" \n }" ),
168
- envs : map [string ]string {
169
- "TEST_ENV" : "123" ,
170
108
},
171
- expectedError : nil ,
172
- },
173
- }
109
+ }
174
110
175
- for _ , test := range tests {
176
- t .Run (test .name , func (t * testing.T ) {
177
- result , err := BuildProjectAndFetchJsonnetFrom (test .dashboard , test .envs )
178
- if fmt .Sprintf ("%v" , err ) != fmt .Sprintf ("%v" , test .expectedError ) {
179
- t .Errorf ("expected error %v, but got %v" , test .expectedError , err )
180
- }
111
+ envs := map [string ]string {
112
+ "TEST_ENV" : "123" ,
113
+ }
181
114
182
- if test .expected != nil && ! normalizeAndCompareJSON (test .expected , result ) {
183
- t .Errorf ("expected string %s, but got %s" , string (test .expected ), string (result ))
184
- }
185
- })
186
- }
115
+ want := []byte ("{\n \" env\" : \" 123\" \n }" )
116
+
117
+ got , err := BuildProjectAndFetchJsonnetFrom (cr , envs )
118
+ require .NoError (t , err )
119
+
120
+ assert .JSONEq (t , string (want ), string (got ))
121
+ })
187
122
}
188
123
189
124
func TestGetJsonProjectBuildRoundName (t * testing.T ) {
0 commit comments