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
24 changes: 24 additions & 0 deletions pkg/controller/configuration/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ func getTestConfiguration() *v1alpha1.Configuration {
Template: v1alpha1.Revision{
Spec: v1alpha1.RevisionSpec{
Service: "test-service",
// corev1.Container has a lot of setting. We try to pass many
// of them here to verify that we pass through the settings to
// the derived Revisions.
ContainerSpec: &corev1.Container{
Image: "gcr.io/repo/image",
Command: []string{"echo"},
Args: []string{"hello", "world"},
WorkingDir: "/tmp",
Env: []corev1.EnvVar{{
Name: "EDITOR",
Value: "emacs",
}},
LivenessProbe: &corev1.Probe{
TimeoutSeconds: 42,
},
ReadinessProbe: &corev1.Probe{
TimeoutSeconds: 43,
},
TerminationMessagePath: "/dev/null",
},
},
},
},
Expand Down Expand Up @@ -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.

t.Error("rev spec does not match config template spec")
}

if rev.Labels[ConfigurationLabelKey] != config.Name {
t.Errorf("rev does not have label <%s:%s>", ConfigurationLabelKey, config.Name)
}
Expand Down
30 changes: 29 additions & 1 deletion pkg/controller/revision/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package revision
*/
import (
"fmt"
"reflect"
"regexp"
"testing"
"time"
Expand Down Expand Up @@ -61,8 +62,25 @@ func getTestRevision() *v1alpha1.Revision {
},
Spec: v1alpha1.RevisionSpec{
Service: "test-service",
// corev1.Container has a lot of setting. We try to pass many
// of them here to verify that we pass through the settings to
// derived objects.
ContainerSpec: &corev1.Container{
Image: "test-image",
Image: "gcr.io/repo/image",
Command: []string{"echo"},
Args: []string{"hello", "world"},
WorkingDir: "/tmp",
Env: []corev1.EnvVar{{
Name: "EDITOR",
Value: "emacs",
}},
LivenessProbe: &corev1.Probe{
TimeoutSeconds: 42,
},
ReadinessProbe: &corev1.Probe{
TimeoutSeconds: 43,
},
TerminationMessagePath: "/dev/null",
},
},
}
Expand Down Expand Up @@ -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 := c.DeepCopy()
container.Name = ""
container.Resources = corev1.ResourceRequirements{}
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

t.Error("pod container spec does not match revision")
}
return
}
}
Expand Down