-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
commands_test.go
259 lines (225 loc) · 9.03 KB
/
commands_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package cmd
import (
"fmt"
"os"
osexec "os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/drud/ddev/pkg/ddevapp"
"github.com/drud/ddev/pkg/exec"
"github.com/drud/ddev/pkg/fileutil"
"github.com/drud/ddev/pkg/nodeps"
"github.com/drud/ddev/pkg/testcommon"
"github.com/drud/ddev/pkg/util"
asrt "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestCustomCommands does basic checks to make sure custom commands work OK.
func TestCustomCommands(t *testing.T) {
assert := asrt.New(t)
runTime := util.TimeTrack(time.Now(), "ddev list")
tmpHome := testcommon.CreateTmpDir(t.Name() + "tempHome")
origHome := os.Getenv("HOME")
origDebug := os.Getenv("DDEV_DEBUG")
// Change the homedir temporarily
err := os.Setenv("HOME", tmpHome)
require.NoError(t, err)
_ = os.Setenv("DDEV_DEBUG", "")
pwd, _ := os.Getwd()
testCustomCommandsDir := filepath.Join(pwd, "testdata", t.Name())
site := TestSites[0]
switchDir := TestSites[0].Chdir()
app, _ := ddevapp.NewApp(TestSites[0].Dir, false, "")
origType := app.Type
t.Cleanup(func() {
runTime()
app.Type = origType
_ = app.WriteConfig()
_ = os.RemoveAll(tmpHome)
_ = os.Setenv("HOME", origHome)
_ = os.Setenv("DDEV_DEBUG", origDebug)
_ = fileutil.PurgeDirectory(filepath.Join(site.Dir, ".ddev", "commands"))
_ = fileutil.PurgeDirectory(filepath.Join(site.Dir, ".ddev", ".global_commands"))
switchDir()
})
err = app.Start()
require.NoError(t, err)
// We can't use the standard getGlobalDDevDir here because *our* global hasn't changed.
// It's changed via $HOME for the ddev subprocess
err = os.MkdirAll(filepath.Join(tmpHome, ".ddev"), 0755)
assert.NoError(err)
tmpHomeGlobalCommandsDir := filepath.Join(tmpHome, ".ddev", "commands")
err = os.RemoveAll(tmpHomeGlobalCommandsDir)
assert.NoError(err)
projectCommandsDir := app.GetConfigPath("commands")
globalCommandsDir := app.GetConfigPath(".global_commands")
_ = os.RemoveAll(globalCommandsDir)
err = fileutil.CopyDir(filepath.Join(testCustomCommandsDir, "global_commands"), tmpHomeGlobalCommandsDir)
require.NoError(t, err)
assert.FileExists(filepath.Join(projectCommandsDir, "db", "mysql"))
assert.FileExists(filepath.Join(projectCommandsDir, "host", "mysqlworkbench.example"))
out, err := exec.RunCommand(DdevBin, []string{})
assert.NoError(err)
assert.Contains(out, "mysql client in db container")
// Test the `ddev mysql` command with stdin
inputFile := filepath.Join(testCustomCommandsDir, "select99.sql")
f, err := os.Open(inputFile)
require.NoError(t, err)
// nolint: errcheck
defer f.Close()
command := osexec.Command(DdevBin, "mysql")
command.Stdin = f
byteOut, err := command.CombinedOutput()
require.NoError(t, err, "Failed ddev mysql; output=%v", string(byteOut))
assert.Contains(string(byteOut), "99\n99\n")
_ = os.RemoveAll(projectCommandsDir)
_ = os.RemoveAll(globalCommandsDir)
// Now copy a project commands and global commands and make sure they show up and execute properly
err = fileutil.CopyDir(filepath.Join(testCustomCommandsDir, "project_commands"), projectCommandsDir)
assert.NoError(err)
out, err = exec.RunCommand(DdevBin, []string{})
assert.NoError(err)
assert.Contains(out, "testhostcmd project (shell host container command)")
assert.Contains(out, "testwebcmd project (shell web container command)")
assert.Contains(out, "testhostglobal global (global shell host container command)")
assert.Contains(out, "testwebglobal global (global shell web container command)")
assert.NotContains(out, "testhostcmd global") //the global testhostcmd should have been overridden by the projct one
assert.NotContains(out, "testwebcmd global") //the global testwebcmd should have been overridden by the projct one
for _, c := range []string{"testhostcmd", "testhostglobal", "testwebcmd", "testwebglobal"} {
args := []string{c, "hostarg1", "hostarg2", "--hostflag1"}
out, err = exec.RunCommand(DdevBin, args)
assert.NoError(err, "Failed to run ddev %s %v", c, args)
expectedHost, _ := os.Hostname()
if !strings.Contains(c, "host") {
expectedHost = site.Name + "-web"
}
assert.Contains(out, fmt.Sprintf("%s was executed with args=hostarg1 hostarg2 --hostflag1 on host %s", c, expectedHost))
}
// Test line breaks in examples
c := "testhostcmd"
args := []string{c, "-h"}
out, err = exec.RunCommand(DdevBin, args)
assert.NoError(err, "Failed to run ddev %s %v", c, args)
assert.Contains(out, "Examples:\n ddev testhostcmd\n ddev testhostcmd -h")
// Provide app configuration
app.Type = nodeps.AppTypePHP
err = app.WriteConfig()
assert.NoError(err)
// Make sure that all the official ddev-provided custom commands are usable by just checking help
for _, c := range []string{"launch", "live", "mysql", "xdebug"} {
_, err = exec.RunCommand(DdevBin, []string{c, "-h"})
assert.NoError(err, "Failed to run ddev %s -h", c)
}
// The various CMS commands should not be available here
for _, c := range []string{"artisan", "drush", "magento", "typo3", "typo3cms", "wp"} {
_, err = exec.RunCommand(DdevBin, []string{c, "-h"})
assert.Error(err, "found command %s when it should not have been there (no error) app.Type=%s", c, app.Type)
}
// TYPO3 commands should only be available for type typo3
app.Type = nodeps.AppTypeTYPO3
_ = app.WriteConfig()
_, _ = exec.RunCommand(DdevBin, nil)
for _, c := range []string{"typo3", "typo3cms"} {
_, err = exec.RunCommand(DdevBin, []string{c, "-h"})
assert.NoError(err)
}
// Drupal types should only be available for type drupal*
app.Type = nodeps.AppTypeDrupal9
_ = app.WriteConfig()
_, _ = exec.RunCommand(DdevBin, nil)
for _, c := range []string{"drush"} {
_, err = exec.RunCommand(DdevBin, []string{c, "-h"})
assert.NoError(err)
}
// Laravel types should only be available for type laravel
app.Type = nodeps.AppTypeLaravel
_ = app.WriteConfig()
_, _ = exec.RunCommand(DdevBin, nil)
for _, c := range []string{"artisan"} {
_, err = exec.RunCommand(DdevBin, []string{c, "-h"})
assert.NoError(err)
}
// Wordpress types should only be available for type drupal*
app.Type = nodeps.AppTypeWordPress
_ = app.WriteConfig()
_, _ = exec.RunCommand(DdevBin, nil)
for _, c := range []string{"wp"} {
_, err = exec.RunCommand(DdevBin, []string{c, "-h"})
assert.NoError(err, "expected to find command %s for app.Type=%s", c, app.Type)
}
// Make sure that the non-command stuff we installed is there
for _, f := range []string{"db/mysqldump.example", "db/README.txt", "web/README.txt", "host/README.txt", "host/phpstorm.example"} {
assert.FileExists(filepath.Join(projectCommandsDir, f))
assert.FileExists(filepath.Join(globalCommandsDir, f))
}
// Make sure we haven't accidentally created anything inappropriate in ~/.ddev
assert.False(fileutil.FileExists(filepath.Join(tmpHome, ".ddev", ".globalcommands")))
assert.False(fileutil.FileExists(filepath.Join(origHome, ".ddev", ".globalcommands")))
}
// TestLaunchCommand tests that the launch command behaves all the ways it should behave
func TestLaunchCommand(t *testing.T) {
assert := asrt.New(t)
pwd, _ := os.Getwd()
// Create a temporary directory and switch to it.
tmpdir := testcommon.CreateTmpDir(t.Name())
err := os.Chdir(tmpdir)
assert.NoError(err)
_ = os.Setenv("DDEV_DEBUG", "true")
app, err := ddevapp.NewApp(tmpdir, false, "")
require.NoError(t, err)
err = app.WriteConfig()
require.NoError(t, err)
t.Cleanup(func() {
err = app.Stop(true, false)
assert.NoError(err)
err = os.Chdir(pwd)
assert.NoError(err)
_ = os.RemoveAll(tmpdir)
})
// This only tests the https port changes, but that might be enough
app.RouterHTTPSPort = "8443"
_ = app.WriteConfig()
err = app.Start()
require.NoError(t, err)
desc, err := app.Describe(false)
require.NoError(t, err)
cases := map[string]string{
"": app.GetPrimaryURL(),
"-p": desc["phpmyadmin_https_url"].(string),
"-m": desc["mailhog_https_url"].(string),
}
for partialCommand, expect := range cases {
// Try with the base URL, simplest case
c := DdevBin + ` launch ` + partialCommand + ` | awk '/FULLURL/ {print $2}'`
out, err := exec.RunCommand("bash", []string{"-c", c})
out = strings.Trim(out, "\n")
assert.NoError(err, `couldn't run "%s"", output=%s`, c, out)
assert.Contains(out, expect, "ouptput of %s is incorrect with app.RouterHTTPSPort=%s: %s", c, app.RouterHTTPSPort, out)
}
}
// TestMysqlCommand tests `ddev mysql``
func TestMysqlCommand(t *testing.T) {
assert := asrt.New(t)
// Create a temporary directory and switch to it.
tmpdir := testcommon.CreateTmpDir(t.Name())
defer testcommon.CleanupDir(tmpdir)
defer testcommon.Chdir(tmpdir)()
app, err := ddevapp.NewApp(tmpdir, false, "")
require.NoError(t, err)
err = app.WriteConfig()
require.NoError(t, err)
err = app.Start()
require.NoError(t, err)
defer func() {
_ = app.Stop(true, false)
}()
// Test ddev mysql -uroot -proot mysql
command := osexec.Command("bash", "-c", "echo 'SHOW TABLES;' | "+DdevBin+" mysql --user=root --password=root --database=mysql")
byteOut, err := command.CombinedOutput()
assert.NoError(err, "byteOut=%v", string(byteOut))
assert.Contains(string(byteOut), `Tables_in_mysql
column_stats
columns_priv`)
}