Skip to content

Commit

Permalink
Stop depending on external jenkins mirror (#376)
Browse files Browse the repository at this point in the history
This causes a JAR to be fetched over a local HTTP server in the
e2e test harness instead of the external Jenkins mirror. Also causes the
JAR input to be randomized so we can re-use the createSignedJar method
in multiple tests.

Fixes: #375

Signed-off-by: Bob Callaway <[email protected]>
  • Loading branch information
bobcallaway authored Jul 17, 2021
1 parent 38d532d commit 5687a24
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
32 changes: 31 additions & 1 deletion tests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -440,8 +443,35 @@ func TestTimestampArtifact(t *testing.T) {
}

func TestJARURL(t *testing.T) {
out := runCli(t, "upload", "--artifact", "https://get.jenkins.io/war-stable/2.277.3/jenkins.war", "--type", "jar", "--artifact-hash=3e22c7e8cd7c8ee1e92cbaa8d0d303a7b53e07bc2a152ddc66f8ce55caea91ab")
td := t.TempDir()
artifactPath := filepath.Join(td, "artifact.jar")

createSignedJar(t, artifactPath)
jarBytes, _ := ioutil.ReadFile(artifactPath)
jarSHA := sha256.Sum256(jarBytes)
testServer := httptest.NewUnstartedServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/jar" {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(jarBytes)
}))
defer testServer.Close()
l, _ := net.Listen("tcp", "172.17.0.1:0")
testServer.Listener.Close()
testServer.Listener = l
testServer.Start()
// ensure hash is required for JAR since signature/public key are embedded
out := runCliErr(t, "upload", "--artifact", testServer.URL+"/jar", "--type", "jar")
outputContains(t, out, "hash value must be provided if URL is specified")
// ensure valid JAR can be fetched over URL and inserted
out = runCli(t, "upload", "--artifact", testServer.URL+"/jar", "--type", "jar", "--artifact-hash="+hex.EncodeToString(jarSHA[:]))
outputContains(t, out, "Created entry at")
// ensure a 404 is handled correctly
out = runCliErr(t, "upload", "--artifact", testServer.URL+"/not_found", "--type", "jar", "--artifact-hash="+hex.EncodeToString(jarSHA[:]))
outputContains(t, out, "404")
}

func TestX509(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion tests/jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"crypto"
"os"
"strings"
"testing"

"github.com/sassoftware/relic/lib/certloader"
Expand All @@ -33,6 +34,7 @@ import (
//note: reuses PKI artifacts from x509 tests

const manifest = `Manifest-Version: 1.0
Created-By: REPLACE
Name: src/some/java/HelloWorld.class
SHA-256-Digest: cp40SgHlLIIr397GHijW7aAmWNLn0rgKm5Ap9B4hLd4=
Expand All @@ -58,7 +60,8 @@ func createSignedJar(t *testing.T, artifactPath string) {
if err != nil {
t.Fatal(err)
}
mf.Write([]byte(manifest))
randManifest := strings.Replace(manifest, "REPLACE", randomRpmSuffix(), 1)
mf.Write([]byte(randManifest))
if err := zw.Close(); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 5687a24

Please sign in to comment.