Permalink
Browse files

some testings

  • Loading branch information...
yaronsumel committed Sep 5, 2016
1 parent 1aea4c0 commit 82b8e827f8ec6879150299cec66302e7262a4ab6
Showing with 546 additions and 57 deletions.
  1. +5 −5 config.go
  2. +56 −14 config_test.go
  3. +14 −14 input.go
  4. +173 −0 input_test.go
  5. +20 −17 main.go
  6. +81 −0 main_test.go
  7. +4 −4 ssh.go
  8. +139 −0 ssh_test.go
  9. +2 −3 { → testFiles}/demo_config.yml
  10. +51 −0 testFiles/id_rsa
  11. +1 −0 testFiles/id_rsa.pub
View
@@ -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
}
}
View
@@ -1,8 +1,6 @@
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"reflect"
"testing"
)
@@ -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) {
@@ -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) {
@@ -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()
}()
}
View
@@ -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() {
@@ -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")
}
}
@@ -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")
}
}
View
@@ -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)
}()
}
Oops, something went wrong.

0 comments on commit 82b8e82

Please sign in to comment.