Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to place SFU files from cache to provisioned media #41

Merged
merged 1 commit into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions cli/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ var (
// Regex for file matching.
regExFileExt = regexp.MustCompile(`\.[A-Za-z.]+`)
regExFileName = regexp.MustCompile(`[\w,\s-]+\.[A-Za-z.]+$`)

// minSFUPartSize represents the minimum partition size for SFU workflow.
minSFUPartSize = 12 * oneGB
)

// httpDoer represents an http client that can retrieve files with the Do
Expand Down Expand Up @@ -448,6 +451,48 @@ func (i *Installer) DownloadSFU() error {
return nil
}

// PlaceSFU copies SFU files onto provisioned media from the local cache.
func (i *Installer) PlaceSFU(d Device) error {
// Find a compatible partition to write the FFU to.
logger.V(2).Infof("Searching for FFU %q for a %q partition larger than %v.", d.FriendlyName(), humanize.Bytes(minSFUPartSize), storage.FAT32)
p, err := selectPart(d, minSFUPartSize, storage.FAT32)
if err != nil {
return fmt.Errorf("SelectPartition(%q, %q, %q) returned %w: %v", d.FriendlyName(), humanize.Bytes(minSFUPartSize), storage.FAT32, errPartition, err)
}
sfus, err := getManifest(filepath.Join(i.cache, i.config.FFUManifest()))
if err != nil {
return fmt.Errorf("getManifest() returned: %w: %v", errManifest, err)
}
for _, sfu := range sfus {
sfu := sfu
func() error{
path := filepath.Join(i.cache, sfu.Filename)
newPath := filepath.Join(p.MountPoint(), sfu.Filename)
// Add colon for windows paths if its a drive root.
if runtime.GOOS == "windows" && len(p.MountPoint()) < 2 {
newPath = filepath.Join(fmt.Sprintf("%s:", p.MountPoint()), sfu.Filename)
}
source, err := os.Open(path)
if err != nil {
return fmt.Errorf("%w: couldn't open file(%s) from cache: %v", errPath, path, err)
}
defer source.Close()
destination, err := os.Create(newPath)
if err != nil {
return fmt.Errorf("%w: couldn't create target file(%s): %v", errFile, path, err)
}
defer destination.Close()
cBytes, err := io.Copy(destination, source)
if err != nil {
return fmt.Errorf("failed to copy file to %s: %v", newPath, err)
}
console.Printf("Copied %d bytes", cBytes)
return nil
}()
}
return nil
}

// selectPartition wraps device.SelectPartition and returns its output wrapped
// in the partition interface.
func selectPartition(d Device, size uint64, fs storage.FileSystem) (partition, error) {
Expand Down
89 changes: 89 additions & 0 deletions cli/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,95 @@ func TestDownloadSFU(t *testing.T) {
}
}

// createFakeSFU is used to create a set of fake SFU files.
func createFakeSFU(fakeCache string) error {
sfus := fakeReadManifest()
for _, sfu := range sfus {
path := filepath.Join(fakeCache, sfu.Filename)
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("ioutil.TempFile(%q, %q) returned %w: %v", fakeCache, sfu.Filename, errFile, err)
}
defer f.Close()
}
return nil
}

func TestPlaceSFU(t *testing.T) {
// Setup a temp folder.
fakeCache, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("ioutil.TempDir('', '') returned %v", err)
}
if err := createFakeSFU(fakeCache); err != nil {
t.Fatalf("createFakeSFU(%s) returned: %v", fakeCache, err)
}

// Temp folders representing file system contents.
mount, contents, err := fakeFileSystems()
if err != nil {
t.Fatalf("fakeFileSystems() returned %v", err)
}
defer os.RemoveAll(mount)

c := &fakeConfig{
track: "stable",
ffuManifest: "manifest.json",
}
tests := []struct {
desc string
installer *Installer
download func(client httpDoer, path string, w io.Writer) error
fakeManifest func(string) ([]SFUManifest, error)
device *fakeDevice
selPart func(Device, uint64, storage.FileSystem) (partition, error)
want error
}{
{
desc: "successful place",
installer: &Installer{cache: fakeCache, config: c},
download: func(client httpDoer, path string, w io.Writer) error { return nil },
fakeManifest: func(string) ([]SFUManifest, error) { return fakeReadManifest(), nil },
selPart: func(Device, uint64, storage.FileSystem) (partition, error) {
return &fakePartition{mount: mount, contents: contents}, nil
},
device: &fakeDevice{},
want: nil,
},
{
desc: "manifest error",
installer: &Installer{cache: fakeCache, config: c},
download: func(client httpDoer, path string, w io.Writer) error { return nil },
fakeManifest: func(string) ([]SFUManifest, error) { return fakeReadManifest(), errManifest },
selPart: func(Device, uint64, storage.FileSystem) (partition, error) {
return &fakePartition{mount: mount, contents: contents}, nil
},
device: &fakeDevice{},
want: errManifest,
},
{
desc: "partition select failure",
installer: &Installer{cache: fakeCache, config: c},
download: func(client httpDoer, path string, w io.Writer) error { return nil },
fakeManifest: func(string) ([]SFUManifest, error) { return fakeReadManifest(), nil },
selPart: func(Device, uint64, storage.FileSystem) (partition, error) {
return &fakePartition{mount: mount, contents: contents}, errPartition
},
device: &fakeDevice{},
want: errPartition,
},
}
for _, tt := range tests {
getManifest = tt.fakeManifest
downloadFile = tt.download
selectPart = tt.selPart
got := tt.installer.PlaceSFU(tt.device)
if !errors.Is(got, tt.want) {
t.Errorf("%s: PlaceSFU() got: %v, want: %v", tt.desc, got, tt.want)
}
}
}

func createFakeJSON(name, fakeJSON, cache string) error {

// A fake manifest for testing.
Expand Down