github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/test/misc_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package test 5 6 import ( 7 "fmt" 8 "os" 9 "os/exec" 10 "strings" 11 "testing" 12 ) 13 14 func TestNew(t *testing.T) { 15 TrySuite(t, testNew, retryCount) 16 } 17 18 func testNew(t *T) { 19 t.Parallel() 20 21 tcs := []struct { 22 svcName string 23 skipBuild bool 24 skipProtoc bool 25 }{ 26 {svcName: "foobarsvc"}, 27 {svcName: "foo-bar"}, 28 {svcName: "foo-bar-baz"}, 29 } 30 31 for _, tc := range tcs { 32 t.t.Run(tc.svcName, func(t *testing.T) { 33 defer func() { 34 exec.Command("rm", "-r", "./"+tc.svcName).CombinedOutput() 35 }() 36 outp, err := exec.Command("micro", "new", tc.svcName).CombinedOutput() 37 if err != nil { 38 t.Fatal(err) 39 return 40 } 41 if !tc.skipProtoc && !strings.Contains(string(outp), "protoc") { 42 t.Fatalf("micro new lacks protobuf install instructions %v", string(outp)) 43 return 44 } 45 46 lines := strings.Split(string(outp), "\n") 47 // executing install instructions 48 for _, line := range lines { 49 if strings.HasPrefix(line, "go get") { 50 parts := strings.Split(line, " ") 51 getOutp, getErr := exec.Command(parts[0], parts[1:]...).CombinedOutput() 52 if getErr != nil { 53 t.Fatal(string(getOutp)) 54 return 55 } 56 } 57 if !tc.skipProtoc && strings.HasPrefix(line, "make proto") { 58 mp := strings.Split(line, " ") 59 protocCmd := exec.Command(mp[0], mp[1:]...) 60 protocCmd.Dir = "./" + tc.svcName 61 pOutp, pErr := protocCmd.CombinedOutput() 62 if pErr != nil { 63 t.Log("That didn't work ", pErr) 64 t.Fatal(string(pOutp)) 65 return 66 } 67 } 68 } 69 if tc.skipBuild { 70 return 71 } 72 73 // for tests, update the micro import to use the current version of the code. 74 fname := fmt.Sprintf("./%v/go.mod", tc.svcName) 75 f, err := os.OpenFile(fname, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) 76 if err != nil { 77 t.Fatal(string(outp)) 78 return 79 } 80 if _, err := f.WriteString("\nreplace github.com/tickoalcantara12/micro/v3 => ../.."); err != nil { 81 t.Fatal(string(outp)) 82 return 83 } 84 f.Close() 85 86 buildCommand := exec.Command("go", "build") 87 buildCommand.Dir = "./" + tc.svcName 88 outp, err = buildCommand.CombinedOutput() 89 if err != nil { 90 t.Fatal(string(outp)) 91 return 92 } 93 94 }) 95 } 96 97 } 98 99 func TestWrongCommands(t *testing.T) { 100 TrySuite(t, testWrongCommands, retryCount) 101 } 102 103 func testWrongCommands(t *T) { 104 // @TODO this is obviously bad that we have to start a server for this. Why? 105 // What happens is in `cmd/cmd.go` `/service/store/cli/util.go`.SetupCommand is called 106 // which does not run for builtin services and help etc but there is no such exception for 107 // missing/unrecognized commands, so the behaviour below will only happen if a `micro server` 108 // is running. This is most likely because some config/auth wrapper in the background failing. 109 // Fix this later. 110 serv := NewServer(t, WithLogin()) 111 defer serv.Close() 112 if err := serv.Run(); err != nil { 113 return 114 } 115 116 t.Parallel() 117 118 cmd := serv.Command() 119 120 outp, err := cmd.Exec() 121 if err == nil { 122 t.Fatal("Missing command should error") 123 } 124 125 if !strings.Contains(string(outp), "No command") { 126 t.Fatalf("Unexpected output for no command: %v", string(outp)) 127 } 128 129 outp, err = cmd.Exec("asdasd") 130 if err == nil { 131 t.Fatal("Wrong command should error") 132 } 133 134 if !strings.Contains(string(outp), "Unrecognized micro command: asdasd. Please refer to 'micro --help'") { 135 t.Fatalf("Unexpected output for unrecognized command: %v", string(outp)) 136 } 137 138 outp, err = cmd.Exec("config", "asdasd") 139 if err == nil { 140 t.Fatal("Wrong subcommand should error") 141 } 142 143 // @todod for some reason this one returns multiple lines so we don't check for line count now 144 if !strings.Contains(string(outp), "Unrecognized subcommand for micro config") { 145 t.Fatalf("Unexpected output for unrecognized subcommand: %v", string(outp)) 146 } 147 148 } 149 150 // TestHelps ensures all `micro [command name] help` && `micro [command name] --help` commands are working. 151 func TestHelps(t *testing.T) { 152 TrySuite(t, testHelps, retryCount) 153 } 154 155 func testHelps(t *T) { 156 comm := exec.Command("micro", "help") 157 outp, err := comm.CombinedOutput() 158 if err != nil { 159 t.Fatal(err) 160 } 161 162 commands := strings.Split(strings.Split(string(outp), "COMMANDS:")[1], "GLOBAL OPTIONS:")[0] 163 for _, line := range strings.Split(commands, "\n") { 164 trimmed := strings.TrimSpace(line) 165 if len(trimmed) == 0 { 166 continue 167 } 168 // no help for help ;) 169 if strings.Contains(trimmed, "help") { 170 continue 171 } 172 commandName := strings.Split(trimmed, " ")[0] 173 174 outp, err = exec.Command("micro", commandName, "--help").CombinedOutput() 175 if err != nil { 176 t.Fatal(fmt.Errorf("Command %v output is wrong: %v", commandName, string(outp))) 177 break 178 } 179 if !strings.Contains(string(outp), "micro "+commandName+" -") { 180 t.Fatal(commandName + " output is wrong: " + string(outp)) 181 break 182 } 183 } 184 }