From 7fa118395f8c432a0d76330fc67227f5b9240fec Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Fri, 15 Nov 2024 16:20:01 +0530 Subject: [PATCH 01/17] feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. --- kaniko.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kaniko.go b/kaniko.go index 5437277..7886b8c 100644 --- a/kaniko.go +++ b/kaniko.go @@ -259,6 +259,9 @@ func (p Plugin) Exec() error { if p.Build.TarPath != "" { cmdArgs = append(cmdArgs, fmt.Sprintf("--tar-path=%s", p.Build.TarPath)) + if err := WriteEnvToFile("PLUGIN_TAR_PATH", p.Build.TarPath); err != nil { + fmt.Fprintf(os.Stderr, "failed to write tar path to output: %v\n", err) + } } if p.Build.CacheCopyLayers { @@ -428,3 +431,20 @@ func getDigest(digestFile string) string { func trace(cmd *exec.Cmd) { fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " ")) } + +func WriteEnvToFile(key, value string) error { + outputFile, err := os.OpenFile(os.Getenv("DRONE_OUTPUT"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to open output file: %v\n", err) + return err + } + defer outputFile.Close() + + fmt.Fprintf(os.Stdout, "Writing %s=%s to DRONE_OUTPUT\n", key, value) + _, err = outputFile.WriteString(key + "=" + value + "\n") + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to write to env: %v\n", err) + return err + } + return nil +} From 82d872fea86eb53cf1911094159f6be7e376fb34 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Fri, 15 Nov 2024 17:18:50 +0530 Subject: [PATCH 02/17] feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. --- kaniko.go | 22 +--------------------- pkg/output/output.go | 5 +++-- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/kaniko.go b/kaniko.go index 7886b8c..25f38ff 100644 --- a/kaniko.go +++ b/kaniko.go @@ -259,9 +259,6 @@ func (p Plugin) Exec() error { if p.Build.TarPath != "" { cmdArgs = append(cmdArgs, fmt.Sprintf("--tar-path=%s", p.Build.TarPath)) - if err := WriteEnvToFile("PLUGIN_TAR_PATH", p.Build.TarPath); err != nil { - fmt.Fprintf(os.Stderr, "failed to write tar path to output: %v\n", err) - } } if p.Build.CacheCopyLayers { @@ -410,7 +407,7 @@ func (p Plugin) Exec() error { } if p.Output.OutputFile != "" { - if err = output.WritePluginOutputFile(p.Output.OutputFile, getDigest(p.Build.DigestFile)); err != nil { + if err = output.WritePluginOutputFile(p.Output.OutputFile, getDigest(p.Build.DigestFile), p.Build.TarPath); err != nil { fmt.Fprintf(os.Stderr, "failed to write plugin output file at path: %s with error: %s\n", p.Output.OutputFile, err) } } @@ -431,20 +428,3 @@ func getDigest(digestFile string) string { func trace(cmd *exec.Cmd) { fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " ")) } - -func WriteEnvToFile(key, value string) error { - outputFile, err := os.OpenFile(os.Getenv("DRONE_OUTPUT"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to open output file: %v\n", err) - return err - } - defer outputFile.Close() - - fmt.Fprintf(os.Stdout, "Writing %s=%s to DRONE_OUTPUT\n", key, value) - _, err = outputFile.WriteString(key + "=" + value + "\n") - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to write to env: %v\n", err) - return err - } - return nil -} diff --git a/pkg/output/output.go b/pkg/output/output.go index de6349d..c087a4a 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -4,9 +4,10 @@ import ( "github.com/joho/godotenv" ) -func WritePluginOutputFile(outputFilePath, digest string) error { +func WritePluginOutputFile(outputFilePath, digest string, pluginTarPath string) error { output := map[string]string{ - "digest": digest, + "digest": digest, + "PLUGIN_TAR_PATH": pluginTarPath, } return godotenv.Write(output, outputFilePath) } From 980189e54ea80180082254cdb12a8f1713c5cf76 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Fri, 15 Nov 2024 17:54:14 +0530 Subject: [PATCH 03/17] feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. --- kaniko.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kaniko.go b/kaniko.go index 25f38ff..492e0bb 100644 --- a/kaniko.go +++ b/kaniko.go @@ -407,7 +407,7 @@ func (p Plugin) Exec() error { } if p.Output.OutputFile != "" { - if err = output.WritePluginOutputFile(p.Output.OutputFile, getDigest(p.Build.DigestFile), p.Build.TarPath); err != nil { + if err = output.WritePluginOutputFile(p.Output.OutputFile, getDigest(p.Build.DigestFile), getTarPath(p.Build.TarPath)); err != nil { fmt.Fprintf(os.Stderr, "failed to write plugin output file at path: %s with error: %s\n", p.Output.OutputFile, err) } } @@ -415,6 +415,10 @@ func (p Plugin) Exec() error { return nil } +func getTarPath(tarPath string) string { + return tarPath +} + func getDigest(digestFile string) string { content, err := ioutil.ReadFile(digestFile) if err != nil { From 637b5e9a215dcc2c8598c4f2d1dfd7d39c216dc3 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Fri, 15 Nov 2024 18:09:00 +0530 Subject: [PATCH 04/17] feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. --- pkg/output/output.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/output/output.go b/pkg/output/output.go index c087a4a..2f1f6ac 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -6,8 +6,8 @@ import ( func WritePluginOutputFile(outputFilePath, digest string, pluginTarPath string) error { output := map[string]string{ - "digest": digest, - "PLUGIN_TAR_PATH": pluginTarPath, + "digest": digest, + "tar_path": pluginTarPath, } return godotenv.Write(output, outputFilePath) } From f6f5632caafc98ed72a8bb57f04d5af3f7bd364f Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Mon, 18 Nov 2024 13:24:42 +0530 Subject: [PATCH 05/17] feat:[CI-15236]: Test commit. --- pkg/output/output.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/output/output.go b/pkg/output/output.go index 2f1f6ac..1f27501 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -6,8 +6,8 @@ import ( func WritePluginOutputFile(outputFilePath, digest string, pluginTarPath string) error { output := map[string]string{ - "digest": digest, - "tar_path": pluginTarPath, + "digest": pluginTarPath, + //"tar_path": pluginTarPath, } return godotenv.Write(output, outputFilePath) } From fccf381535ffe9eff49b10b69f92b8f23f298316 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Mon, 18 Nov 2024 13:59:26 +0530 Subject: [PATCH 06/17] feat:[CI-15236]: Test commit. --- kaniko.go | 9 +++++++++ pkg/output/output.go | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/kaniko.go b/kaniko.go index 492e0bb..fb6dc94 100644 --- a/kaniko.go +++ b/kaniko.go @@ -416,6 +416,15 @@ func (p Plugin) Exec() error { } func getTarPath(tarPath string) string { + if tarPath == "" { + fmt.Fprintf(os.Stderr, "Warning: tar_path is empty\n") + return "" + } + if _, err := os.Stat(tarPath); err != nil && os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "Warning: tar path does not exist: %s\n", tarPath) + return "" + } + fmt.Fprintf(os.Stdout, "Debug: tar path found: %s\n", tarPath) return tarPath } diff --git a/pkg/output/output.go b/pkg/output/output.go index 1f27501..cec2a26 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -1,13 +1,39 @@ package output import ( + "fmt" "github.com/joho/godotenv" ) func WritePluginOutputFile(outputFilePath, digest string, pluginTarPath string) error { - output := map[string]string{ - "digest": pluginTarPath, - //"tar_path": pluginTarPath, + fmt.Printf("Debug: Writing output file with digest: %s and tar_path: %s\n", digest, pluginTarPath) + output := make(map[string]string) + if digest != "" { + output["digest"] = digest } - return godotenv.Write(output, outputFilePath) + if pluginTarPath != "" { + output["tar_path"] = pluginTarPath + fmt.Printf("Debug: Added tar_path to output map: %s\n", pluginTarPath) + } else { + fmt.Printf("Warning: tar_path is empty, skipping\n") + } + + // Verify we have at least one value to write + if len(output) == 0 { + return fmt.Errorf("no values to write to output file") + } + + // Write the file + if err := godotenv.Write(output, outputFilePath); err != nil { + return fmt.Errorf("failed to write output file: %w", err) + } + + // Verify the file was written correctly + written, err := godotenv.Read(outputFilePath) + if err != nil { + return fmt.Errorf("failed to verify written output: %w", err) + } + + fmt.Printf("Debug: Verified written output - tar_path: %s\n", written["tar_path"]) + return nil } From 7f57d529a31301d60777d035580a2afc997d46dc Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Mon, 18 Nov 2024 15:27:26 +0530 Subject: [PATCH 07/17] feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. --- kaniko.go | 1 - pkg/output/output.go | 22 +++------------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/kaniko.go b/kaniko.go index fb6dc94..2d252c4 100644 --- a/kaniko.go +++ b/kaniko.go @@ -424,7 +424,6 @@ func getTarPath(tarPath string) string { fmt.Fprintf(os.Stderr, "Warning: tar path does not exist: %s\n", tarPath) return "" } - fmt.Fprintf(os.Stdout, "Debug: tar path found: %s\n", tarPath) return tarPath } diff --git a/pkg/output/output.go b/pkg/output/output.go index cec2a26..d05a2f3 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -6,34 +6,18 @@ import ( ) func WritePluginOutputFile(outputFilePath, digest string, pluginTarPath string) error { - fmt.Printf("Debug: Writing output file with digest: %s and tar_path: %s\n", digest, pluginTarPath) output := make(map[string]string) if digest != "" { output["digest"] = digest } + if pluginTarPath != "" { - output["tar_path"] = pluginTarPath - fmt.Printf("Debug: Added tar_path to output map: %s\n", pluginTarPath) - } else { - fmt.Printf("Warning: tar_path is empty, skipping\n") + output["IMAGE_TAR_PATH"] = pluginTarPath } - // Verify we have at least one value to write if len(output) == 0 { return fmt.Errorf("no values to write to output file") } - // Write the file - if err := godotenv.Write(output, outputFilePath); err != nil { - return fmt.Errorf("failed to write output file: %w", err) - } - - // Verify the file was written correctly - written, err := godotenv.Read(outputFilePath) - if err != nil { - return fmt.Errorf("failed to verify written output: %w", err) - } - - fmt.Printf("Debug: Verified written output - tar_path: %s\n", written["tar_path"]) - return nil + return godotenv.Write(output, outputFilePath) } From ea1f8ae72860d6e56099efe9e2835c31b2bd785e Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Mon, 25 Nov 2024 14:42:50 +0530 Subject: [PATCH 08/17] feat:[CI-15236]: Directory check and UTs. --- kaniko.go | 4 +- kaniko_test.go | 93 +++++++++++++++++++++++++++++++ pkg/output/output_test.go | 113 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 pkg/output/output_test.go diff --git a/kaniko.go b/kaniko.go index 2d252c4..6555837 100644 --- a/kaniko.go +++ b/kaniko.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "os/exec" + "path/filepath" "strings" "github.com/drone/drone-kaniko/pkg/artifact" @@ -420,7 +421,8 @@ func getTarPath(tarPath string) string { fmt.Fprintf(os.Stderr, "Warning: tar_path is empty\n") return "" } - if _, err := os.Stat(tarPath); err != nil && os.IsNotExist(err) { + tarDir := filepath.Dir(tarPath) + if _, err := os.Stat(tarDir); err != nil && os.IsNotExist(err) { fmt.Fprintf(os.Stderr, "Warning: tar path does not exist: %s\n", tarPath) return "" } diff --git a/kaniko_test.go b/kaniko_test.go index e61c1cb..c9fcded 100644 --- a/kaniko_test.go +++ b/kaniko_test.go @@ -1,6 +1,8 @@ package kaniko import ( + "os" + "path/filepath" "testing" "github.com/google/go-cmp/cmp" @@ -148,3 +150,94 @@ func TestBuild_AutoTags(t *testing.T) { } }) } + +func TestTarPathValidation(t *testing.T) { + tests := []struct { + name string + tarPath string + setup func(string) error + cleanup func(string) error + expectSuccess bool + privileged bool + }{ + { + name: "valid_path_privileged", + tarPath: "/tmp/test/image.tar", + setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, + cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + expectSuccess: true, + privileged: true, + }, + { + name: "valid_path_unprivileged", + tarPath: "./test/image.tar", + setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, + cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + expectSuccess: true, + privileged: false, + }, + { + name: "invalid_path_no_permissions", + tarPath: "/root/test/image.tar", + setup: func(path string) error { return nil }, + cleanup: func(path string) error { return nil }, + expectSuccess: false, + privileged: false, + }, + { + name: "empty_path", + tarPath: "", + setup: func(path string) error { return nil }, + cleanup: func(path string) error { return nil }, + expectSuccess: false, + privileged: false, + }, + { + name: "relative_path_dots", + tarPath: "../test/image.tar", + setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, + cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + expectSuccess: true, + privileged: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Skip privileged tests if not running as root + if tt.privileged && os.Getuid() != 0 { + t.Skip("Skipping privileged test as not running as root") + } + + if err := tt.setup(tt.tarPath); err != nil { + t.Fatalf("Setup failed: %v", err) + } + defer tt.cleanup(tt.tarPath) + + p := Plugin{ + Build: Build{ + TarPath: tt.tarPath, + }, + } + + tarDir := filepath.Dir(p.Build.TarPath) + err := os.MkdirAll(tarDir, 0755) + if tt.expectSuccess { + if err != nil { + t.Errorf("Expected directory creation to succeed, got error: %v", err) + } + if _, err := os.Stat(tarDir); err != nil { + t.Errorf("Expected directory to exist after creation, got error: %v", err) + } + } + + result := getTarPath(tt.tarPath) + if tt.expectSuccess && result == "" { + t.Error("Expected non-empty tar path, got empty string") + } + if !tt.expectSuccess && result != "" { + t.Error("Expected empty tar path, got non-empty string") + } + }) + } +} diff --git a/pkg/output/output_test.go b/pkg/output/output_test.go new file mode 100644 index 0000000..4180877 --- /dev/null +++ b/pkg/output/output_test.go @@ -0,0 +1,113 @@ +package output + +import ( + "os" + "path/filepath" + "testing" +) + +func TestWritePluginOutputFile(t *testing.T) { + tests := []struct { + name string + outputPath string + digest string + tarPath string + setup func(string) error + cleanup func(string) error + expectError bool + privileged bool + }{ + { + name: "valid_output_privileged", + outputPath: "/tmp/test/output.env", + digest: "sha256:test", + tarPath: "/tmp/test/image.tar", + setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, + cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + expectError: false, + privileged: true, + }, + { + name: "valid_output_unprivileged", + outputPath: "./test/output.env", + digest: "sha256:test", + tarPath: "./test/image.tar", + setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, + cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + expectError: false, + privileged: false, + }, + { + name: "invalid_output_path", + outputPath: "/root/test/output.env", + digest: "sha256:test", + tarPath: "/root/test/image.tar", + setup: func(path string) error { return nil }, + cleanup: func(path string) error { return nil }, + expectError: true, + privileged: false, + }, + { + name: "empty_values", + outputPath: "./test/output.env", + digest: "", + tarPath: "", + setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, + cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + expectError: true, + privileged: false, + }, + { + name: "digest_only", + outputPath: "./test/output.env", + digest: "sha256:test", + tarPath: "", + setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, + cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + expectError: false, + privileged: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Skip privileged tests if not running as root + if tt.privileged && os.Getuid() != 0 { + t.Skip("Skipping privileged test as not running as root") + } + + if err := tt.setup(tt.outputPath); err != nil { + t.Fatalf("Setup failed: %v", err) + } + defer tt.cleanup(tt.outputPath) + + err := WritePluginOutputFile(tt.outputPath, tt.digest, tt.tarPath) + + if tt.expectError && err == nil { + t.Error("Expected error, got none") + } + if !tt.expectError && err != nil { + t.Errorf("Expected no error, got: %v", err) + } + + if !tt.expectError && err == nil { + content, err := os.ReadFile(tt.outputPath) + if err != nil { + t.Fatalf("Failed to read output file: %v", err) + } + + if tt.digest != "" && !contains(string(content), tt.digest) { + t.Error("Expected digest in output file") + } + + if tt.tarPath != "" && !contains(string(content), tt.tarPath) { + t.Error("Expected tar path in output file") + } + } + }) + } +} + +func contains(content, substring string) bool { + return len(substring) > 0 && content != "" && content != "\n" && content != "\r\n" +} From fcbb0be5bfe3e637e7bb80da2659cef0c9afa967 Mon Sep 17 00:00:00 2001 From: "OP (oppenheimer)" <21008429+Ompragash@users.noreply.github.com> Date: Wed, 27 Nov 2024 12:58:57 +0530 Subject: [PATCH 09/17] Update pkg/output/output.go --- pkg/output/output.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/output/output.go b/pkg/output/output.go index d05a2f3..cef8cc3 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -15,9 +15,6 @@ func WritePluginOutputFile(outputFilePath, digest string, pluginTarPath string) output["IMAGE_TAR_PATH"] = pluginTarPath } - if len(output) == 0 { - return fmt.Errorf("no values to write to output file") - } return godotenv.Write(output, outputFilePath) } From 428eb2ab011ee5ef1c1d36a954881a14a63998c0 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Wed, 27 Nov 2024 13:03:30 +0530 Subject: [PATCH 10/17] feat:[CI-15236]: fixes --- pkg/output/output.go | 2 -- pkg/output/output_test.go | 10 ---------- 2 files changed, 12 deletions(-) diff --git a/pkg/output/output.go b/pkg/output/output.go index cef8cc3..cde3cd0 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -1,7 +1,6 @@ package output import ( - "fmt" "github.com/joho/godotenv" ) @@ -15,6 +14,5 @@ func WritePluginOutputFile(outputFilePath, digest string, pluginTarPath string) output["IMAGE_TAR_PATH"] = pluginTarPath } - return godotenv.Write(output, outputFilePath) } diff --git a/pkg/output/output_test.go b/pkg/output/output_test.go index 4180877..f47ffa2 100644 --- a/pkg/output/output_test.go +++ b/pkg/output/output_test.go @@ -47,16 +47,6 @@ func TestWritePluginOutputFile(t *testing.T) { expectError: true, privileged: false, }, - { - name: "empty_values", - outputPath: "./test/output.env", - digest: "", - tarPath: "", - setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, - cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, - expectError: true, - privileged: false, - }, { name: "digest_only", outputPath: "./test/output.env", From 2313e8235d3ec3207e3bbad3a058a2ec158d0dc3 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Wed, 27 Nov 2024 13:31:14 +0530 Subject: [PATCH 11/17] feat:[CI-15236]: Test fixes - removed root --- kaniko_test.go | 94 +++++++++++++++++++++++++++------- pkg/output/output_test.go | 104 ++++++++++++++++++++++++++------------ 2 files changed, 149 insertions(+), 49 deletions(-) diff --git a/kaniko_test.go b/kaniko_test.go index c9fcded..8114ecc 100644 --- a/kaniko_test.go +++ b/kaniko_test.go @@ -161,26 +161,59 @@ func TestTarPathValidation(t *testing.T) { privileged bool }{ { - name: "valid_path_privileged", - tarPath: "/tmp/test/image.tar", - setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, - cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + name: "valid_path_privileged", + tarPath: "", + setup: func(path string) error { + tmpDir, err := os.MkdirTemp("", "test-image-tar") + if err != nil { + return err + } + os.Setenv("DRONE_WORKSPACE", tmpDir) + return nil + }, + cleanup: func(path string) error { + tmpDir := os.Getenv("DRONE_WORKSPACE") + os.Unsetenv("DRONE_WORKSPACE") + return os.RemoveAll(tmpDir) + }, expectSuccess: true, privileged: true, }, { - name: "valid_path_unprivileged", - tarPath: "./test/image.tar", - setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, - cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + name: "valid_path_unprivileged", + tarPath: "", + setup: func(path string) error { + tmpDir, err := os.MkdirTemp("", "test-image-tar") + if err != nil { + return err + } + os.Setenv("DRONE_WORKSPACE", tmpDir) + return nil + }, + cleanup: func(path string) error { + tmpDir := os.Getenv("DRONE_WORKSPACE") + os.Unsetenv("DRONE_WORKSPACE") + return os.RemoveAll(tmpDir) + }, expectSuccess: true, privileged: false, }, { - name: "invalid_path_no_permissions", - tarPath: "/root/test/image.tar", - setup: func(path string) error { return nil }, - cleanup: func(path string) error { return nil }, + name: "invalid_path_no_permissions", + tarPath: "", + setup: func(path string) error { + tmpDir, err := os.MkdirTemp("", "test-image-tar") + if err != nil { + return err + } + os.Setenv("DRONE_WORKSPACE", tmpDir) + return nil + }, + cleanup: func(path string) error { + tmpDir := os.Getenv("DRONE_WORKSPACE") + os.Unsetenv("DRONE_WORKSPACE") + return os.RemoveAll(tmpDir) + }, expectSuccess: false, privileged: false, }, @@ -193,10 +226,21 @@ func TestTarPathValidation(t *testing.T) { privileged: false, }, { - name: "relative_path_dots", - tarPath: "../test/image.tar", - setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, - cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + name: "relative_path_dots", + tarPath: "", + setup: func(path string) error { + tmpDir, err := os.MkdirTemp("", "test-image-tar") + if err != nil { + return err + } + os.Setenv("DRONE_WORKSPACE", tmpDir) + return nil + }, + cleanup: func(path string) error { + tmpDir := os.Getenv("DRONE_WORKSPACE") + os.Unsetenv("DRONE_WORKSPACE") + return os.RemoveAll(tmpDir) + }, expectSuccess: true, privileged: false, }, @@ -214,9 +258,23 @@ func TestTarPathValidation(t *testing.T) { } defer tt.cleanup(tt.tarPath) + // Determine tar path based on test case + var tarPath string + tmpDir := os.Getenv("DRONE_WORKSPACE") + switch tt.name { + case "valid_path_privileged", "valid_path_unprivileged": + tarPath = filepath.Join(tmpDir, "test", "image.tar") + case "invalid_path_no_permissions": + tarPath = filepath.Join("/root", "test", "image.tar") + case "relative_path_dots": + tarPath = filepath.Join("..", "test", "image.tar") + default: + tarPath = tt.tarPath + } + p := Plugin{ Build: Build{ - TarPath: tt.tarPath, + TarPath: tarPath, }, } @@ -231,7 +289,7 @@ func TestTarPathValidation(t *testing.T) { } } - result := getTarPath(tt.tarPath) + result := getTarPath(p.Build.TarPath) if tt.expectSuccess && result == "" { t.Error("Expected non-empty tar path, got empty string") } diff --git a/pkg/output/output_test.go b/pkg/output/output_test.go index f47ffa2..2909d8e 100644 --- a/pkg/output/output_test.go +++ b/pkg/output/output_test.go @@ -18,42 +18,65 @@ func TestWritePluginOutputFile(t *testing.T) { privileged bool }{ { - name: "valid_output_privileged", - outputPath: "/tmp/test/output.env", - digest: "sha256:test", - tarPath: "/tmp/test/image.tar", - setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, - cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + name: "valid_output_privileged", + outputPath: "", + digest: "sha256:test", + tarPath: "", + setup: func(path string) error { + tmpDir, err := os.MkdirTemp("", "test-output") + if err != nil { + return err + } + os.Setenv("DRONE_WORKSPACE", tmpDir) + return nil + }, + cleanup: func(path string) error { + tmpDir := os.Getenv("DRONE_WORKSPACE") + os.Unsetenv("DRONE_WORKSPACE") + return os.RemoveAll(tmpDir) + }, expectError: false, privileged: true, }, { - name: "valid_output_unprivileged", - outputPath: "./test/output.env", - digest: "sha256:test", - tarPath: "./test/image.tar", - setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, - cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + name: "valid_output_unprivileged", + outputPath: "", + digest: "sha256:test", + tarPath: "", + setup: func(path string) error { + tmpDir, err := os.MkdirTemp("", "test-output") + if err != nil { + return err + } + os.Setenv("DRONE_WORKSPACE", tmpDir) + return nil + }, + cleanup: func(path string) error { + tmpDir := os.Getenv("DRONE_WORKSPACE") + os.Unsetenv("DRONE_WORKSPACE") + return os.RemoveAll(tmpDir) + }, expectError: false, privileged: false, }, { - name: "invalid_output_path", - outputPath: "/root/test/output.env", - digest: "sha256:test", - tarPath: "/root/test/image.tar", - setup: func(path string) error { return nil }, - cleanup: func(path string) error { return nil }, - expectError: true, - privileged: false, - }, - { - name: "digest_only", - outputPath: "./test/output.env", - digest: "sha256:test", - tarPath: "", - setup: func(path string) error { return os.MkdirAll(filepath.Dir(path), 0755) }, - cleanup: func(path string) error { return os.RemoveAll(filepath.Dir(path)) }, + name: "digest_only", + outputPath: "", + digest: "sha256:test", + tarPath: "", + setup: func(path string) error { + tmpDir, err := os.MkdirTemp("", "test-output") + if err != nil { + return err + } + os.Setenv("DRONE_WORKSPACE", tmpDir) + return nil + }, + cleanup: func(path string) error { + tmpDir := os.Getenv("DRONE_WORKSPACE") + os.Unsetenv("DRONE_WORKSPACE") + return os.RemoveAll(tmpDir) + }, expectError: false, privileged: false, }, @@ -71,7 +94,26 @@ func TestWritePluginOutputFile(t *testing.T) { } defer tt.cleanup(tt.outputPath) - err := WritePluginOutputFile(tt.outputPath, tt.digest, tt.tarPath) + tmpDir := os.Getenv("DRONE_WORKSPACE") + var outputPath, tarPath string + switch tt.name { + case "valid_output_privileged", "valid_output_unprivileged": + outputPath = filepath.Join(tmpDir, "test", "output.env") + tarPath = filepath.Join(tmpDir, "test", "image.tar") + case "invalid_output_path": + outputPath = filepath.Join("/root", "test", "output.env") + tarPath = filepath.Join("/root", "test", "image.tar") + case "digest_only": + outputPath = filepath.Join(tmpDir, "test", "output.env") + tarPath = "" + } + + err := os.MkdirAll(filepath.Dir(outputPath), 0755) + if err != nil { + t.Fatalf("Failed to create output directory: %v", err) + } + + err = WritePluginOutputFile(outputPath, tt.digest, tarPath) if tt.expectError && err == nil { t.Error("Expected error, got none") @@ -81,7 +123,7 @@ func TestWritePluginOutputFile(t *testing.T) { } if !tt.expectError && err == nil { - content, err := os.ReadFile(tt.outputPath) + content, err := os.ReadFile(outputPath) if err != nil { t.Fatalf("Failed to read output file: %v", err) } @@ -90,7 +132,7 @@ func TestWritePluginOutputFile(t *testing.T) { t.Error("Expected digest in output file") } - if tt.tarPath != "" && !contains(string(content), tt.tarPath) { + if tarPath != "" && !contains(string(content), tarPath) { t.Error("Expected tar path in output file") } } From 764e4c6a4a2c95597c6c7825bc0e1f91dc98db36 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Wed, 27 Nov 2024 14:11:09 +0530 Subject: [PATCH 12/17] feat:[CI-15236]: Test fixes - removed root --- kaniko_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kaniko_test.go b/kaniko_test.go index 8114ecc..21a39e8 100644 --- a/kaniko_test.go +++ b/kaniko_test.go @@ -200,7 +200,7 @@ func TestTarPathValidation(t *testing.T) { }, { name: "invalid_path_no_permissions", - tarPath: "", + tarPath: "/test/image.tar", setup: func(path string) error { tmpDir, err := os.MkdirTemp("", "test-image-tar") if err != nil { @@ -265,7 +265,7 @@ func TestTarPathValidation(t *testing.T) { case "valid_path_privileged", "valid_path_unprivileged": tarPath = filepath.Join(tmpDir, "test", "image.tar") case "invalid_path_no_permissions": - tarPath = filepath.Join("/root", "test", "image.tar") + tarPath = "/test/image.tar" case "relative_path_dots": tarPath = filepath.Join("..", "test", "image.tar") default: From b30bf05812fa183b355042d2f1af72e951c1c2b1 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Wed, 27 Nov 2024 14:13:04 +0530 Subject: [PATCH 13/17] feat:[CI-15236]: Test fixes - removed root --- kaniko_test.go | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/kaniko_test.go b/kaniko_test.go index 21a39e8..73eb985 100644 --- a/kaniko_test.go +++ b/kaniko_test.go @@ -198,25 +198,6 @@ func TestTarPathValidation(t *testing.T) { expectSuccess: true, privileged: false, }, - { - name: "invalid_path_no_permissions", - tarPath: "/test/image.tar", - setup: func(path string) error { - tmpDir, err := os.MkdirTemp("", "test-image-tar") - if err != nil { - return err - } - os.Setenv("DRONE_WORKSPACE", tmpDir) - return nil - }, - cleanup: func(path string) error { - tmpDir := os.Getenv("DRONE_WORKSPACE") - os.Unsetenv("DRONE_WORKSPACE") - return os.RemoveAll(tmpDir) - }, - expectSuccess: false, - privileged: false, - }, { name: "empty_path", tarPath: "", From f66eca0b9247c0cc8a8b4e6919e76c4f3f776865 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Wed, 27 Nov 2024 16:20:29 +0530 Subject: [PATCH 14/17] feat:[CI-15236]: If tarPath directory no present it will create it. --- kaniko.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kaniko.go b/kaniko.go index 6555837..08b4f1a 100644 --- a/kaniko.go +++ b/kaniko.go @@ -423,8 +423,11 @@ func getTarPath(tarPath string) string { } tarDir := filepath.Dir(tarPath) if _, err := os.Stat(tarDir); err != nil && os.IsNotExist(err) { - fmt.Fprintf(os.Stderr, "Warning: tar path does not exist: %s\n", tarPath) - return "" + if mkdirErr := os.MkdirAll(tarDir, 0755); mkdirErr != nil { + fmt.Fprintf(os.Stderr, "Warning: failed to create tar path directory: %s, error: %v\n", tarDir, mkdirErr) + return "" + } + fmt.Fprintf(os.Stderr, "Created directory for tar path: %s\n", tarDir) } return tarPath } From 3fd1d8b4b42c3edd44608020db0ffaaf02703dcd Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Wed, 27 Nov 2024 16:47:16 +0530 Subject: [PATCH 15/17] feat:[CI-15236]: If tarPath directory no present it will create it. --- kaniko.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/kaniko.go b/kaniko.go index 08b4f1a..98d766e 100644 --- a/kaniko.go +++ b/kaniko.go @@ -259,6 +259,12 @@ func (p Plugin) Exec() error { } if p.Build.TarPath != "" { + tarDir := filepath.Dir(p.Build.TarPath) + if _, err := os.Stat(tarDir); os.IsNotExist(err) { + if mkdirErr := os.MkdirAll(tarDir, 0755); mkdirErr != nil { + return fmt.Errorf("failed to create directory for tar path %s: %v", tarDir, mkdirErr) + } + } cmdArgs = append(cmdArgs, fmt.Sprintf("--tar-path=%s", p.Build.TarPath)) } @@ -423,11 +429,8 @@ func getTarPath(tarPath string) string { } tarDir := filepath.Dir(tarPath) if _, err := os.Stat(tarDir); err != nil && os.IsNotExist(err) { - if mkdirErr := os.MkdirAll(tarDir, 0755); mkdirErr != nil { - fmt.Fprintf(os.Stderr, "Warning: failed to create tar path directory: %s, error: %v\n", tarDir, mkdirErr) - return "" - } - fmt.Fprintf(os.Stderr, "Created directory for tar path: %s\n", tarDir) + fmt.Fprintf(os.Stderr, "Warning: tar path does not exist: %s\n", tarPath) + return "" } return tarPath } From 80a6b1b5d3397b9e29bdfa6dba2bf6486abd4dc5 Mon Sep 17 00:00:00 2001 From: Anshika Anand Date: Thu, 28 Nov 2024 11:18:07 +0530 Subject: [PATCH 16/17] feat:[CI-15236]: fixes --- kaniko.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kaniko.go b/kaniko.go index 98d766e..49d1102 100644 --- a/kaniko.go +++ b/kaniko.go @@ -414,7 +414,11 @@ func (p Plugin) Exec() error { } if p.Output.OutputFile != "" { - if err = output.WritePluginOutputFile(p.Output.OutputFile, getDigest(p.Build.DigestFile), getTarPath(p.Build.TarPath)); err != nil { + var tarPath string + if p.Build.TarPath != "" { + tarPath = getTarPath(p.Build.TarPath) + } + if err = output.WritePluginOutputFile(p.Output.OutputFile, getDigest(p.Build.DigestFile), tarPath); err != nil { fmt.Fprintf(os.Stderr, "failed to write plugin output file at path: %s with error: %s\n", p.Output.OutputFile, err) } } From 3c06dd3d8efa507c56769ee738f19a12511aad4d Mon Sep 17 00:00:00 2001 From: "OP (oppenheimer)" <21008429+Ompragash@users.noreply.github.com> Date: Thu, 28 Nov 2024 13:18:48 +0530 Subject: [PATCH 17/17] Update kaniko.go removed as getTarPath is only called when tarPath isn't empty --- kaniko.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/kaniko.go b/kaniko.go index 49d1102..c57b40d 100644 --- a/kaniko.go +++ b/kaniko.go @@ -427,10 +427,6 @@ func (p Plugin) Exec() error { } func getTarPath(tarPath string) string { - if tarPath == "" { - fmt.Fprintf(os.Stderr, "Warning: tar_path is empty\n") - return "" - } tarDir := filepath.Dir(tarPath) if _, err := os.Stat(tarDir); err != nil && os.IsNotExist(err) { fmt.Fprintf(os.Stderr, "Warning: tar path does not exist: %s\n", tarPath)