From 7c80d18ed3c4dc68dab74ad646666fb8e6d168b8 Mon Sep 17 00:00:00 2001 From: Daniel Stone Date: Wed, 1 May 2024 13:20:20 +0100 Subject: [PATCH 1/2] actions: Don't ignore YAML parsing errors When we're unpacking individual actions, pass YAML parsing errors up to the caller instead of silently ignoring them. Signed-off-by: Daniel Stone --- actions/recipe.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/actions/recipe.go b/actions/recipe.go index adc706f0..0909117a 100644 --- a/actions/recipe.go +++ b/actions/recipe.go @@ -147,7 +147,10 @@ func (y *YamlAction) UnmarshalYAML(unmarshal func(interface{}) error) error { return fmt.Errorf("Unknown action: %v", aux.Action) } - unmarshal(y.Action) + err = unmarshal(y.Action) + if err != nil { + return err + } return nil } From 27da56fa8250ca6683722b058df2226e5d5cd819 Mon Sep 17 00:00:00 2001 From: Daniel Stone Date: Wed, 1 May 2024 13:26:04 +0100 Subject: [PATCH 2/2] recipe: Add test for type mismatch during recipe parse Check that debos more obviously errors out when you do what I tried to do. Signed-off-by: Daniel Stone --- actions/recipe_test.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/actions/recipe_test.go b/actions/recipe_test.go index ef2a755d..c549d87e 100644 --- a/actions/recipe_test.go +++ b/actions/recipe_test.go @@ -192,7 +192,8 @@ type subRecipe struct { type testSubRecipe struct { recipe string subrecipe subRecipe - err string + err string + parseErr string } func TestSubRecipe(t *testing.T) { @@ -242,6 +243,7 @@ actions: `, recipeAmd64, "", // Do not expect failure + "", // Do not expect parse failure }, { // Test recipe with inherited architecture OK @@ -254,6 +256,7 @@ actions: `, recipeInheritedArch, "", // Do not expect failure + "", // Do not expect parse failure }, { // Fail with unknown recipe @@ -266,6 +269,7 @@ actions: `, recipeAmd64, "stat /tmp/unknown_recipe.yaml: no such file or directory", + "", // Do not expect parse failure }, { // Fail with different architecture recipe @@ -278,6 +282,22 @@ actions: `, recipeArmhf, "Expect architecture 'amd64' but got 'armhf'", + "", // Do not expect parse failure + }, + { + // Fail with type mismatch during parse + ` +architecture: armhf + +actions: + - action: recipe + recipe: armhf.yaml + variables: + - foo +`, + recipeArmhf, + "", + "yaml: unmarshal errors:\n line 8: cannot unmarshal !!seq into map[string]string", }, } @@ -306,6 +326,8 @@ func runTestWithSubRecipes(t *testing.T, test testSubRecipe, templateVars ...map file_subrecipe.WriteString(test.subrecipe.recipe) file_subrecipe.Close() + failed := false + r := actions.Recipe{} if len(templateVars) == 0 { err = r.Parse(file.Name(), false, false) @@ -313,10 +335,15 @@ func runTestWithSubRecipes(t *testing.T, test testSubRecipe, templateVars ...map err = r.Parse(file.Name(), false, false, templateVars[0]) } - // Should not expect error during parse - failed := !assert.Empty(t, err) + if len(test.parseErr) > 0 { + // Expected parse error? + failed = !assert.EqualError(t, err, test.parseErr) + } else { + // Unexpected error + failed = !assert.Empty(t, err) + } - if !failed { + if err == nil { context.Architecture = r.Architecture context.RecipeDir = dir