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

Validate Test section to avoid panic if test section is not populated properly #4309

Merged
merged 1 commit into from
May 22, 2024

Conversation

andreafalzetti
Copy link
Contributor

@andreafalzetti andreafalzetti commented May 22, 2024

Proposed changes

Fixes DEV-348

In this PR we're adding a validation layer for the Test section of the Okteto Manifest. Currently, passing empty section, or empty test throws panics.

Important Note: Curently, if no tests are configured, the Okteto CLI will just deploy the application. But there is no logs or anything that informs the user that "nothing" has been done test-wise. With this change we make sure they are informed if they run test without configuring them. It also exit non-zero, so if this was intended to always exit 0 (for CI reasons for example), I can make it a warning.

BEFORE (Empty test section)
Screenshot 2024-05-22 at 09 36 47

NOW
Screenshot 2024-05-22 at 09 38 54

How to validate

Using the following manifests, and run okteto test

Missing test section

dev:
  hello-world:
    image: okteto/golang:1
    command: bash
    sync:
      - .:/usr/src/app
    volumes:
      - /go
      - /root/.cache
    securityContext:
      capabilities:
        add:
          - SYS_PTRACE
    forward:
      - 2345:2345

Empty test section

test:

dev:
  hello-world:
    image: okteto/golang:1
    command: bash
    sync:
      - .:/usr/src/app
    volumes:
      - /go
      - /root/.cache
    securityContext:
      capabilities:
        add:
          - SYS_PTRACE
    forward:
      - 2345:2345

Empty test

test:
  unit:

dev:
  hello-world:
    image: okteto/golang:1
    command: bash
    sync:
      - .:/usr/src/app
    volumes:
      - /go
      - /root/.cache
    securityContext:
      capabilities:
        add:
          - SYS_PTRACE
    forward:
      - 2345:2345

CLI Quality Reminders 🔧

For both authors and reviewers:

  • Scrutinize for potential regressions
  • Ensure key automated tests are in place
  • Build the CLI and test using the validation steps
  • Assess Developer Experience impact (log messages, performances, etc)
  • If too broad, consider breaking into smaller PRs
  • Adhere to our code style and code review guidelines

Signed-off-by: Andrea Falzetti <andrea@okteto.com>
Copy link

codecov bot commented May 22, 2024

Codecov Report

Attention: Patch coverage is 0% with 13 lines in your changes are missing coverage. Please review.

Project coverage is 43.20%. Comparing base (452f2e9) to head (8c79f27).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4309      +/-   ##
==========================================
- Coverage   43.22%   43.20%   -0.02%     
==========================================
  Files         371      371              
  Lines       30027    30040      +13     
==========================================
  Hits        12979    12979              
- Misses      15924    15937      +13     
  Partials     1124     1124              

@ifbyol
Copy link
Member

ifbyol commented May 22, 2024

Important Note: Curently, if no tests are configured, the Okteto CLI will just deploy the application. But there is no logs or anything that informs the user that "nothing" has been done test-wise. With this change we make sure they are informed if they run test without configuring them. It also exit non-zero, so if this was intended to always exit 0 (for CI reasons for example), I can make it a warning.

What other tools or languages behaves when no tests are defined? I mean, for example image I execute go test in a directory without tests. Does it fails or just print no tests were run and that's it?

@andreafalzetti
Copy link
Contributor Author

What other tools or languages behaves when no tests are defined? I mean, for example image I execute go test in a directory without tests. Does it fails or just print no tests were run and that's it?

I you run go test in an empty directory, it fails with: exit 1: go: go.mod file not found in current directory or any parent directory; see 'go help modules'.

Once you init the project (go mod init..) it fails exit 1: no Go files in /var/folders/nt/ggnw9z4d1712sftc8606lj7r0000gn/T/tmp.j2E7UpOEcD

hasAtLeastOne := false
for _, t := range test {
if t != nil {
hasAtLeastOne = true
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this code could be simplified by removing the bool and returning nil as soon as we detect there is one without the break. WDYT?

Suggested change
hasAtLeastOne = true
for _, t := range test {
if t != nil {
return nil
}
}

Copy link
Contributor Author

@andreafalzetti andreafalzetti May 22, 2024

Choose a reason for hiding this comment

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

@jLopezbarb I personally find more readable and easier to maintain the current code, because having return nil means we cannot add additional validations after this check. If we want to add one extra check in the future, we need to refactor and add the bool again. Your suggestion uses less memory but in our context, I think the benefits are minimal compared to keeping the code easy to change. WDYT?

@jLopezbarb
Copy link
Contributor

@andreafalzetti I know that is out of scope of this PR but I also found one more panic that maybe we could fix it on this PR.
Steps to reproduce:

  1. Using the following okteto.yml:
build:
  backend:
    context: .
test:
  backend:
    image: okteto/golang:1
    context: backend
    commands:
      - name: command1
        command: echo "this is command 1"
      - name: command2
        command: echo "this is command 2"
  1. Run okteto test

@andreafalzetti
Copy link
Contributor Author

@andreafalzetti I know that is out of scope of this PR but I also found one more panic that maybe we could fix it on this PR. Steps to reproduce:

1. Using the following `okteto.yml`:
build:
  backend:
    context: .
test:
  backend:
    image: okteto/golang:1
    context: backend
    commands:
      - name: command1
        command: echo "this is command 1"
      - name: command2
        command: echo "this is command 2"
2. Run `okteto test`

Good catch! As discussed offline, we'll tackle that case in DEV-359

@andreafalzetti andreafalzetti merged commit 25db547 into master May 22, 2024
15 of 17 checks passed
@andreafalzetti andreafalzetti deleted the af/dev-348-okteto-test-panic branch May 22, 2024 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants