GithubHelp home page GithubHelp logo

common-tests's People

Contributors

anqipang avatar austinvazquez avatar azhouwd avatar chandrushetty avatar davidhsingyuchen avatar dependabot[bot] avatar estesp avatar ginglis13 avatar github-actions[bot] avatar haytok avatar mharwani avatar monirul avatar ningziwen avatar pendo324 avatar sam-berning avatar shubhranshu153 avatar vsiravar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

common-tests's Issues

Refactor error handling to improve test failure logging

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

Proposal

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

Refactor tests to work with or without local registry setup

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.