Browse code

TestBuildWithInaccessibleFilesInContext: use sub-tests

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/11/10 19:55:40
Showing 1 changed files
... ...
@@ -1044,96 +1044,87 @@ func (s *DockerCLIBuildSuite) TestBuildAddBadLinksVolume(c *testing.T) {
1044 1044
 // Issue #5270 - ensure we throw a better error than "unexpected EOF"
1045 1045
 // when we can't access files in the context.
1046 1046
 func (s *DockerCLIBuildSuite) TestBuildWithInaccessibleFilesInContext(c *testing.T) {
1047
+	t := c
1047 1048
 	testRequires(c, DaemonIsLinux, UnixCli, testEnv.IsLocalDaemon) // test uses chown/chmod: not available on windows
1048 1049
 
1049
-	{
1050
+	t.Run("inaccessible files", func(t *testing.T) {
1050 1051
 		const name = "testbuildinaccessiblefiles"
1051
-		ctx := fakecontext.New(c, "",
1052
+		buildCTX := fakecontext.New(t, "",
1052 1053
 			fakecontext.WithDockerfile("FROM scratch\nADD . /foo/"),
1053 1054
 			fakecontext.WithFiles(map[string]string{"fileWithoutReadAccess": "foo"}),
1054 1055
 		)
1055
-		defer ctx.Close()
1056
+		defer buildCTX.Close()
1056 1057
 		// This is used to ensure we detect inaccessible files early during build in the cli client
1057
-		pathToFileWithoutReadAccess := filepath.Join(ctx.Dir, "fileWithoutReadAccess")
1058
+		pathToFileWithoutReadAccess := filepath.Join(buildCTX.Dir, "fileWithoutReadAccess")
1058 1059
 
1059 1060
 		if err := os.Chown(pathToFileWithoutReadAccess, 0, 0); err != nil {
1060
-			c.Fatalf("failed to chown file to root: %s", err)
1061
+			t.Fatalf("failed to chown file to root: %s", err)
1061 1062
 		}
1062 1063
 		if err := os.Chmod(pathToFileWithoutReadAccess, 0o700); err != nil {
1063
-			c.Fatalf("failed to chmod file to 700: %s", err)
1064
+			t.Fatalf("failed to chmod file to 700: %s", err)
1064 1065
 		}
1065 1066
 		result := icmd.RunCmd(icmd.Cmd{
1066
-			Command: []string{"su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name)},
1067
-			Dir:     ctx.Dir,
1067
+			Command: []string{"su", "unprivilegeduser", "-c", fmt.Sprintf("DOCKER_BUILDKIT=0 %s build -t %s .", dockerBinary, name)},
1068
+			Dir:     buildCTX.Dir,
1068 1069
 		})
1069 1070
 		if result.Error == nil {
1070
-			c.Fatalf("build should have failed: %s %s", result.Error, result.Combined())
1071
+			t.Fatalf("build should have failed: %s %s", result.Error, result.Combined())
1071 1072
 		}
1072 1073
 
1073 1074
 		// check if we've detected the failure before we started building
1074
-		if !strings.Contains(result.Combined(), "no permission to read from ") {
1075
-			c.Fatalf("output should've contained the string: no permission to read from but contained: %s", result.Combined())
1076
-		}
1077
-
1078
-		if !strings.Contains(result.Combined(), "error checking context") {
1079
-			c.Fatalf("output should've contained the string: error checking context")
1080
-		}
1081
-	}
1082
-	{
1075
+		assert.Check(t, is.Contains(result.Combined(), "no permission to read from"))
1076
+		assert.Check(t, is.Contains(result.Combined(), "checking context"))
1077
+	})
1078
+	t.Run("inaccessible directory", func(t *testing.T) {
1083 1079
 		const name = "testbuildinaccessibledirectory"
1084
-		ctx := fakecontext.New(c, "",
1080
+		buildCTX := fakecontext.New(t, "",
1085 1081
 			fakecontext.WithDockerfile("FROM scratch\nADD . /foo/"),
1086 1082
 			fakecontext.WithFiles(map[string]string{"directoryWeCantStat/bar": "foo"}),
1087 1083
 		)
1088
-		defer ctx.Close()
1084
+		defer buildCTX.Close()
1089 1085
 		// This is used to ensure we detect inaccessible directories early during build in the cli client
1090
-		pathToDirectoryWithoutReadAccess := filepath.Join(ctx.Dir, "directoryWeCantStat")
1086
+		pathToDirectoryWithoutReadAccess := filepath.Join(buildCTX.Dir, "directoryWeCantStat")
1091 1087
 		pathToFileInDirectoryWithoutReadAccess := filepath.Join(pathToDirectoryWithoutReadAccess, "bar")
1092 1088
 
1093 1089
 		if err := os.Chown(pathToDirectoryWithoutReadAccess, 0, 0); err != nil {
1094
-			c.Fatalf("failed to chown directory to root: %s", err)
1090
+			t.Fatalf("failed to chown directory to root: %s", err)
1095 1091
 		}
1096 1092
 		if err := os.Chmod(pathToDirectoryWithoutReadAccess, 0o444); err != nil {
1097
-			c.Fatalf("failed to chmod directory to 444: %s", err)
1093
+			t.Fatalf("failed to chmod directory to 444: %s", err)
1098 1094
 		}
1099 1095
 		if err := os.Chmod(pathToFileInDirectoryWithoutReadAccess, 0o700); err != nil {
1100
-			c.Fatalf("failed to chmod file to 700: %s", err)
1096
+			t.Fatalf("failed to chmod file to 700: %s", err)
1101 1097
 		}
1102 1098
 
1103 1099
 		result := icmd.RunCmd(icmd.Cmd{
1104
-			Command: []string{"su", "unprivilegeduser", "-c", fmt.Sprintf("%s build -t %s .", dockerBinary, name)},
1105
-			Dir:     ctx.Dir,
1100
+			Command: []string{"su", "unprivilegeduser", "-c", fmt.Sprintf("DOCKER_BUILDKIT=0 %s build -t %s .", dockerBinary, name)},
1101
+			Dir:     buildCTX.Dir,
1106 1102
 		})
1107 1103
 		if result.Error == nil {
1108
-			c.Fatalf("build should have failed: %s %s", result.Error, result.Combined())
1104
+			t.Fatalf("build should have failed: %s %s", result.Error, result.Combined())
1109 1105
 		}
1110 1106
 
1111 1107
 		// check if we've detected the failure before we started building
1112
-		if !strings.Contains(result.Combined(), "can't stat") {
1113
-			c.Fatalf("output should've contained the string: can't access %s", result.Combined())
1114
-		}
1115
-
1116
-		if !strings.Contains(result.Combined(), "error checking context") {
1117
-			c.Fatalf("output should've contained the string: error checking context\ngot:%s", result.Combined())
1118
-		}
1119
-	}
1120
-	{
1108
+		assert.Check(t, is.Contains(result.Combined(), "can't stat"))
1109
+		assert.Check(t, is.Contains(result.Combined(), "checking context"))
1110
+	})
1111
+	t.Run("links OK", func(t *testing.T) {
1121 1112
 		const name = "testlinksok"
1122
-		ctx := fakecontext.New(c, "", fakecontext.WithDockerfile("FROM scratch\nADD . /foo/"))
1123
-		defer ctx.Close()
1113
+		buildCTX := fakecontext.New(t, "", fakecontext.WithDockerfile("FROM scratch\nADD . /foo/"))
1114
+		defer buildCTX.Close()
1124 1115
 
1125 1116
 		target := "../../../../../../../../../../../../../../../../../../../azA"
1126
-		if err := os.Symlink(filepath.Join(ctx.Dir, "g"), target); err != nil {
1127
-			c.Fatal(err)
1117
+		if err := os.Symlink(filepath.Join(buildCTX.Dir, "g"), target); err != nil {
1118
+			t.Fatal(err)
1128 1119
 		}
1129 1120
 		defer os.Remove(target)
1130 1121
 		// This is used to ensure we don't follow links when checking if everything in the context is accessible
1131 1122
 		// This test doesn't require that we run commands as an unprivileged user
1132
-		cli.BuildCmd(c, name, build.WithExternalBuildContext(ctx))
1133
-	}
1134
-	{
1123
+		cli.BuildCmd(t, name, build.WithExternalBuildContext(buildCTX))
1124
+	})
1125
+	t.Run("inaccessible ignored files", func(t *testing.T) {
1135 1126
 		const name = "testbuildignoredinaccessible"
1136
-		ctx := fakecontext.New(c, "",
1127
+		ctx := fakecontext.New(t, "",
1137 1128
 			fakecontext.WithDockerfile("FROM scratch\nADD . /foo/"),
1138 1129
 			fakecontext.WithFiles(map[string]string{
1139 1130
 				"directoryWeCantStat/bar": "foo",
... ...
@@ -1145,24 +1136,24 @@ func (s *DockerCLIBuildSuite) TestBuildWithInaccessibleFilesInContext(c *testing
1145 1145
 		pathToDirectoryWithoutReadAccess := filepath.Join(ctx.Dir, "directoryWeCantStat")
1146 1146
 		pathToFileInDirectoryWithoutReadAccess := filepath.Join(pathToDirectoryWithoutReadAccess, "bar")
1147 1147
 		if err := os.Chown(pathToDirectoryWithoutReadAccess, 0, 0); err != nil {
1148
-			c.Fatalf("failed to chown directory to root: %s", err)
1148
+			t.Fatalf("failed to chown directory to root: %s", err)
1149 1149
 		}
1150 1150
 		if err := os.Chmod(pathToDirectoryWithoutReadAccess, 0o444); err != nil {
1151
-			c.Fatalf("failed to chmod directory to 444: %s", err)
1151
+			t.Fatalf("failed to chmod directory to 444: %s", err)
1152 1152
 		}
1153 1153
 		if err := os.Chmod(pathToFileInDirectoryWithoutReadAccess, 0o700); err != nil {
1154
-			c.Fatalf("failed to chmod file to 700: %s", err)
1154
+			t.Fatalf("failed to chmod file to 700: %s", err)
1155 1155
 		}
1156 1156
 
1157 1157
 		result := icmd.RunCmd(icmd.Cmd{
1158 1158
 			Dir: ctx.Dir,
1159 1159
 			Command: []string{
1160 1160
 				"su", "unprivilegeduser", "-c",
1161
-				fmt.Sprintf("%s build -t %s .", dockerBinary, name),
1161
+				fmt.Sprintf("DOCKER_BUILDKIT=0 %s build -t %s .", dockerBinary, name),
1162 1162
 			},
1163 1163
 		})
1164
-		result.Assert(c, icmd.Expected{})
1165
-	}
1164
+		result.Assert(t, icmd.Success)
1165
+	})
1166 1166
 }
1167 1167
 
1168 1168
 func (s *DockerCLIBuildSuite) TestBuildForceRm(c *testing.T) {