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

Make debugging easier; replace ioutil #26

Merged
merged 8 commits into from
Jul 31, 2023
9 changes: 5 additions & 4 deletions acceptance/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ var _ = Describe("acceptance", func() {
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
Expect(string(session.Out.Contents())).To(ContainSubstring(`
--input-tile, -i path to input tile (example: /path/to/input.pivotal)
--output-tile, -o path to output tile (example: /path/to/output.pivotal)
--registry, -r path to docker registry (example: /path/to/registry, default: "https://registry.hub.docker.com")
--help, -h prints this usage information`))
--input-tile, -i path to input tile (example: /path/to/input.pivotal)
--output-tile, -o path to output tile (example: /path/to/output.pivotal)
--preserve-extracted, -p preserve the files created during the tile extraction process (useful for debugging)
--registry, -r path to docker registry (example: /path/to/registry, default: "https://registry.hub.docker.com")
--help, -h prints this usage information`))
})
ebroberson marked this conversation as resolved.
Show resolved Hide resolved
})
})
27 changes: 16 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
ebroberson marked this conversation as resolved.
Show resolved Hide resolved
"log"
"os"

Expand All @@ -14,18 +13,20 @@ import (
const usageText = `winfs-injector injects the Windows 2016 root file system into the Windows 2016 Runtime Tile.

Usage: winfs-injector
--input-tile, -i path to input tile (example: /path/to/input.pivotal)
--output-tile, -o path to output tile (example: /path/to/output.pivotal)
--registry, -r path to docker registry (example: /path/to/registry, default: "https://registry.hub.docker.com")
--help, -h prints this usage information
--input-tile, -i path to input tile (example: /path/to/input.pivotal)
--output-tile, -o path to output tile (example: /path/to/output.pivotal)
--preserve-extracted, -p preserve the files created during the tile extraction process (useful for debugging)
--registry, -r path to docker registry (example: /path/to/registry, default: "https://registry.hub.docker.com")
--help, -h prints this usage information
`

func main() {
var arguments struct {
InputTile string `short:"i" long:"input-tile"`
OutputTile string `short:"o" long:"output-tile"`
Registry string `short:"r" long:"registry" default:"https://registry.hub.docker.com"`
Help bool `short:"h" long:"help"`
InputTile string `short:"i" long:"input-tile"`
OutputTile string `short:"o" long:"output-tile"`
PreserveExtracted bool `short:"p" long:"preserve-extracted"`
Registry string `short:"r" long:"registry" default:"https://registry.hub.docker.com"`
Help bool `short:"h" long:"help"`
}

_, err := jhanda.Parse(&arguments, os.Args[1:])
Expand All @@ -42,11 +43,15 @@ func main() {
var zipper = tile.NewZipper()
var releaseCreator = winfsinjector.ReleaseCreator{}

wd, err := ioutil.TempDir("", "")
wd, err := os.MkdirTemp("", "")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(wd)

fmt.Fprintf(os.Stdout, "tile extraction directory: %s\n", wd)
if !arguments.PreserveExtracted {
defer os.RemoveAll(wd)
}
ebroberson marked this conversation as resolved.
Show resolved Hide resolved

app := winfsinjector.NewApplication(releaseCreator, tileInjector, zipper)

Expand Down
6 changes: 3 additions & 3 deletions tile/tile_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tile

import (
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -37,7 +37,7 @@ func (i TileInjector) AddReleaseToMetadata(releasePath, releaseName, releaseVers
}
defer f.Close()

data, err := ioutil.ReadAll(f)
data, err := io.ReadAll(f)
if err != nil {
return err
}
Expand All @@ -59,5 +59,5 @@ func (i TileInjector) AddReleaseToMetadata(releasePath, releaseName, releaseVers
return err
}

return ioutil.WriteFile(metadataFilePath, contents, 0644)
return os.WriteFile(metadataFilePath, contents, 0644)
}
17 changes: 8 additions & 9 deletions tile/tile_injector_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tile_test

import (
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -30,30 +29,30 @@ var _ = Describe("TileInjector", func() {
releaseVersion = "1.2.3"

var err error
baseTmpDir, err = ioutil.TempDir("", "")
baseTmpDir, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())

releasePath = filepath.Join(baseTmpDir, "some-release.tgz")
err = ioutil.WriteFile(releasePath, []byte("something"), 0644)
err = os.WriteFile(releasePath, []byte("something"), 0644)
Expect(err).NotTo(HaveOccurred())

tileDir = filepath.Join(baseTmpDir, "some-tile")
err = os.Mkdir(tileDir, 0755)
Expect(err).NotTo(HaveOccurred())

initialMetadataPath := filepath.Join("fixtures", "initial_metadata.yml")
initialMetadataContents, err := ioutil.ReadFile(initialMetadataPath)
initialMetadataContents, err := os.ReadFile(initialMetadataPath)
Expect(err).NotTo(HaveOccurred())

err = os.Mkdir(filepath.Join(tileDir, "metadata"), 0755)
Expect(err).NotTo(HaveOccurred())

metadataPath = filepath.Join(tileDir, "metadata", "some-product-metadata.yml")
err = ioutil.WriteFile(metadataPath, initialMetadataContents, 0644)
err = os.WriteFile(metadataPath, initialMetadataContents, 0644)
Expect(err).NotTo(HaveOccurred())

expectedMetadataPath := filepath.Join("fixtures", "expected_metadata.yml")
expectedMetadataContents, err := ioutil.ReadFile(expectedMetadataPath)
expectedMetadataContents, err := os.ReadFile(expectedMetadataPath)
Expect(err).NotTo(HaveOccurred())

err = yaml.Unmarshal(expectedMetadataContents, &expectedMetadata)
Expand All @@ -71,7 +70,7 @@ var _ = Describe("TileInjector", func() {
err := tileInjector.AddReleaseToMetadata(releasePath, releaseName, releaseVersion, tileDir)
Expect(err).NotTo(HaveOccurred())

rawMetadata, err := ioutil.ReadFile(metadataPath)
rawMetadata, err := os.ReadFile(metadataPath)
Expect(err).NotTo(HaveOccurred())

var actualMetadata tile.Metadata
Expand All @@ -97,7 +96,7 @@ var _ = Describe("TileInjector", func() {
})

It("returns an error when metadata contains malformed yaml", func() {
err := ioutil.WriteFile(metadataPath, []byte("%%%%"), 0644)
err := os.WriteFile(metadataPath, []byte("%%%%"), 0644)
Expect(err).NotTo(HaveOccurred())

err = tileInjector.AddReleaseToMetadata(releasePath, releaseName, releaseVersion, tileDir)
Expand All @@ -106,7 +105,7 @@ var _ = Describe("TileInjector", func() {

It("returns an error when multiple yaml files exist in the metadata directory", func() {
secondMetadataPath := filepath.Join(filepath.Dir(metadataPath), "second.yml")
err := ioutil.WriteFile(secondMetadataPath, []byte("{}"), 0644)
err := os.WriteFile(secondMetadataPath, []byte("{}"), 0644)
Expect(err).NotTo(HaveOccurred())

err = tileInjector.AddReleaseToMetadata(releasePath, releaseName, releaseVersion, tileDir)
Expand Down
18 changes: 9 additions & 9 deletions tile/zipper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tile_test

import (
"archive/zip"
"io/ioutil"
"io"
"os"
"path/filepath"

Expand All @@ -23,19 +23,19 @@ var _ = Describe("Zipper", func() {
zipper = tile.NewZipper()

var err error
srcDir, err = ioutil.TempDir("", "")
srcDir, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())

zipFile, err = ioutil.TempFile("", "")
zipFile, err = os.CreateTemp("", "")
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(filepath.Join(srcDir, "top-level-file"), []byte("foo"), os.FileMode(0644))
err = os.WriteFile(filepath.Join(srcDir, "top-level-file"), []byte("foo"), os.FileMode(0644))
Expect(err).NotTo(HaveOccurred())

err = os.Mkdir(filepath.Join(srcDir, "second-level-dir"), os.FileMode(0755))
Expect(err).NotTo(HaveOccurred())

err = ioutil.WriteFile(filepath.Join(srcDir, "second-level-dir", "second-level-file"), []byte("bar"), os.FileMode(0644))
err = os.WriteFile(filepath.Join(srcDir, "second-level-dir", "second-level-file"), []byte("bar"), os.FileMode(0644))
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -60,8 +60,8 @@ var _ = Describe("Zipper", func() {
Expect(actualZip.File).To(HaveLen(3))

fileAssertions := map[string]string{
"top-level-file": "foo",
"second-level-dir": "",
"top-level-file": "foo",
"second-level-dir": "",
filepath.Join("second-level-dir", "second-level-file"): "bar",
}

Expand All @@ -71,7 +71,7 @@ var _ = Describe("Zipper", func() {

defer openedFile.Close()

fileContents, err := ioutil.ReadAll(openedFile)
fileContents, err := io.ReadAll(openedFile)
Expect(err).NotTo(HaveOccurred())
Expect(string(fileContents)).To(Equal(fileAssertions[f.Name]))
}
Expand Down Expand Up @@ -105,7 +105,7 @@ var _ = Describe("Zipper", func() {
inputTile = filepath.Join("fixtures", "test.zip")

var err error
destDir, err = ioutil.TempDir("", "")
destDir, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
})

Expand Down
5 changes: 2 additions & 3 deletions winfsinjector/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package winfsinjector
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -15,7 +14,7 @@ import (
)

var (
readFile = ioutil.ReadFile
readFile = os.ReadFile
removeAll = os.RemoveAll
)

Expand Down Expand Up @@ -71,7 +70,7 @@ func (a Application) Run(inputTile, outputTile, registry, workingDir string) err

embeddedReleaseDir := filepath.Join(extractedTileDir, "embed/windowsfs-release")
if _, err := os.Stat(embeddedReleaseDir); os.IsNotExist(err) {
fmt.Println("The file system has already been injected in the tile; skipping injection")
fmt.Println("No file system found or the file system has already been injected in the tile; skipping injection")
ebroberson marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
releaseVersion, err := a.extractReleaseVersion(embeddedReleaseDir)
Expand Down
8 changes: 4 additions & 4 deletions winfsinjector/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package winfsinjector_test
import (
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -39,7 +39,7 @@ var _ = Describe("application", func() {
outputTile = "/path/to/output/tile"
registry = "/path/to/docker/registry"

workingDir, err = ioutil.TempDir("", "")
workingDir, err = os.MkdirTemp("", "")
Expect(err).ToNot(HaveOccurred())

embedFilePath := fmt.Sprintf("%s/extracted-tile/embed", workingDir)
Expand Down Expand Up @@ -246,8 +246,8 @@ windows2019fs/windows2016fs-MISSING-IMAGE-TAG.tgz:
w.Close()

Expect(err).ToNot(HaveOccurred())
stdout, _ := ioutil.ReadAll(r)
Expect(string(stdout)).To(ContainSubstring("The file system has already been injected in the tile; skipping injection"))
stdout, _ := io.ReadAll(r)
Expect(string(stdout)).To(ContainSubstring("No file system found or the file system has already been injected in the tile; skipping injection"))
})
})

Expand Down
3 changes: 1 addition & 2 deletions winfsinjector/exports_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package winfsinjector

import (
"io/ioutil"
"os"
)

Expand All @@ -10,7 +9,7 @@ func SetReadFile(f func(string) ([]byte, error)) {
}

func ResetReadFile() {
readFile = ioutil.ReadFile
readFile = os.ReadFile
}

func SetRemoveAll(f func(string) error) {
Expand Down
3 changes: 1 addition & 2 deletions winfsinjector/release_creator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package winfsinjector

import (
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -49,7 +48,7 @@ func (rc ReleaseCreator) CreateRelease(releaseName, imageName, releaseDir, tarba
}

// bosh create-release adds ~7GB of temp files that should be cleaned up
tmpDir, err := ioutil.TempDir("", "winfs-create-release")
tmpDir, err := os.MkdirTemp("", "winfs-create-release")
if err != nil {
return err
}
Expand Down