Skip to content
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

#49 Validate .proctor.yaml configuration #50

Merged
merged 10 commits into from
Nov 8, 2018
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ build-deps:
update-deps:
glide update

generate:
go get github.com/go-bindata/go-bindata
$(GOPATH)/bin/go-bindata -pkg config -o config/data.go data/config_template.yaml

compile:
mkdir -p out/
go build -race $(GLIDE_NOVENDOR)
Expand Down
10 changes: 1 addition & 9 deletions cmd/description/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package description

import (
"fmt"
"net/http"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proc"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
)

Expand All @@ -26,12 +23,7 @@ func NewCmd(printer io.Printer, proctorEngineClient daemon.Client) *cobra.Comman

procList, err := proctorEngineClient.ListProcs()
if err != nil {
if err.Error() == http.StatusText(http.StatusUnauthorized) {
printer.Println(utility.UnauthorizedError, color.FgRed)
return
}

printer.Println(utility.GenericDescribeCmdError, color.FgRed)
printer.Println(err.Error(), color.FgRed)
return
}

Expand Down
16 changes: 2 additions & 14 deletions cmd/description/descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package description
import (
"errors"
"fmt"
"net/http"
"testing"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proc"
"github.com/gojektech/proctor/proc/env"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -90,8 +88,8 @@ func (s *DescribeCmdTestSuite) TestDescribeCmdForIncorrectUsage() {
}

func (s *DescribeCmdTestSuite) TestDescribeCmdRunProctorEngineClientFailure() {
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New("error")).Once()
s.mockPrinter.On("Println", utility.GenericDescribeCmdError, color.FgRed).Once()
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New("test error")).Once()
s.mockPrinter.On("Println", "test error", color.FgRed).Once()

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

Expand All @@ -109,16 +107,6 @@ func (s *DescribeCmdTestSuite) TestDescribeCmdRunProcNotSupported() {
s.mockPrinter.AssertExpectations(s.T())
}

func (s *DescribeCmdTestSuite) TestDescribeCmdRunProcForUnauthorizedUser() {
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New(http.StatusText(http.StatusUnauthorized))).Once()
s.mockPrinter.On("Println", utility.UnauthorizedError, color.FgRed).Once()

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

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

func TestDescribeCmdTestSuite(t *testing.T) {
suite.Run(t, new(DescribeCmdTestSuite))
}
8 changes: 1 addition & 7 deletions cmd/execution/executioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package execution

import (
"fmt"
"net/http"
"strings"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -50,11 +48,7 @@ func NewCmd(printer io.Printer, proctorEngineClient daemon.Client) *cobra.Comman

executedProcName, err := proctorEngineClient.ExecuteProc(procName, procArgs)
if err != nil {
if err.Error() == http.StatusText(http.StatusUnauthorized) {
printer.Println(utility.UnauthorizedError, color.FgRed)
return
}
printer.Println(utility.GenericProcCmdError, color.FgRed)
printer.Println(err.Error(), color.FgRed)
return
}

Expand Down
23 changes: 2 additions & 21 deletions cmd/execution/executioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package execution
import (
"errors"
"fmt"
"net/http"
"testing"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -118,26 +116,9 @@ func (s *ExecutionCmdTestSuite) TestExecutionCmdForProctorEngineExecutionFailure
s.mockPrinter.On("Println", "With No Variables", color.FgRed).Once()

procArgs := make(map[string]string)
s.mockProctorEngineClient.On("ExecuteProc", "say-hello-world", procArgs).Return("", errors.New("error")).Once()
s.mockProctorEngineClient.On("ExecuteProc", "say-hello-world", procArgs).Return("", errors.New("test error")).Once()

s.mockPrinter.On("Println", utility.GenericProcCmdError, color.FgRed).Once()

s.testExecutionCmd.Run(&cobra.Command{}, args)

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

func (s *ExecutionCmdTestSuite) TestExecutionCmdForProctorEngineExecutionForUnauthorizedUser() {
args := []string{"say-hello-world"}

s.mockPrinter.On("Println", fmt.Sprintf("%-40s %-100s", "Executing Proc", "say-hello-world"), color.Reset).Once()
s.mockPrinter.On("Println", "With No Variables", color.FgRed).Once()

procArgs := make(map[string]string)
s.mockProctorEngineClient.On("ExecuteProc", "say-hello-world", procArgs).Return("", errors.New(http.StatusText(http.StatusUnauthorized))).Once()

s.mockPrinter.On("Println", utility.UnauthorizedError, color.FgRed).Once()
s.mockPrinter.On("Println", "test error", color.FgRed).Once()

s.testExecutionCmd.Run(&cobra.Command{}, args)

Expand Down
10 changes: 1 addition & 9 deletions cmd/list/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package list

import (
"fmt"
"net/http"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
)

Expand All @@ -20,12 +17,7 @@ func NewCmd(printer io.Printer, proctorEngineClient daemon.Client) *cobra.Comman
Run: func(cmd *cobra.Command, args []string) {
procList, err := proctorEngineClient.ListProcs()
if err != nil {
if err.Error() == http.StatusText(http.StatusUnauthorized) {
printer.Println(utility.UnauthorizedError, color.FgRed)
return
}

printer.Println(utility.GenericListCmdError, color.FgRed)
printer.Println(err.Error(), color.FgRed)
return
}

Expand Down
16 changes: 2 additions & 14 deletions cmd/list/lister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package list
import (
"errors"
"fmt"
"net/http"
"testing"

"github.com/fatih/color"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"
"github.com/gojektech/proctor/proc"
"github.com/gojektech/proctor/proctord/utility"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -64,18 +62,8 @@ func (s *ListCmdTestSuite) TestListCmdRun() {
}

func (s *ListCmdTestSuite) TestListCmdRunProctorEngineClientFailure() {
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New("error")).Once()
s.mockPrinter.On("Println", utility.GenericListCmdError, color.FgRed).Once()

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

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

func (s *ListCmdTestSuite) TestListCmdRunProctorEngineClientForUnauthorizedUser() {
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New(http.StatusText(http.StatusUnauthorized))).Once()
s.mockPrinter.On("Println", utility.UnauthorizedError, color.FgRed).Once()
s.mockProctorEngineClient.On("ListProcs").Return([]proc.Metadata{}, errors.New("Error!!!\nUnknown Error.")).Once()
s.mockPrinter.On("Println", "Error!!!\nUnknown Error.", color.FgRed).Once()

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

Expand Down
3 changes: 0 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/gojektech/proctor/cmd/list"
"github.com/gojektech/proctor/cmd/procs"
"github.com/gojektech/proctor/cmd/version"
"github.com/gojektech/proctor/config"
"github.com/gojektech/proctor/daemon"
"github.com/gojektech/proctor/io"

Expand All @@ -25,8 +24,6 @@ var (
)

func Execute(printer io.Printer, proctorEngineClient daemon.Client) {
cobra.OnInitialize(config.InitConfig)

versionCmd := version.NewCmd(printer)
rootCmd.AddCommand(versionCmd)

Expand Down
83 changes: 56 additions & 27 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,76 @@ package config

import (
"fmt"
"github.com/gojektech/proctor/proctord/utility"
"github.com/pkg/errors"
"os"
"time"

"github.com/spf13/viper"
)

func InitConfig() {
viper.SetConfigType("yaml")
viper.AutomaticEnv()
var configFileDir string
const (
Environment = "ENVIRONMENT"
ProctorHost = "PROCTOR_HOST"
EmailId = "EMAIL_ID"
AccessToken = "ACCESS_TOKEN"
ConnectionTimeoutSecs = "CONNECTION_TIMEOUT_SECS"
)

if viper.GetString("ENVIRONMENT") == "test" {
configFileDir = "/tmp"
} else {
configFileDir = "$HOME/.proctor"
}
type ProctorConfig struct {
Host string
Email string
AccessToken string
ConnectionTimeoutSecs time.Duration
}

type ConfigError struct {
jensoncs marked this conversation as resolved.
Show resolved Hide resolved
error
Message string
}

func (c *ConfigError) RootError() error {
return c.error
}

viper.AddConfigPath(configFileDir)
func LoadConfig() (ProctorConfig, ConfigError) {
viper.SetDefault(ConnectionTimeoutSecs, 10)
viper.AutomaticEnv()

viper.AddConfigPath(ConfigFileDir())
viper.SetConfigName("proctor")
viper.SetConfigType("yaml")

err := viper.ReadInConfig()

if err != nil {
fmt.Println("Error reading proctor config")
os.Exit(1)
configFileUsed := viper.ConfigFileUsed()
message := ""
if _, err := os.Stat(configFileUsed); os.IsNotExist(err) {
bytes, _ := dataConfig_templateYamlBytes()
template := string(bytes)
message = fmt.Sprintf("Config file not found in %s/proctor.yaml\n", ConfigFileDir())
message += fmt.Sprintf("Create a config file with template:\n\n%s\n", template)
}
return ProctorConfig{}, ConfigError{error: err, Message: message}
}
}

func ProctorHost() string {
InitConfig()
proctorHost := viper.GetString("PROCTOR_HOST")
return proctorHost
}

func EmailId() string {
InitConfig()
emailId := viper.GetString("EMAIL_ID")
return emailId
proctorHost := viper.GetString(ProctorHost)
if proctorHost == "" {
return ProctorConfig{}, ConfigError{error: errors.New("Mandatory Config Missing"), Message: utility.ConfigProctorHostMissingError}
}
emailId := viper.GetString(EmailId)
accessToken := viper.GetString(AccessToken)
connectionTimeout := time.Duration(viper.GetInt(ConnectionTimeoutSecs)) * time.Second
return ProctorConfig{Host: proctorHost, Email: emailId, AccessToken: accessToken, ConnectionTimeoutSecs: connectionTimeout}, ConfigError{}
}

func AccessToken() string {
InitConfig()
accessToken := viper.GetString("ACCESS_TOKEN")
return accessToken
// Returns Config file directory
jensoncs marked this conversation as resolved.
Show resolved Hide resolved
// This allows to test on dev environment without conflicting with installed proctor config file
func ConfigFileDir() string {
if os.Getenv(Environment) == "test" {
return "/tmp"
} else {
return fmt.Sprintf("%s/.proctor", os.Getenv("HOME"))
}
}
Loading