-
Notifications
You must be signed in to change notification settings - Fork 771
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
Functional tests in golang #518
Conversation
620b50d
to
94a1180
Compare
This looks great to me! 👍 Awesome work! Just missing ExpectFailure right? |
@cdrage Thanks. I'll finish it soon |
82aedf2
to
96e5705
Compare
@cdrage Please review. |
script/test/cmd/tests.go
Outdated
func main() { | ||
ExpectSuccessK8s() | ||
ExpectSuccessOpenShift() | ||
ExpectSuccessAndWarningK8s() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gofmt issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hence travis failing
96e5705
to
c03b74b
Compare
Travis still failing / not working :(
ping @procrypt |
script/test/cmd/tests.go
Outdated
"strings" | ||
) | ||
|
||
func ExpectSuccessK8s(dockerComposeFile, result string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about prefixing the test cases with Test
?
script/test/cmd/tests.go
Outdated
ExpectSuccessAndWarningOpenShift() | ||
ExpectFailureK8s() | ||
ExpectFailureOpenShift() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd highly suggest doing this using the testing library. Please go through - https://golang.org/pkg/testing/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@procrypt nice work. Wanted to know what is the scope of these tests, have you moved all the kompose convert
tests to golang, or just a few?
script/test/cmd/tests.go
Outdated
ExpectSuccessAndWarningOpenShift() | ||
ExpectFailureK8s() | ||
ExpectFailureOpenShift() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would also be great if you could do it using the golang subtests way, please read about it, and you could also use this as a reference - https://github.com/redhat-developer/opencompose/blob/master/pkg/encoding/v1/encoding_test.go
Check for the use of tt.Run()
in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@containscafeine These tests are just comparing the output of kompose convert
, so I recommended we use these tests like the way they are.
@containscafeine I have moved all |
f973b17
to
3e16596
Compare
@procrypt Tests still failing :( |
a50c24f
to
a06e5ac
Compare
1b11ec1
to
6b44f13
Compare
@cdrage Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of defining so many functions for every kompose command, it'd be great if we use one function signature to encompass all/most of the kompose CLI options, WDYT?
if err != nil || fileNotExist(cmd) { | ||
home := os.Getenv("HOME") | ||
if home == "" { | ||
fmt.Println("No $HOME environment variable found") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@procrypt did you make any changes to this? The error is still not getting handled, what am I missing?
cmdArgs := []string{"rev-parse", "--abbrev-ref", "HEAD"} | ||
cmdOut, err := exec.Command(cmdName, cmdArgs...).CombinedOutput() | ||
if err != nil { | ||
fmt.Println("") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we printing here?
} | ||
|
||
//UpdateUri Replaces variables with current branch and uri | ||
func UpdateURI(uri, branch, inputfile, output string) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, currently this function is tied to replacing only uri and branch, what do you think about making this more generic?
So, as an input we accept a map[string]string, in which pass key value pairs that we want to replace, which in this case will be, "%URI%": uri
, and "%REF", branch
, and then we iterate over the map and replace all the occurrences; this way we can replace any parameter from now.
cmdArgs := []string{"remote", "get-url", "origin"} | ||
cmdOut, err := exec.Command(cmdName, cmdArgs...).CombinedOutput() | ||
if err != nil { | ||
fmt.Println("") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we printing here?
script/test/cmd/cmd_test.go
Outdated
if err != nil { | ||
return errors.Wrapf(err, "Error in %s application", file[1]) | ||
} | ||
er := ioutil.WriteFile(outFile, out, 0644) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do err
here, and reuse the err
variable already created without using :=
script/test/cmd/cmd_test.go
Outdated
cmdArgs := []string{command, "--provider", provider, "-f", dockerComposeFile, "--stdout", "-j"} | ||
out, err := exec.Command(cmdName, cmdArgs...).CombinedOutput() | ||
if err != nil { | ||
fmt.Println("\x1b[31;1m===> Test Fail <===\x1b[0m") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Test Failed"
script/test/cmd/cmd_test.go
Outdated
} | ||
|
||
// Kompose command | ||
func Kompose(command, provider, dockerComposeFile, result string) (cmd, output string, error error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about returning and accepting pointers to string instead of values of string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for all the functions
script/test/cmd/cmd_test.go
Outdated
} | ||
|
||
// Kompose command for Multiple files | ||
func MultipleFiles(command, provider, dockerComposeFileOne, dockerComposeFiletwo, result string) (cmd, output string, error error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a lot of repeated code here; so, if I understand correctly, this function is the same as the function above, but accepts 2 docker compose files.
So, how about removing this and refactoring the function above to accept a []string
as docker compose file and iterate on it. This way we can pass one or as many docker compose files that we want, without having to define the function again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[]string
will not work here as we provide multiple compose files as f <file1> -f <file2>
not as -f <file1> <file2>
script/test/cmd/cmd_test.go
Outdated
} | ||
|
||
// Kompose command for bundle | ||
func KomposeBundle(command, dockerComposeFile, result string) (cmd, output string, error error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even for this, we should have one Kompose
function to which we should be able to pass which kompose subcommand to run, and if we pass bundle
there, then the --bundle
flag should get substituted accordingly.
WDYT?
script/test/cmd/cmd_test.go
Outdated
return cmd, output, nil | ||
} | ||
|
||
//GenerateArtifacts generates artifacts in file with -o command |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
…al tests in golang kubernetes#518 to fail
6b44f13
to
65e4247
Compare
@containscafeine review please. |
65e4247
to
43c8be9
Compare
…al tests in golang kubernetes#518 to fail
added functional in golang for the test cases present in script/test/cmd/test.sh
moved fixtures from script/test to script/test/cmd because according to the go best pactices fixtures should be in the same directory where the test files are
Interesting, If I run |
43c8be9
to
f06c414
Compare
…al tests in golang kubernetes#518 to fail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a first part, haven looked at everything yet ;-)
if err != nil || fileNotExist(cmd) { | ||
home := os.Getenv("HOME") | ||
if home == "" { | ||
fmt.Println("No $HOME environment variable found") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is $HOME tested?
Haven't found anything that would depend on $HOME
"testing" | ||
) | ||
|
||
func fileNotExist(path string) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this function even exists?
it just call sand verifies findExectuable
, that function could be called directly
if m := d.Mode(); !m.IsDir() { | ||
return nil | ||
} | ||
return os.ErrPermission |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is called findExecutable but all it does is it verifies that file is not a dir.
And if its dir it returns osErrPermission
, why?
} | ||
|
||
//Git get the branch | ||
func Git() (branch string, error error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have function getGitCurrentBranch
in openshift.go
that is doing a similar thing.
It would be better to reuse that. (may require small refactoring and should be handled in separate PR). We could put all git related stuff into new 'git' package.
} | ||
|
||
//Get origin | ||
func GitOrigin() (origin, branch string, error error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, this might be similar to "getGitCurrentRemoteURL" in openshift.go
} | ||
|
||
// Kompose command for buildContext | ||
func KomposeCommand(command, provider, outFile, dockerComposeFile string) (error error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this name is ultra confusing :-)
} | ||
|
||
//Exec runs the kompose command | ||
func Exec(cmdName string, cmdArgs, dockerComposeFiles []string) (cmd string, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't think this function is even needed
…al tests in golang kubernetes#518 to fail
Issues go stale after 90d of inactivity. Prevent issues from auto-closing with an If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or |
Stale issues rot after 30d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
Rotten issues close after 30d of inactivity. Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
This is the work I had done last week for writing functional test in golang. Here I am providing the
output.json
file toioutil.ReadFile
and then comparing it with the stdout ofkompose convert
.In context to #432
@kadel @cdrage @containscafeine Let me know if this is the correct way to proceed.