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

🌱 add dependency package in test/api #375

Merged
merged 10 commits into from
Jun 9, 2023

Conversation

khareyash05
Copy link
Contributor

@khareyash05 khareyash05 commented Jun 6, 2023

Upstream issue konveyor/operator#220

Issue in the repo #370

added api_test.go,pkg.go,samples.go in dependency directory. Also added dependency.go in binding package and added dependency client in binding/richclient.go

Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@khareyash05 khareyash05 changed the title add dependency directory necessary files :seedling: add dependency directory necessary files Jun 6, 2023
@khareyash05 khareyash05 changed the title :seedling: add dependency directory necessary files 🌱 add dependency directory necessary files Jun 6, 2023
@khareyash05 khareyash05 changed the title 🌱 add dependency directory necessary files 🌱 add dependency package in test/api Jun 6, 2023
@aufi aufi self-requested a review June 6, 2023 07:03
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@khareyash05
Copy link
Contributor Author

khareyash05 commented Jun 6, 2023

Working on handling the Samples array to take TestCase as argument as it expects []api.Dependency{}

Copy link
Member

@aufi aufi left a comment

Choose a reason for hiding this comment

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

Hey Yash, thanks for your PR 👍 There are few things we need to work on (comments in the source code), but overall looks good!

binding/dependency.go Outdated Show resolved Hide resolved
for _, r := range Samples {
t.Run("Dependency_CRUD", func(t *testing.T) {
// Create.
err := Dependency.Create(&r)
Copy link
Member

Choose a reason for hiding this comment

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

The API Dependency links two Applications and keeps their IDs in From/To fields. These fields are Refs (https://github.com/konveyor/tackle2-hub/blob/main/api/base.go#L379) so contain just IDs (and optionally Name, not that not important), but the Refs are not actual Applications. The From and To Applications need to be created before creating Dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understood from the review that dependency need To/From fields which will be used to refer to the applications?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and that To/From will be generated by creating an instance of application in api_test.go. Am i in the right direction?

Copy link
Member

Choose a reason for hiding this comment

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

Yes


for name := range samples {
sample := samples[name]
assert.Must(t, Dependency.Create(&sample))
Copy link
Member

Choose a reason for hiding this comment

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

Same with creating Dependencies - 1st Applications, 2nd Dependency between them.

test/api/dependency/samples.go Outdated Show resolved Hide resolved
test/api/dependency/samples.go Outdated Show resolved Hide resolved
}
tc = []TestCase{testCase}
Samples = []api.Dependency{tc}
)
Copy link
Member

@aufi aufi Jun 6, 2023

Choose a reason for hiding this comment

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

Definition of Dependencies (and likely also TestCases) will need to be updated. There is some limitation caused by api.Dependency keeping just Ref of Aplication, so we need store application elsewhere. I can see few options:

  • Use TestCases struct that might even not contain the Dependency itself, but something like just ApplicationFrom and ApplicationTo, so the test will go through testcases and create ApplicationTo, ApplicationFrom and then dependency between these applications (the dependency doesn't have any special fields than the Application refs, so it should work well).
  • Use model.Dependency model, that stores links to Application (not just Refs), see https://github.com/konveyor/tackle2-hub/blob/main/migration/v2/model/dependency.go#L12-L18 (yes, we do have slightly different models for database and API). The Samples array would be array of these model.Dependency and test could create both Applications and "convert" the model.Dependency to api.Dependency using method With() https://github.com/konveyor/tackle2-hub/blob/main/api/dependency.go#L157-L161
  • It would great if you can try both ways (or maybe find something else if you'd like it more than suggestions above) and we can discuss how it works if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definition of Dependencies (and likely also TestCases) will need to be updated. There is some limitation caused by api.Dependency keeping just Ref of Aplication, so we need store application elsewhere. I can see few options:

  • Use TestCases struct that might even not contain the Dependency itself, but something like just ApplicationFrom and ApplicationTo, so the test will go through testcases and create ApplicationTo, ApplicationFrom and then dependency between these applications (the dependency doesn't have any special fields than the Application refs, so it should work well).
  • Use model.Dependency model, that stores links to Application (not just Refs), see https://github.com/konveyor/tackle2-hub/blob/main/migration/v2/model/dependency.go#L12-L18 (yes, we do have slightly different models for database and API). The Samples array would be array of these model.Dependency and test could create both Applications and "convert" the model.Dependency to api.Dependency using method With() https://github.com/konveyor/tackle2-hub/blob/main/api/dependency.go#L157-L161
  • It would great if you can try both ways (or maybe find something else if you'd like it more than suggestions above) and we can discuss how it works if needed.

I think 1st option is easier to implement because it involves less complexities and 2nd option is is less intuitive which can be easily dealt by the 1st. What's your say on that?

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 a thought process of injecting dependency withdependecy-less TestCase in Samples[]. and then use From/To in the api_test.go to access them in the dependency to be used by various CRUD functions.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I fully understand, but sounds good.

Copy link
Member

Choose a reason for hiding this comment

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

A Followup to this discussion and the 1st option, the dependency variable (and Samples containing only the dependency when api_test.go creates new applicationFrom/To instances) here seems to me to be overcomplicated.
What about drop the dependency variable, considering update/rename TestCase and putting to Samples (or renamed array for tests) and doing all this in since var( ... ) block?

Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@khareyash05 khareyash05 requested a review from aufi June 6, 2023 13:45
Copy link
Member

@aufi aufi left a comment

Choose a reason for hiding this comment

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

Thanks for update! Left few comments, let's sync tomorrow.

test/api/dependency/api_test.go Outdated Show resolved Hide resolved
test/api/dependency/api_test.go Outdated Show resolved Hide resolved
}

var (
dependency = api.Dependency{
Copy link
Member

Choose a reason for hiding this comment

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

Some of these lines seems to be redundant to me (the dependency here and init() below). But I'd suggest get it to (partially) working state (passing tests against locally running Hub https://github.com/konveyor/tackle2-hub/tree/main/test#rest-api) and we can refactor it then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored the code Kindly review

test/api/dependency/api_test.go Outdated Show resolved Hide resolved
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@khareyash05 khareyash05 requested a review from aufi June 7, 2023 03:28
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@khareyash05 khareyash05 marked this pull request as ready for review June 7, 2023 12:49
Copy link
Member

@aufi aufi left a comment

Choose a reason for hiding this comment

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

Thanks for update @khareyash05, I'm happy that there is already a working/test passing code, great job!

I can see two things to be done before mering this PR:

  • remove unnecessary parts from the code (comments inline)
  • add test to ensure there are no cyclic dependencies
    • direct: when Gateway->Inventory dependency exists, API shoud not allow Inventory->Gateway dependency to be created

api/dependency.go Show resolved Hide resolved
api/dependency.go Show resolved Hide resolved
test/api/dependency/api_test.go Outdated Show resolved Hide resolved
test/api/dependency/api_test.go Outdated Show resolved Hide resolved
test/api/dependency/pkg.go Show resolved Hide resolved
}
tc = []TestCase{testCase}
Samples = []api.Dependency{tc}
)
Copy link
Member

Choose a reason for hiding this comment

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

A Followup to this discussion and the 1st option, the dependency variable (and Samples containing only the dependency when api_test.go creates new applicationFrom/To instances) here seems to me to be overcomplicated.
What about drop the dependency variable, considering update/rename TestCase and putting to Samples (or renamed array for tests) and doing all this in since var( ... ) block?

for _, r := range Samples {
t.Run(fmt.Sprintf("Dependency from %s -> %s", r.From.Name, r.To.Name), func(t *testing.T) {

applicationFrom := api.Application{
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about creating new instances of applicationFrom and To, but that depends on samples.go, see comment there.

@aufi aufi added the test/api Hub API tests-related work label Jun 7, 2023
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@khareyash05
Copy link
Contributor Author

TODO: Cyclic Dependency.

Copy link
Member

@aufi aufi left a comment

Choose a reason for hiding this comment

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

Looks good! Added few minor comments, once resolved those, I'm OK merging this PR. The cyclic dependency test can be in a follow-up PR.

api/dependency.go Show resolved Hide resolved
test/api/dependency/api_test.go Outdated Show resolved Hide resolved
test/api/dependency/api_test.go Outdated Show resolved Hide resolved
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@khareyash05 khareyash05 requested a review from aufi June 8, 2023 10:25
Copy link
Member

@aufi aufi left a comment

Choose a reason for hiding this comment

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

👍 Needed some clarification on Must/Should and fix List test - recommend run the test locally more than once to be sure, that one test run doesn't leave data for another test run.

test/api/dependency/api_test.go Outdated Show resolved Hide resolved
test/api/dependency/api_test.go Show resolved Hide resolved
test/api/dependency/api_test.go Outdated Show resolved Hide resolved
test/api/dependency/api_test.go Outdated Show resolved Hide resolved
test/api/dependency/api_test.go Outdated Show resolved Hide resolved

// Create applications.
assert.Should(t, Application.Create(&sample.ApplicationFrom))
assert.Should(t, Application.Create(&sample.ApplicationTo))
Copy link
Member

Choose a reason for hiding this comment

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

Lines assigning IDs to sample to allow its deletion in the end of the test should have stay here (sample.ApplicationFrom.ID = applicationFrom.ID).

Copy link
Contributor Author

@khareyash05 khareyash05 Jun 8, 2023

Choose a reason for hiding this comment

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

but we are deleting applicationFrom.ID as it is not needed. I am failing to understand as to why will we need this line

assert.Should(t, Application.Create(&sample.ApplicationFrom))
assert.Should(t, Application.Create(&sample.ApplicationTo))
}

Copy link
Member

Choose a reason for hiding this comment

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

There should be a Dependency creation and condition below should check if got is an array with the created dependency. Sorry, I missed this part before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shall I just compare the ID's of dependency created and the ApplicationFrom/To ID's will it suffice?

Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
@khareyash05
Copy link
Contributor Author

@aufi Kindly review

@khareyash05 khareyash05 requested a review from aufi June 8, 2023 13:00
api/dependency.go Outdated Show resolved Hide resolved
}
if assert.FlatEqual(got, Samples) {
t.Errorf("Different response error. Got %v, expected %v", got, Samples)
}
Copy link
Member

Choose a reason for hiding this comment

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

Found out we're using the assert.FlatEqual method in a wrong way (also in some other tests outside of this PR), the condition should look like if !assert.FlatEqual(got, Samples) { //error

Copy link
Contributor Author

@khareyash05 khareyash05 Jun 8, 2023

Choose a reason for hiding this comment

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

Was wondering too when working on the part before! This might be a breaking change in the code, as inverting the condition will make the test fail. Will dig deep on the same .

}

// Delete Dependencies.
for _, dependency := range got {
Copy link
Member

Choose a reason for hiding this comment

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

Thihs would delete all dependencies in API, not only ones created by this test. Not saying it is blocker in this case, but better track created objects to be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ya that's something should be taken care of. A way to tackle this what I can think of is creating an array of ID's and store ID's of dependencies at their time of creation. The Delete() of the Dependency will work on the array of ID's and will only delete them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As we have (or we will) ensure that all the created Dependencies are present in the Dependency.List(), we will ensure that all the dependencies are used and thus they are allowed to be deleted.

@khareyash05 khareyash05 marked this pull request as draft June 8, 2023 15:42
Signed-off-by: Yash Khare <yash2010118@akgec.ac.in>
Copy link
Member

@aufi aufi left a comment

Choose a reason for hiding this comment

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

There are some things (assert with FlatEqual and add cyclic test), that need to be updated in followup PR, but:

Looks good to me, nice work Yash!

@aufi aufi marked this pull request as ready for review June 9, 2023 07:03
@khareyash05
Copy link
Contributor Author

There are some things (assert with FlatEqual and add cyclic test), that need to be updated in followup PR, but:

Looks good to me, nice work Yash!

Thank you and will do that in the followup PR

@aufi aufi merged commit 69ca6ac into konveyor:main Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test/api Hub API tests-related work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants