diff --git a/action/container/container.go b/action/container/container.go index dd78ebe8..4ee906f2 100644 --- a/action/container/container.go +++ b/action/container/container.go @@ -28,11 +28,10 @@ type SetupOpts struct { } // Setup for setting up a Docker container via dagger -func Setup(ctx context.Context, client *dagger.Client, opts *SetupOpts) (*dagger.Container, error) { - // Make sure there is a non-empty URL or name provided - if opts.ContainerURL == "" { - return nil, errEmptyURL - } +func Setup(ctx context.Context, client *dagger.Client, opts *SetupOpts, dockerfileDirectoryPath string) (*dagger.Container, error) { + // dockerfileDirectoryPath allows to use Dockerfile and build locally, + // which is handy for testing changes to said Dockerfile without the need to + // have the container uploaded into package registry // None of the directories can be empty string for _, val := range []string{opts.MountContainerDir, opts.MountHostDir, opts.WorkdirContainer} { @@ -46,8 +45,27 @@ func Setup(ctx context.Context, client *dagger.Client, opts *SetupOpts) (*dagger return nil, errDirectoryInvalid } - // Pull docker container - container := client.Container().From(opts.ContainerURL) + // Setup container either from URL or build from Dockerfile + var container *dagger.Container + if dockerfileDirectoryPath == "" { + // Use URL + fmt.Printf("Container setup: URL mode") + + // Make sure there is a non-empty URL or name provided + if opts.ContainerURL == "" { + return nil, errEmptyURL + } + + // Pull docker container + container = client.Container().From(opts.ContainerURL) + } else { + // Use Dockerfile + fmt.Printf("Container setup: Dockerfile mode") + + container = client.Container().Build( + client.Host().Directory(dockerfileDirectoryPath), + ) + } // Mount repository into the container return container. diff --git a/action/container/container_test.go b/action/container/container_test.go index c75d11c0..db47b51c 100644 --- a/action/container/container_test.go +++ b/action/container/container_test.go @@ -38,9 +38,9 @@ func TestSetup(t *testing.T) { name: "empty URL", opts: SetupOpts{ ContainerURL: "", - MountContainerDir: "", - MountHostDir: "", - WorkdirContainer: "", + MountContainerDir: "/src", + MountHostDir: ".", + WorkdirContainer: "/src", }, wantErr: errEmptyURL, lsContains: "", @@ -97,7 +97,7 @@ func TestSetup(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - container, err := Setup(ctx, client, &tc.opts) + container, err := Setup(ctx, client, &tc.opts, "") assert.ErrorIs(t, err, tc.wantErr) if err != nil { // No need to continue on err @@ -238,7 +238,7 @@ func TestGetArtifacts(t *testing.T) { } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - container, err := Setup(ctx, client, &opts) + container, err := Setup(ctx, client, &opts, "") assert.NoError(t, err) // Run commands in container diff --git a/action/recipes/coreboot.go b/action/recipes/coreboot.go index fba69e5b..5be3c892 100644 --- a/action/recipes/coreboot.go +++ b/action/recipes/coreboot.go @@ -11,10 +11,10 @@ import ( ) // coreboot builds coreboot with all blobs and stuff -func coreboot(ctx context.Context, client *dagger.Client, common *commonOpts, opts *corebootOpts, artifacts *[]container.Artifacts) error { +func coreboot(ctx context.Context, client *dagger.Client, common *commonOpts, dockerfileDirectoryPath string, opts *corebootOpts, artifacts *[]container.Artifacts) error { // TODO: get blobs in place! _ = opts envVars := map[string]string{} - return buildWithKernelBuildSystem(ctx, client, common, envVars, artifacts) + return buildWithKernelBuildSystem(ctx, client, common, dockerfileDirectoryPath, envVars, artifacts) } diff --git a/action/recipes/coreboot_test.go b/action/recipes/coreboot_test.go index 57226cda..7d65542a 100644 --- a/action/recipes/coreboot_test.go +++ b/action/recipes/coreboot_test.go @@ -89,7 +89,7 @@ func TestCoreboot(t *testing.T) { } // Try to build coreboot - err = coreboot(ctx, client, &common, &corebootOpts, &artifacts) + err = coreboot(ctx, client, &common, "", &corebootOpts, &artifacts) assert.NoError(t, err) // Check artifacts diff --git a/action/recipes/linux.go b/action/recipes/linux.go index d1f569c7..00d5f2ec 100644 --- a/action/recipes/linux.go +++ b/action/recipes/linux.go @@ -14,7 +14,7 @@ import ( var errUnknownArchCrossCompile = errors.New("unknown architecture for cross-compilation") // linux builds linux kernel -func linux(ctx context.Context, client *dagger.Client, common *commonOpts, opts *linuxOpts, artifacts *[]container.Artifacts) error { +func linux(ctx context.Context, client *dagger.Client, common *commonOpts, dockerfileDirectoryPath string, opts *linuxOpts, artifacts *[]container.Artifacts) error { // Not sure if there will be any linuxOpts _ = opts @@ -37,5 +37,5 @@ func linux(ctx context.Context, client *dagger.Client, common *commonOpts, opts envVars["CROSS_COMPILE"] = val } - return buildWithKernelBuildSystem(ctx, client, common, envVars, artifacts) + return buildWithKernelBuildSystem(ctx, client, common, dockerfileDirectoryPath, envVars, artifacts) } diff --git a/action/recipes/linux_test.go b/action/recipes/linux_test.go index e798243b..52937fcb 100644 --- a/action/recipes/linux_test.go +++ b/action/recipes/linux_test.go @@ -29,6 +29,14 @@ func TestLinux(t *testing.T) { pwd, err := os.Getwd() assert.NoError(t, err) + // Use "" if you want to test containers from github package registry + // Use "../../docker/linux" if you want to test containers built fresh from Dockerfile + dockerfilePath := "" + if true { + dockerfilePath, err = filepath.Abs("../../docker/linux") + assert.NoError(t, err) + } + testCases := []struct { name string linuxVersion string @@ -120,11 +128,13 @@ func TestLinux(t *testing.T) { // Copy over defconfig file into tmpDir/linux defconfigPath := filepath.Join(common.repoPath, common.defconfigPath) + defconfigLocalPath, err := filepath.Abs(filepath.Join(pwd, fmt.Sprintf("../../tests/linux_%s/linux.defconfig", linuxVersion.String()))) + // ^^^ this relative path might be funky + assert.NoError(t, err) err = filesystem.CopyFile( - filepath.Join(pwd, fmt.Sprintf("../../tests/linux_%s/linux.defconfig", linuxVersion.String())), + defconfigLocalPath, defconfigPath, ) - // ^^^ this relative path might be funky assert.NoError(t, err) // Artifacts @@ -145,12 +155,14 @@ func TestLinux(t *testing.T) { } // Try to build linux kernel - err = linux(ctx, client, &common, &linuxOpts, &artifacts) + err = linux(ctx, client, &common, dockerfilePath, &linuxOpts, &artifacts) assert.ErrorIs(t, err, tc.wantErr) // Check artifacts assert.ErrorIs(t, filesystem.CheckFileExists(filepath.Join(outputPath, "vmlinux")), os.ErrExist) assert.ErrorIs(t, filesystem.CheckFileExists(filepath.Join(outputPath, "defconfig")), os.ErrExist) + assert.NoError(t, os.Chdir(pwd)) // just to make sure }) } + assert.NoError(t, os.Chdir(pwd)) // just to make sure } diff --git a/action/recipes/recipes.go b/action/recipes/recipes.go index 2940df21..c978fb3f 100644 --- a/action/recipes/recipes.go +++ b/action/recipes/recipes.go @@ -158,7 +158,7 @@ func Execute(ctx context.Context, client *dagger.Client, action *githubactions.A HostPath: common.outputDir, }, } - return coreboot(ctx, client, &common, &opts, &artifacts) + return coreboot(ctx, client, &common, "", &opts, &artifacts) case "linux": opts, err := linuxGetOpts(action.GetInput) if err != nil { @@ -176,7 +176,7 @@ func Execute(ctx context.Context, client *dagger.Client, action *githubactions.A HostPath: common.outputDir, }, } - return linux(ctx, client, &common, &opts, &artifacts) + return linux(ctx, client, &common, "", &opts, &artifacts) /* case "edk2": return edk2(ctx, action, client) @@ -191,7 +191,7 @@ func Execute(ctx context.Context, client *dagger.Client, action *githubactions.A // buildWithKernelBuildSystem is a generic function to build stuff with Kernel Build System // usable for linux kernel and coreboot // https://www.kernel.org/doc/html/latest/kbuild/index.html -func buildWithKernelBuildSystem(ctx context.Context, client *dagger.Client, common *commonOpts, envVars map[string]string, artifacts *[]container.Artifacts) error { +func buildWithKernelBuildSystem(ctx context.Context, client *dagger.Client, common *commonOpts, dockerfileDirectoryPath string, envVars map[string]string, artifacts *[]container.Artifacts) error { // Spin up container containerOpts := container.SetupOpts{ ContainerURL: common.sdkVersion, @@ -199,7 +199,7 @@ func buildWithKernelBuildSystem(ctx context.Context, client *dagger.Client, comm MountHostDir: common.repoPath, WorkdirContainer: common.containerWorkDir, } - myContainer, err := container.Setup(ctx, client, &containerOpts) + myContainer, err := container.Setup(ctx, client, &containerOpts, dockerfileDirectoryPath) if err != nil { return err }