-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
config_test.go
802 lines (693 loc) · 28.6 KB
/
config_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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package cmd
import (
"github.com/drud/ddev/pkg/fileutil"
"github.com/drud/ddev/pkg/nodeps"
"github.com/drud/ddev/pkg/version"
"github.com/stretchr/testify/require"
"testing"
"os"
"path/filepath"
"strings"
"fmt"
"github.com/drud/ddev/pkg/ddevapp"
"github.com/drud/ddev/pkg/exec"
"github.com/drud/ddev/pkg/testcommon"
asrt "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
// TestCmdConfigHooks tests that pre-config and post-config hooks run
func TestCmdConfigHooks(t *testing.T) {
// Change to the first DevTestSite for the duration of this test.
site := TestSites[0]
defer site.Chdir()()
assert := asrt.New(t)
app, err := ddevapp.NewApp(site.Dir, true)
assert.NoError(err)
app.Hooks = map[string][]ddevapp.YAMLTask{"post-config": {{"exec-host": "touch hello-post-config-" + app.Name}}, "pre-config": {{"exec-host": "touch hello-pre-config-" + app.Name}}}
err = app.WriteConfig()
assert.NoError(err)
// Make sure we get rid of this for other uses
defer func() {
app.Hooks = nil
_ = app.WriteConfig()
}()
_, err = exec.RunCommand(DdevBin, []string{"config", "--project-type=" + app.Type})
assert.NoError(err)
assert.FileExists("hello-pre-config-" + app.Name)
assert.FileExists("hello-post-config-" + app.Name)
err = os.Remove("hello-pre-config-" + app.Name)
assert.NoError(err)
err = os.Remove("hello-post-config-" + app.Name)
assert.NoError(err)
}
// TestConfigDescribeLocation tries out the --show-config-location flag.
func TestConfigDescribeLocation(t *testing.T) {
assert := asrt.New(t)
// Create a temporary directory and switch to it.
tmpdir := testcommon.CreateTmpDir("config-show-location")
defer testcommon.CleanupDir(tmpdir)
defer testcommon.Chdir(tmpdir)()
// Create a config
args := []string{"config", "--docroot=."}
out, err := exec.RunCommand(DdevBin, args)
assert.NoError(err)
assert.Contains(string(out), "Found a php codebase")
// Now see if we can detect it
args = []string{"config", "--show-config-location"}
out, err = exec.RunCommand(DdevBin, args)
assert.NoError(err)
assert.Contains(string(out), tmpdir)
// Now try it in a directory that doesn't have a config
tmpdir = testcommon.CreateTmpDir("config_show_location")
defer testcommon.CleanupDir(tmpdir)
defer testcommon.Chdir(tmpdir)()
args = []string{"config", "--show-config-location"}
out, err = exec.RunCommand(DdevBin, args)
assert.Error(err)
assert.Contains(string(out), "No project configuration currently exists")
}
// TestConfigWithSitenameFlagDetectsDocroot tests that the docroot is detected when
// flags like --project-name are passed.
func TestConfigWithSitenameFlagDetectsDocroot(t *testing.T) {
assert := asrt.New(t)
// Create a temporary directory and switch to it.
testDocrootName := "web"
tmpdir := testcommon.CreateTmpDir(t.Name())
defer testcommon.CleanupDir(tmpdir)
defer testcommon.Chdir(tmpdir)()
// Create a document root folder.
expectedFile := filepath.Join(tmpdir, testDocrootName, "misc/ahah.js")
err := os.MkdirAll(filepath.Dir(expectedFile), 0777)
assert.NoError(err)
// create index.php that defines docroot
_, err = os.OpenFile(filepath.Join(tmpdir, testDocrootName, "index.php"), os.O_RDONLY|os.O_CREATE, 0666)
assert.NoError(err)
// create the misc/ahah.js that signals drupal6
_, err = os.OpenFile(expectedFile, os.O_RDONLY|os.O_CREATE, 0666)
assert.NoError(err)
// Create a config
args := []string{"config", "--project-name=config-with-sitename", "--php-version=7.2"}
out, err := exec.RunCommand(DdevBin, args)
assert.NoError(err)
defer func() {
_, _ = exec.RunCommand(DdevBin, []string{"delete", "-Oy", "config-with-sitename"})
}()
assert.Contains(string(out), "Found a drupal6 codebase", nodeps.AppTypeDrupal6)
}
// TestConfigSetValues sets all available configuration values using command flags, then confirms that the
// values have been correctly written to the config file.
func TestConfigSetValues(t *testing.T) {
var err error
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)()
// Create an existing docroot
docroot := "web"
if err = os.MkdirAll(filepath.Join(tmpdir, docroot), 0755); err != nil {
t.Errorf("Could not create docroot %s in %s", docroot, tmpdir)
}
err = os.Chdir(tmpdir)
assert.NoError(err)
// Build config args
projectName := "my-project-name"
projectType := nodeps.AppTypeTYPO3
phpVersion := nodeps.PHP71
httpPort := "81"
httpsPort := "444"
hostDBPort := "60001"
hostWebserverPort := "60002"
hostHTTPSPort := "60003"
xdebugEnabled := true
noProjectMount := true
composerVersion := "2.0.0-RC2"
additionalHostnamesSlice := []string{"abc", "123", "xyz"}
additionalHostnames := strings.Join(additionalHostnamesSlice, ",")
additionalFQDNsSlice := []string{"abc.com", "123.pizza", "xyz.co.uk"}
additionalFQDNs := strings.Join(additionalFQDNsSlice, ",")
omitContainersSlice := []string{"dba", "ddev-ssh-agent"}
omitContainers := strings.Join(omitContainersSlice, ",")
webimageExtraPackagesSlice := []string{"php-bcmath", "php7.3-tidy"}
webimageExtraPackages := strings.Join(webimageExtraPackagesSlice, ",")
dbimageExtraPackagesSlice := []string{"netcat", "ncdu"}
dbimageExtraPackages := strings.Join(dbimageExtraPackagesSlice, ",")
uploadDir := filepath.Join("custom", "config", "path")
webserverType := nodeps.WebserverApacheFPM
webImage := "custom-web-image"
dbImage := "custom-db-image"
dbaImage := "custom-dba-image"
webWorkingDir := "/custom/web/dir"
dbWorkingDir := "/custom/db/dir"
dbaWorkingDir := "/custom/dba/dir"
phpMyAdminPort := "5000"
mailhogPort := "5001"
projectTLD := "nowhere.example.com"
useDNSWhenPossible := false
timezone := "America/Chicago"
webEnv := "SOMEENV=some+val"
args := []string{
"config",
"--project-name", projectName,
"--docroot", docroot,
"--project-type", projectType,
"--php-version", phpVersion,
"--composer-version", composerVersion,
"--http-port", httpPort,
"--https-port", httpsPort,
fmt.Sprintf("--xdebug-enabled=%t", xdebugEnabled),
fmt.Sprintf("--no-project-mount=%t", noProjectMount),
"--additional-hostnames", additionalHostnames,
"--additional-fqdns", additionalFQDNs,
"--upload-dir", uploadDir,
"--webserver-type", webserverType,
"--web-image", webImage,
"--db-image", dbImage,
"--dba-image", dbaImage,
"--web-working-dir", webWorkingDir,
"--db-working-dir", dbWorkingDir,
"--dba-working-dir", dbaWorkingDir,
"--omit-containers", omitContainers,
"--host-db-port", hostDBPort,
"--host-webserver-port", hostWebserverPort,
"--host-https-port", hostHTTPSPort,
"--webimage-extra-packages", webimageExtraPackages,
"--dbimage-extra-packages", dbimageExtraPackages,
"--phpmyadmin-port", phpMyAdminPort,
"--mailhog-port", mailhogPort,
"--project-tld", projectTLD,
"--web-environment", webEnv,
fmt.Sprintf("--use-dns-when-possible=%t", useDNSWhenPossible),
"--timezone", timezone,
}
out, err := exec.RunCommand(DdevBin, args)
assert.NoError(err, "error running ddev %v: %v, output=%s", args, err, out)
configFile := filepath.Join(tmpdir, ".ddev", "config.yaml")
configContents, err := os.ReadFile(configFile)
if err != nil {
t.Errorf("Unable to read %s: %v", configFile, err)
}
app := &ddevapp.DdevApp{}
if err = yaml.Unmarshal(configContents, app); err != nil {
t.Errorf("Could not unmarshal %s: %v", configFile, err)
}
assert.Equal(projectName, app.Name)
assert.Equal(docroot, app.Docroot)
assert.Equal(projectType, app.Type)
assert.Equal(phpVersion, app.PHPVersion)
assert.Equal(composerVersion, app.ComposerVersion)
assert.Equal(httpPort, app.RouterHTTPPort)
assert.Equal(httpsPort, app.RouterHTTPSPort)
assert.Equal(hostWebserverPort, app.HostWebserverPort)
assert.Equal(hostDBPort, app.HostDBPort)
assert.Equal(xdebugEnabled, app.XdebugEnabled)
assert.Equal(noProjectMount, app.NoProjectMount)
assert.Equal(additionalHostnamesSlice, app.AdditionalHostnames)
assert.Equal(additionalFQDNsSlice, app.AdditionalFQDNs)
assert.Equal(uploadDir, app.UploadDir)
assert.Equal(webserverType, app.WebserverType)
assert.Equal(webImage, app.WebImage)
assert.Equal(dbImage, app.DBImage)
assert.Equal(dbaImage, app.DBAImage)
assert.Equal(webWorkingDir, app.WorkingDir["web"])
assert.Equal(dbWorkingDir, app.WorkingDir["db"])
assert.Equal(dbaWorkingDir, app.WorkingDir["dba"])
assert.Equal(webimageExtraPackagesSlice, app.WebImageExtraPackages)
assert.Equal(dbimageExtraPackagesSlice, app.DBImageExtraPackages)
assert.Equal(phpMyAdminPort, app.PHPMyAdminPort)
assert.Equal(mailhogPort, app.MailhogPort)
assert.Equal(useDNSWhenPossible, app.UseDNSWhenPossible)
assert.Equal(projectTLD, app.ProjectTLD)
assert.Equal(timezone, app.Timezone)
require.NotEmpty(t, app.WebEnvironment)
assert.Equal(webEnv, app.WebEnvironment[0])
// Test that container images and working dirs can be unset with default flags
args = []string{
"config",
"--web-image-default",
"--db-image-default",
"--dba-image-default",
"--web-working-dir-default",
"--db-working-dir-default",
"--dba-working-dir-default",
}
_, err = exec.RunCommand(DdevBin, args)
assert.NoError(err)
configContents, err = os.ReadFile(configFile)
assert.NoError(err, "Unable to read %s: %v", configFile, err)
app = &ddevapp.DdevApp{}
err = yaml.Unmarshal(configContents, app)
assert.NoError(err, "Could not unmarshal %s: %v", configFile, err)
assert.Equal(app.WebImage, "")
assert.Equal(app.DBImage, "")
assert.Equal(app.DBAImage, "")
assert.Equal(len(app.WorkingDir), 0)
// Test that all container images and working dirs can each be unset with single default images flag
args = []string{
"config",
"--web-image", webImage,
"--db-image", dbImage,
"--dba-image", dbaImage,
"--web-working-dir", webWorkingDir,
"--db-working-dir", dbWorkingDir,
"--dba-working-dir", dbaWorkingDir,
}
_, err = exec.RunCommand(DdevBin, args)
assert.NoError(err)
args = []string{
"config",
"--image-defaults",
"--working-dir-defaults",
}
_, err = exec.RunCommand(DdevBin, args)
assert.NoError(err)
configContents, err = os.ReadFile(configFile)
assert.NoError(err, "Unable to read %s: %v", configFile, err)
app = &ddevapp.DdevApp{}
err = yaml.Unmarshal(configContents, app)
assert.NoError(err, "Could not unmarshal %s: %v", configFile, err)
assert.Equal(app.WebImage, "")
assert.Equal(app.DBImage, "")
assert.Equal(app.DBAImage, "")
assert.Equal(len(app.WorkingDir), 0)
}
// TestConfigInvalidProjectname tests to make sure that invalid projectnames
// are not accepted and valid names are accepted.
func TestConfigInvalidProjectname(t *testing.T) {
var err error
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)()
// Create an existing docroot
docroot := "web"
if err = os.MkdirAll(filepath.Join(tmpdir, docroot), 0755); err != nil {
t.Errorf("Could not create docroot %s in %s", docroot, tmpdir)
}
// Test some valid project names
for _, projName := range []string{"no-spaces-but-hyphens", "UpperAndLower", "should.work.with.dots"} {
args := []string{
"config",
"--project-name", projName,
}
out, err := exec.RunCommand(DdevBin, args)
assert.NoError(err)
assert.NotContains(out, "is not a valid project name")
assert.Contains(out, "You may now run 'ddev start'")
args = []string{
"stop",
"--unlist", projName,
}
_, _ = exec.RunCommand(DdevBin, args)
_ = os.Remove(filepath.Join(tmpdir, ".ddev", "config.yaml"))
}
// test some invalid project names.
for _, projName := range []string{"with spaces", "with_underscores", "no,commas-will-make-it"} {
args := []string{
"config",
"--project-name", projName,
}
out, err := exec.RunCommand(DdevBin, args)
assert.Error(err)
assert.Contains(out, fmt.Sprintf("%s is not a valid project name", projName))
assert.NotContains(out, "You may now run 'ddev start'")
_ = os.Remove(filepath.Join(tmpdir, ".ddev", "config.yaml"))
}
}
// TestCmdDisasterConfig tests to make sure we can't accidentally
// config in homedir, and that config in a subdir is handled correctly
func TestCmdDisasterConfig(t *testing.T) {
var err error
assert := asrt.New(t)
testDir, _ := os.Getwd()
// Make sure we're not allowed to config in home directory.
home, _ := os.UserHomeDir()
err = os.Chdir(home)
assert.NoError(err)
out, err := exec.RunCommand(DdevBin, []string{"config", "--project-type=php"})
assert.Error(err)
assert.Contains(out, "not useful in")
_ = out
err = os.Chdir(testDir)
assert.NoError(err)
// Create a temporary directory and switch to it.
tmpdir := testcommon.CreateTmpDir(t.Name())
defer testcommon.CleanupDir(tmpdir)
defer testcommon.Chdir(tmpdir)()
// Create a project
_, err = exec.RunCommand(DdevBin, []string{"config", "--project-type=php"})
assert.NoError(err)
subdirName := t.Name() + fileutil.RandomFilenameBase()
subdir := filepath.Join(tmpdir, subdirName)
err = os.Mkdir(subdir, 0777)
assert.NoError(err)
err = os.Chdir(subdir)
assert.NoError(err)
// Make sure that ddev config in a subdir gives a warning
out, err = exec.RunCommand(DdevBin, []string{"config", "--project-type=php"})
assert.NoError(err)
assert.Contains(out, "possible you wanted to")
assert.Contains(out, fmt.Sprintf("parent directory %s?", tmpdir))
assert.FileExists(filepath.Join(subdir, ".ddev/config.yaml"))
}
/**
Overall expected behavior of `ddev config` and config.yaml is this:
* If no config.yaml exists when `ddev config` is run, a new one will be created with default
values except whatever is specified in the ddev config args.
* If an item exists in config.yaml, it will be rewritten there after a `ddev config` action
* If the `ddev config` action overrides explicitly the item in config.yaml, the config.yaml
will be rewritten with the updated value and no other changes.
In the case of mariadb_version and dbimage there's quite a lot of overlap sadly, but
the bottom line is that dbimage must win any battles, and if specified either via
`ddev config --db-image` or in the config.yaml, it wins when the dbimage is chosen for the
ddevapp. So:
* `ddev config --mariadb-version` must
* Never add a dbimage line to the config.yaml
* Add or update the mariadb_version line in config.yaml
* `ddev config --db-image` must
* Not change the mariadb_version (although people may be broken if they do it wrong)
* Add a line specifying the dbimage
* ddevapp.NewApp() must:
* Set the dbimage based on any dbimage found in the config.yaml if there is one
* Otherwise (default case) set it based on mariadb_version and the default dbimage.
*/
// TestConfigMariaDBVersion checks to make sure that both
// ddev config --mariadb-version and ddev config --dbimage behave correctly,
// either separately or together.
func TestConfigMariaDBVersion(t *testing.T) {
assert := asrt.New(t)
pwd, _ := os.Getwd()
versionsToTest := nodeps.ValidMariaDBVersions
// Create a temporary directory and switch to it.
tmpDir := testcommon.CreateTmpDir(t.Name())
defer testcommon.CleanupDir(tmpDir)
defer testcommon.Chdir(tmpDir)()
args := []string{
"config",
"--mariadb-version",
}
// Use a config file that does not specify mariadb version
// it should end up with default mariadb version dbimage
_ = os.RemoveAll(filepath.Join(tmpDir, ".ddev"))
_ = os.MkdirAll(filepath.Join(tmpDir, ".ddev"), 0777)
err := fileutil.CopyFile(filepath.Join(pwd, "testdata", t.Name(), "config.yaml.empty"), filepath.Join(tmpDir, ".ddev", "config.yaml"))
assert.NoError(err)
app, err := ddevapp.NewApp(tmpDir, false)
//nolint: errcheck
defer app.Stop(true, false)
assert.NoError(err)
_, err = app.ReadConfig(false)
assert.NoError(err)
assert.Equal(nodeps.MariaDBDefaultVersion, app.MariaDBVersion)
err = app.Start()
assert.NoError(err)
assert.EqualValues(version.GetDBImage(nodeps.MariaDB, nodeps.MariaDBDefaultVersion), app.DBImage)
_ = app.Stop(true, false)
// Verify behavior with no existing config.yaml. It should
// just add mariadb_version into the config and nothing else
// The app.DBImage should end up being the constructed one based
// on the default and the mariadb_version
for cmdMariaDBVersion := range versionsToTest {
_ = os.RemoveAll(filepath.Join(tmpDir, ".ddev"))
configArgs := append(args, cmdMariaDBVersion, "--project-name", "noconfigyet-"+cmdMariaDBVersion)
ddevCmd := "ddev " + strings.Join(configArgs, " ")
out, err := exec.RunCommand(DdevBin, configArgs)
assert.NoError(err)
assert.Contains(out, "You may now run 'ddev start'")
// First test the bare explicit values found in the config.yaml,
// without the NewApp adjustments
app := &ddevapp.DdevApp{}
assert.NoError(err)
err = app.LoadConfigYamlFile(filepath.Join(tmpDir, ".ddev", "config.yaml"))
assert.NoError(err)
assert.Equal(cmdMariaDBVersion, app.MariaDBVersion)
assert.Empty(app.DBImage, "generated config.yaml dbimage should have been empty for command '%s'", ddevCmd)
// Now use NewApp() to load, so that we get the full logic of that function.
app, err = ddevapp.NewApp(tmpDir, false)
//nolint: errcheck
defer app.Stop(true, false)
assert.NoError(err)
_, err = app.ReadConfig(false)
assert.NoError(err)
assert.Equal(cmdMariaDBVersion, app.MariaDBVersion)
_ = app.Stop(true, false)
}
// If we start with a config.yaml specifying basically anything for mariadb_version
// using ddev config --mariadb-version should overwrite it with the specified version.
for cmdMariaDBVersion := range versionsToTest {
for configMariaDBVersion := range versionsToTest {
_ = os.Remove(filepath.Join(tmpDir, ".ddev", "config.yaml"))
err := fileutil.CopyFile(filepath.Join(pwd, "testdata/TestConfigMariaDBVersion", "config.yaml."+configMariaDBVersion), filepath.Join(tmpDir, ".ddev", "config.yaml"))
assert.NoError(err)
configArgs := append(args, cmdMariaDBVersion, "--project-name", "hasconfig-"+cmdMariaDBVersion)
out, err := exec.RunCommand(DdevBin, configArgs)
assert.NoError(err)
assert.Contains(out, "You may now run 'ddev start'")
// First test the bare explicit values found in the config.yaml,
// without the NewApp adjustments
app := &ddevapp.DdevApp{}
assert.NoError(err)
err = app.LoadConfigYamlFile(filepath.Join(tmpDir, ".ddev", "config.yaml"))
assert.NoError(err)
assert.Equal(cmdMariaDBVersion, app.MariaDBVersion)
assert.Empty(app.DBImage)
// Now test with the logical additions made by NewApp()
app, err = ddevapp.NewApp(tmpDir, false)
//nolint: errcheck
defer app.Stop(true, false)
assert.NoError(err)
_, err = app.ReadConfig(false)
assert.NoError(err)
assert.Equal(cmdMariaDBVersion, app.MariaDBVersion)
_ = app.Stop(true, false)
}
}
// If we start with a config.yaml specifying a dbimage, the mariadb_version
// should be set but the dbimage should then override all other values
for cmdMariaDBVersion := range versionsToTest {
for configMariaDBVersion := range versionsToTest {
_ = os.Remove(filepath.Join(tmpDir, ".ddev", "config.yaml"))
testConfigFile := filepath.Join(pwd, "testdata/TestConfigMariaDBVersion", "config.yaml.imagespec."+configMariaDBVersion)
err := fileutil.CopyFile(testConfigFile, filepath.Join(tmpDir, ".ddev", "config.yaml"))
assert.NoError(err)
tmpProjectName := "imagespec-" + cmdMariaDBVersion + ".cmdversion." + cmdMariaDBVersion
configArgs := append(args, cmdMariaDBVersion, "--project-name", tmpProjectName)
out, err := exec.RunCommand(DdevBin, configArgs)
assert.NoError(err)
assert.Contains(out, "You may now run 'ddev start'")
// First test the bare explicit values found in the config.yaml,
// without the NewApp adjustments
app := &ddevapp.DdevApp{}
assert.NoError(err)
err = app.LoadConfigYamlFile(filepath.Join(tmpDir, ".ddev", "config.yaml"))
assert.NoError(err)
assert.Equal(cmdMariaDBVersion, app.MariaDBVersion)
// The ddev config --mariadb-version should *not* have changed the dbimg
// which was in the config.yaml
assert.Equal("somerandomdbimg-"+nodeps.MariaDB+"-"+configMariaDBVersion, app.DBImage, "ddev %s did not result in respect for existing configured dbimg in file=%s", strings.Join(configArgs, " "))
// Now test with NewApp's additions, which should leave app.DBImage alone.
app, err = ddevapp.NewApp(tmpDir, false)
//nolint: errcheck
defer app.Stop(true, false)
assert.NoError(err)
_, err = app.ReadConfig(false)
assert.NoError(err)
assert.Equal(cmdMariaDBVersion, app.MariaDBVersion)
assert.Equal("somerandomdbimg-"+nodeps.MariaDB+"-"+configMariaDBVersion, app.DBImage)
_ = app.Stop(true, false)
}
}
// If we specify both the --mariadb-version and the --dbimage flags,
// both should be set, but the actual NewApp() will result in
// both items, and an app.Start() would use the dbimage specified.
for cmdMariaDBVersion := range versionsToTest {
for cmdDBImageVersion := range versionsToTest {
_ = os.Remove(filepath.Join(tmpDir, ".ddev", "config.yaml"))
configArgs := append(args, cmdMariaDBVersion)
configArgs = append(configArgs, []string{"--db-image", version.GetDBImage(nodeps.MariaDB, cmdDBImageVersion)}...)
configArgs = append(configArgs, "--project-name", "both-args-"+cmdMariaDBVersion)
out, err := exec.RunCommand(DdevBin, configArgs)
assert.NoError(err)
assert.Contains(out, "You may now run 'ddev start'")
// First test the bare explicit values found in the config.yaml,
// without the NewApp adjustments
app := &ddevapp.DdevApp{}
//assert.NoError(err)
err = app.LoadConfigYamlFile(filepath.Join(tmpDir, ".ddev", "config.yaml"))
assert.NoError(err)
assert.Equal(cmdMariaDBVersion, app.MariaDBVersion)
// Loaded app dbimage version should be the explicit value found in
// config.yaml. Or it should be empty if the cmdDbImage is the default
if app.DBImage == "" && cmdDBImageVersion == cmdMariaDBVersion {
// Should have blank dbimage
// if the specified dbimage is the default for this mariadb version
} else {
assert.Equal(version.GetDBImage(nodeps.MariaDB, cmdDBImageVersion), app.DBImage, "Incorrect dbimage '%s' for cmdMariaDBVersion '%s' and cmdDBImageVersion=%s", app.DBImage, cmdMariaDBVersion, cmdDBImageVersion)
}
// Now test with NewApp()'s adjustments
app, err = ddevapp.NewApp(tmpDir, false)
//nolint: errcheck
defer app.Stop(true, false)
assert.NoError(err)
_, err = app.ReadConfig(false)
assert.NoError(err)
assert.Equal(cmdMariaDBVersion, app.MariaDBVersion)
// app.DBImage should be "" if it's the default for that maria version
if cmdDBImageVersion == cmdMariaDBVersion {
assert.Equal("", app.DBImage)
} else {
assert.EqualValues(app.DBImage, version.GetDBImage(nodeps.MariaDB, cmdDBImageVersion))
}
_ = app.Stop(true, false)
}
}
}
// TestConfigMySQLVersion tests various permutations of mysql version
// and mariadb version, etc.
func TestConfigMySQLVersion(t *testing.T) {
assert := asrt.New(t)
versionsToTest := nodeps.ValidMySQLVersions
pwd, _ := os.Getwd()
// Create a temporary directory and switch to it.
testDir := testcommon.CreateTmpDir(t.Name())
_ = os.Chdir(testDir)
t.Cleanup(func() {
err := os.Chdir(pwd)
assert.NoError(err)
err = os.RemoveAll(testDir)
assert.NoError(err)
})
err := os.MkdirAll(filepath.Join(testDir, ".ddev"), 0777)
require.NoError(t, err)
args := []string{
"config",
"--mysql-version=5.6",
"--mariadb-version=10.1",
}
// Try conflicting configurations
_ = os.RemoveAll(filepath.Join(testDir, ".ddev"))
configArgs := append(args, "--project-name=conflicting-db-versions")
out, err := exec.RunCommand(DdevBin, configArgs)
assert.Error(err)
assert.Contains(out, "mysql-version cannot be set if mariadb-version is already set. mariadb-version is set to 10.1")
for cmdMySQLVersion := range versionsToTest {
for cmdDBImageVersion := range versionsToTest {
args := []string{
"config",
}
_ = os.RemoveAll(filepath.Join(testDir, ".ddev"))
projectName := "mysqlversion-" + cmdMySQLVersion + "-dbimageversion-" + cmdDBImageVersion
configArgs := append(args, []string{
"--project-name=" + projectName,
"--db-image=" + version.GetDBImage(nodeps.MySQL, cmdDBImageVersion),
"--mysql-version=" + cmdMySQLVersion,
`--mariadb-version=`,
}...)
ddevCmd := strings.Join(configArgs, " ")
out, err = exec.RunCommand(DdevBin, configArgs)
assert.NoError(err, "failed to run ddevcmd=%s, out=%s", ddevCmd, out)
assert.Contains(out, "You may now run 'ddev start'")
app, err := ddevapp.NewApp(testDir, false)
assert.NoError(err)
// If the two versions are equal, we expect the app.DBImage to be empty
// because it's identical to the image we'd get with just app.MySQLVersion
if cmdMySQLVersion == cmdDBImageVersion {
assert.EqualValues("", app.DBImage)
} else {
assert.EqualValues(version.GetDBImage(nodeps.MySQL, cmdDBImageVersion), app.DBImage)
}
_, _ = exec.RunCommand(DdevBin, []string{"remove", "-RO"})
}
}
}
// TestMariaMysqlConflicts tests the various ways that mysql_version
// and mariadb_version can interact.
func TestMariaMysqlConflicts(t *testing.T) {
assert := asrt.New(t)
pwd, _ := os.Getwd()
// Create a temporary directory and switch to it.
testDir := testcommon.CreateTmpDir(t.Name())
_ = os.Chdir(testDir)
t.Cleanup(func() {
err := os.Chdir(pwd)
assert.NoError(err)
err = os.RemoveAll(testDir)
assert.NoError(err)
})
_ = os.MkdirAll(filepath.Join(testDir, ".ddev"), 0777)
// Use a config file that does not specify mariadb version
// but does specify mysql_version
err := fileutil.CopyFile(filepath.Join(pwd, "testdata", t.Name(), "config.yaml.mysql8only"), filepath.Join(testDir, ".ddev", "config.yaml"))
assert.NoError(err)
app, err := ddevapp.NewApp(testDir, false)
assert.NoError(err)
assert.Equal(nodeps.MySQL80, app.MySQLVersion)
assert.Empty(app.MariaDBVersion)
// Use a config file that specifies both but with empty mariadb_version
err = fileutil.CopyFile(filepath.Join(pwd, "testdata", t.Name(), "config.yaml.mysqlwithemptymaria"), filepath.Join(testDir, ".ddev", "config.yaml"))
assert.NoError(err)
app, err = ddevapp.NewApp(testDir, false)
assert.NoError(err)
assert.Equal(nodeps.MySQL80, app.MySQLVersion)
assert.Empty(app.MariaDBVersion)
// Use a config file that specifies both but with empty mysql_version
err = fileutil.CopyFile(filepath.Join(pwd, "testdata", t.Name(), "config.yaml.mariawithemptymysql"), filepath.Join(testDir, ".ddev", "config.yaml"))
assert.NoError(err)
app, err = ddevapp.NewApp(testDir, false)
assert.NoError(err)
assert.Equal(nodeps.MariaDBDefaultVersion, app.MariaDBVersion)
assert.Empty(app.MySQLVersion)
// Use a config file that specifies neither.
err = fileutil.CopyFile(filepath.Join(pwd, "testdata", t.Name(), "config.yaml.nodbspecified"), filepath.Join(testDir, ".ddev", "config.yaml"))
assert.NoError(err)
app, err = ddevapp.NewApp(testDir, false)
assert.NoError(err)
assert.Equal(nodeps.MariaDBDefaultVersion, app.MariaDBVersion)
assert.Empty(app.MySQLVersion)
}
//TestConfigGitignore checks that our gitignore is ignoring the right things.
func TestConfigGitignore(t *testing.T) {
assert := asrt.New(t)
origDir, _ := os.Getwd()
// Create a temporary directory and switch to it.
testDir := testcommon.CreateTmpDir(t.Name())
err := os.Chdir(testDir)
require.NoError(t, err)
_, err = exec.RunHostCommand(DdevBin, "config", "--auto")
assert.NoError(err)
t.Cleanup(func() {
_, err = exec.RunHostCommand(DdevBin, "delete", "-Oy")
assert.NoError(err)
err = os.Chdir(origDir)
assert.NoError(err)
_, err = exec.RunHostCommand("bash", "-c", fmt.Sprintf("rm -f ~/.ddev/commands/web/%s ~/.ddev/homeadditions/%s", t.Name(), t.Name()))
assert.NoError(err)
err = os.RemoveAll(testDir)
assert.NoError(err)
})
_, err = exec.RunHostCommand("git", "init")
assert.NoError(err)
_, err = exec.RunHostCommand("git", "add", ".")
assert.NoError(err)
out, err := exec.RunHostCommand("git", "status")
assert.NoError(err)
// git status should have one new file, config.yaml
assert.Contains(out, "new file: .ddev/config.yaml")
// .ddev/config.yaml should be the only new file, remove it and check
out = strings.ReplaceAll(out, "new file: .ddev/config.yaml", "")
assert.NotContains(out, "new file:")
_, err = exec.RunHostCommand("bash", "-c", fmt.Sprintf("touch ~/.ddev/commands/web/%s ~/.ddev/homeadditions/%s", t.Name(), t.Name()))
assert.NoError(err)
if err != nil {
out, err = exec.RunHostCommand("bash", "-c", "ls -l ~/.ddev && ls -lR ~/.ddev/commands ~/.ddev/homeadditions")
assert.NoError(err)
t.Logf("Contents of global .ddev: \n=====\n%s\n====", out)
}
_, err = exec.RunHostCommand(DdevBin, "start", "-y")
assert.NoError(err)
statusOut, err := exec.RunHostCommand("bash", "-c", "git status")
assert.NoError(err)
_, err = exec.RunHostCommand("bash", "-c", "git status | grep 'Untracked files'")
assert.Error(err, "Untracked files were found where we didn't expect them: %s", statusOut)
}