blob: d1f6908262a1c898ddb78ef8558160839ff3dfa1 [file] [log] [blame]
package dkms
import (
"context"
"path"
"testing"
"cos.googlesource.com/cos/tools.git/src/pkg/fakes"
"cos.googlesource.com/cos/tools.git/src/pkg/fs"
"cos.googlesource.com/cos/tools.git/src/pkg/gcs"
)
func TestAdd(t *testing.T) {
options := &Options{}
trees := &Trees{
Dkms: t.TempDir(),
Source: "testdata/source-tree",
}
pkg := &Package{
Name: "mymodule",
Version: "1.0",
KernelVersion: "6.1.100",
Arch: "x86_64",
Trees: trees,
}
err := Add(pkg, options)
if err != nil {
t.Fatalf("%v", err)
}
makefilePath := path.Join(trees.Dkms, "mymodule", "1.0", "6.1.100", "x86_64", "source", "Makefile")
if !fs.IsFile(makefilePath) {
t.Fatalf("expected to find file %s after adding sources", makefilePath)
}
}
func TestCachedAdd(t *testing.T) {
options := &Options{
Upload: true,
}
trees := &Trees{
Dkms: t.TempDir(),
Source: "testdata/source-tree",
}
pkg := &Package{
Name: "mymodule",
Version: "1.0",
KernelVersion: "6.1.100",
Arch: "x86_64",
Trees: trees,
}
ctx := context.Background()
fakeGCS := fakes.GCSForTest(t)
fakeGCS.LogMissing = false
bucket := gcs.NewGCSBucket(fakeGCS.Client, "test", "dkms")
err := CachedAdd(ctx, pkg, bucket, options)
if err != nil {
t.Fatalf("%v", err)
}
cacheMakefilePath := path.Join("mymodule", "1.0", "6.1.100", "x86_64", "source", "Makefile")
makefilePath := path.Join(trees.Dkms, cacheMakefilePath)
if !fs.IsFile(makefilePath) {
t.Fatalf("expected to find file %s after adding sources", makefilePath)
}
exists, err := bucket.Exists(ctx, cacheMakefilePath)
if err != nil {
t.Fatalf("failed to check if %s exists", cacheMakefilePath)
}
if !exists {
t.Fatalf("expected to find file %s in cache after adding sources", cacheMakefilePath)
}
}
func TestCachedAddNoLocalSources(t *testing.T) {
options := &Options{
Upload: true,
}
trees := &Trees{
Dkms: t.TempDir(),
Source: "testdata/source-tree",
}
pkg := &Package{
Name: "mymodule",
Version: "1.0",
KernelVersion: "6.1.100",
Arch: "x86_64",
Trees: trees,
}
ctx := context.Background()
fakeGCS := fakes.GCSForTest(t)
fakeGCS.LogMissing = false
bucket := gcs.NewGCSBucket(fakeGCS.Client, "test", "dkms")
err := CachedAdd(ctx, pkg, bucket, options)
if err != nil {
t.Fatalf("%v", err)
}
t.Log("resetting trees")
trees = nil // make sure we don't accidentally refer to the old trees in our checks
// Use a different DKMS tree and an empty source tree so that we're forced to load from the cache
pkg.Trees = &Trees{
Dkms: t.TempDir(),
Source: t.TempDir(),
}
err = Add(pkg, options)
if err == nil {
t.Fatalf("expected error when calling Add without local sources")
}
t.Logf("got expected error: %v", err)
err = CachedAdd(ctx, pkg, bucket, options)
if err != nil {
t.Fatalf("%v", err)
}
cacheMakefilePath := path.Join("mymodule", "1.0", "6.1.100", "x86_64", "source", "Makefile")
makefilePath := path.Join(pkg.Trees.Dkms, cacheMakefilePath)
if !fs.IsFile(makefilePath) {
t.Fatalf("expected to find file %s after adding sources", makefilePath)
}
exists, err := bucket.Exists(ctx, cacheMakefilePath)
if err != nil {
t.Fatalf("failed to check if %s exists", cacheMakefilePath)
}
if !exists {
t.Fatalf("expected to find file %s in cache after adding sources", cacheMakefilePath)
}
}