Skip to content

Unit tests for api.go #27

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

Merged
merged 1 commit into from
Jul 16, 2021
Merged

Conversation

VenkatRamaraju
Copy link
Contributor

Added unit tests for v1alpha/api.go

Note: These tests depend on the v1_suite_test.go test suite file from this PR: #24.

@laxmikantbpandhare
Copy link
Member

Working fine.

Running Suite: V1alpha Suite
============================
Random Seed: 1625769627
Will run 18 of 18 specs

••••••••••••••••••
Ran 18 of 18 Specs in 0.000 seconds
SUCCESS! -- 18 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS

Ginkgo ran 1 suite in 1.119981223s
Test Suite Passed

Copy link
Member

@laxmikantbpandhare laxmikantbpandhare left a comment

Choose a reason for hiding this comment

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

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jul 8, 2021
@laxmikantbpandhare laxmikantbpandhare linked an issue Jul 8, 2021 that may be closed by this pull request
26 tasks
Copy link
Member

@jmrodri jmrodri left a comment

Choose a reason for hiding this comment

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

A few changes to the test structure.

Comment on lines 38 to 47
testAPIOptions := &createAPIOptions{
CRDVersion: "testVersion",
Namespaced: true,
}
updateTestResource := resource.Resource{}
testAPIOptions.UpdateResource(&updateTestResource)

It("verify that resource API's CRDVersion was set", func() {
Expect(updateTestResource.API.CRDVersion, testAPIOptions.CRDVersion)
})

It("verify that resource API's Namespaced was set", func() {
Expect(updateTestResource.API.Namespaced, testAPIOptions.Namespaced)
})

It("verify that resource path is an empty string", func() {
Expect(updateTestResource.Path, "")
})

It("verify that resource controller is false", func() {
Expect(updateTestResource.Controller).To(BeFalse())
})
Copy link
Member

Choose a reason for hiding this comment

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

The UpdateResource method call should be in the It blocks. OR put it in a BeforeEach block.

Comment on lines 38 to 42
testAPIOptions := &createAPIOptions{
CRDVersion: "testVersion",
Namespaced: true,
}
updateTestResource := resource.Resource{}
Copy link
Member

Choose a reason for hiding this comment

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

I would move these into a BeforeEach block.

Suggested change
testAPIOptions := &createAPIOptions{
CRDVersion: "testVersion",
Namespaced: true,
}
updateTestResource := resource.Resource{}
var (
testAPIOptions createAPIOptions
updateTestResource resource.Resource
)
BeforeEach(func() {
testAPIOptions = &createAPIOptions{
CRDVersion: "testVersion",
Namespaced: true,
}
updateTestResource = resource.Resource{}
})

Namespaced: true,
}
updateTestResource := resource.Resource{}
testAPIOptions.UpdateResource(&updateTestResource)
Copy link
Member

Choose a reason for hiding this comment

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

Make this call inside the It blocks. Or you could put all of the expects in a single It. Either way is fine. I'm okay with it being in multiple Its.

Suggested change
testAPIOptions.UpdateResource(&updateTestResource)
It("...", func() {
testAPIOptions.UpdateResource(&updateTestResource)
Expect(....)
})

testAPIOptions.UpdateResource(&updateTestResource)

It("verify that resource API's CRDVersion was set", func() {
Expect(updateTestResource.API.CRDVersion, testAPIOptions.CRDVersion)
Copy link
Member

Choose a reason for hiding this comment

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

This suggestion applies to all the Expects

Suggested change
Expect(updateTestResource.API.CRDVersion, testAPIOptions.CRDVersion)
Expect(updateTestResource.API.CRDVersion).To(Equal(testAPIOptions.CRDVersion))


Describe("BindFlags", func() {
flagTest := pflag.NewFlagSet("testFlag", -1)
testAPISubcommand.BindFlags(flagTest)
Copy link
Member

Choose a reason for hiding this comment

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

Put in the It

Comment on lines +92 to +85
Describe("Run", func() {
It("should return nil", func() {
Expect(testAPISubcommand.Run(machinery.Filesystem{})).To(BeNil())
})
})

Describe("Validate", func() {
It("should return nil", func() {
Expect(testAPISubcommand.Validate()).To(BeNil())
})
})

Describe("PostScaffold", func() {
It("should return nil", func() {
Expect(testAPISubcommand.PostScaffold()).To(BeNil())
})
})
Copy link
Member

Choose a reason for hiding this comment

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

These tests are using a testApiSubcommand that you created in another test. You want tests to always have a clean version, they should not depend on what happens in another test. That's why the BeforeEach comes in handy.

Plural: "test-plural",
}

groupErr := failAPISubcommand.InjectResource(&failResource)
Copy link
Member

Choose a reason for hiding this comment

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

These calls should be included with the It blocks. Basically consider the It as a specific test and it should include the call and the expectations.

Comment on lines 131 to 132
failResource.GVK.Group = "test-group"
versionErr := failAPISubcommand.InjectResource(&failResource)
Copy link
Member

Choose a reason for hiding this comment

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

This has to be inside the It block. Otherwise you are affecting subsequent tests.

Comment on lines 138 to 139
failResource.GVK.Version = "v1"
kindError := failAPISubcommand.InjectResource(&failResource)
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as above

Expect(kindError).To(HaveOccurred())
})

noErr := testAPISubcommand.InjectResource(&testResource)
Copy link
Member

Choose a reason for hiding this comment

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

This too

@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Jul 14, 2021
@VenkatRamaraju VenkatRamaraju requested a review from jmrodri July 14, 2021 18:19
Copy link
Member

@jmrodri jmrodri left a comment

Choose a reason for hiding this comment

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

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jul 16, 2021
@jmrodri jmrodri merged commit 458fd27 into operator-framework:main Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Meta-issue: Java Operator
3 participants