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

Tests to make sure we pass through fields of corev1.Container #242

Merged
merged 10 commits into from
Feb 28, 2018

Conversation

tcnghia
Copy link
Contributor

@tcnghia tcnghia commented Feb 26, 2018

Fixes #176

Nghia Tran and others added 7 commits February 22, 2018 14:34
Even though ContainerSpec accepts all fields of core/v1/Container,
the controller will silently overwrite some fields, like Name,
Resources, Ports, and VolumeMounts.  This will cause confusions if the
users expect their settings to be effective.  This change disallows
specifying those fields to avoid the confusions.
@tcnghia tcnghia requested a review from grantr February 26, 2018 22:29
@tcnghia
Copy link
Contributor Author

tcnghia commented Feb 28, 2018

@grantr take a look? thanks

@grantr
Copy link
Contributor

grantr commented Feb 28, 2018

Oops, missed this one @tcnghia, 👀 now

Copy link
Contributor

@grantr grantr left a comment

Choose a reason for hiding this comment

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

Thanks @tcnghia! Always good to see tests improvements. I'd like to standardize on cmp.Diff in tests instead of reflect.DeepEqual.

@@ -147,6 +167,10 @@ func TestCreateConfigurationsCreatesRevision(t *testing.T) {
t.Errorf("rev service was not %s", config.Spec.Template.Spec.Service)
}

if !reflect.DeepEqual(rev.Spec, config.Spec.Template.Spec) {
Copy link
Contributor

Choose a reason for hiding this comment

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

use cmp.Diff here. It gives a nice message showing the differences between objects. Here's an example:

https://github.com/google/elafros/blob/1b819750ff22b05ad7d6238f424a16836d17f27b/pkg/controller/route/controller_test.go#L250-L252

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -158,6 +176,16 @@ func TestCreateRevCreatesStuff(t *testing.T) {
func(t *testing.T) {
for _, c := range p.Spec.Containers {
if c.Image == rev.Spec.ContainerSpec.Image {
// Make a copy and removed fields set by the controller.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: should be "remove"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

container.Ports = nil
container.VolumeMounts = nil
// Verify that all other fields match revision container spec.
if !reflect.DeepEqual(container, rev.Spec.ContainerSpec) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use cmp.Diff here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -158,6 +171,12 @@ func TestCreateRevCreatesStuff(t *testing.T) {
func(t *testing.T) {
for _, c := range p.Spec.Containers {
if c.Image == rev.Spec.ContainerSpec.Image {
// Ignoring fields set by Elafros controller.
ignored := cmpopts.IgnoreFields(corev1.Container{}, "Name", "Ports", "Resources", "VolumeMounts")
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the change to using IgnoreFields here!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks about cmp.Diff suggestion 👍

I am pretty new to Go, so would love to receive lots of recommendations like these.

@tcnghia tcnghia merged commit 820c2a3 into knative:master Feb 28, 2018
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.

2 participants