Skip to content

Commit

Permalink
Added moar coverage for the worker.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Aug 25, 2019
1 parent f773964 commit 1029308
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
6 changes: 5 additions & 1 deletion workers/server/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (w *WorkServer) GetWork(workInst *pb.WorkerInstance, serv pb.Worker_GetWork
return nil
}

// GetGitRepo checks out the code for a git repository.
// GetGitRepo retrieves repository information associated with a pipline.
func (w *WorkServer) GetGitRepo(ctx context.Context, in *pb.PipelineID) (*pb.GitRepo, error) {
repo := &pb.GitRepo{}

Expand All @@ -124,6 +124,10 @@ func (w *WorkServer) GetGitRepo(ctx context.Context, in *pb.PipelineID) (*pb.Git
return repo, err
}

if repoInfo == nil {
return nil, fmt.Errorf("pipeline for id %d not found\n", int(in.Id))
}

pk := pb.PrivateKey{}
pk.Key = repoInfo.Repo.PrivateKey.Key
pk.Username = repoInfo.Repo.PrivateKey.Username
Expand Down
71 changes: 66 additions & 5 deletions workers/server/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
"github.com/gaia-pipeline/gaia/store"
Expand All @@ -14,11 +20,6 @@ import (
"github.com/hashicorp/go-hclog"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

type mockMemDBService struct {
Expand All @@ -42,12 +43,16 @@ func (mm *mockMemDBService) DeleteWorker(id string, persist bool) error {

type mockStorageService struct {
store.GaiaStore
mockPipeline *gaia.Pipeline
}

func (s *mockStorageService) PipelineGetRunByPipelineIDAndID(pipelineid int, runid int) (*gaia.PipelineRun, error) {
return generateTestData(), nil
}
func (s *mockStorageService) PipelinePutRun(r *gaia.PipelineRun) error { return nil }
func (s *mockStorageService) PipelineGet(id int) (pipeline *gaia.Pipeline, err error) {
return s.mockPipeline, nil
}

type mockGetWorkServ struct {
grpc.ServerStream
Expand Down Expand Up @@ -317,3 +322,59 @@ func TestDeregister(t *testing.T) {
t.Fatal(err)
}
}

func TestGetGitRepository(t *testing.T) {
gaia.Cfg = &gaia.Config{}
gaia.Cfg.Logger = hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Name: "Gaia",
})
ms := mockStorageService{mockPipeline: &gaia.Pipeline{
ID: 1,
Name: "testPipeline",
Repo: &gaia.GitRepo{
URL: "https://github.com/gaia-pipeline/go-example",
},
}}
services.MockMemDBService(&mockMemDBService{})
services.MockStorageService(&ms)

// Mock gRPC server
mw := mockGetWorkServ{}

// Run deregister
ws := WorkServer{}
repo, err := ws.GetGitRepo(mw.Context(), &pb.PipelineID{Id: 1})
if err != nil {
t.Fatal(err)
}
expectedRepoURL := "https://github.com/gaia-pipeline/go-example"
if repo.Url != expectedRepoURL {
t.Fatalf("expected git repo url: %s, got: %s\n", expectedRepoURL, repo.Url)
}
}

func TestGetGitRepositoryRepoNotFound(t *testing.T) {
gaia.Cfg = &gaia.Config{}
gaia.Cfg.Logger = hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
Name: "Gaia",
})
services.MockMemDBService(&mockMemDBService{})
services.MockStorageService(&mockStorageService{})

// Mock gRPC server
mw := mockGetWorkServ{}

// Run deregister
ws := WorkServer{}
_, err := ws.GetGitRepo(mw.Context(), &pb.PipelineID{Id: 9999})
if err == nil {
t.Fatal("should have gotten an error because pipeline doesn't exist")
}

expectedError := fmt.Sprintf("pipeline for id %d not found\n", 9999)
if err.Error() != expectedError {
t.Fatalf("expected error message: %s, got: %s\n", expectedError, err.Error())
}
}

0 comments on commit 1029308

Please sign in to comment.