9
9
"fmt"
10
10
"io/ioutil"
11
11
"net/http"
12
+ "net/http/httptest"
12
13
"os"
14
+ "os/exec"
13
15
"path/filepath"
14
16
"sort"
15
17
"strings"
@@ -27,6 +29,7 @@ import (
27
29
"github.com/moby/buildkit/util/testutil/httpserver"
28
30
"github.com/moby/buildkit/util/testutil/integration"
29
31
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
32
+ "github.com/pkg/errors"
30
33
"github.com/stretchr/testify/require"
31
34
)
32
35
@@ -41,6 +44,7 @@ func TestIntegration(t *testing.T) {
41
44
testExposeExpansion ,
42
45
testUser ,
43
46
testDockerignore ,
47
+ testDockerfileFromGit ,
44
48
})
45
49
}
46
50
@@ -745,6 +749,104 @@ USER nobody
745
749
require .Equal (t , "nobody" , ociimg .Config .User )
746
750
}
747
751
752
+ func testDockerfileFromGit (t * testing.T , sb integration.Sandbox ) {
753
+ t .Parallel ()
754
+
755
+ gitDir , err := ioutil .TempDir ("" , "buildkit" )
756
+ require .NoError (t , err )
757
+ defer os .RemoveAll (gitDir )
758
+
759
+ dockerfile := `
760
+ FROM busybox AS build
761
+ RUN echo -n fromgit > foo
762
+ FROM scratch
763
+ COPY --from=build foo bar
764
+ `
765
+
766
+ err = ioutil .WriteFile (filepath .Join (gitDir , "Dockerfile" ), []byte (dockerfile ), 0600 )
767
+ require .NoError (t , err )
768
+
769
+ err = runShell (gitDir ,
770
+ "git init" ,
771
+ "git config --local user.email test" ,
772
+ "git config --local user.name test" ,
773
+ "git add Dockerfile" ,
774
+ "git commit -m initial" ,
775
+ "git branch first" ,
776
+ )
777
+ require .NoError (t , err )
778
+
779
+ dockerfile += `
780
+ COPY --from=build foo bar2
781
+ `
782
+
783
+ err = ioutil .WriteFile (filepath .Join (gitDir , "Dockerfile" ), []byte (dockerfile ), 0600 )
784
+ require .NoError (t , err )
785
+
786
+ err = runShell (gitDir ,
787
+ "git add Dockerfile" ,
788
+ "git commit -m second" ,
789
+ "git update-server-info" ,
790
+ )
791
+ require .NoError (t , err )
792
+
793
+ server := httptest .NewServer (http .FileServer (http .Dir (filepath .Join (gitDir , ".git" ))))
794
+ defer server .Close ()
795
+
796
+ destDir , err := ioutil .TempDir ("" , "buildkit" )
797
+ require .NoError (t , err )
798
+ defer os .RemoveAll (destDir )
799
+
800
+ c , err := client .New (sb .Address ())
801
+ require .NoError (t , err )
802
+ defer c .Close ()
803
+
804
+ err = c .Solve (context .TODO (), nil , client.SolveOpt {
805
+ Frontend : "dockerfile.v0" ,
806
+ FrontendAttrs : map [string ]string {
807
+ "context" : "git://" + server .URL + "/#first" ,
808
+ },
809
+ Exporter : client .ExporterLocal ,
810
+ ExporterAttrs : map [string ]string {
811
+ "output" : destDir ,
812
+ },
813
+ }, nil )
814
+ require .NoError (t , err )
815
+
816
+ dt , err := ioutil .ReadFile (filepath .Join (destDir , "bar" ))
817
+ require .NoError (t , err )
818
+ require .Equal (t , "fromgit" , string (dt ))
819
+
820
+ _ , err = os .Stat (filepath .Join (destDir , "bar2" ))
821
+ require .Error (t , err )
822
+ require .True (t , os .IsNotExist (err ))
823
+
824
+ // second request from master branch contains both files
825
+ destDir , err = ioutil .TempDir ("" , "buildkit" )
826
+ require .NoError (t , err )
827
+ defer os .RemoveAll (destDir )
828
+
829
+ err = c .Solve (context .TODO (), nil , client.SolveOpt {
830
+ Frontend : "dockerfile.v0" ,
831
+ FrontendAttrs : map [string ]string {
832
+ "context" : "git://" + server .URL + "/" ,
833
+ },
834
+ Exporter : client .ExporterLocal ,
835
+ ExporterAttrs : map [string ]string {
836
+ "output" : destDir ,
837
+ },
838
+ }, nil )
839
+ require .NoError (t , err )
840
+
841
+ dt , err = ioutil .ReadFile (filepath .Join (destDir , "bar" ))
842
+ require .NoError (t , err )
843
+ require .Equal (t , "fromgit" , string (dt ))
844
+
845
+ dt , err = ioutil .ReadFile (filepath .Join (destDir , "bar2" ))
846
+ require .NoError (t , err )
847
+ require .Equal (t , "fromgit" , string (dt ))
848
+ }
849
+
748
850
func tmpdir (appliers ... fstest.Applier ) (string , error ) {
749
851
tmpdir , err := ioutil .TempDir ("" , "buildkit-dockerfile" )
750
852
if err != nil {
@@ -760,3 +862,14 @@ func dfCmdArgs(ctx, dockerfile string) (string, string) {
760
862
traceFile := filepath .Join (os .TempDir (), "trace" + identity .NewID ())
761
863
return fmt .Sprintf ("build --no-progress --frontend dockerfile.v0 --local context=%s --local dockerfile=%s --trace=%s" , ctx , dockerfile , traceFile ), traceFile
762
864
}
865
+
866
+ func runShell (dir string , cmds ... string ) error {
867
+ for _ , args := range cmds {
868
+ cmd := exec .Command ("sh" , "-c" , args )
869
+ cmd .Dir = dir
870
+ if err := cmd .Run (); err != nil {
871
+ return errors .Wrapf (err , "error running %v" , args )
872
+ }
873
+ }
874
+ return nil
875
+ }
0 commit comments