Skip to content

Commit

Permalink
Hello Proctor!
Browse files Browse the repository at this point in the history
  • Loading branch information
olttwa committed Mar 11, 2018
1 parent 76b8739 commit 0507bbf
Show file tree
Hide file tree
Showing 27 changed files with 1,047 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.swp
proctor
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
dist: trusty
sudo: required
language: go
go:
- "1.8"

before_script:
# Install glide
- curl https://glide.sh/get | sh

stages:
- test

jobs:
include:
- stage: test
script:
- glide install
- go test -race -cover $(glide novendor)

4 changes: 4 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Proctor Team

* [Akshat](//github.com/olttwa)

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Proctor

Proctor is an automation framework. It helps everyone contribute to automation, mange it and use it.
52 changes: 52 additions & 0 deletions cmd/jobs/description/descriptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package description

import (
"fmt"

"github.com/fatih/color"
"github.com/gojekfarm/proctor/engine"
"github.com/gojekfarm/proctor/io"
"github.com/gojekfarm/proctor/jobs"
"github.com/spf13/cobra"
)

func NewCmd(printer io.Printer, proctorEngineClient engine.Client) *cobra.Command {
return &cobra.Command{
Use: "describe",
Short: "Describe a job, list help for variables and constants",
Long: `Example: proctor job describe say-hello-world`,
Run: func(cmd *cobra.Command, args []string) {
jobList, err := proctorEngineClient.ListJobs()
if err != nil {
printer.Println("Error fetching list of jobs. Please check configuration and network connectivity", color.FgRed)
return
}

desiredJob := jobs.Metadata{}
for _, job := range jobList {
if args[0] == job.Name {
desiredJob = job
}
}
if len(desiredJob.Name) == 0 {
printer.Println(fmt.Sprintf("Proctor doesn't support job: %s", args[0]), color.FgRed)
return
}

printer.Println(fmt.Sprintf("%-40s %-100s", "Job Name", desiredJob.Name), color.Reset)
printer.Println(fmt.Sprintf("%-40s %-100s", "Job Description", desiredJob.Description), color.Reset)

printer.Println("\nVariables", color.FgMagenta)
for _, arg := range desiredJob.EnvVars.Args {
printer.Println(fmt.Sprintf("%-40s %-100s", arg.Name, arg.Description), color.Reset)
}

printer.Println("\nConstants", color.FgMagenta)
for _, secret := range desiredJob.EnvVars.Secrets {
printer.Println(fmt.Sprintf("%-40s %-100s", secret.Name, secret.Description), color.Reset)
}

printer.Println("\nFor executing a job, run:\nproctor job execute <job_name> <args_name>", color.FgGreen)
},
}
}
99 changes: 99 additions & 0 deletions cmd/jobs/description/descriptor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package description

import (
"errors"
"fmt"
"testing"

"github.com/fatih/color"
"github.com/gojekfarm/proctor/engine"
"github.com/gojekfarm/proctor/io"
"github.com/gojekfarm/proctor/jobs"
"github.com/gojekfarm/proctor/jobs/env"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type DescribeCmdTestSuite struct {
suite.Suite
mockPrinter *io.MockPrinter
mockProctorEngineClient *engine.MockClient
testDescribeCmd *cobra.Command
}

func (s *DescribeCmdTestSuite) SetupTest() {
s.mockPrinter = &io.MockPrinter{}
s.mockProctorEngineClient = &engine.MockClient{}
s.testDescribeCmd = NewCmd(s.mockPrinter, s.mockProctorEngineClient)
}

func (s *DescribeCmdTestSuite) TestDescribeCmdUsage() {
assert.Equal(s.T(), "describe", s.testDescribeCmd.Use)
}

func (s *DescribeCmdTestSuite) TestDescribeCmdHelp() {
assert.Equal(s.T(), "Describe a job, list help for variables and constants", s.testDescribeCmd.Short)
assert.Equal(s.T(), "Example: proctor job describe say-hello-world", s.testDescribeCmd.Long)
}

func (s *DescribeCmdTestSuite) TestDescribeCmdRun() {
arg := env.VarMetadata{
Name: "arg-one",
Description: "arg one description",
}

secret := env.VarMetadata{
Name: "secret-one",
Description: "secret one description",
}

doSomething := jobs.Metadata{
Name: "do-something",
Description: "does something",
EnvVars: env.Vars{
Args: []env.VarMetadata{arg},
Secrets: []env.VarMetadata{secret},
},
}
jobList := []jobs.Metadata{doSomething}

s.mockProctorEngineClient.On("ListJobs").Return(jobList, nil).Once()

s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", "Job Name", doSomething.Name), color.Reset).Once()
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", "Job Description", doSomething.Description), color.Reset).Once()
s.mockPrinter.On("Println", "\nVariables", color.FgMagenta).Once()
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", arg.Name, arg.Description), color.Reset).Once()
s.mockPrinter.On("Println", "\nConstants", color.FgMagenta).Once()
s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", secret.Name, secret.Description), color.Reset).Once()
s.mockPrinter.On("Println", "\nFor executing a job, run:\nproctor job execute <job_name> <args_name>", color.FgGreen).Once()

s.testDescribeCmd.Run(&cobra.Command{}, []string{"do-something"})

s.mockProctorEngineClient.AssertExpectations(s.T())
s.mockPrinter.AssertExpectations(s.T())
}

func (s *DescribeCmdTestSuite) TestDescribeCmdRunProctorEngineClientFailure() {
s.mockProctorEngineClient.On("ListJobs").Return([]jobs.Metadata{}, errors.New("error")).Once()
s.mockPrinter.On("Println", "Error fetching list of jobs. Please check configuration and network connectivity", color.FgRed).Once()

s.testDescribeCmd.Run(&cobra.Command{}, []string{})

s.mockProctorEngineClient.AssertExpectations(s.T())
s.mockPrinter.AssertExpectations(s.T())
}

func (s *DescribeCmdTestSuite) TestDescribeCmdRunJobNotSupported() {
s.mockProctorEngineClient.On("ListJobs").Return([]jobs.Metadata{}, nil).Once()
s.mockPrinter.On("Println", fmt.Sprintf("Proctor doesn't support job: %s", "any-job"), color.FgRed).Once()

s.testDescribeCmd.Run(&cobra.Command{}, []string{"any-job"})

s.mockProctorEngineClient.AssertExpectations(s.T())
s.mockPrinter.AssertExpectations(s.T())
}

func TestDescribeCmdTestSuite(t *testing.T) {
suite.Run(t, new(DescribeCmdTestSuite))
}
64 changes: 64 additions & 0 deletions cmd/jobs/execution/executioner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package execution

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/gojekfarm/proctor/engine"
"github.com/gojekfarm/proctor/io"
"github.com/spf13/cobra"
)

func NewCmd(printer io.Printer, proctorEngineClient engine.Client) *cobra.Command {
return &cobra.Command{
Use: "execute",
Short: "Execute a job with arguments given",
Long: `Example: proctor job execute say-hello-world SAMPLE_ARG_ONE=any SAMPLE_ARG_TWO=variable`,

Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
printer.Println("Incorrect usage of proctor job execute", color.FgRed)
return
}

jobName := args[0]
printer.Println(fmt.Sprintf("%-40s %-100s", "Executing Job", jobName), color.Reset)

jobArgs := make(map[string]string)
if len(args) > 1 {
printer.Println("With Variables", color.FgMagenta)
for _, v := range args[1:] {
arg := strings.Split(v, "=")

if len(arg) < 2 {
printer.Println(fmt.Sprintf("%-40s %-100s", "\nIncorrect variable format\n", v), color.FgRed)
continue
}

combinedArgValue := strings.Join(arg[1:], "=")
jobArgs[arg[0]] = combinedArgValue

printer.Println(fmt.Sprintf("%-40s %-100s", arg[0], combinedArgValue), color.Reset)
}
} else {
printer.Println("With No Variables", color.FgRed)
}

executedJobName, err := proctorEngineClient.ExecuteJob(jobName, jobArgs)
if err != nil {
printer.Println("\nError executing job. Please check configuration and network connectivity", color.FgRed)
return
}

printer.Println("Job execution successful. \nStreaming logs:", color.FgGreen)
err = proctorEngineClient.StreamJobLogs(executedJobName)
if err != nil {
printer.Println("\nError Streaming Logs", color.FgRed)
return
}

printer.Println("\nLog stream of job completed.", color.FgGreen)
},
}
}
Loading

0 comments on commit 0507bbf

Please sign in to comment.