@@ -719,6 +719,91 @@ func TestNoMethodFails(t *testing.T) {
719
719
require .ErrorContains (t , err , envbuilder .ErrNoFallbackImage .Error ())
720
720
}
721
721
722
+ func TestDockerfileBuildContext (t * testing.T ) {
723
+ t .Parallel ()
724
+
725
+ inclFile := "myfile"
726
+ dockerfile := fmt .Sprintf (`FROM %s
727
+ COPY %s .` , testImageAlpine , inclFile )
728
+
729
+ tests := []struct {
730
+ name string
731
+ files map [string ]string
732
+ dockerfilePath string
733
+ buildContextPath string
734
+ expectedErr string
735
+ }{
736
+ {
737
+ // Dockerfile & build context are in the same dir, copying inclFile should work.
738
+ name : "same build context (default)" ,
739
+ files : map [string ]string {
740
+ "Dockerfile" : dockerfile ,
741
+ inclFile : "..." ,
742
+ },
743
+ dockerfilePath : "Dockerfile" ,
744
+ buildContextPath : "" , // use default
745
+ expectedErr : "" , // expect no errors
746
+ },
747
+ {
748
+ // Dockerfile & build context are not in the same dir, build context is still the default; this should fail
749
+ // to copy inclFile since it is not in the same dir as the Dockerfile.
750
+ name : "different build context (default)" ,
751
+ files : map [string ]string {
752
+ "a/Dockerfile" : dockerfile ,
753
+ "a/" + inclFile : "..." ,
754
+ },
755
+ dockerfilePath : "a/Dockerfile" ,
756
+ buildContextPath : "" , // use default
757
+ expectedErr : inclFile + ": no such file or directory" ,
758
+ },
759
+ {
760
+ // Dockerfile & build context are not in the same dir, but inclFile is in the default build context dir;
761
+ // this should allow inclFile to be copied. This is probably not desirable though?
762
+ name : "different build context (default, different content roots)" ,
763
+ files : map [string ]string {
764
+ "a/Dockerfile" : dockerfile ,
765
+ inclFile : "..." ,
766
+ },
767
+ dockerfilePath : "a/Dockerfile" ,
768
+ buildContextPath : "" , // use default
769
+ expectedErr : "" ,
770
+ },
771
+ {
772
+ // Dockerfile is not in the default build context dir, but the build context has been overridden; this should
773
+ // allow inclFile to be copied.
774
+ name : "different build context (custom)" ,
775
+ files : map [string ]string {
776
+ "a/Dockerfile" : dockerfile ,
777
+ "a/" + inclFile : "..." ,
778
+ },
779
+ dockerfilePath : "a/Dockerfile" ,
780
+ buildContextPath : "a/" ,
781
+ expectedErr : "" ,
782
+ },
783
+ }
784
+
785
+ for _ , tc := range tests {
786
+ tc := tc
787
+
788
+ t .Run (tc .name , func (t * testing.T ) {
789
+ url := createGitServer (t , gitServerOptions {
790
+ files : tc .files ,
791
+ })
792
+ _ , err := runEnvbuilder (t , options {env : []string {
793
+ "GIT_URL=" + url ,
794
+ "DOCKERFILE_PATH=" + tc .dockerfilePath ,
795
+ "BUILD_CONTEXT_PATH=" + tc .buildContextPath ,
796
+ }})
797
+
798
+ if tc .expectedErr == "" {
799
+ require .NoError (t , err )
800
+ } else {
801
+ require .ErrorContains (t , err , tc .expectedErr )
802
+ }
803
+ })
804
+ }
805
+ }
806
+
722
807
// TestMain runs before all tests to build the envbuilder image.
723
808
func TestMain (m * testing.M ) {
724
809
cleanOldEnvbuilders ()
0 commit comments