-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:[CI-15236]: Added IMAGE_TAR_PATH as output variable for the plug…
…in. (#132) * feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. * feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. * feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. * feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. * feat:[CI-15236]: Test commit. * feat:[CI-15236]: Test commit. * feat:[CI-15236]: Added PLUGIN_TAR_PATH as output variable for the plugin. * feat:[CI-15236]: Directory check and UTs. * Update pkg/output/output.go * feat:[CI-15236]: fixes * feat:[CI-15236]: Test fixes - removed root * feat:[CI-15236]: Test fixes - removed root * feat:[CI-15236]: Test fixes - removed root * feat:[CI-15236]: If tarPath directory no present it will create it. * feat:[CI-15236]: If tarPath directory no present it will create it. * feat:[CI-15236]: fixes * Update kaniko.go removed as getTarPath is only called when tarPath isn't empty --------- Co-authored-by: OP (oppenheimer) <[email protected]>
- Loading branch information
1 parent
113a61b
commit e6ab8aa
Showing
4 changed files
with
307 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
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: "", | ||
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: "", | ||
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: "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, | ||
}, | ||
} | ||
|
||
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) | ||
|
||
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") | ||
} | ||
if !tt.expectError && err != nil { | ||
t.Errorf("Expected no error, got: %v", err) | ||
} | ||
|
||
if !tt.expectError && err == nil { | ||
content, err := os.ReadFile(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 tarPath != "" && !contains(string(content), 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" | ||
} |