github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/cmd/internal/svc/svc_test.go (about) 1 package svc_test 2 3 import ( 4 "fmt" 5 "github.com/radovskyb/watcher" 6 "github.com/stretchr/testify/assert" 7 "github.com/unionj-cloud/go-doudou/cmd/internal/astutils" 8 "github.com/unionj-cloud/go-doudou/cmd/internal/executils" 9 "github.com/unionj-cloud/go-doudou/cmd/internal/svc" 10 "github.com/unionj-cloud/go-doudou/toolkit/pathutils" 11 "os" 12 "os/exec" 13 "path/filepath" 14 "testing" 15 "time" 16 ) 17 18 var testDir string 19 20 func init() { 21 testDir = pathutils.Abs("testdata") 22 } 23 24 // NewMockSvc new Svc instance for unit test purpose 25 func NewMockSvc(dir string, opts ...svc.SvcOption) svc.ISvc { 26 return svc.NewSvc(dir, svc.WithRunner(mockRunner{})) 27 } 28 29 type mockRunner struct { 30 } 31 32 func (r mockRunner) Output(command string, args ...string) ([]byte, error) { 33 return []byte("go version go1.17.8 darwin/amd64"), nil 34 } 35 36 func (r mockRunner) Run(command string, args ...string) error { 37 cs := []string{"-test.run=TestHelperProcess", "--"} 38 cs = append(cs, args...) 39 cmd := exec.Command(os.Args[0], cs...) 40 cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} 41 cmd.Stdout = os.Stdout 42 cmd.Stderr = os.Stderr 43 if err := cmd.Run(); err != nil { 44 panic(err) 45 } 46 return nil 47 } 48 49 func (r mockRunner) Start(command string, args ...string) (*exec.Cmd, error) { 50 cs := []string{"-test.run=TestHelperProcess", "--"} 51 cs = append(cs, args...) 52 cmd := exec.Command(os.Args[0], cs...) 53 cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} 54 cmd.Stdout = os.Stdout 55 cmd.Stderr = os.Stderr 56 if err := cmd.Start(); err != nil { 57 panic(err) 58 } 59 return cmd, nil 60 } 61 62 func TestSvc_Create(t *testing.T) { 63 type fields struct { 64 Dir string 65 } 66 tests := []struct { 67 name string 68 fields fields 69 }{ 70 { 71 name: "1", 72 fields: fields{ 73 Dir: testDir + "1", 74 }, 75 }, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 receiver := svc.NewSvc(tt.fields.Dir) 80 receiver.Init() 81 defer os.RemoveAll(tt.fields.Dir) 82 }) 83 } 84 } 85 86 func TestSvc_Http(t *testing.T) { 87 type fields struct { 88 Dir string 89 Handler bool 90 Client bool 91 Omitempty bool 92 Doc bool 93 Jsonattrcase string 94 } 95 tests := []struct { 96 name string 97 fields fields 98 }{ 99 { 100 name: "", 101 fields: fields{ 102 Dir: testDir + "2", 103 Handler: true, 104 Client: true, 105 Omitempty: true, 106 Doc: true, 107 Jsonattrcase: "snake", 108 }, 109 }, 110 { 111 name: "", 112 fields: fields{ 113 Dir: testDir + "3", 114 Handler: true, 115 Client: true, 116 Omitempty: false, 117 Doc: false, 118 }, 119 }, 120 { 121 name: "", 122 fields: fields{ 123 Dir: testDir + "4", 124 Handler: false, 125 Client: true, 126 Omitempty: false, 127 Doc: false, 128 }, 129 }, 130 } 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 receiver := svc.NewSvc(tt.fields.Dir) 134 s := receiver.(*svc.Svc) 135 s.Handler = tt.fields.Handler 136 s.Client = tt.fields.Client 137 s.Omitempty = tt.fields.Omitempty 138 s.Doc = tt.fields.Doc 139 s.Jsonattrcase = tt.fields.Jsonattrcase 140 assert.NotPanics(t, func() { 141 receiver.Init() 142 }) 143 defer os.RemoveAll(tt.fields.Dir) 144 assert.NotPanics(t, func() { 145 receiver.Http() 146 }) 147 }) 148 } 149 } 150 151 func Test_checkIc(t *testing.T) { 152 svcfile := testDir + "/svc.go" 153 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 154 type args struct { 155 ic astutils.InterfaceCollector 156 } 157 tests := []struct { 158 name string 159 args args 160 }{ 161 { 162 name: "", 163 args: args{ 164 ic: ic, 165 }, 166 }, 167 } 168 for _, tt := range tests { 169 t.Run(tt.name, func(t *testing.T) { 170 assert.NotPanics(t, func() { 171 svc.ValidateRestApi(testDir, ic) 172 }) 173 }) 174 } 175 } 176 177 func Test_checkIc2(t *testing.T) { 178 svcfile := filepath.Join(testDir, "checkIc2", "svc.go") 179 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 180 type args struct { 181 ic astutils.InterfaceCollector 182 } 183 tests := []struct { 184 name string 185 args args 186 }{ 187 { 188 name: "", 189 args: args{ 190 ic: ic, 191 }, 192 }, 193 } 194 for _, tt := range tests { 195 t.Run(tt.name, func(t *testing.T) { 196 assert.NotPanics(t, func() { 197 svc.ValidateRestApi(testDir, ic) 198 }) 199 }) 200 } 201 } 202 203 func Test_checkIc1(t *testing.T) { 204 svcfile := testDir + "/svcp.go" 205 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 206 type args struct { 207 ic astutils.InterfaceCollector 208 } 209 tests := []struct { 210 name string 211 args args 212 }{ 213 { 214 name: "", 215 args: args{ 216 ic: ic, 217 }, 218 }, 219 } 220 for _, tt := range tests { 221 t.Run(tt.name, func(t *testing.T) { 222 assert.Panics(t, func() { 223 svc.ValidateRestApi(testDir, ic) 224 }) 225 }) 226 } 227 } 228 229 func Test_checkIc_no_interface(t *testing.T) { 230 svcfile := filepath.Join(testDir, "nosvc", "svc.go") 231 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 232 type args struct { 233 ic astutils.InterfaceCollector 234 } 235 tests := []struct { 236 name string 237 args args 238 }{ 239 { 240 name: "", 241 args: args{ 242 ic: ic, 243 }, 244 }, 245 } 246 for _, tt := range tests { 247 t.Run(tt.name, func(t *testing.T) { 248 assert.Panics(t, func() { 249 svc.ValidateRestApi(testDir, ic) 250 }) 251 }) 252 } 253 } 254 255 func Test_checkIc_input_anonystruct(t *testing.T) { 256 svcfile := filepath.Join(testDir, "inputanonystruct", "svc.go") 257 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 258 type args struct { 259 ic astutils.InterfaceCollector 260 } 261 tests := []struct { 262 name string 263 args args 264 }{ 265 { 266 name: "", 267 args: args{ 268 ic: ic, 269 }, 270 }, 271 } 272 for _, tt := range tests { 273 t.Run(tt.name, func(t *testing.T) { 274 assert.Panics(t, func() { 275 svc.ValidateRestApi(testDir, ic) 276 }) 277 }) 278 } 279 } 280 281 func Test_checkIc_output_anonystruct(t *testing.T) { 282 svcfile := filepath.Join(testDir, "outputanonystruct", "svc.go") 283 ic := astutils.BuildInterfaceCollector(svcfile, astutils.ExprString) 284 type args struct { 285 ic astutils.InterfaceCollector 286 } 287 tests := []struct { 288 name string 289 args args 290 }{ 291 { 292 name: "", 293 args: args{ 294 ic: ic, 295 }, 296 }, 297 } 298 for _, tt := range tests { 299 t.Run(tt.name, func(t *testing.T) { 300 assert.Panics(t, func() { 301 svc.ValidateRestApi(testDir, ic) 302 }) 303 }) 304 } 305 } 306 307 func TestSvc_Deploy(t *testing.T) { 308 dir := testDir + "/deploy" 309 receiver := NewMockSvc(dir) 310 receiver.Init() 311 defer os.RemoveAll(dir) 312 receiver.Deploy("") 313 } 314 315 func TestSvc_Shutdown(t *testing.T) { 316 dir := testDir + "/shutdown" 317 receiver := NewMockSvc(dir) 318 receiver.Init() 319 defer os.RemoveAll(dir) 320 receiver.Shutdown("") 321 } 322 323 func Test_validateDataType(t *testing.T) { 324 assert.NotPanics(t, func() { 325 svc.ValidateDataType(testDir) 326 }) 327 } 328 329 func Test_validateDataType_shouldpanic(t *testing.T) { 330 assert.Panics(t, func() { 331 svc.ValidateDataType(pathutils.Abs("testdata1")) 332 }) 333 } 334 335 func Test_GenClient(t *testing.T) { 336 defer os.RemoveAll(filepath.Join(testDir, "client")) 337 receiver := svc.NewSvc(testDir) 338 s := receiver.(*svc.Svc) 339 s.DocPath = filepath.Join(testDir, "testfilesdoc1_openapi3.json") 340 s.ClientPkg = "client" 341 s.Omitempty = true 342 assert.NotPanics(t, func() { 343 s.GenClient() 344 }) 345 } 346 347 func TestSvc_Push(t *testing.T) { 348 receiver := NewMockSvc(pathutils.Abs("./testdata")) 349 s := receiver.(*svc.Svc) 350 s.ImagePrefix = "go-doudou-" 351 s.Push("wubin1989") 352 } 353 354 func TestHelperProcess(*testing.T) { 355 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { 356 return 357 } 358 defer os.Exit(0) 359 fmt.Println("testing helper process") 360 } 361 362 func TestSvc_run(t *testing.T) { 363 s := NewMockSvc(pathutils.Abs("./testdata")) 364 s.DoRun() 365 } 366 367 func TestSvc_restart(t *testing.T) { 368 s := NewMockSvc(pathutils.Abs("./testdata")) 369 s.DoRun() 370 s.DoRestart() 371 } 372 373 func TestSvc_watch(t *testing.T) { 374 s := NewMockSvc(pathutils.Abs("./testdata/change")) 375 s.SetWatcher(watcher.New()) 376 go s.DoWatch() 377 time.Sleep(1 * time.Second) 378 f, _ := os.Create(filepath.Join(s.GetDir(), "change.go")) 379 defer f.Close() 380 f.WriteString("test") 381 time.Sleep(6 * time.Second) 382 s.GetWatcher().Close() 383 } 384 385 func TestSvc_Run(t *testing.T) { 386 s := NewMockSvc(pathutils.Abs("./testdata/change")) 387 s.SetWatcher(watcher.New()) 388 defer s.GetWatcher().Close() 389 go s.Run(true) 390 time.Sleep(1 * time.Second) 391 f, _ := os.Create(filepath.Join(s.GetDir(), "change.go")) 392 defer f.Close() 393 f.WriteString("test") 394 time.Sleep(6 * time.Second) 395 } 396 397 func TestSvc_Run_unwatch(t *testing.T) { 398 s := NewMockSvc("") 399 s.Run(false) 400 } 401 402 func TestSvc_GenClient_DocPathEmpty2(t *testing.T) { 403 type fields struct { 404 dir string 405 Handler bool 406 Client string 407 Omitempty bool 408 Doc bool 409 Jsonattrcase string 410 DocPath string 411 Env string 412 ClientPkg string 413 cmd *exec.Cmd 414 restartSig chan int 415 RoutePatternStrategy int 416 runner executils.Runner 417 w *watcher.Watcher 418 ModName string 419 } 420 tests := []struct { 421 name string 422 fields fields 423 }{ 424 { 425 name: "", 426 fields: fields{}, 427 }, 428 } 429 for _, tt := range tests { 430 t.Run(tt.name, func(t *testing.T) { 431 receiver := svc.Svc{} 432 assert.Panics(t, func() { 433 receiver.GenClient() 434 }) 435 }) 436 } 437 } 438 439 func TestSvc_GenClient_DocPathEmpty1(t *testing.T) { 440 defer os.RemoveAll(filepath.Join(testDir, "openapi", "client")) 441 receiver := svc.NewSvc(filepath.Join(testDir, "openapi")) 442 s := receiver.(*svc.Svc) 443 s.ClientPkg = "client" 444 assert.NotPanics(t, func() { 445 receiver.GenClient() 446 }) 447 } 448 449 func TestNewSvc(t *testing.T) { 450 assert.NotPanics(t, func() { 451 svc.NewSvc("") 452 }) 453 }