Skip to content

Commit

Permalink
some testings
Browse files Browse the repository at this point in the history
  • Loading branch information
yaronsumel committed Sep 5, 2016
1 parent 1aea4c0 commit 82b8e82
Show file tree
Hide file tree
Showing 11 changed files with 546 additions and 57 deletions.
10 changes: 5 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ type (
func (conf *config) set(configPath configPath) {
data, err := ioutil.ReadFile(string(configPath))
if err != nil {
fatal(fmt.Sprintf("Could not open %s ", configPath))
panic(fmt.Sprintf("Could not open %s ", configPath))
}
if err := yaml.Unmarshal([]byte(data), &conf); err != nil {
fatal(fmt.Sprintf("Could not parse config file. make sure its yaml."))
panic(fmt.Sprintf("Could not parse config file. make sure its yaml."))
}
}

func (conf *config) getServersFromConfig(serverGroup serverGroup) servers {
group, ok := conf.Servers[string(serverGroup)]
if !ok {
fatal(fmt.Sprintf("Could not find [%s] in server group.", serverGroup))
panic(fmt.Sprintf("Could not find [%s] in server group.", serverGroup))
}
return group
}

func (conf *config) getCommandsFromConfig(commandName commandName) commands {
commands, ok := conf.Commands[string(commandName)]
if !ok {
fatal(fmt.Sprintf("Command %s was not found.", commandName))
panic(fmt.Sprintf("Command %s was not found.", commandName))
}
return commands
}
}
70 changes: 56 additions & 14 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"gopkg.in/yaml.v2"
"io/ioutil"
"reflect"
"testing"
)
Expand Down Expand Up @@ -34,20 +32,42 @@ func getDemoConfig() config {
var demoConfig = getDemoConfig()

func TestSet(t *testing.T) {
testPath := configPath("demo_config.yml")
c1 := config{}
c2 := config{}
c1.set(testPath)
data, err := ioutil.ReadFile(string(testPath))
if err != nil {
t.FailNow()
}
if err := yaml.Unmarshal([]byte(data), &c2); err != nil {

//should not panic
func() {
defer func() {
if err := recover(); err != nil {
t.FailNow()
}
}()
cOk := config{}
cOk.set(configPath("testFiles/demo_config.yml"))
}()

//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
cNotOk := config{}
cNotOk.set(configPath("not_demo_config.yml"))
t.FailNow()
}
if !reflect.DeepEqual(c1, c2) {
}()

//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
cNotOk1 := config{}
cNotOk1.set(configPath("main.go"))
t.FailNow()
}
}()

}

func TestGetServersFromConfig(t *testing.T) {
Expand All @@ -56,6 +76,18 @@ func TestGetServersFromConfig(t *testing.T) {
if !reflect.DeepEqual(demoConfig.Servers["test1"], serversArray) {
t.FailNow()
}

//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
demoConfig.getServersFromConfig(serverGroup("false_group_name"))
t.FailNow()
}()

}

func TestGetCommandsFromConfig(t *testing.T) {
Expand All @@ -64,4 +96,14 @@ func TestGetCommandsFromConfig(t *testing.T) {
if !reflect.DeepEqual(demoConfig.Commands["test_cmd"], cmds) {
t.FailNow()
}
//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
demoConfig.getCommandsFromConfig(commandName("false_command"))
t.FailNow()
}()
}
28 changes: 14 additions & 14 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ type (
serverGroup string
commandName string
verifyFlag bool
)

type input struct {
asyncFlag asyncFlag
configPath configPath
keyPath keyPath
serverGroup serverGroup
commandName commandName
verifyFlag verifyFlag
}
input struct {
asyncFlag asyncFlag
configPath configPath
keyPath keyPath
serverGroup serverGroup
commandName commandName
verifyFlag verifyFlag
}
)

func (input *input) parse() {

Expand Down Expand Up @@ -52,25 +52,25 @@ func (input *input) validate() {

func (val *configPath) validate() {
if *val == "" {
fatal("configPath is empty please set grapes -c config.yml")
panic("configPath is empty please set grapes -c config.yml")
}
}

func (val *keyPath) validate() {
if *val == "" {
fatal("idendity file path is empty please set grapes -i ~/.ssh/id_rsaa")
panic("idendity file path is empty please set grapes -i ~/.ssh/id_rsaa")
}
}

func (val *serverGroup) validate() {
if *val == "" {
fatal("server group is empty please set grapes -s server_group")
panic("server group is empty please set grapes -s server_group")
}
}

func (val *commandName) validate() {
if *val == "" {
fatal("command name is empty please set grapes -cmd whats_up")
panic("command name is empty please set grapes -cmd whats_up")
}
}

Expand All @@ -90,6 +90,6 @@ func (input *input) verifyAction(servers servers) {

fmt.Print("\n -- are your sure? [y/N] : ")
if _, err := fmt.Scanf("%s", &char); err != nil || char != "y" {
fatal("type y to continue")
panic("type y to continue")
}
}
173 changes: 173 additions & 0 deletions input_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package main

import (
"testing"
)

var demoInputOk input = input{
asyncFlag:asyncFlag(false),
configPath:configPath("/demo"),
keyPath:keyPath("/demo"),
serverGroup:serverGroup("demoServerGroup"),
commandName:commandName("demo"),
verifyFlag:verifyFlag(false),
}

var demoInputNotOk input = input{
asyncFlag:asyncFlag(false),
configPath:configPath(""),
keyPath:keyPath(""),
serverGroup:serverGroup(""),
verifyFlag:verifyFlag(true),
}

func TestParse(t *testing.T) {
in := input{}
in.parse()
}

func TestValidateInput(t *testing.T) {
//should not panic
func() {
defer func() {
if err := recover(); err != nil {
t.FailNow()
}
}()
demoInputOk.validate()
}()
//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
demoInputNotOk.validate()
t.FailNow()
}()

}

func TestValidateConfigPath(t *testing.T) {
//should not panic
func() {
defer func() {
if err := recover(); err != nil {
t.FailNow()
}
}()
demoInputOk.configPath.validate()
}()
//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
demoInputNotOk.configPath.validate()
t.FailNow()
}()
}

func TestValidateKeyPath(t *testing.T) {
//should not panic
func() {
defer func() {
if err := recover(); err != nil {
t.FailNow()
}
}()
demoInputOk.keyPath.validate()
}()
//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
demoInputNotOk.keyPath.validate()
t.FailNow()
}()
}

func TestValidateServerGroup(t *testing.T) {
//should not panic
func() {
defer func() {
if err := recover(); err != nil {
t.FailNow()
}
}()
demoInputOk.serverGroup.validate()
}()
//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
demoInputNotOk.serverGroup.validate()
t.FailNow()
}()
}

func TestValidateCommandName(t *testing.T) {
//should not panic
func() {
defer func() {
if err := recover(); err != nil {
t.FailNow()
}
}()
demoInputOk.commandName.validate()
}()
//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
demoInputNotOk.commandName.validate()
t.FailNow()
}()
}

func TestVerifyAction(t *testing.T) {

s := servers{
server{
Name:"testN",
Host:"testH",
User:"testU",
},
}

//should panic
func() {
defer func() {
if err := recover(); err != nil {
recover()
}
}()
demoInputOk.verifyAction(s)
t.FailNow()
}()

//should not panic
func() {
defer func() {
if err := recover(); err != nil {
t.FailNow()
}
}()
demoInputOk.verifyFlag = true
demoInputOk.verifyAction(s)
}()


}
Loading

0 comments on commit 82b8e82

Please sign in to comment.