Skip to content

Commit

Permalink
refactored tests to error gracefully without crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
nikogura committed Dec 13, 2022
1 parent 285af1b commit 0be74b7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 94 deletions.
149 changes: 56 additions & 93 deletions pkg/gomason/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gomason

import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand All @@ -17,15 +16,14 @@ func TestCreateGoPath(t *testing.T) {
lang, _ := GetByName(LanguageGolang)
_, err := lang.CreateWorkDir(TestTmpDir)
if err != nil {
log.Printf("Error creating gopath in %q: %s", TestTmpDir, err)
t.FailNow()
t.Errorf("Error creating gopath in %q: %s", TestTmpDir, err)
}

dirs := []string{"go", "go/src", "go/pkg", "go/bin"}

for _, dir := range dirs {
if _, err := os.Stat(filepath.Join(TestTmpDir, dir)); os.IsNotExist(err) {
t.Fail()
t.Errorf("GoPath not created.")
}
}
}
Expand All @@ -34,79 +32,69 @@ func TestCheckoutDefault(t *testing.T) {
lang, _ := GetByName(LanguageGolang)
gopath, err := lang.CreateWorkDir(TestTmpDir)
if err != nil {
log.Printf("Error creating GOPATH in %s: %s", TestTmpDir, err)
t.FailNow()
t.Errorf("Error creating GOPATH in %s: %s", TestTmpDir, err)
}

log.Printf("Checking out Master Branch")
log.Printf("Checking out Master Branch\n")
err = lang.Checkout(gopath, testMetadataObj(), "")
if err != nil {
log.Printf("Failed to checkout module: %s", err)
t.FailNow()
t.Errorf("Failed to checkout module: %s", err)
}

metaPath := filepath.Join(gopath, "src", testModuleName(), METADATA_FILENAME)
if _, err := os.Stat(metaPath); os.IsNotExist(err) {
log.Printf("Failed to checkout module")
t.FailNow()
t.Errorf("Failed to checkout module")
}
}

func TestCheckoutBranch(t *testing.T) {
log.Printf("Checking out Test Branch")
log.Printf("Checking out Test Branch\n")

// making a separate temp dir here cos it steps on the other tests
dir, err := ioutil.TempDir("", "gomason")
dir, err := os.MkdirTemp("", "gomason")
if err != nil {
log.Fatal("Error creating temp dir\n")
t.Errorf("Error creating temp dir\n")
}
defer os.RemoveAll(dir)

lang, _ := GetByName(LanguageGolang)
gopath, err := lang.CreateWorkDir(dir)
if err != nil {
log.Printf("Error creating GOPATH in %s: %s", dir, err)
t.FailNow()
t.Errorf("Error creating GOPATH in %s: %s", dir, err)
}

err = lang.Checkout(gopath, testMetadataObj(), "testbranch")
if err != nil {
log.Printf("Failed to checkout module: %s", err)
t.FailNow()
t.Errorf("Failed to checkout module: %s", err)
}

testFilePath := filepath.Join(gopath, "src", testModuleName(), "test_file")
if _, err := os.Stat(testFilePath); os.IsNotExist(err) {
log.Printf("Failed to checkout branch")
t.FailNow()
t.Errorf("Failed to checkout branch")
}
}

func TestPrep(t *testing.T) {
log.Printf("Checking out Master Branch")
log.Printf("Checking out Master Branch\n")
lang, _ := GetByName(LanguageGolang)
gopath, err := lang.CreateWorkDir(TestTmpDir)
if err != nil {
log.Printf("Error creating GOPATH in %s: %s", TestTmpDir, err)
t.FailNow()
t.Errorf("Error creating GOPATH in %s: %s", TestTmpDir, err)
}

err = lang.Checkout(gopath, testMetadataObj(), "")
if err != nil {
log.Printf("Failed to checkout module: %s", err)
t.FailNow()
t.Errorf("Failed to checkout module: %s", err)
}

metaPath := filepath.Join(gopath, "src", testModuleName(), METADATA_FILENAME)
if _, err := os.Stat(metaPath); os.IsNotExist(err) {
log.Printf("Failed to checkout module")
t.FailNow()
t.Errorf("Failed to checkout module")
}

err = lang.Prep(gopath, testMetadataObj())
if err != nil {
log.Printf("error running prep steps: %s", err)
t.FailNow()
t.Errorf("error running prep steps: %s", err)
}
}

Expand All @@ -116,19 +104,16 @@ func TestBuildGoxInstall(t *testing.T) {
log.Printf("Installing Gox\n")
gopath, err := lang.CreateWorkDir(TestTmpDir)
if err != nil {
log.Printf("Error creating GOPATH in %s: %s", TestTmpDir, err)
t.FailNow()
t.Errorf("Error creating GOPATH in %s: %s", TestTmpDir, err)
}

err = GoxInstall(gopath)
if err != nil {
log.Printf("Error installing Gox: %s\n", err)
t.FailNow()
t.Errorf("Error installing Gox: %s\n", err)
}

if _, err := os.Stat(filepath.Join(gopath, "bin/gox")); os.IsNotExist(err) {
log.Printf("Gox failed to install.")
t.FailNow()
t.Errorf("Gox failed to install.")
}
}

Expand Down Expand Up @@ -167,69 +152,62 @@ func TestBuild(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
lang, err := GetByName(tc.lang)
if err != nil {
t.Fatalf(err.Error())
t.Errorf(err.Error())
}

log.Printf("Running Build\n")
gopath, err := lang.CreateWorkDir(TestTmpDir)
if err != nil {
log.Printf("Error creating GOPATH in %s: %s\n", TestTmpDir, err)
t.FailNow()
t.Errorf("Error creating GOPATH in %s: %s\n", TestTmpDir, err)
}

gomodule := testMetadataObj().Package

log.Printf("Checking out Master Branch")
log.Printf("Checking out Master Branch\n")

err = lang.Checkout(gopath, testMetadataObj(), "")
if err != nil {
log.Printf("Failed to checkout module: %s", err)
t.FailNow()
t.Errorf("Failed to checkout module: %s", err)
}

metaPath := filepath.Join(gopath, "src", testModuleName(), METADATA_FILENAME)
if _, err := os.Stat(metaPath); os.IsNotExist(err) {
log.Printf("Failed to checkout module")
t.FailNow()
t.Errorf("Failed to checkout module")
}

err = lang.Prep(gopath, testMetadataObj())
if err != nil {
log.Printf("error running prep steps: %s", err)
t.FailNow()
t.Errorf("error running prep steps: %s", err)
}

err = lang.Build(gopath, testMetadataObj(), tc.skipTargets)
if err != nil {
log.Printf("Error building: %s", err)
t.FailNow()
t.Errorf("Error building: %s", err)
}

for _, artifact := range tc.artifactsPresent {
workdir := filepath.Join(gopath, "src", gomodule)
binary := fmt.Sprintf("%s/%s", workdir, artifact)

log.Printf("Looking for binary present: %s", binary)
log.Printf("Looking for binary present: %s\n", binary)

if _, err := os.Stat(binary); os.IsNotExist(err) {
log.Printf("Gox failed to build binary: %s.\n", binary)
t.FailNow()
t.Errorf("Gox failed to build binary: %s.\n", binary)
} else {
log.Printf("Binary found.")
log.Printf("Binary found.\n")
}
}

for _, artifact := range tc.artifactsMissing {
workdir := filepath.Join(gopath, "src", gomodule)
binary := fmt.Sprintf("%s/%s", workdir, artifact)

log.Printf("Looking for binary not present: %s", binary)
log.Printf("Looking for binary not present: %s\n", binary)

if _, err := os.Stat(binary); os.IsNotExist(err) {
log.Printf("Binary not found - as intended.")
log.Printf("Binary not found - as intended.\n")
} else {
log.Printf("Gox built binary: %s when it shouldn't have.\n", binary)
t.FailNow()
t.Errorf("Gox built binary: %s when it shouldn't have.\n", binary)
}
}
})
Expand All @@ -239,35 +217,30 @@ func TestBuild(t *testing.T) {
func TestTest(t *testing.T) {
lang, _ := GetByName(LanguageGolang)

log.Printf("Checking out Master Branch")
log.Printf("Checking out Master Branch\n")
gopath, err := lang.CreateWorkDir(TestTmpDir)
if err != nil {
log.Printf("Error creating GOPATH in %s: %s", TestTmpDir, err)
t.FailNow()
t.Errorf("Error creating GOPATH in %s: %s", TestTmpDir, err)
}

err = lang.Checkout(gopath, testMetadataObj(), "")
if err != nil {
log.Printf("Failed to checkout module: %s", err)
t.FailNow()
t.Errorf("Failed to checkout module: %s", err)
}

metaPath := filepath.Join(gopath, "src", testModuleName(), METADATA_FILENAME)
if _, err := os.Stat(metaPath); os.IsNotExist(err) {
log.Printf("Failed to checkout module")
t.FailNow()
t.Errorf("Failed to checkout module")
}

err = lang.Prep(gopath, testMetadataObj())
if err != nil {
log.Printf("error running prep steps: %s", err)
t.FailNow()
t.Errorf("error running prep steps: %s", err)
}

err = lang.Test(gopath, testMetadataObj().Package, "10m")
if err != nil {
log.Printf("error running go test: %s", err)
t.FailNow()
t.Errorf("error running go test: %s", err)
}
}

Expand All @@ -280,17 +253,15 @@ func TestSignVerifyBinary(t *testing.T) {
}
shellCmd, err := exec.LookPath("gpg")
if err != nil {
log.Printf("Failed to check if gpg is installed:%s", err)
t.FailNow()
t.Errorf("Failed to check if gpg is installed:%s", err)
}

lang, _ := GetByName(LanguageGolang)

// create workspace
gopath, err := lang.CreateWorkDir(TestTmpDir)
if err != nil {
log.Printf("Error creating GOPATH in %s: %s\n", TestTmpDir, err)
t.FailNow()
t.Errorf("Error creating GOPATH in %s: %s\n", TestTmpDir, err)
}

meta := testMetadataObj()
Expand All @@ -301,8 +272,7 @@ func TestSignVerifyBinary(t *testing.T) {
log.Printf("Running Build\n")
err = lang.Build(gopath, meta, "")
if err != nil {
log.Printf("Error building: %s", err)
t.FailNow()
t.Errorf("Error building: %s", err)
}

// set up test keys
Expand All @@ -327,25 +297,23 @@ Expire-Date: 0
%echo done
`
keyFile := filepath.Join(TestTmpDir, "testkey")
err = ioutil.WriteFile(keyFile, []byte(defaultKeyText), 0644)
err = os.WriteFile(keyFile, []byte(defaultKeyText), 0644)
if err != nil {
log.Printf("Error writing test key generation file: %s", err)
t.FailNow()
t.Errorf("Error writing test key generation file: %s", err)
}

log.Printf("Keyring file: %s", keyring)
log.Printf("Trustdb file: %s", trustdb)
log.Printf("Test key generation file: %s", keyFile)
log.Printf("Keyring file: %s\n", keyring)
log.Printf("Trustdb file: %s\n", trustdb)
log.Printf("Test key generation file: %s\n", keyFile)

// generate a test key
cmd := exec.Command(shellCmd, "--trustdb", trustdb, "--no-default-keyring", "--keyring", keyring, "--batch", "--generate-key", keyFile)
err = cmd.Run()
if err != nil {
log.Printf("****** Error creating test key: %s *****", err)
t.FailNow()
t.Errorf("****** Error creating test key: %s *****", err)
}

log.Printf("Done creating keyring and test keys")
log.Printf("Done creating keyring and test keys\n")

// sign binaries
parts := strings.Split(meta.Package, "/")
Expand All @@ -361,46 +329,41 @@ Expire-Date: 0
binary := fmt.Sprintf("%s/%s_%s_%s", workdir, binaryPrefix, osname, archname)

if _, err := os.Stat(binary); os.IsNotExist(err) {
fmt.Printf("Gox failed to build binary: %s\n", binary)
log.Printf("Failed to find binary %s", binary)
t.FailNow()
t.Errorf("Gox failed to build binary: %s\n", binary)
}

err = g.SignBinary(meta, binary)
if err != nil {
err = errors.Wrap(err, "failed to sign binary")
log.Printf("Failed to sign binary %s: %s", binary, err)
t.FailNow()
t.Errorf("Failed to sign binary %s: %s", binary, err)
}

// verify binaries
ok, err := VerifyBinary(binary, meta)
if err != nil {
log.Printf("Error verifying signature: %s", err)
//t.Fail()
t.Errorf("Error verifying signature: %s", err)
}

if !ok {
log.Printf("Failed to verify signature on %s", binary)
t.FailNow()
t.Errorf("Failed to verify signature on %s", binary)
}

}

cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to get current working directory: %s", err)
t.Errorf("Failed to get current working directory: %s", err)
}

fmt.Printf("Publishing\n")

err = g.HandleArtifacts(meta, gopath, cwd, false, true, true, "")
if err != nil {
log.Fatalf("post-build processing failed: %s", err)
t.Errorf("post-build processing failed: %s", err)
}

err = g.HandleExtras(meta, gopath, cwd, false, true)
if err != nil {
log.Fatalf("Extra artifact processing failed: %s", err)
t.Errorf("Extra artifact processing failed: %s", err)
}
}
2 changes: 1 addition & 1 deletion pkg/gomason/gomason.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (g *Gomason) HandleExtras(meta Metadata, gopath string, cwd string, sign bo
return err
}

// CollectFileAndSignature grabs a file and the signature if it exists and moves it from the temp workspace into the CWD where gomason was called.
// CollectFileAndSignature grabs a file and the signature if it exists and copies it from the temp workspace into the CWD where gomason was called.
func CollectFileAndSignature(cwd string, filename string) (err error) {
binaryDestinationPath := fmt.Sprintf("%s/%s", cwd, filepath.Base(filename))

Expand Down

0 comments on commit 0be74b7

Please sign in to comment.