Skip to content

Commit

Permalink
Refactoring and test fixes. (#70)
Browse files Browse the repository at this point in the history
* Refactoring and test fixes.

* Increased coverage in service provider.
  • Loading branch information
Skarlso authored and michelvocks committed Aug 6, 2018
1 parent 81d8f41 commit 52d975c
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GO_LDFLAGS_STATIC=-ldflags "-s -w -extldflags -static"
default: dev

dev:
go run ./cmd/gaia/main.go -homepath=${PWD}/tmp -dev=true -poll=true
go run ./cmd/gaia/main.go -homepath=${PWD}/tmp -dev=true

compile_frontend:
cd ./frontend && \
Expand Down
47 changes: 22 additions & 25 deletions handlers/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ func (ms *mockScheduleService) SchedulePipeline(p *gaia.Pipeline) (*gaia.Pipelin
}

func TestPipelineGitLSRemote(t *testing.T) {
dataDir, err := ioutil.TempDir("", "temp")
if err != nil {
t.Fatalf("error creating data dir %v", err.Error())
}
dataDir, _ := ioutil.TempDir("", "TestPipelineGitLSRemote")

defer func() {
gaia.Cfg = nil
os.RemoveAll(dataDir)
}()

gaia.Cfg = &gaia.Config{
Expand Down Expand Up @@ -84,7 +80,7 @@ func TestPipelineGitLSRemote(t *testing.T) {
})

t.Run("otherwise succeed", func(t *testing.T) {
repoURL := "https://github.com/gaia-pipeline/gaia"
repoURL := "https://github.com/gaia-pipeline/pipeline-test"
body := map[string]string{
"url": repoURL,
"username": "admin",
Expand All @@ -105,19 +101,20 @@ func TestPipelineGitLSRemote(t *testing.T) {
}

func TestPipelineUpdate(t *testing.T) {
dataDir, err := ioutil.TempDir("", "temp")
if err != nil {
t.Fatalf("error creating data dir %v", err.Error())
}
defer os.RemoveAll(dataDir)
tmp, _ := ioutil.TempDir("", "TestPipelineUpdate")
dataDir := tmp

gaia.Cfg = &gaia.Config{
Logger: hclog.NewNullLogger(),
DataPath: dataDir,
Logger: hclog.NewNullLogger(),
DataPath: dataDir,
HomePath: dataDir,
PipelinePath: dataDir,
}

// Initialize store
dataStore, _ := services.StorageService()
dataStore.Init()
defer func() { services.MockStorageService(nil) }()
// Initialize global active pipelines
ap := pipeline.NewActivePipelines()
pipeline.GlobalActivePipelines = ap
Expand All @@ -141,7 +138,7 @@ func TestPipelineUpdate(t *testing.T) {
}

// Add to store
err = dataStore.PipelinePut(&pipeline1)
err := dataStore.PipelinePut(&pipeline1)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -190,24 +187,20 @@ func TestPipelineUpdate(t *testing.T) {
}

func TestPipelineDelete(t *testing.T) {
dataDir, err := ioutil.TempDir("", "temp")
if err != nil {
t.Fatalf("error creating data dir %v", err.Error())
}
defer os.RemoveAll(dataDir)
tmp, _ := ioutil.TempDir("", "TestPipelineDelete")
dataDir := tmp

gaia.Cfg = &gaia.Config{
Logger: hclog.NewNullLogger(),
HomePath: dataDir,
DataPath: dataDir,
PipelinePath: dataDir,
}

// Initialize store
dataStore, _ := services.StorageService()
err = dataStore.Init()
if err != nil {
t.Fatalf("cannot initialize store: %v", err.Error())
}
dataStore.Init()
defer func() { services.MockStorageService(nil) }()

// Initialize global active pipelines
ap := pipeline.NewActivePipelines()
Expand All @@ -225,7 +218,7 @@ func TestPipelineDelete(t *testing.T) {
}

// Add to store
err = dataStore.PipelinePut(&p)
err := dataStore.PipelinePut(&p)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -274,8 +267,12 @@ func TestPipelineDelete(t *testing.T) {
}

func TestPipelineStart(t *testing.T) {
tmp, _ := ioutil.TempDir("", "TestPipelineStart")
gaia.Cfg = &gaia.Config{
Logger: hclog.NewNullLogger(),
Logger: hclog.NewNullLogger(),
HomePath: tmp,
DataPath: tmp,
PipelinePath: tmp,
}

// Initialize global active pipelines
Expand Down
16 changes: 4 additions & 12 deletions handlers/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http/httptest"

"io/ioutil"
"os"

"crypto/rand"
"crypto/rsa"
Expand All @@ -21,15 +20,11 @@ import (
)

func TestUserLoginHMACKey(t *testing.T) {

dataDir, err := ioutil.TempDir("", "hmac")
if err != nil {
t.Fatalf("error creating data dir %v", err.Error())
}
tmp, _ := ioutil.TempDir("", "TestUserLoginHMACKey")
dataDir := tmp

defer func() {
gaia.Cfg = nil
os.RemoveAll(dataDir)
}()

gaia.Cfg = &gaia.Config{
Expand Down Expand Up @@ -77,14 +72,11 @@ func TestUserLoginHMACKey(t *testing.T) {
}

func TestUserLoginRSAKey(t *testing.T) {
dataDir, err := ioutil.TempDir("", "rsa")
if err != nil {
t.Fatalf("error creating data dir %v", err.Error())
}
tmp, _ := ioutil.TempDir("", "TestUserLoginRSAKey")
dataDir := tmp

defer func() {
gaia.Cfg = nil
os.RemoveAll(dataDir)
}()

key, _ := rsa.GenerateKey(rand.Reader, 2048)
Expand Down
19 changes: 6 additions & 13 deletions handlers/vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/gaia-pipeline/gaia"
Expand All @@ -16,14 +15,10 @@ import (
)

func TestVaultWorkflowAddListDelete(t *testing.T) {
dataDir, err := ioutil.TempDir("", "temp")
if err != nil {
t.Fatalf("error creating data dir %v", err.Error())
}
dataDir, _ := ioutil.TempDir("", "TestVaultWorkflowAddListDelete")

defer func() {
gaia.Cfg = nil
os.RemoveAll(dataDir)
}()

gaia.Cfg = &gaia.Config{
Expand All @@ -33,17 +28,15 @@ func TestVaultWorkflowAddListDelete(t *testing.T) {
VaultPath: dataDir,
}

dataStore, _ := services.StorageService()
err = dataStore.Init()
if err != nil {
t.Fatalf("cannot initialize store: %v", err.Error())
}

_, err = services.CertificateService()
ce, err := services.CertificateService()
if err != nil {
t.Fatalf("cannot initialize certificate service: %v", err.Error())
}

// Make sure the cert exists because if the service was alreay
// created, Init won't be called again.
ce.CreateSignedCert()

e := echo.New()
InitHandlers(e)
t.Run("can add secret", func(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions pipeline/build_golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (m *mockStorer) PipelinePut(p *gaia.Pipeline) error {
}

func TestPrepareEnvironmentGo(t *testing.T) {
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestPrepareEnvironmentGo")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
b := new(BuildPipelineGolang)
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestExecuteBuildGo(t *testing.T) {
defer func() {
execCommandContext = exec.CommandContext
}()
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestExecuteBuildGo")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
b := new(BuildPipelineGolang)
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestExecuteBuildContextTimeoutGo(t *testing.T) {
execCommandContext = exec.CommandContext
buildKillContext = false
}()
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestExecuteBuildContextTimeoutGo")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
// Initialize shared logger
Expand All @@ -139,7 +139,7 @@ func TestExecuteBuildContextTimeoutGo(t *testing.T) {
}

func TestExecuteBuildBinaryNotFoundErrorGo(t *testing.T) {
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestExecuteBuildBinaryNotFoundErrorGo")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
// Initialize shared logger
Expand All @@ -164,7 +164,7 @@ func TestExecuteBuildBinaryNotFoundErrorGo(t *testing.T) {
}

func TestCopyBinaryGo(t *testing.T) {
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestCopyBinaryGo")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
// Initialize shared logger
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestCopyBinaryGo(t *testing.T) {
}

func TestCopyBinarySrcDoesNotExistGo(t *testing.T) {
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestCopyBinarySrcDoesNotExistGo")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
// Initialize shared logger
Expand Down
12 changes: 6 additions & 6 deletions pipeline/build_java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func TestPrepareEnvironmentJava(t *testing.T) {
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestPrepareEnvironmentJava")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
b := new(BuildPipelineJava)
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestExecuteBuildJava(t *testing.T) {
defer func() {
execCommandContext = exec.CommandContext
}()
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestExecuteBuildJava")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
b := new(BuildPipelineJava)
Expand All @@ -73,7 +73,7 @@ func TestExecuteBuildContextTimeoutJava(t *testing.T) {
execCommandContext = exec.CommandContext
buildKillContext = false
}()
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestExecuteBuildContextTimeoutJava")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
// Initialize shared logger
Expand All @@ -97,7 +97,7 @@ func TestExecuteBuildContextTimeoutJava(t *testing.T) {
}

func TestCopyBinaryJava(t *testing.T) {
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestCopyBinaryJava")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
// Initialize shared logger
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestCopyBinaryJava(t *testing.T) {
}

func TestCopyBinarySrcDoesNotExistJava(t *testing.T) {
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestCopyBinarySrcDoesNotExistJava")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
// Initialize shared logger
Expand Down Expand Up @@ -169,7 +169,7 @@ func (m *javaMockStorer) PipelinePut(p *gaia.Pipeline) error {
}

func TestSavePipelineJava(t *testing.T) {
tmp := os.TempDir()
tmp, _ := ioutil.TempDir("", "TestSavePipelineJava")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = tmp
m := new(javaMockStorer)
Expand Down
Loading

0 comments on commit 52d975c

Please sign in to comment.