Skip to content
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

feature: skip-if conditional scenario skip #5

Merged
merged 1 commit into from
Jul 28, 2023
Merged

feature: skip-if conditional scenario skip #5

merged 1 commit into from
Jul 28, 2023

Conversation

jaypipes
Copy link
Member

Adds support for skip-if collection of evaluable conditions for a scenario. If any of these conditions fails, the test will be skipped.

SkipIf contains a list of evaluable conditions that must evaluate successfully before the scenario's tests are executed. This allows test authors to specify "pre-flight checks" that should pass before attempting any of the actions in the scenario's tests.

For example, let's assume you have a gdt-kube scenario that looks like this:

tests:
 - kube.create: manifests/nginx-deployment.yaml
 - kube:
   get: deployments/nginx
   assert:
     matches:
       status:
         readyReplicas: 2
 - kube.delete: deployments/nginx

If you execute the above test and there is already an 'nginx' deployment, the kube.create test will fail. To prevent the scenario from proceeding with the tests if an 'nginx' deployment already exists, you could add the following

skip-if:
 - kube.get: deployments/nginx
tests:
 - kube.create: manifests/nginx-deployment.yaml
 - kube:
   get: deployments/nginx
   assert:
     matches:
       status:
         readyReplicas: 2
 - kube.delete: deployments/nginx

With the above, if an 'nginx' deployment exists already, the scenario will skip all the tests.

"github.com/gdt-dev/gdt/scenario"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func (s *fooSpec) Eval(ctx context.Context, t *testing.T) *result.Result {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to @a-hilaly I just moved this into the scenario/stub_plugins_test.go file along with all the other stub plugin and spec definitions used in unit testing.

@jaypipes jaypipes marked this pull request as draft July 28, 2023 12:47
Adds support for `skip-if` collection of evaluable conditions for a
scenario. If any of these conditions succeed, the test will be skipped.
This allows test authors to specify "pre-flight checks" before
attempting any of the actions in the scenario's tests.

For example, let's assume you have a `gdt-kube` scenario that looks like
this:

```yaml
tests:
 - kube.create: manifests/nginx-deployment.yaml
 - kube:
   get: deployments/nginx
   assert:
     matches:
       status:
         readyReplicas: 2
 - kube.delete: deployments/nginx
```

If you execute the above test and there is already an 'nginx'
deployment, the `kube.create` test will fail. To prevent the scenario
from proceeding with the tests if an 'nginx' deployment already exists,
you could add the following

```yaml
skip-if:
 - kube.get: deployments/nginx
tests:
 - kube.create: manifests/nginx-deployment.yaml
 - kube:
   get: deployments/nginx
   assert:
     matches:
       status:
         readyReplicas: 2
 - kube.delete: deployments/nginx
```

With the above, if an 'nginx' deployment exists already, the scenario
will skip all the tests.

Signed-off-by: Jay Pipes <jaypipes@gmail.com>
@jaypipes jaypipes marked this pull request as ready for review July 28, 2023 12:54
@@ -89,8 +89,9 @@ func (s *Scenario) UnmarshalYAML(node *yaml.Node) error {
return gdterrors.ExpectedScalarAt(keyNode)
}
key := keyNode.Value
if key == "tests" {
valNode := node.Content[i+1]
valNode := node.Content[i+1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to dig deeper in the yaml library to understand why we do i += 2 and not just a i++.
But overall looks good!
/lgtm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-hilaly yeah, it's weird... see the comment above:

	// maps/structs are stored in a top-level Node.Content field which is a
	// concatenated slice of Node pointers in pairs of key/values.

@a-hilaly a-hilaly merged commit dbab87c into main Jul 28, 2023
6 checks passed
@jaypipes jaypipes deleted the skip-if branch July 30, 2023 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants