@@ -19,6 +19,7 @@ package builder
19
19
import (
20
20
"errors"
21
21
"fmt"
22
+ "path/filepath"
22
23
"runtime"
23
24
"strings"
24
25
"testing"
@@ -29,6 +30,7 @@ import (
29
30
"github.com/containerd/nerdctl/mod/tigron/require"
30
31
"github.com/containerd/nerdctl/mod/tigron/test"
31
32
33
+ "github.com/containerd/nerdctl/v2/pkg/buildkitutil"
32
34
"github.com/containerd/nerdctl/v2/pkg/platformutil"
33
35
"github.com/containerd/nerdctl/v2/pkg/testutil"
34
36
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
@@ -977,3 +979,87 @@ RUN ping -c 5 beta
977
979
978
980
testCase .Run (t )
979
981
}
982
+
983
+ func TestBuildWithBuildkitConfig (t * testing.T ) {
984
+ nerdtest .Setup ()
985
+
986
+ testCase := & test.Case {
987
+ Require : require .All (
988
+ nerdtest .Build ,
989
+ require .Not (nerdtest .Docker ),
990
+ ),
991
+ Setup : func (data test.Data , helpers test.Helpers ) {
992
+ dockerfile := fmt .Sprintf (`FROM %s
993
+ CMD ["echo", "nerdctl-build-test-string"]` , testutil .CommonImage )
994
+ data .Temp ().Save (dockerfile , "Dockerfile" )
995
+ data .Labels ().Set ("buildCtx" , data .Temp ().Path ())
996
+
997
+ },
998
+ SubTests : []* test.Case {
999
+ {
1000
+ Description : "build with buildkit-host" ,
1001
+ Setup : func (data test.Data , helpers test.Helpers ) {
1002
+ // Get BuildkitAddr
1003
+ buildkitAddr , err := buildkitutil .GetBuildkitHost (testutil .Namespace )
1004
+ assert .NilError (helpers .T (), err )
1005
+ buildkitAddr = strings .TrimPrefix (buildkitAddr , "unix://" )
1006
+
1007
+ // Symlink the buildkit Socket for testing
1008
+ symlinkedBuildkitAddr := filepath .Join (data .Temp ().Path (), "buildkit.sock" )
1009
+
1010
+ // Do a negative test to check the setup
1011
+ helpers .Fail ("build" , "-t" , data .Identifier (), "--buildkit-host" , fmt .Sprintf ("unix://%s" , symlinkedBuildkitAddr ), data .Labels ().Get ("buildCtx" ))
1012
+
1013
+ // Test build with the symlinked socket
1014
+ cmd := helpers .Custom ("ln" , "-s" , buildkitAddr , symlinkedBuildkitAddr )
1015
+ cmd .Run (& test.Expected {
1016
+ ExitCode : 0 ,
1017
+ })
1018
+ helpers .Ensure ("build" , "-t" , data .Identifier (), "--buildkit-host" , fmt .Sprintf ("unix://%s" , symlinkedBuildkitAddr ), data .Labels ().Get ("buildCtx" ))
1019
+ },
1020
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
1021
+ return helpers .Command ("run" , "--rm" , data .Identifier ())
1022
+ },
1023
+ Cleanup : func (data test.Data , helpers test.Helpers ) {
1024
+ helpers .Anyhow ("rmi" , "-f" , data .Identifier ())
1025
+ },
1026
+ Expected : test .Expects (0 , nil , expect .Equals ("nerdctl-build-test-string\n " )),
1027
+ },
1028
+ {
1029
+ Description : "build with env specified" ,
1030
+ Setup : func (data test.Data , helpers test.Helpers ) {
1031
+ // Get BuildkitAddr
1032
+ buildkitAddr , err := buildkitutil .GetBuildkitHost (testutil .Namespace )
1033
+ assert .NilError (helpers .T (), err )
1034
+ buildkitAddr = strings .TrimPrefix (buildkitAddr , "unix://" )
1035
+
1036
+ // Symlink the buildkit Socket for testing
1037
+ symlinkedBuildkitAddr := filepath .Join (data .Temp ().Path (), "buildkit-env.sock" )
1038
+
1039
+ // Do a negative test to ensure setting up the env variable is effective
1040
+ cmd := helpers .Command ("build" , "-t" , data .Identifier (), data .Labels ().Get ("buildCtx" ))
1041
+ cmd .Setenv ("BUILDKIT_HOST" , fmt .Sprintf ("unix://%s" , symlinkedBuildkitAddr ))
1042
+ cmd .Run (& test.Expected {ExitCode : expect .ExitCodeGenericFail })
1043
+
1044
+ // Symlink the buildkit socket for testing
1045
+ cmd = helpers .Custom ("ln" , "-s" , buildkitAddr , symlinkedBuildkitAddr )
1046
+ cmd .Run (& test.Expected {
1047
+ ExitCode : 0 ,
1048
+ })
1049
+
1050
+ cmd = helpers .Command ("build" , "-t" , data .Identifier (), data .Labels ().Get ("buildCtx" ))
1051
+ cmd .Setenv ("BUILDKIT_HOST" , fmt .Sprintf ("unix://%s" , symlinkedBuildkitAddr ))
1052
+ cmd .Run (& test.Expected {ExitCode : expect .ExitCodeSuccess })
1053
+ },
1054
+ Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
1055
+ return helpers .Command ("run" , "--rm" , data .Identifier ())
1056
+ },
1057
+ Cleanup : func (data test.Data , helpers test.Helpers ) {
1058
+ helpers .Anyhow ("rmi" , "-f" , data .Identifier ())
1059
+ },
1060
+ Expected : test .Expects (0 , nil , expect .Equals ("nerdctl-build-test-string\n " )),
1061
+ },
1062
+ },
1063
+ }
1064
+ testCase .Run (t )
1065
+ }
0 commit comments