github.com/go-chef/chef@v0.30.1/run_list_item_test.go (about) 1 package chef 2 3 import ( 4 "testing" 5 ) 6 7 func TestQualifiedRecipeWithVersion(t *testing.T) { 8 runListItem := "recipe[my_recipe@1.0.0]" 9 rli, err := NewRunListItem(runListItem) 10 if err != nil { 11 t.Errorf(`NewRunListItem("%s") did not correctly parse.`, runListItem) 12 } 13 if rli.Name != "my_recipe" { 14 t.Errorf(`NewRunListItem("%s") did not correctly set name`, runListItem) 15 } 16 if rli.Type != "recipe" { 17 t.Errorf(`NewRunListItem("%s") did not correctly set type`, runListItem) 18 } 19 if rli.Version != "1.0.0" { 20 t.Errorf(`NewRunListItem("%s") did not correctly set version`, runListItem) 21 } 22 } 23 24 func TestQualifiedRecipe(t *testing.T) { 25 runListItem := "recipe[my_recipe]" 26 rli, err := NewRunListItem(runListItem) 27 if err != nil { 28 t.Errorf(`NewRunListItem("%s") did not correctly parse.`, runListItem) 29 } 30 if rli.Name != "my_recipe" { 31 t.Errorf(`NewRunListItem("%s") did not correctly set name`, runListItem) 32 } 33 if rli.Type != "recipe" { 34 t.Errorf(`NewRunListItem("%s") did not correctly set type`, runListItem) 35 } 36 if rli.Version != "" { 37 t.Errorf(`NewRunListItem("%s") did not correctly set version`, runListItem) 38 } 39 } 40 41 func TestQualifiedRole(t *testing.T) { 42 runListItem := "role[my_role]" 43 rli, err := NewRunListItem(runListItem) 44 if err != nil { 45 t.Errorf(`NewRunListItem("%s") did not correctly parse.`, runListItem) 46 } 47 if rli.Name != "my_role" { 48 t.Errorf(`NewRunListItem("%s") did not correctly set name`, runListItem) 49 } 50 if rli.Type != "role" { 51 t.Errorf(`NewRunListItem("%s") did not correctly set type, %s`, runListItem, rli.Type) 52 } 53 if rli.Version != "" { 54 t.Errorf(`NewRunListItem("%s") did not correctly set version`, runListItem) 55 } 56 } 57 58 func TestVersionedUnqualifiedRecipe(t *testing.T) { 59 runListItem := "my_recipe@1.0.0" 60 rli, err := NewRunListItem(runListItem) 61 if err != nil { 62 t.Errorf(`NewRunListItem("%s") did not correctly parse.`, runListItem) 63 } 64 if rli.Name != "my_recipe" { 65 t.Errorf(`NewRunListItem("%s") did not correctly set name`, runListItem) 66 } 67 if rli.Type != "recipe" { 68 t.Errorf(`NewRunListItem("%s") did not correctly set type`, runListItem) 69 } 70 if rli.Version != "1.0.0" { 71 t.Errorf(`NewRunListItem("%s") did not correctly set version`, runListItem) 72 } 73 } 74 75 func TestRecipeNameAlone(t *testing.T) { 76 runListItem := "my_recipe" 77 rli, err := NewRunListItem(runListItem) 78 if err != nil { 79 t.Errorf(`NewRunListItem("%s") did not correctly parse.`, runListItem) 80 } 81 if rli.Name != "my_recipe" { 82 t.Errorf(`NewRunListItem("%s") did not correctly set name`, runListItem) 83 } 84 if rli.Type != "recipe" { 85 t.Errorf(`NewRunListItem("%s") did not correctly set type`, runListItem) 86 } 87 if rli.Version != "" { 88 t.Errorf(`NewRunListItem("%s") did not correctly set version`, runListItem) 89 } 90 } 91 92 func TestBadCase(t *testing.T) { 93 runListItem := "Recipe[my_recipe]" // Capital R 94 _, err := NewRunListItem(runListItem) 95 96 if err == nil { 97 t.Errorf(`NewRunListItem("%s") should have returned and error and it didn't.`, runListItem) 98 } 99 } 100 101 func TestSpaceThrowsError(t *testing.T) { 102 runListItem := "recipe [my_recipe]" 103 _, err := NewRunListItem(runListItem) 104 105 if err == nil { 106 t.Errorf(`NewRunListItem("%s") should have returned and error and it didn't.`, runListItem) 107 } 108 } 109 110 func TestMissingClosingBracketThrowsError(t *testing.T) { 111 runListItem := "recipe[my_recipe" 112 _, err := NewRunListItem(runListItem) 113 114 if err == nil { 115 t.Errorf(`NewRunListItem("%s") should have returned and error and it didn't.`, runListItem) 116 } 117 } 118 119 func TestStringConversion(t *testing.T) { 120 runListItems := []string{ 121 "recipe[my_recipe@1.0.0]", 122 "recipe[my_recipe]", 123 "role[my_recipe]", 124 } 125 126 for _, runListItem := range runListItems { 127 rli, err := NewRunListItem(runListItem) 128 if err != nil || rli.String() != runListItem { 129 t.Errorf(`NewRunListItem("%s").String() does not match %s`, runListItem, runListItem) 130 } 131 } 132 } 133 134 func TestIsRecipe(t *testing.T) { 135 recipe := RunListItem{ 136 Type: "recipe", 137 } 138 if recipe.IsRecipe() != true { 139 t.Error(`IsRecipe() should return true for recipe`) 140 } 141 142 role := RunListItem{ 143 Type: "role", 144 } 145 if role.IsRecipe() != false { 146 t.Error(`IsRecipe() should return false for role`) 147 } 148 } 149 150 func TestIsRole(t *testing.T) { 151 recipe := RunListItem{ 152 Type: "role", 153 } 154 if recipe.IsRole() != true { 155 t.Error(`IsRole() should return true for role`) 156 } 157 158 role := RunListItem{ 159 Type: "recipe", 160 } 161 if role.IsRole() != false { 162 t.Error(`IsRole() should return false for recipe`) 163 } 164 }