Skip to content

Commit ed09399

Browse files
authored
fix: return error instead of log.Fatalf() (#5625)
* fix: return error instead of log.Fatalf() * chore: add meaningful message to error output * chore: add meaningful message to fatal function
1 parent 82ee768 commit ed09399

File tree

11 files changed

+412
-190
lines changed

11 files changed

+412
-190
lines changed

api/internal/accumulator/namereferencetransformer_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
590590
func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
591591
rf := provider.NewDefaultDepProvider().GetResourceFactory()
592592

593-
v1 := rf.FromMapWithName(
593+
v1, err := rf.FromMapWithName(
594594
"volume1",
595595
map[string]interface{}{
596596
"apiVersion": "v1",
@@ -599,7 +599,10 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
599599
"name": "someprefix-volume1",
600600
},
601601
})
602-
c1 := rf.FromMapWithName(
602+
if err != nil {
603+
t.Fatalf("failed to get new instance with given name: %v", err)
604+
}
605+
c1, err := rf.FromMapWithName(
603606
"claim1",
604607
map[string]interface{}{
605608
"apiVersion": "v1",
@@ -612,9 +615,11 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
612615
"volumeName": "volume1",
613616
},
614617
})
615-
618+
if err != nil {
619+
t.Fatalf("failed to get new instance with given name: %v", err)
620+
}
616621
v2 := v1.DeepCopy()
617-
c2 := rf.FromMapWithName(
622+
c2, err := rf.FromMapWithName(
618623
"claim1",
619624
map[string]interface{}{
620625
"apiVersion": "v1",
@@ -627,6 +632,9 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
627632
"volumeName": "someprefix-volume1",
628633
},
629634
})
635+
if err != nil {
636+
t.Fatalf("failed to get new instance with given name: %v", err)
637+
}
630638

631639
m1 := resmaptest_test.NewRmBuilder(t, rf).AddR(v1).AddR(c1).ResMap()
632640

api/internal/accumulator/resaccumulator_test.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func makeResAccumulator(t *testing.T) *ResAccumulator {
6464
"name": "backendTwo",
6565
}}).ResMap())
6666
if err != nil {
67-
t.Fatalf("unexpected err: %v", err)
67+
t.Fatalf("failed to append resources: %v", err)
6868
}
6969
return ra
7070
}
@@ -143,22 +143,26 @@ func expectLog(t *testing.T, log bytes.Buffer, expect string) {
143143
func TestResolveVarsVarNeedsDisambiguation(t *testing.T) {
144144
ra := makeResAccumulator(t)
145145
rm0 := resmap.New()
146-
err := rm0.Append(
147-
provider.NewDefaultDepProvider().GetResourceFactory().FromMap(
148-
map[string]interface{}{
149-
"apiVersion": "v1",
150-
"kind": "Service",
151-
"metadata": map[string]interface{}{
152-
"name": "backendOne",
153-
"namespace": "fooNamespace",
154-
},
155-
}))
146+
147+
r, err := provider.NewDefaultDepProvider().GetResourceFactory().FromMap(
148+
map[string]interface{}{
149+
"apiVersion": "v1",
150+
"kind": "Service",
151+
"metadata": map[string]interface{}{
152+
"name": "backendOne",
153+
"namespace": "fooNamespace",
154+
},
155+
})
156156
if err != nil {
157-
t.Fatalf("unexpected err: %v", err)
157+
t.Fatalf("failed to get instance of resources: %v", err)
158+
}
159+
err = rm0.Append(r)
160+
if err != nil {
161+
t.Fatalf("failed to append a resource to ResMap: %v", err)
158162
}
159163
err = ra.AppendAll(rm0)
160164
if err != nil {
161-
t.Fatalf("unexpected err: %v", err)
165+
t.Fatalf("failed to append a resource to ResAccumulator: %v", err)
162166
}
163167

164168
err = ra.MergeVars([]types.Var{
@@ -227,7 +231,11 @@ func TestResolveVarConflicts(t *testing.T) {
227231
// create accumulators holding apparently conflicting vars that are not
228232
// actually in conflict because they point to the same concrete value.
229233
rm0 := resmap.New()
230-
err := rm0.Append(rf.FromMap(fooAws))
234+
r0, err0 := rf.FromMap(fooAws)
235+
if err0 != nil {
236+
t.Fatalf("failed to get instance of resources: %v", err0)
237+
}
238+
err := rm0.Append(r0)
231239
require.NoError(t, err)
232240
ac0 := MakeEmptyAccumulator()
233241
err = ac0.AppendAll(rm0)
@@ -236,7 +244,11 @@ func TestResolveVarConflicts(t *testing.T) {
236244
require.NoError(t, err)
237245

238246
rm1 := resmap.New()
239-
err = rm1.Append(rf.FromMap(barAws))
247+
r1, err1 := rf.FromMap(barAws)
248+
if err1 != nil {
249+
t.Fatalf("failed to get instance of resources: %v", err1)
250+
}
251+
err = rm1.Append(r1)
240252
require.NoError(t, err)
241253
ac1 := MakeEmptyAccumulator()
242254
err = ac1.AppendAll(rm1)
@@ -255,7 +267,11 @@ func TestResolveVarConflicts(t *testing.T) {
255267
// two above (because it contains a variable whose name is used in the other
256268
// accumulators AND whose concrete values are different).
257269
rm2 := resmap.New()
258-
err = rm2.Append(rf.FromMap(barGcp))
270+
r2, err2 := rf.FromMap(barGcp)
271+
if err2 != nil {
272+
t.Fatalf("failed to get instance of resources: %v", err2)
273+
}
274+
err = rm2.Append(r2)
259275
require.NoError(t, err)
260276
ac2 := MakeEmptyAccumulator()
261277
err = ac2.AppendAll(rm2)

api/internal/plugins/execplugin/execplugin_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ s/$BAR/bar baz/g
4141
}
4242
pvd := provider.NewDefaultDepProvider()
4343
rf := resmap.NewFactory(pvd.GetResourceFactory())
44-
pluginConfig := rf.RF().FromMap(
44+
pluginConfig, err := rf.RF().FromMap(
4545
map[string]interface{}{
4646
"apiVersion": "someteam.example.com/v1",
4747
"kind": "SedTransformer",
@@ -51,6 +51,9 @@ s/$BAR/bar baz/g
5151
"argsOneLiner": "one two 'foo bar'",
5252
"argsFromFile": "sed-input.txt",
5353
})
54+
if err != nil {
55+
t.Fatalf("failed to writes the data to a file: %v", err)
56+
}
5457

5558
pluginConfig.RemoveBuildAnnotations()
5659
pc := types.DisabledPluginConfig()

api/internal/plugins/utils/utils_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ func TestDeterminePluginSrcRoot(t *testing.T) {
3232
}
3333

3434
func makeConfigMap(rf *resource.Factory, name, behavior string, hashValue *string) *resource.Resource {
35-
r := rf.FromMap(map[string]interface{}{
35+
r, err := rf.FromMap(map[string]interface{}{
3636
"apiVersion": "v1",
3737
"kind": "ConfigMap",
3838
"metadata": map[string]interface{}{"name": name},
3939
})
40+
if err != nil {
41+
panic(err)
42+
}
4043
annotations := map[string]string{}
4144
if behavior != "" {
4245
annotations[BehaviorAnnotation] = behavior

0 commit comments

Comments
 (0)