mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-05 04:27:09 +00:00
test(sidecar): split tests across stub and withsidecar builds
Default !withsidecar build: assert IsPlaceholder, empty JAR, Extract errors. withsidecar build: existing extract/checksum coverage.
This commit is contained in:
1 parent
4744736dc5
commit
ae10354ff0
2 files changed
+104
-89
No files matched your search
@@ -0,0 +1,90 @@
|
||||
//go:build withsidecar
|
||||
|
||||
package sidecar
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEmbeddedNonZero(t *testing.T) {
|
||||
if EmbeddedSize() == 0 {
|
||||
t.Errorf("expected embedded JAR to be non-empty")
|
||||
}
|
||||
if IsPlaceholder() {
|
||||
t.Errorf("withsidecar build should not be a placeholder")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtract_WritesJARAndChecksum(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
path, err := Extract(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if path != filepath.Join(directory, "uatu-sidecar.jar") {
|
||||
t.Errorf("unexpected path: %s", path)
|
||||
}
|
||||
body, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if EmbeddedSize() != len(body) {
|
||||
t.Errorf("size mismatch: embedded=%d, written=%d", EmbeddedSize(), len(body))
|
||||
}
|
||||
sum := sha256.Sum256(body)
|
||||
if hex.EncodeToString(sum[:]) != EmbeddedSHA256() {
|
||||
t.Errorf("hash mismatch")
|
||||
}
|
||||
checksum, err := os.ReadFile(path + ".sha256")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(checksum) != EmbeddedSHA256() {
|
||||
t.Errorf("checksum file content wrong: %q", checksum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtract_ReusesIdenticalFile(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
path, err := Extract(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
originalStat, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
originalModTime := originalStat.ModTime()
|
||||
|
||||
if _, err := Extract(directory); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secondStat, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !secondStat.ModTime().Equal(originalModTime) {
|
||||
t.Errorf("second extract should not have rewritten the file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtract_RewritesIfChecksumMissing(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
path, err := Extract(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Remove(path + ".sha256"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Extract(directory); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(path + ".sha256"); err != nil {
|
||||
t.Errorf("checksum should have been written: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user