github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/prompt/prompt_test.go (about) 1 /* 2 Copyright 2020 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package prompt 18 19 import ( 20 "errors" 21 "io/ioutil" 22 "testing" 23 24 "github.com/AlecAivazis/survey/v2" 25 26 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" 27 "github.com/GoogleContainerTools/skaffold/testutil" 28 ) 29 30 func TestWriteSkaffoldConfig(t *testing.T) { 31 tests := []struct { 32 description string 33 config *latest.SkaffoldConfig 34 promptResponse bool 35 expectedDone bool 36 shouldErr bool 37 }{ 38 { 39 description: "yes response", 40 config: &latest.SkaffoldConfig{}, 41 promptResponse: true, 42 expectedDone: false, 43 shouldErr: false, 44 }, 45 { 46 description: "no response", 47 config: &latest.SkaffoldConfig{}, 48 promptResponse: false, 49 expectedDone: true, 50 shouldErr: false, 51 }, 52 { 53 description: "error", 54 config: &latest.SkaffoldConfig{}, 55 promptResponse: false, 56 expectedDone: true, 57 shouldErr: true, 58 }, 59 } 60 for _, test := range tests { 61 testutil.Run(t, test.description, func(t *testutil.T) { 62 t.Override(&askOne, func(_ survey.Prompt, response interface{}, _ ...survey.AskOpt) error { 63 r := response.(*bool) 64 *r = test.promptResponse 65 66 if test.shouldErr { 67 return errors.New("error") 68 } 69 return nil 70 }) 71 72 done, err := WriteSkaffoldConfig(ioutil.Discard, []byte{}, nil, "") 73 t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedDone, done) 74 }) 75 } 76 } 77 78 func TestChooseBuilders(t *testing.T) { 79 tests := []struct { 80 description string 81 choices []string 82 promptResponse []string 83 expected []string 84 shouldErr bool 85 }{ 86 { 87 description: "couple chosen", 88 choices: []string{"a", "b", "c"}, 89 promptResponse: []string{"a", "c"}, 90 expected: []string{"a", "c"}, 91 shouldErr: false, 92 }, 93 { 94 description: "none chosen", 95 choices: []string{"a", "b", "c"}, 96 promptResponse: []string{}, 97 expected: []string{}, 98 shouldErr: false, 99 }, 100 { 101 description: "error", 102 choices: []string{"a", "b", "c"}, 103 promptResponse: []string{"a", "b"}, 104 expected: []string{}, 105 shouldErr: true, 106 }, 107 } 108 for _, test := range tests { 109 testutil.Run(t, test.description, func(t *testutil.T) { 110 t.Override(&askOne, func(_ survey.Prompt, response interface{}, _ ...survey.AskOpt) error { 111 r := response.(*[]string) 112 *r = test.promptResponse 113 114 if test.shouldErr { 115 return errors.New("error") 116 } 117 return nil 118 }) 119 120 chosen, err := ChooseBuildersFunc(test.choices) 121 t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, chosen) 122 }) 123 } 124 } 125 126 func TestPortForwardResource(t *testing.T) { 127 tests := []struct { 128 description string 129 config *latest.SkaffoldConfig 130 promptResponse string 131 expected int 132 shouldErr bool 133 }{ 134 { 135 description: "valid response", 136 config: &latest.SkaffoldConfig{}, 137 promptResponse: "8080", 138 expected: 8080, 139 shouldErr: false, 140 }, 141 { 142 description: "empty response", 143 config: &latest.SkaffoldConfig{}, 144 promptResponse: "", 145 expected: 0, 146 shouldErr: false, 147 }, 148 { 149 description: "error", 150 config: &latest.SkaffoldConfig{}, 151 promptResponse: "", 152 expected: 0, 153 shouldErr: true, 154 }, 155 } 156 for _, test := range tests { 157 testutil.Run(t, test.description, func(t *testutil.T) { 158 t.Override(&ask, func(_ []*survey.Question, response interface{}, _ ...survey.AskOpt) error { 159 r := response.(*string) 160 *r = test.promptResponse 161 162 if test.shouldErr { 163 return errors.New("error") 164 } 165 return nil 166 }) 167 168 port, err := portForwardResource(ioutil.Discard, "image-name") 169 t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, port) 170 }) 171 } 172 } 173 174 func TestConfirmInitOptions(t *testing.T) { 175 tests := []struct { 176 description string 177 config *latest.SkaffoldConfig 178 promptResponse bool 179 expectedDone bool 180 shouldErr bool 181 }{ 182 { 183 description: "yes response", 184 config: &latest.SkaffoldConfig{}, 185 promptResponse: true, 186 expectedDone: false, 187 shouldErr: false, 188 }, 189 { 190 description: "no response", 191 config: &latest.SkaffoldConfig{}, 192 promptResponse: false, 193 expectedDone: true, 194 shouldErr: false, 195 }, 196 { 197 description: "error", 198 config: &latest.SkaffoldConfig{}, 199 promptResponse: false, 200 expectedDone: true, 201 shouldErr: true, 202 }, 203 } 204 for _, test := range tests { 205 testutil.Run(t, test.description, func(t *testutil.T) { 206 t.Override(&askOne, func(_ survey.Prompt, response interface{}, _ ...survey.AskOpt) error { 207 r := response.(*bool) 208 *r = test.promptResponse 209 210 if test.shouldErr { 211 return errors.New("error") 212 } 213 return nil 214 }) 215 216 done, err := ConfirmInitOptions(ioutil.Discard, test.config) 217 t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedDone, done) 218 }) 219 } 220 }