@ -17,20 +17,15 @@ package plugin // import "k8s.io/helm/pkg/plugin"
import (
import (
"reflect"
"reflect"
"runtime"
"testing"
"testing"
)
)
func TestPrepareCommand ( t * testing . T ) {
func checkCommand ( p * Plugin , extraArgs [ ] string , osStrCmp string , t * testing . T ) {
p := & Plugin {
cmd , args , err := p . PrepareCommand ( extraArgs )
Dir : "/tmp" , // Unused
if err != nil {
Metadata : & Metadata {
t . Errorf ( err . Error ( ) )
Name : "test" ,
Command : "echo -n foo" ,
} ,
}
}
argv := [ ] string { "--debug" , "--foo" , "bar" }
cmd , args := p . PrepareCommand ( argv )
if cmd != "echo" {
if cmd != "echo" {
t . Errorf ( "Expected echo, got %q" , cmd )
t . Errorf ( "Expected echo, got %q" , cmd )
}
}
@ -39,7 +34,7 @@ func TestPrepareCommand(t *testing.T) {
t . Errorf ( "expected 5 args, got %d" , l )
t . Errorf ( "expected 5 args, got %d" , l )
}
}
expect := [ ] string { "-n" , "foo" , "--debug" , "--foo" , "bar" }
expect := [ ] string { "-n" , osStrCmp , "--debug" , "--foo" , "bar" }
for i := 0 ; i < len ( args ) ; i ++ {
for i := 0 ; i < len ( args ) ; i ++ {
if expect [ i ] != args [ i ] {
if expect [ i ] != args [ i ] {
t . Errorf ( "Expected arg=%q, got %q" , expect [ i ] , args [ i ] )
t . Errorf ( "Expected arg=%q, got %q" , expect [ i ] , args [ i ] )
@ -48,14 +43,17 @@ func TestPrepareCommand(t *testing.T) {
// Test with IgnoreFlags. This should omit --debug, --foo, bar
// Test with IgnoreFlags. This should omit --debug, --foo, bar
p . Metadata . IgnoreFlags = true
p . Metadata . IgnoreFlags = true
cmd , args = p . PrepareCommand ( argv )
cmd , args , err = p . PrepareCommand ( extraArgs )
if err != nil {
t . Errorf ( err . Error ( ) )
}
if cmd != "echo" {
if cmd != "echo" {
t . Errorf ( "Expected echo, got %q" , cmd )
t . Errorf ( "Expected echo, got %q" , cmd )
}
}
if l := len ( args ) ; l != 2 {
if l := len ( args ) ; l != 2 {
t . Errorf ( "expected 2 args, got %d" , l )
t . Errorf ( "expected 2 args, got %d" , l )
}
}
expect = [ ] string { "-n" , "foo" }
expect = [ ] string { "-n" , osStrCmp }
for i := 0 ; i < len ( args ) ; i ++ {
for i := 0 ; i < len ( args ) ; i ++ {
if expect [ i ] != args [ i ] {
if expect [ i ] != args [ i ] {
t . Errorf ( "Expected arg=%q, got %q" , expect [ i ] , args [ i ] )
t . Errorf ( "Expected arg=%q, got %q" , expect [ i ] , args [ i ] )
@ -63,6 +61,109 @@ func TestPrepareCommand(t *testing.T) {
}
}
}
}
func TestPrepareCommand ( t * testing . T ) {
p := & Plugin {
Dir : "/tmp" , // Unused
Metadata : & Metadata {
Name : "test" ,
Command : "echo -n foo" ,
} ,
}
argv := [ ] string { "--debug" , "--foo" , "bar" }
checkCommand ( p , argv , "foo" , t )
}
func TestPlatformPrepareCommand ( t * testing . T ) {
p := & Plugin {
Dir : "/tmp" , // Unused
Metadata : & Metadata {
Name : "test" ,
Command : "echo -n os-arch" ,
PlatformCommand : [ ] PlatformCommand {
{ OperatingSystem : "linux" , Architecture : "i386" , Command : "echo -n linux-i386" } ,
{ OperatingSystem : "linux" , Architecture : "amd64" , Command : "echo -n linux-amd64" } ,
{ OperatingSystem : "windows" , Architecture : "amd64" , Command : "echo -n win-64" } ,
} ,
} ,
}
argv := [ ] string { "--debug" , "--foo" , "bar" }
var osStrCmp string
os := runtime . GOOS
arch := runtime . GOARCH
if os == "linux" && arch == "i386" {
osStrCmp = "linux-i386"
} else if os == "linux" && arch == "amd64" {
osStrCmp = "linux-amd64"
} else if os == "windows" && arch == "amd64" {
osStrCmp = "win-64"
} else {
osStrCmp = "os-arch"
}
checkCommand ( p , argv , osStrCmp , t )
}
func TestPartialPlatformPrepareCommand ( t * testing . T ) {
p := & Plugin {
Dir : "/tmp" , // Unused
Metadata : & Metadata {
Name : "test" ,
Command : "echo -n os-arch" ,
PlatformCommand : [ ] PlatformCommand {
{ OperatingSystem : "linux" , Architecture : "i386" , Command : "echo -n linux-i386" } ,
{ OperatingSystem : "windows" , Architecture : "amd64" , Command : "echo -n win-64" } ,
} ,
} ,
}
argv := [ ] string { "--debug" , "--foo" , "bar" }
var osStrCmp string
os := runtime . GOOS
arch := runtime . GOARCH
if os == "linux" {
osStrCmp = "linux-i386"
} else if os == "windows" && arch == "amd64" {
osStrCmp = "win-64"
} else {
osStrCmp = "os-arch"
}
checkCommand ( p , argv , osStrCmp , t )
}
func TestNoPrepareCommand ( t * testing . T ) {
p := & Plugin {
Dir : "/tmp" , // Unused
Metadata : & Metadata {
Name : "test" ,
} ,
}
argv := [ ] string { "--debug" , "--foo" , "bar" }
_ , _ , err := p . PrepareCommand ( argv )
if err == nil {
t . Errorf ( "Expected error to be returned" )
}
}
func TestNoMatchPrepareCommand ( t * testing . T ) {
p := & Plugin {
Dir : "/tmp" , // Unused
Metadata : & Metadata {
Name : "test" ,
PlatformCommand : [ ] PlatformCommand {
{ OperatingSystem : "no-os" , Architecture : "amd64" , Command : "echo -n linux-i386" } ,
} ,
} ,
}
argv := [ ] string { "--debug" , "--foo" , "bar" }
_ , _ , err := p . PrepareCommand ( argv )
if err == nil {
t . Errorf ( "Expected error to be returned" )
}
}
func TestLoadDir ( t * testing . T ) {
func TestLoadDir ( t * testing . T ) {
dirname := "testdata/plugdir/hello"
dirname := "testdata/plugdir/hello"
plug , err := LoadDir ( dirname )
plug , err := LoadDir ( dirname )