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

update kubernetes/openshift endpoint validation #1098

Merged
merged 4 commits into from
Apr 13, 2023

Conversation

yangcao77
Copy link
Contributor

What does this PR do?

This PR updates the kubernetes and openshift component endpoint validation, only checks for name conflict and allows port conflict.

This PR also updates the validation rule and unit tests. Briefly reviewed the validation rule, the unit test was wrong testing port conflict within same component, but was not caught. Updated the error check to fix the issue.

Which issue(s) does this PR fix

#1049

PR acceptance criteria

Testing and documentation do not need to be complete in order for this PR to be approved. We just need to ensure tracking issues are opened.

  • Open new test/doc issues under the devfile/api repo
  • Check each criteria if:
  • There is a separate tracking issue. Add the issue link under the criteria
    or
  • test/doc updates are made as part of this PR
  • If unchecked, explain why it's not needed

How to test changes / Special notes to the reviewer

Signed-off-by: Stephanie <yangcao@redhat.com>
Signed-off-by: Stephanie <yangcao@redhat.com>
@codecov
Copy link

codecov bot commented Apr 11, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.15 🎉

Comparison is base (832b661) 35.64% compared to head (459031e) 35.79%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1098      +/-   ##
==========================================
+ Coverage   35.64%   35.79%   +0.15%     
==========================================
  Files          52       52              
  Lines        6669     6679      +10     
==========================================
+ Hits         2377     2391      +14     
+ Misses       4147     4144       -3     
+ Partials      145      144       -1     
Impacted Files Coverage Δ
pkg/validation/components.go 90.13% <100.00%> (+2.79%) ⬆️
pkg/validation/endpoints.go 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines +346 to +347
generateDummyContainerComponent("name1", nil, []v1alpha2.Endpoint{endpointUrl18080}, nil, v1alpha2.Annotation{}, false),
generateDummyKubernetesComponent("name2", []v1alpha2.Endpoint{endpointUrl28080}, ""),
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to double check -- is this test intended to use a container component and a kube component? I don't think duplicate endpoints between a container and kube component was ever an issue, was it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was an issue. previously we do not allow any port duplication across components

Copy link
Contributor

@amisevsk amisevsk Apr 12, 2023

Choose a reason for hiding this comment

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

Ah, makes sense. Perhaps it makes sense to add another test case for two Kubernetes components that use the same port number as well? The name of this test case suggests it's covering something like

generateDummyKubernetesComponent("name1", []v1alpha2.Endpoint{endpointUrl18080}, ""),
generateDummyKubernetesComponent("name2", []v1alpha2.Endpoint{endpointUrl28080}, ""),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

Comment on lines 541 to 558
} else {
} else if tt.wantErr == nil {
assert.Equal(t, nil, err, "Error should be nil")
} else if tt.wantErr != nil {
assert.NotEqual(t, nil, err, "Error should not be nil")
Copy link
Contributor

Choose a reason for hiding this comment

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

For clarity, it might make sense to do these checks first -- as it stands it looks like we check a subcase of tt.wantErr != nil first. Something like

if tt.wantErr == nil {
	assert.NoError(t, err, "Error should be nil")
	return
} else {
	if !assert.Error(t, err, "Should return error") {
		return
	}
}

merr, ok := err.(*multierror.Error)
if !ok {
	t.Fatalf("Unexpected error: %w", err)
}
if assert.Equal(t, len(tt.wantErr), len(merr.Errors), "Error list length should match") {
	for i := 0; i < len(merr.Errors); i++ {
		assert.Regexp(t, tt.wantErr[i], merr.Errors[i].Error(), "Error message should match")
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Signed-off-by: Stephanie <yangcao@redhat.com>
@openshift-ci openshift-ci bot removed the lgtm label Apr 12, 2023
@yangcao77 yangcao77 requested a review from amisevsk April 12, 2023 02:49
Comment on lines 557 to 559
} else {
assert.Equal(t, nil, err, "Error should be nil")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this branch is still not reachable; if err == nil, then the type assertion above will not succeed (i.e. we'll always have ok == false)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this else branch is for line 551 -> if tt.wantErr!= nil, but ok==true. which means we want the error to be nil, but we got something returned from the ValidateComponents

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated to use t.Errorf instead of assert.Equal to avoid confusion.

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 the issue is that line 551 is never reached, though:

merr, ok := err.(*multierror.Error) // If err is nil, then we'll have merr == nil, ok == false
if ok {                             // ok == true implies err != nil
  if tt.wantErr != nil {
    if assert.Equal(t, len(tt.wantErr), len(merr.Errors), "Error list length should match") {
      for i := 0; i < len(merr.Errors); i++ {
        assert.Regexp(t, tt.wantErr[i], merr.Errors[i].Error(), "Error message should match")
      }
    }
  } else {
    assert.Equal(t, nil, err, "Error should be nil")
  }

Copy link
Contributor Author

@yangcao77 yangcao77 Apr 12, 2023

Choose a reason for hiding this comment

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

the else statement is for if tt.wantErr != nil, and within the if ok block. Which means, ok && tt.wantErr == nil, implies the actual err returned from the validate function is not nil, but the test case expects the error to be nil. that's why the test need to error out.

merr, ok := err.(*multierror.Error) 
if ok {                           // err != nil
  if tt.wantErr != nil { // err != nil && wantErr != nil, need to process and check if each error matches
    if assert.Equal(t, len(tt.wantErr), len(merr.Errors), "Error list length should match") {
      for i := 0; i < len(merr.Errors); i++ {
        assert.Regexp(t, tt.wantErr[i], merr.Errors[i].Error(), "Error message should match")
      }
    }
  } else { // this is a else statement of if wantErr != nil. so err != nil && wantErr == nil. and test to error out
    t.Errorf("Error should be nil, got %v", err)
  }

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right -- maybe my brain just isn't working today 🙂. Sorry for the hassle.

To try and explain where I'm coming from -- I feel like it'd be simpler to check whether an error exists first before asserting the type. As it stands, the case "returns nil and we expect nil" is not explicitly checked, and is verified only by dodging two conditionals.

Returns nil Returns multierror.Error Returns other error
Expect no error Not checked directly Checked line 558 Test succeeds even though an error was returned
Expect an error Checked at line 561 Checked lines 551-556 Test fails but prints "Error should not be nil" even though error is not nil

I understand (after reading the validation code) that only multierror.Error is returned, but from reading the test case, this behavior is not clear, as there's no explicit assertion that any error returned from ValidateComponents has to be of type multierror.Error.

Either way, I'll approve PR so it can be merged.

Signed-off-by: Stephanie <yangcao@redhat.com>
@openshift-ci
Copy link

openshift-ci bot commented Apr 12, 2023

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: amisevsk, maysunfaisal, yangcao77

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@yangcao77 yangcao77 merged commit a6c32fc into devfile:main Apr 13, 2023
yangcao77 added a commit that referenced this pull request Oct 11, 2023
* 2.2.0 release cut

Signed-off-by: Stephanie <yangcao@redhat.com>

* enable gosec (#969)

* enable gosec

Signed-off-by: Kim Tsao <ktsao@redhat.com>

* Fix G601 errors

Signed-off-by: Kim Tsao <ktsao@redhat.com>

* Fix warnings

Signed-off-by: Kim Tsao <ktsao@redhat.com>

* Add newline

Signed-off-by: Kim Tsao <ktsao@redhat.com>

Signed-off-by: Kim Tsao <ktsao@redhat.com>

* Update the devfile spec to 2.2.1 alpha (#980)

Signed-off-by: Paul Schultz <pschultz@pobox.com>

* Bump kubernetes-client/gen revision to fix git dubious ownership error (#1045)

* bump kubernetes-client/gen revision to fix git dubious ownership error

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* whitespace changes.

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* set openapi generator ref to version which supports typescript generation.

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

---------

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* Fix Dependabot issues (#1047)

* fix dependabot issues, requires a bump in k8s versions

Signed-off-by: Kim Tsao <ktsao@redhat.com>

* re-generated devworkspace files

Signed-off-by: Kim Tsao <ktsao@redhat.com>

* Vendor updates

Signed-off-by: Kim Tsao <ktsao@redhat.com>

---------

Signed-off-by: Kim Tsao <ktsao@redhat.com>

* fix gosec v2.14.0 (#1034)

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* Update python runtime version for `release-typescript-models` job (#1051)

* bump python runtime version for 'release-typescript-models' job.

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* bump setup-python action to v4 for 'release-typescript-models' job.

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

---------

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* Add workflow for managing stale issues & PRs (#1055)

Adds a GitHub workflow for managing stale & rotten issues and PRs. 

Issues & PRs are marked stale after 90 days of inactivity. After 60 further days, they are marked as rotten and closed.

* Clarify hotReloadCapable field (#1091)

* Clarify hotReloadCapable field

Signed-off-by: Philippe Martin <phmartin@redhat.com>

* Auto-generated

Signed-off-by: Philippe Martin <phmartin@redhat.com>

* Specify isDefault

Signed-off-by: Philippe Martin <phmartin@redhat.com>

* Clarify description

Signed-off-by: Philippe Martin <phmartin@redhat.com>

---------

Signed-off-by: Philippe Martin <phmartin@redhat.com>

* update kubernetes/openshift endpoint validation (#1098)

* update endpoint validation

Signed-off-by: Stephanie <yangcao@redhat.com>

* update unit test

Signed-off-by: Stephanie <yangcao@redhat.com>

* update the test error check to be more clear

Signed-off-by: Stephanie <yangcao@redhat.com>

* add unit test for two kube components

Signed-off-by: Stephanie <yangcao@redhat.com>

---------

Signed-off-by: Stephanie <yangcao@redhat.com>

* Update release doc (#1110)

Signed-off-by: Kim Tsao <ktsao@redhat.com>

* Update directly executed scripts inside workflows (#1107)

* Update directly executed scripts on workflows

Signed-off-by: thepetk <thepetk@gmail.com>

* Update directly executed scripts on bash scripts

Signed-off-by: thepetk <thepetk@gmail.com>

---------

Signed-off-by: thepetk <thepetk@gmail.com>

* Add area alizer label to all issue templates (#1149)

Signed-off-by: thepetk <thepetk@gmail.com>

* Update deprecated github actions to latest version (#1231)

* Update deprecated github actions

Signed-off-by: thepetk <thepetk@gmail.com>

* Remove jsonschema dep from validate samples

Signed-off-by: thepetk <thepetk@gmail.com>

---------

Signed-off-by: thepetk <thepetk@gmail.com>

* Add license header file and script (#1256)

* add license header file

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* addlicense script

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* update README with instructions on adding license headers

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* update license headers under source files

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* remove license header from generated source

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* add_license.sh ignores zz_generated.* source files

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* check license headers script added

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* check license headers script added to CI workflow

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

---------

Signed-off-by: Michael Valdron <mvaldron@redhat.com>

* add signoff

Signed-off-by: Stephanie <yangcao@redhat.com>

---------

Signed-off-by: Stephanie <yangcao@redhat.com>
Signed-off-by: Kim Tsao <ktsao@redhat.com>
Signed-off-by: Paul Schultz <pschultz@pobox.com>
Signed-off-by: Michael Valdron <mvaldron@redhat.com>
Signed-off-by: Philippe Martin <phmartin@redhat.com>
Signed-off-by: thepetk <thepetk@gmail.com>
Co-authored-by: Kim Tsao <84398375+kim-tsao@users.noreply.github.com>
Co-authored-by: Paul Schultz <pschultz@pobox.com>
Co-authored-by: Michael Valdron <michael.valdron@gmail.com>
Co-authored-by: Michael Valdron <mvaldron@redhat.com>
Co-authored-by: John Collier <jcollier@redhat.com>
Co-authored-by: Philippe Martin <phmartin@redhat.com>
Co-authored-by: Theofanis Petkos <thepetk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants