@@ -545,95 +545,132 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
545
545
require .Equal (t , "hello from test 3!" , strings .TrimSpace (test3Output ))
546
546
}
547
547
548
- func TestBuildFromDockerfile (t * testing.T ) {
549
- // Ensures that a Git repository with a Dockerfile is cloned and built.
550
- srv := gittest .CreateGitServer (t , gittest.Options {
551
- Files : map [string ]string {
552
- "Dockerfile" : "FROM " + testImageAlpine ,
553
- },
554
- })
555
- logbuf := new (bytes.Buffer )
556
- ctr , err := runEnvbuilder (t , runOpts {
557
- env : []string {
558
- envbuilderEnv ("GIT_URL" , srv .URL ),
559
- envbuilderEnv ("DOCKERFILE_PATH" , "Dockerfile" ),
560
- envbuilderEnv ("DOCKER_CONFIG_BASE64" , base64 .StdEncoding .EncodeToString ([]byte (`{"experimental": "enabled"}` ))),
561
- "DOCKER_CONFIG=/config" , // Ignored, because we're setting DOCKER_CONFIG_BASE64.
562
- },
563
- logbuf : logbuf ,
564
- })
565
- require .NoError (t , err )
548
+ func TestBuildFromDockerfileAndConfig (t * testing.T ) {
549
+ t .Parallel ()
566
550
567
- require .Contains (t , logbuf .String (), "Set DOCKER_CONFIG to /.envbuilder" )
568
- require .NotContains (t , logbuf .String (), "Using DOCKER_CONFIG at " )
551
+ type configFile struct {
552
+ name string
553
+ data string
554
+ }
555
+ type testCase struct {
556
+ name string
557
+ env []string
558
+ configFile configFile
559
+ configBase64 string
560
+ validate func (t * testing.T , tc testCase , ctrID , logs string )
561
+ }
569
562
570
- output := execContainer (t , ctr , "echo hello" )
571
- require .Equal (t , "hello" , strings .TrimSpace (output ))
563
+ validateDockerConfig := func (t * testing.T , tc testCase , ctrID , logs string ) {
564
+ t .Helper ()
565
+ got := execContainer (t , ctrID , "cat /docker_config_json" )
566
+ got = strings .TrimSpace (got )
567
+ want := tc .configBase64
568
+ if want == "" {
569
+ want = tc .configFile .data
570
+ }
571
+ if want != "" {
572
+ require .Contains (t , logs , "Set DOCKER_CONFIG to /.envbuilder/.docker" )
573
+ require .Equal (t , want , got )
574
+ }
575
+ }
572
576
573
- // Verify that the Docker configuration secret file is removed
574
- configJSONContainerPath := workingdir .Default .Join ("config.json" )
575
- output = execContainer (t , ctr , "stat " + configJSONContainerPath )
576
- require .Contains (t , output , "No such file or directory" )
577
- }
577
+ configJSONContainerPath := workingdir .Default .Join (".docker" , "config.json" )
578
+ defaultConfigJSON := `{"experimental": "enabled"}`
578
579
579
- func TestBuildDockerConfigPathFromEnv (t * testing.T ) {
580
- // Ensures that a Git repository with a Dockerfile is cloned and built.
581
- srv := gittest .CreateGitServer (t , gittest.Options {
582
- Files : map [string ]string {
583
- "Dockerfile" : "FROM " + testImageAlpine ,
584
- },
585
- })
586
- config , err := json .Marshal (envbuilder.DockerConfig {
587
- AuthConfigs : map [string ]clitypes.AuthConfig {
588
- "mytestimage" : {
589
- Username : "user" ,
590
- Password : "test" ,
580
+ tests := []testCase {
581
+ {
582
+ name : "Plain" ,
583
+ validate : func (t * testing.T , tc testCase , ctrID , logs string ) {
584
+ output := execContainer (t , ctrID , "echo hello" )
585
+ require .Equal (t , "hello" , strings .TrimSpace (output ))
591
586
},
592
587
},
593
- })
594
- require .NoError (t , err )
595
- dir := t .TempDir ()
596
- err = os .WriteFile (filepath .Join (dir , "config.json" ), config , 0o644 )
597
- require .NoError (t , err )
598
-
599
- logbuf := new (bytes.Buffer )
600
- _ , err = runEnvbuilder (t , runOpts {
601
- env : []string {
602
- envbuilderEnv ("GIT_URL" , srv .URL ),
603
- envbuilderEnv ("DOCKERFILE_PATH" , "Dockerfile" ),
604
- "DOCKER_CONFIG=/config" ,
588
+ {
589
+ name : "ConfigBase64" ,
590
+ configBase64 : defaultConfigJSON ,
591
+ validate : validateDockerConfig ,
605
592
},
606
- privileged : true ,
607
- binds : []string {fmt .Sprintf ("%s:/config:ro" , dir )},
608
- logbuf : logbuf ,
609
- })
610
- require .NoError (t , err )
611
-
612
- // Logs that the DOCKER_CONFIG is used.
613
- require .Contains (t , logbuf .String (), "Using DOCKER_CONFIG at /config" )
614
- // Logs registry auth info from existing file.
615
- require .Contains (t , logbuf .String (), "mytestimage" )
616
- }
617
-
618
- func TestBuildDockerConfigDefaultPath (t * testing.T ) {
619
- // Ensures that a Git repository with a Dockerfile is cloned and built.
620
- srv := gittest .CreateGitServer (t , gittest.Options {
621
- Files : map [string ]string {
622
- "Dockerfile" : "FROM " + testImageAlpine ,
593
+ {
594
+ name : "BindConfigToKnownLocation" ,
595
+ configFile : configFile {"/.envbuilder/config.json" , defaultConfigJSON },
596
+ validate : validateDockerConfig ,
623
597
},
624
- })
625
- logbuf := new (bytes.Buffer )
626
- _ , err := runEnvbuilder (t , runOpts {
627
- env : []string {
628
- envbuilderEnv ("GIT_URL" , srv .URL ),
629
- envbuilderEnv ("DOCKERFILE_PATH" , "Dockerfile" ),
598
+ {
599
+ name : "BindConfigToPath" ,
600
+ env : []string {"DOCKER_CONFIG=/secret" },
601
+ configFile : configFile {"/secret/config.json" , defaultConfigJSON },
602
+ validate : validateDockerConfig ,
630
603
},
631
- logbuf : logbuf ,
632
- })
633
- require .NoError (t , err )
604
+ {
605
+ name : "BindConfigToCustomFile" ,
606
+ env : []string {"DOCKER_CONFIG=/secret/my.json" },
607
+ configFile : configFile {"/secret/my.json" , defaultConfigJSON },
608
+ validate : validateDockerConfig ,
609
+ },
610
+ {
611
+ name : "ConfigBase64AndBindUsesBase64" ,
612
+ configFile : configFile {"/.envbuilder/config.json" , `{"experimental": "disabled"}` },
613
+ configBase64 : defaultConfigJSON ,
614
+ validate : validateDockerConfig ,
615
+ },
616
+ {
617
+ name : "ConfigBase64AndCustomConfigPath" ,
618
+ env : []string {"DOCKER_CONFIG=/secret" },
619
+ configBase64 : defaultConfigJSON ,
620
+ validate : validateDockerConfig ,
621
+ },
622
+ }
623
+ for _ , tt := range tests {
624
+ t .Run (tt .name , func (t * testing.T ) {
625
+ t .Parallel ()
626
+
627
+ // Ensures that a Git repository with a Dockerfile is cloned and built.
628
+ srv := gittest .CreateGitServer (t , gittest.Options {
629
+ Files : map [string ]string {
630
+ "Dockerfile" : fmt .Sprintf (`
631
+ FROM %[1]s
632
+ RUN if [ -f %[2]q ]; then cat %[2]q > /docker_config_json; fi
633
+ ` , testImageAlpine , configJSONContainerPath ),
634
+ },
635
+ })
636
+
637
+ logbuf := new (bytes.Buffer )
638
+ opts := runOpts {
639
+ env : []string {
640
+ envbuilderEnv ("GIT_URL" , srv .URL ),
641
+ envbuilderEnv ("DOCKERFILE_PATH" , "Dockerfile" ),
642
+ },
643
+ logbuf : logbuf ,
644
+ }
634
645
635
- require .Contains (t , logbuf .String (), "Set DOCKER_CONFIG to /.envbuilder" )
636
- require .NotContains (t , logbuf .String (), "Using DOCKER_CONFIG at " )
646
+ if tt .configFile .name != "" {
647
+ dir := t .TempDir ()
648
+ configFile := filepath .Join (dir , filepath .Base (tt .configFile .name ))
649
+ err := os .WriteFile (configFile , []byte (tt .configFile .data ), 0o600 )
650
+ require .NoError (t , err , "failed to write config" )
651
+
652
+ opts .privileged = true
653
+ opts .binds = []string {fmt .Sprintf ("%s:%s:rw" , configFile , tt .configFile .name )}
654
+ }
655
+ t .Log (opts .binds )
656
+ if tt .configBase64 != "" {
657
+ enc := base64 .StdEncoding .EncodeToString ([]byte (tt .configBase64 ))
658
+ tt .env = append (tt .env , envbuilderEnv ("DOCKER_CONFIG_BASE64" , enc ))
659
+ }
660
+
661
+ opts .env = append (opts .env , tt .env ... )
662
+
663
+ ctrID , err := runEnvbuilder (t , opts )
664
+ require .NoError (t , err )
665
+
666
+ tt .validate (t , tt , ctrID , logbuf .String ())
667
+
668
+ // Always verify that the Docker configuration secret file is removed.
669
+ configJSONContainerPath := workingdir .Default .Join (".docker" , "config.json" )
670
+ output := execContainer (t , ctrID , "stat " + configJSONContainerPath )
671
+ require .Contains (t , output , "No such file or directory" )
672
+ })
673
+ }
637
674
}
638
675
639
676
func TestBuildPrintBuildOutput (t * testing.T ) {
0 commit comments