Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #174 from openhie/PLAT-37-go-cli-unit-tests
Browse files Browse the repository at this point in the history
Plat 37 go cli unit tests
  • Loading branch information
MattyJ007 authored Feb 9, 2022
2 parents 9a4a3f1 + e6ab9df commit a771a80
Show file tree
Hide file tree
Showing 7 changed files with 1,126 additions and 32 deletions.
8 changes: 8 additions & 0 deletions goinstant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ Therefore, this app is not meant to be used for container and cluster management

For development, run the app using `go run *.go`.

### Testing

To run the unit tests, ensure you're in the goinstant directory, then do

```
go test . -v
```

### Building

```sh
Expand Down
68 changes: 47 additions & 21 deletions goinstant/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ import (
"golang.org/x/net/context"
)

var (
OsCreate = os.Create
IoCopy = io.Copy
ZipOpenReader = zip.OpenReader
OsMkdirAll = os.MkdirAll
FilepathJoin = filepath.Join
OsOpenFile = os.OpenFile
OsRemove = os.Remove
execCommand = exec.Command
runDeployCommand = RunDeployCommand
RunCommand = runCommand
MountCustomPackage = mountCustomPackage
)

func debugDocker() error {
fmt.Printf("...checking your Docker setup")

Expand Down Expand Up @@ -115,7 +129,7 @@ func extractCommands(startupCommands []string) (environmentVariables []string, d
}

if targetLauncher == "" {
targetLauncher = cfg.DefaultTargetLauncher
targetLauncher = customOptions.targetLauncher
}

return
Expand All @@ -140,7 +154,7 @@ func RunDeployCommand(startupCommands []string) error {
if deployCommand == "init" {
fmt.Println("\n\nDelete a pre-existing instant volume...")
commandSlice := []string{"volume", "rm", "instant"}
_, err = runCommand("docker", []string{"Error: No such volume: instant"}, commandSlice...)
_, err = RunCommand("docker", []string{"Error: No such volume: instant"}, commandSlice...)
if err != nil {
return err
}
Expand All @@ -160,7 +174,7 @@ func RunDeployCommand(startupCommands []string) error {
commandSlice = append(commandSlice, otherFlags...)
commandSlice = append(commandSlice, []string{"-t", targetLauncher}...)
commandSlice = append(commandSlice, packages...)
_, err = runCommand("docker", nil, commandSlice...)
_, err = RunCommand("docker", nil, commandSlice...)
if err != nil {
return err
}
Expand All @@ -169,30 +183,30 @@ func RunDeployCommand(startupCommands []string) error {

for _, c := range customPackagePaths {
fmt.Print("- " + c)
err = mountCustomPackage(c)
err = MountCustomPackage(c)
if err != nil {
return err
}
}

fmt.Println("\nRun Instant OpenHIE Installer Container")
commandSlice = []string{"start", "-a", "instant-openhie"}
_, err = runCommand("docker", nil, commandSlice...)
_, err = RunCommand("docker", nil, commandSlice...)
if err != nil {
return err
}

if deployCommand == "destroy" {
fmt.Println("Delete instant volume...")
commandSlice := []string{"volume", "rm", "instant"}
_, err = runCommand("docker", nil, commandSlice...)
_, err = RunCommand("docker", nil, commandSlice...)
}

return err
}

func runCommand(commandName string, suppressErrors []string, commandSlice ...string) (pathToPackage string, err error) {
cmd := exec.Command(commandName, commandSlice...)
var runCommand = func(commandName string, suppressErrors []string, commandSlice ...string) (pathToPackage string, err error) {
cmd := execCommand(commandName, commandSlice...)
cmdReader, err := cmd.StdoutPipe()
var stderr bytes.Buffer
cmd.Stderr = &stderr
Expand Down Expand Up @@ -288,73 +302,85 @@ func mountCustomPackage(pathToPackage string) error {
}

func createZipFile(file string, content io.Reader) error {
output, err := os.Create(file)
output, err := OsCreate(file)
if err != nil {
return errors.Wrap(err, "Error in creating zip file:")
}
defer output.Close()

_, err = io.Copy(output, content)
bytesWritten, err := IoCopy(output, content)
if err != nil {
return errors.Wrap(err, "Error in copying zip file content:")
}
if bytesWritten < 1 {
return errors.New("File created but no content written.")
}

return nil
}

func unzipPackage(zipContent io.ReadCloser) (pathToPackage string, err error) {
var unzipPackage = func(zipContent io.ReadCloser) (pathToPackage string, err error) {
tempZipFile := "temp.zip"
err = createZipFile(tempZipFile, zipContent)
if err != nil {
return "", err
}

// Unzip file
archive, err := zip.OpenReader(tempZipFile)
archive, err := ZipOpenReader(tempZipFile)
if err != nil {
return "", errors.Wrap(err, "Error in unzipping file:")
}
defer archive.Close()

packageName := ""
for _, file := range archive.File {
filePath := filepath.Join(".", file.Name)
filePath := FilepathJoin(".", file.Name)

if file.FileInfo().IsDir() {
if packageName == "" {
packageName = file.Name
}
os.MkdirAll(filePath, os.ModePerm)
err = OsMkdirAll(filePath, os.ModePerm)
if err != nil {
return "", err
}
continue
}

content, err := file.Open()
if err != nil {
return "", errors.Wrap(err, "Error in unzipping file:")
}
defer content.Close()

dest, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
dest, err := OsOpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
return "", errors.Wrap(err, "Error in unzipping file:")
}
_, err = io.Copy(dest, content)
defer dest.Close()

written, err := IoCopy(dest, content)
if err != nil {
return "", errors.Wrap(err, "Error in copying unzipping file:")
}
content.Close()
if written < 1 {
return "", errors.New("No content copied")
}
}

// Remove temp zip file
tempFilePath := filepath.Join(".", tempZipFile)
tempFilePath := FilepathJoin(".", tempZipFile)
archive.Close()
err = os.Remove(tempFilePath)
err = OsRemove(tempFilePath)
if err != nil {
return "", errors.Wrap(err, "Error in deleting temp.zip file:")
}

return filepath.Join(".", packageName), nil
return FilepathJoin(".", packageName), nil
}

func untarPackage(tarContent io.ReadCloser) (pathToPackage string, err error) {
var untarPackage = func(tarContent io.ReadCloser) (pathToPackage string, err error) {
packageName := ""
gzipReader, err := gzip.NewReader(tarContent)
if err != nil {
Expand Down
Loading

0 comments on commit a771a80

Please sign in to comment.