Skip to content

WIP: tests #12

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 24 additions & 24 deletions soos.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package soos

import (
"bytes"
Expand All @@ -18,7 +18,7 @@ type Configuration struct {
ExposePorts []string
}

func getConfig() Configuration {
func GetConfig() Configuration {
file, _ := os.Open(".soos.json")
decoder := json.NewDecoder(file)
configuration := Configuration{}
Expand All @@ -29,7 +29,7 @@ func getConfig() Configuration {
return configuration
}

func tokenizer() string {
func Tokenizer() string {
f, err := os.Open("package.json")
if err != nil {
log.Fatal(err)
Expand All @@ -43,16 +43,16 @@ func tokenizer() string {

fileSum := fmt.Sprintf("%x", h.Sum(nil))

imageName := getConfig().ImageName
imageName := GetConfig().ImageName

if imageName == "" {
imageName = filepath.Base(cwd())
imageName = filepath.Base(Cwd())
}

return imageName + ":" + fileSum
}

func checkImagePresence(imageNameWithTag string) bool {
func CheckImagePresence(imageNameWithTag string) bool {
cmd := exec.Command("docker", "image", "ls", "-q", imageNameWithTag)
var out bytes.Buffer
cmd.Stdout = &out
Expand All @@ -70,7 +70,7 @@ func checkImagePresence(imageNameWithTag string) bool {

}

func genDockerfile() {
func GenDockerfile() {

dockerfileContent := `
FROM node:9.2.0
Expand Down Expand Up @@ -106,7 +106,7 @@ CMD ["start"]
}
}

func buildImage(imageNameWithTag string) {
func BuildImage(imageNameWithTag string) {

cmd := exec.Command("docker", "build", "-t", imageNameWithTag, ".")
var out bytes.Buffer
Expand All @@ -122,24 +122,24 @@ func buildImage(imageNameWithTag string) {

}

func cwd() string {
func Cwd() string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return dir
}

func runImage(imageNameWithTag string) {
func RunImage(imageNameWithTag string) {

args := []string{"run"}

if len(getConfig().ExposePorts) != 0 {
exposePortsArg := "-p" + getConfig().ExposePorts[0]
args = append([]string{"run", "--rm", exposePortsArg, "-v", cwd() + ":/build/app", imageNameWithTag}, os.Args[1:]...)
if len(GetConfig().ExposePorts) != 0 {
exposePortsArg := "-p" + GetConfig().ExposePorts[0]
args = append([]string{"run", "--rm", exposePortsArg, "-v", Cwd() + ":/build/app", imageNameWithTag}, os.Args[1:]...)
} else {

args = append([]string{"run", "--rm", "-v", cwd() + ":/build/app", imageNameWithTag}, os.Args[1:]...)
args = append([]string{"run", "--rm", "-v", Cwd() + ":/build/app", imageNameWithTag}, os.Args[1:]...)
}

cmd := exec.Command("docker", args...)
Expand All @@ -158,7 +158,7 @@ func runImage(imageNameWithTag string) {
fmt.Print(out.String())
}

func pullImage(imageNameWithTag string) {
func PullImage(imageNameWithTag string) {

cmd := exec.Command("docker", "pull", imageNameWithTag)
var out bytes.Buffer
Expand All @@ -175,7 +175,7 @@ func pullImage(imageNameWithTag string) {
fmt.Print(out.String())
}

func pushImage(imageNameWithTag string) {
func PushImage(imageNameWithTag string) {

cmd := exec.Command("docker", "push", imageNameWithTag)
var out bytes.Buffer
Expand All @@ -196,37 +196,37 @@ func pushImage(imageNameWithTag string) {
func main() {
fmt.Printf("<*> Soos start\n")

imageReference := tokenizer()
imageReference := Tokenizer()
fmt.Printf("<-> Generated image name is %s\n", imageReference)

fmt.Printf("<-> Verifying/Generating Dockerfile presence...")
genDockerfile()
GenDockerfile()
fmt.Printf("done\n")

localImageIsPresent := checkImagePresence(imageReference)
localImageIsPresent := CheckImagePresence(imageReference)

localImageIsPresent2 := false

if !localImageIsPresent {
fmt.Printf("<-> Image is missing, trying to pull...")
pullImage(imageReference)
localImageIsPresent2 := checkImagePresence(imageReference)
PullImage(imageReference)
localImageIsPresent2 := CheckImagePresence(imageReference)
fmt.Printf("<-> result: %t done\n", localImageIsPresent2)
}

if !localImageIsPresent && !localImageIsPresent2 {
fmt.Printf("<-> Image is missing, building...")
buildImage(imageReference)
BuildImage(imageReference)
fmt.Printf("<-> done\n")
}

fmt.Printf("<-> Running image...\n\n")
runImage(imageReference)
RunImage(imageReference)
fmt.Printf("\n\ndone\n")

if !localImageIsPresent2 {
fmt.Printf("<-> Pushing image...")
pushImage(imageReference)
PushImage(imageReference)
fmt.Printf("done\n")
}

Expand Down
51 changes: 51 additions & 0 deletions soos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package soos

import "testing"

func TestGetConfig(t *testing.T) {

var config Configuration

config = GetConfig()

if config.ImageName != "elmariofredo/soos-npm" {
t.Error("Expected 'elmariofredo/soos-npm', got ", config.ImageName)
}

if config.ExposePorts[0] != "3000:3000" {
t.Error("Expected '3000:3000', got ", config.ExposePorts)
}

}

func TestTokenizer(t *testing.T) {
var token string

token = Tokenizer()

if token != "elmariofredo/soos-npm:ca72c8880484f8dc8db7f765b9a353110f3b56ce" {
t.Error("Expected 'elmariofredo/soos-npm:ca72c8880484f8dc8db7f765b9a353110f3b56ce', got", token)
}
}

func TestCheckImagePresence(t *testing.T) {
t.Error("Expected 'implemented tests', got")
}
func TestGenDockerfile(t *testing.T) {
t.Error("Expected 'implemented tests', got")
}
func TestBuildImage(t *testing.T) {
t.Error("Expected 'implemented tests', got")
}
func TestCwd(t *testing.T) {
t.Error("Expected 'implemented tests', got")
}
func TestRunImage(t *testing.T) {
t.Error("Expected 'implemented tests', got")
}
func TestPullImage(t *testing.T) {
t.Error("Expected 'implemented tests', got")
}
func TestPushImage(t *testing.T) {
t.Error("Expected 'implemented tests', got")
}