Skip to content

Commit

Permalink
Standalone mirror tests
Browse files Browse the repository at this point in the history
Signed-off-by: Janos Bonic <[email protected]>
  • Loading branch information
Janos Bonic committed Jul 30, 2024
1 parent 8579ed8 commit 3bd8529
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 142 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*~
tofu
tofu.exe
fake
fake.exe
26 changes: 9 additions & 17 deletions mockmirror/fake.go → internal/helloworld/fake.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0

package mockmirror
package helloworld

import (
"errors"
Expand All @@ -12,18 +12,14 @@ import (
"testing"
)

func buildFake(t *testing.T) []byte {
_, filename, _, _ := runtime.Caller(1)
fakeDir := path.Join(path.Dir(filename), "fake")
if err := os.MkdirAll(fakeDir, 0755); err != nil {
t.Fatalf("Failed to create fake dir (%v)", err)
}
binaryPath := path.Join(fakeDir, "fake")
if contents, err := os.ReadFile(binaryPath); err == nil {
return contents
// Build creates a hello-world binary for the current platform you can use to test.
func Build(t *testing.T) []byte {
fakeName := "fake"
if runtime.GOOS == "windows" {
fakeName += ".exe"
}

dir := path.Join(os.TempDir(), "fake")
dir := path.Join(os.TempDir(), fakeName)
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
Expand All @@ -37,7 +33,7 @@ func buildFake(t *testing.T) []byte {
t.Fatal()
}

cmd := exec.Command("go", "build", "-ldflags", "-s -w", "-o", "fake")
cmd := exec.Command("go", "build", "-ldflags", "-s -w", "-o", fakeName)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
Expand All @@ -50,14 +46,10 @@ func buildFake(t *testing.T) []byte {
}
}

contents, err := os.ReadFile(path.Join(dir, "fake"))
contents, err := os.ReadFile(path.Join(dir, fakeName))
if err != nil {
t.Fatalf("Failed to read compiled fake (%v)", err)
}

if err := os.WriteFile(binaryPath, contents, 0700); err != nil { //nolint:gosec //This needs to be executable.
t.Fatalf("Failed to create fake binary at %s (%v)", binaryPath, err)
}
return contents
}

Expand Down
124 changes: 0 additions & 124 deletions internal/tools/mockmirror-build-fake/main.go

This file was deleted.

55 changes: 55 additions & 0 deletions mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ package tofudl_test

import (
"context"
"runtime"
"testing"
"time"

"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/opentofu/tofudl"
"github.com/opentofu/tofudl/branding"
"github.com/opentofu/tofudl/internal/helloworld"
"github.com/opentofu/tofudl/mockmirror"
)

Expand Down Expand Up @@ -92,3 +96,54 @@ func TestMirroringE2E(t *testing.T) {
t.Fatal("Empty artifact!")
}
}

func TestMirrorStandalone(t *testing.T) {
binaryContents := helloworld.Build(t)

ctx := context.Background()
key, err := crypto.GenerateKey(branding.ProductName+" Test", "[email protected]", "rsa", 2048)
if err != nil {
t.Fatal(err)
}
pubKey, err := key.GetArmoredPublicKey()
if err != nil {
t.Fatal(err)
}
builder, err := tofudl.NewReleaseBuilder(key)
if err != nil {
t.Fatal(err)
}
fakeName := "fake"
if runtime.GOOS == "windows" {
fakeName += ".exe"

Check failure on line 118 in mirror_test.go

View workflow job for this annotation

GitHub Actions / Lint

ineffectual assignment to fakeName (ineffassign)

Check failure on line 118 in mirror_test.go

View workflow job for this annotation

GitHub Actions / Lint

ineffectual assignment to fakeName (ineffassign)
}
if err := builder.PackageBinary(tofudl.PlatformAuto, tofudl.ArchitectureAuto, binaryContents, nil); err != nil {
t.Fatalf("failed to package tofu binary (%v)", err)
}

mirrorStorage, err := tofudl.NewFilesystemStorage(t.TempDir())
if err != nil {
t.Fatalf("failed to set up TofuDL mirror")
}
downloader, err := tofudl.NewMirror(
tofudl.MirrorConfig{
GPGKey: pubKey,
},
mirrorStorage,
nil,
)
if err != nil {
t.Fatal(err)
}
if err := builder.Build(ctx, "1.9.0", downloader); err != nil {
t.Fatal(err)
}
_, err = downloader.Download(ctx)
if err != nil {
t.Fatal(err)
}
// Make sure all file handles are closed.
if runtime.GOOS == "windows" {
runtime.GC()
}
}
3 changes: 2 additions & 1 deletion mockmirror/mockmirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/opentofu/tofudl"
"github.com/opentofu/tofudl/branding"
"github.com/opentofu/tofudl/internal/helloworld"
)

// New returns a mirror serving a fake archive signed with a GPG key for testing purposes.
func New(
t *testing.T,
) Mirror {
return NewFromBinary(t, buildFake(t))
return NewFromBinary(t, helloworld.Build(t))
}

// NewFromBinary returns a mirror serving a binary passed and signed with a GPG key for testing purposes.
Expand Down

0 comments on commit 3bd8529

Please sign in to comment.