Skip to content

Commit

Permalink
fix: return error instead of log.Fatalf() (#5625)
Browse files Browse the repository at this point in the history
* fix: return error instead of log.Fatalf()

* chore: add meaningful message to error output

* chore: add meaningful message to fatal function
  • Loading branch information
chansuke committed Apr 18, 2024
1 parent 82ee768 commit ed09399
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 190 deletions.
16 changes: 12 additions & 4 deletions api/internal/accumulator/namereferencetransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ func TestNameReferenceUnhappyRun(t *testing.T) {
func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
rf := provider.NewDefaultDepProvider().GetResourceFactory()

v1 := rf.FromMapWithName(
v1, err := rf.FromMapWithName(
"volume1",
map[string]interface{}{
"apiVersion": "v1",
Expand All @@ -599,7 +599,10 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
"name": "someprefix-volume1",
},
})
c1 := rf.FromMapWithName(
if err != nil {
t.Fatalf("failed to get new instance with given name: %v", err)
}
c1, err := rf.FromMapWithName(
"claim1",
map[string]interface{}{
"apiVersion": "v1",
Expand All @@ -612,9 +615,11 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
"volumeName": "volume1",
},
})

if err != nil {
t.Fatalf("failed to get new instance with given name: %v", err)
}
v2 := v1.DeepCopy()
c2 := rf.FromMapWithName(
c2, err := rf.FromMapWithName(
"claim1",
map[string]interface{}{
"apiVersion": "v1",
Expand All @@ -627,6 +632,9 @@ func TestNameReferencePersistentVolumeHappyRun(t *testing.T) {
"volumeName": "someprefix-volume1",
},
})
if err != nil {
t.Fatalf("failed to get new instance with given name: %v", err)
}

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

Expand Down
48 changes: 32 additions & 16 deletions api/internal/accumulator/resaccumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func makeResAccumulator(t *testing.T) *ResAccumulator {
"name": "backendTwo",
}}).ResMap())
if err != nil {
t.Fatalf("unexpected err: %v", err)
t.Fatalf("failed to append resources: %v", err)
}
return ra
}
Expand Down Expand Up @@ -143,22 +143,26 @@ func expectLog(t *testing.T, log bytes.Buffer, expect string) {
func TestResolveVarsVarNeedsDisambiguation(t *testing.T) {
ra := makeResAccumulator(t)
rm0 := resmap.New()
err := rm0.Append(
provider.NewDefaultDepProvider().GetResourceFactory().FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Service",
"metadata": map[string]interface{}{
"name": "backendOne",
"namespace": "fooNamespace",
},
}))

r, err := provider.NewDefaultDepProvider().GetResourceFactory().FromMap(
map[string]interface{}{
"apiVersion": "v1",
"kind": "Service",
"metadata": map[string]interface{}{
"name": "backendOne",
"namespace": "fooNamespace",
},
})
if err != nil {
t.Fatalf("unexpected err: %v", err)
t.Fatalf("failed to get instance of resources: %v", err)
}
err = rm0.Append(r)
if err != nil {
t.Fatalf("failed to append a resource to ResMap: %v", err)
}
err = ra.AppendAll(rm0)
if err != nil {
t.Fatalf("unexpected err: %v", err)
t.Fatalf("failed to append a resource to ResAccumulator: %v", err)
}

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

rm1 := resmap.New()
err = rm1.Append(rf.FromMap(barAws))
r1, err1 := rf.FromMap(barAws)
if err1 != nil {
t.Fatalf("failed to get instance of resources: %v", err1)
}
err = rm1.Append(r1)
require.NoError(t, err)
ac1 := MakeEmptyAccumulator()
err = ac1.AppendAll(rm1)
Expand All @@ -255,7 +267,11 @@ func TestResolveVarConflicts(t *testing.T) {
// two above (because it contains a variable whose name is used in the other
// accumulators AND whose concrete values are different).
rm2 := resmap.New()
err = rm2.Append(rf.FromMap(barGcp))
r2, err2 := rf.FromMap(barGcp)
if err2 != nil {
t.Fatalf("failed to get instance of resources: %v", err2)
}
err = rm2.Append(r2)
require.NoError(t, err)
ac2 := MakeEmptyAccumulator()
err = ac2.AppendAll(rm2)
Expand Down
5 changes: 4 additions & 1 deletion api/internal/plugins/execplugin/execplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ s/$BAR/bar baz/g
}
pvd := provider.NewDefaultDepProvider()
rf := resmap.NewFactory(pvd.GetResourceFactory())
pluginConfig := rf.RF().FromMap(
pluginConfig, err := rf.RF().FromMap(
map[string]interface{}{
"apiVersion": "someteam.example.com/v1",
"kind": "SedTransformer",
Expand All @@ -51,6 +51,9 @@ s/$BAR/bar baz/g
"argsOneLiner": "one two 'foo bar'",
"argsFromFile": "sed-input.txt",
})
if err != nil {
t.Fatalf("failed to writes the data to a file: %v", err)
}

pluginConfig.RemoveBuildAnnotations()
pc := types.DisabledPluginConfig()
Expand Down
5 changes: 4 additions & 1 deletion api/internal/plugins/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ func TestDeterminePluginSrcRoot(t *testing.T) {
}

func makeConfigMap(rf *resource.Factory, name, behavior string, hashValue *string) *resource.Resource {
r := rf.FromMap(map[string]interface{}{
r, err := rf.FromMap(map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{"name": name},
})
if err != nil {
panic(err)
}
annotations := map[string]string{}
if behavior != "" {
annotations[BehaviorAnnotation] = behavior
Expand Down

0 comments on commit ed09399

Please sign in to comment.