@@ -207,6 +207,7 @@ var allTests = integration.TestFuncs(
207
207
testHistoryFinalizeTrace ,
208
208
testEmptyStages ,
209
209
testLocalCustomSessionID ,
210
+ testTargetStageNameArg ,
210
211
)
211
212
212
213
// Tests that depend on the `security.*` entitlements
@@ -1803,6 +1804,121 @@ COPY Dockerfile .
1803
1804
}
1804
1805
}
1805
1806
1807
+ func testTargetStageNameArg (t * testing.T , sb integration.Sandbox ) {
1808
+ integration .SkipOnPlatform (t , "windows" )
1809
+ f := getFrontend (t , sb )
1810
+
1811
+ dockerfile := []byte (`
1812
+ FROM alpine AS base
1813
+ WORKDIR /out
1814
+ RUN echo -n "value:$TARGETSTAGE" > /out/first
1815
+ ARG TARGETSTAGE
1816
+ RUN echo -n "value:$TARGETSTAGE" > /out/second
1817
+
1818
+ FROM scratch AS foo
1819
+ COPY --from=base /out/ /
1820
+
1821
+ FROM scratch
1822
+ COPY --from=base /out/ /
1823
+ ` )
1824
+
1825
+ dir := integration .Tmpdir (
1826
+ t ,
1827
+ fstest .CreateFile ("Dockerfile" , dockerfile , 0600 ),
1828
+ )
1829
+
1830
+ destDir := t .TempDir ()
1831
+
1832
+ c , err := client .New (sb .Context (), sb .Address ())
1833
+ require .NoError (t , err )
1834
+ defer c .Close ()
1835
+
1836
+ _ , err = f .Solve (sb .Context (), c , client.SolveOpt {
1837
+ LocalMounts : map [string ]fsutil.FS {
1838
+ dockerui .DefaultLocalNameDockerfile : dir ,
1839
+ dockerui .DefaultLocalNameContext : dir ,
1840
+ },
1841
+ FrontendAttrs : map [string ]string {
1842
+ "target" : "foo" ,
1843
+ },
1844
+ Exports : []client.ExportEntry {
1845
+ {
1846
+ Type : client .ExporterLocal ,
1847
+ OutputDir : destDir ,
1848
+ },
1849
+ },
1850
+ }, nil )
1851
+ require .NoError (t , err )
1852
+
1853
+ dt , err := os .ReadFile (filepath .Join (destDir , "first" ))
1854
+ require .NoError (t , err )
1855
+ require .Equal (t , "value:" , string (dt ))
1856
+
1857
+ dt , err = os .ReadFile (filepath .Join (destDir , "second" ))
1858
+ require .NoError (t , err )
1859
+ require .Equal (t , "value:foo" , string (dt ))
1860
+
1861
+ destDir = t .TempDir ()
1862
+
1863
+ _ , err = f .Solve (sb .Context (), c , client.SolveOpt {
1864
+ LocalMounts : map [string ]fsutil.FS {
1865
+ dockerui .DefaultLocalNameDockerfile : dir ,
1866
+ dockerui .DefaultLocalNameContext : dir ,
1867
+ },
1868
+ Exports : []client.ExportEntry {
1869
+ {
1870
+ Type : client .ExporterLocal ,
1871
+ OutputDir : destDir ,
1872
+ },
1873
+ },
1874
+ }, nil )
1875
+ require .NoError (t , err )
1876
+
1877
+ dt , err = os .ReadFile (filepath .Join (destDir , "first" ))
1878
+ require .NoError (t , err )
1879
+ require .Equal (t , "value:" , string (dt ))
1880
+
1881
+ dt , err = os .ReadFile (filepath .Join (destDir , "second" ))
1882
+ require .NoError (t , err )
1883
+ require .Equal (t , "value:default" , string (dt ))
1884
+
1885
+ // stage name defined in Dockerfile but not passed in request
1886
+ dockerfile = append (dockerfile , []byte (`
1887
+
1888
+ FROM scratch AS final
1889
+ COPY --from=base /out/ /
1890
+ ` )... )
1891
+
1892
+ dir = integration .Tmpdir (
1893
+ t ,
1894
+ fstest .CreateFile ("Dockerfile" , dockerfile , 0600 ),
1895
+ )
1896
+
1897
+ destDir = t .TempDir ()
1898
+
1899
+ _ , err = f .Solve (sb .Context (), c , client.SolveOpt {
1900
+ LocalMounts : map [string ]fsutil.FS {
1901
+ dockerui .DefaultLocalNameDockerfile : dir ,
1902
+ dockerui .DefaultLocalNameContext : dir ,
1903
+ },
1904
+ Exports : []client.ExportEntry {
1905
+ {
1906
+ Type : client .ExporterLocal ,
1907
+ OutputDir : destDir ,
1908
+ },
1909
+ },
1910
+ }, nil )
1911
+ require .NoError (t , err )
1912
+
1913
+ dt , err = os .ReadFile (filepath .Join (destDir , "first" ))
1914
+ require .NoError (t , err )
1915
+ require .Equal (t , "value:" , string (dt ))
1916
+
1917
+ dt , err = os .ReadFile (filepath .Join (destDir , "second" ))
1918
+ require .NoError (t , err )
1919
+ require .Equal (t , "value:final" , string (dt ))
1920
+ }
1921
+
1806
1922
func testExportMultiPlatform (t * testing.T , sb integration.Sandbox ) {
1807
1923
integration .SkipOnPlatform (t , "windows" )
1808
1924
workers .CheckFeatureCompat (t , sb , workers .FeatureOCIExporter , workers .FeatureMultiPlatform )
0 commit comments