runfinch / common-tests Goto Github PK
View Code? Open in Web Editor NEWEnd-to-end tests for Finch
License: Apache License 2.0
End-to-end tests for Finch
License: Apache License 2.0
In many places throughout the test suite, we make use of helper functions (such as containerShouldNotExist used by compose down) to encapsulate assertions. This approach makes the tests more readable and modular, but it also results in less informative logs when a test fails. In particular, the logged error only indicates the line number where the gomega.Expect function is called within the helper function, rather than the line in the main ginkgo.It block that actually triggered the test failure.
For instance, consider the following code:
ginkgo.It(fmt.Sprintf("should stop compose services and delete volumes by specifying %s flag", volumes), func() {
volumes := volumes
output := command.StdoutStr(o, "compose", "down", volumes, "--file", composeFilePath)
containerShouldNotExist(o, containerNames...)
if !isVolumeInUse(output) {
volumeShouldNotExist(o, "compose_data_volume")
}
})
func containerShouldNotExist(o *option.Option, containerNames ...string) {
for _, containerName := range containerNames {
gomega.Expect(command.Stdout(o, "ps", "-a", "-q", "--filter",
fmt.Sprintf("name=%s", containerName))).To(gomega.BeEmpty())
}
}
If a test failure occurs because a container unexpectedly exists, the logged error will indicate the line in containerShouldNotExist where gomega.Expect was called. It won’t, however, reveal which line in the ginkgo.It block is responsible for the failure.
Example log
• [FAILED] [4.510 seconds]
Finch Container Development E2E Tests Compose down command [It] should stop compose services and delete volumes by specifying -v flag
/Users/ec2-user/go/pkg/mod/github.com/runfinch/[email protected]/tests/compose_down.go:45
[FAILED] Expected
<[]uint8 | len:13, cap:13>: [51, 55, 54, 49, 50, 100, 48, 48, 100, 48, 97, 53, 10]
to be empty
In [It] at: /Users/ec2-user/go/pkg/mod/github.com/runfinch/[email protected]/tests/tests.go:137 @ 06/23/23 09:24:20.648
I propose refactoring these helper functions to return an error that includes context about what went wrong, instead of calling gomega.Expect within them. The test case itself can them handle the error and fail if necessary.
Here is the example revised code.
ginkgo.It(fmt.Sprintf("should stop compose services and delete volumes by specifying %s flag", volumes), func() {
volumes := volumes
output := command.StdoutStr(o, "compose", "down", volumes, "--file", composeFilePath)
err := containerShouldNotExist(o, containerNames...)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
if !isVolumeInUse(output) {
volumeShouldNotExist(o, "compose_data_volume")
}
})
func containerShouldNotExist(o *option.Option, containerNames ...string) error {
for _, containerName := range containerNames {
containerExists := command.Stdout(o, "ps", "-a", "-q", "--filter",
fmt.Sprintf("name=%s", containerName))
if containerExists != nil && len(containerExists) > 0 {
return fmt.Errorf("containerd '%s' exists but should not", containerName)
}
}
return nil
}
The logs will be like this with the change:
• [FAILED] [4.854 seconds]
Finch Shared E2E Tests Compose down command [It] should stop compose services and delete volumes by specifying -v flag
/Users/ningziwe/common-tests/tests/compose_down.go:46
[FAILED] Unexpected error:
<*errors.errorString | 0x1400050e350>:
containerd 'container1_compose_down' exists but should not
{
s: "containerd 'container1_compose_down' exists but should not",
}
occurred
In [It] at: /Users/ningziwe/common-tests/tests/compose_down.go:50 @ 06/23/23 15:03:16.459
Right now, all of the tests are written to use the localImages
map which is only populated if the SetupLocalRegistry
function is first called.
The code should be refactored such that the tests don't care about such details and work regardless of whether the images are local or not (maybe through a helper struct with a GetImage(localImage)
method).
See here for an example error.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.