Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:[CI-15236]: Added IMAGE_TAR_PATH as output variable for the plugin. #132

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/drone/drone-kaniko/pkg/artifact"
Expand Down Expand Up @@ -407,14 +408,27 @@ 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), 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)
}
}

return nil
}

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)
return ""
}
return tarPath
}

func getDigest(digestFile string) string {
content, err := ioutil.ReadFile(digestFile)
if err != nil {
Expand Down
93 changes: 93 additions & 0 deletions kaniko_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kaniko

import (
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -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")
}
})
}
}
17 changes: 14 additions & 3 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package output

import (
"fmt"
"github.com/joho/godotenv"
)

func WritePluginOutputFile(outputFilePath, digest string) error {
output := map[string]string{
"digest": digest,
func WritePluginOutputFile(outputFilePath, digest string, pluginTarPath string) error {
output := make(map[string]string)
if digest != "" {
output["digest"] = digest
}

if pluginTarPath != "" {
output["IMAGE_TAR_PATH"] = pluginTarPath
}

if len(output) == 0 {
return fmt.Errorf("no values to write to output file")
}
Anshika2203 marked this conversation as resolved.
Show resolved Hide resolved
Ompragash marked this conversation as resolved.
Show resolved Hide resolved

return godotenv.Write(output, outputFilePath)
}
113 changes: 113 additions & 0 deletions pkg/output/output_test.go
Original file line number Diff line number Diff line change
@@ -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"
}