github.com/abemedia/appcast@v0.4.0/integrations/yum/build_test.go (about)

     1  package yum_test
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"io/fs"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/abemedia/appcast/integrations/yum"
    13  	"github.com/abemedia/appcast/internal/test"
    14  	"github.com/abemedia/appcast/pkg/crypto/pgp"
    15  	source "github.com/abemedia/appcast/source/file"
    16  	target "github.com/abemedia/appcast/target/file"
    17  	"github.com/google/go-cmp/cmp"
    18  )
    19  
    20  func TestBuild(t *testing.T) {
    21  	want := readTestData(t)
    22  	now := time.Date(2023, 11, 19, 23, 37, 12, 0, time.UTC)
    23  
    24  	dir := t.TempDir() + "/rpm"
    25  	src, _ := source.New(source.Config{Path: "../../testdata"})
    26  	tgt, _ := target.New(target.Config{Path: dir})
    27  	key, _ := pgp.NewPrivateKey("test", "test@example.com")
    28  
    29  	test.Golden(t, "testdata", dir)
    30  
    31  	c := &yum.Config{
    32  		Source: src,
    33  		Target: tgt,
    34  		PGPKey: key,
    35  	}
    36  
    37  	t.Run("New", func(t *testing.T) {
    38  		testBuild(t, c, want, now)
    39  	})
    40  
    41  	t.Run("NoChange", func(t *testing.T) {
    42  		testBuild(t, c, want, now.Add(time.Hour))
    43  	})
    44  
    45  	t.Run("PGP", func(t *testing.T) {
    46  		dir := t.TempDir()
    47  		pgpKey, _ := pgp.NewPrivateKey("test", "test@example.com")
    48  		tgt, _ := target.New(target.Config{Path: dir})
    49  
    50  		c := &yum.Config{
    51  			Source: src,
    52  			Target: tgt,
    53  			PGPKey: pgpKey,
    54  		}
    55  
    56  		testBuild(t, c, want, now)
    57  
    58  		data, _ := os.ReadFile(filepath.Join(dir, "repodata", "repomd.xml"))
    59  		key, _ := os.ReadFile(filepath.Join(dir, "repodata", "repomd.xml.key"))
    60  		sig, _ := os.ReadFile(filepath.Join(dir, "repodata", "repomd.xml.asc"))
    61  		pub, _ := pgp.UnmarshalPublicKey(key)
    62  
    63  		if !pgp.Verify(pub, data, sig) {
    64  			t.Error("should pass pgp verification")
    65  		}
    66  	})
    67  }
    68  
    69  func readTestData(t *testing.T) map[string][]byte {
    70  	t.Helper()
    71  
    72  	want := make(map[string][]byte)
    73  
    74  	err := fs.WalkDir(os.DirFS("testdata"), ".", func(path string, d fs.DirEntry, err error) error {
    75  		if err != nil || d.IsDir() {
    76  			return err
    77  		}
    78  		b, err := fs.ReadFile(os.DirFS("testdata"), path)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		want[path] = b
    83  		return nil
    84  	})
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	return want
    90  }
    91  
    92  func testBuild(t *testing.T, c *yum.Config, want map[string][]byte, now time.Time) {
    93  	t.Helper()
    94  
    95  	yum.SetTime(now)
    96  
    97  	err := yum.Build(context.Background(), c)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	for name, data := range want {
   103  		r, err := c.Target.NewReader(context.Background(), name)
   104  		if err != nil {
   105  			t.Fatal(name, err)
   106  		}
   107  		defer r.Close()
   108  
   109  		got, err := io.ReadAll(r)
   110  		if err != nil {
   111  			t.Fatal(name, err)
   112  		}
   113  
   114  		if diff := cmp.Diff(data, got); diff != "" {
   115  			t.Error(name, diff)
   116  		}
   117  	}
   118  }