| package dkms |
| |
| import ( |
| "context" |
| "os" |
| "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") |
| // Add the module to the cache and the local directory. We'll remove it from |
| // the local directory later to make sure we can download it and add it from |
| // the cache. |
| 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) |
| } |
| } |
| |
| func TestAddWithEmptySourceTreeDir(t *testing.T) { |
| options := &Options{ |
| Upload: true, |
| } |
| |
| trees := &Trees{ |
| Dkms: t.TempDir(), |
| Source: t.TempDir(), |
| } |
| |
| pkg := &Package{ |
| Name: "mymodule", |
| Version: "1.0", |
| KernelVersion: "6.1.100", |
| Arch: "x86_64", |
| Trees: trees, |
| } |
| |
| if err := os.Mkdir(pkg.SourceTreeDir(), 0777); err != nil { |
| t.Fatalf("could not make source dir for testing") |
| } |
| |
| err := Add(pkg, options) |
| if err == nil { |
| t.Fatalf("expected error when calling Add empty local source dir") |
| } |
| t.Logf("got expected error: %v", err) |
| } |