Skip to content

Commit

Permalink
Use random namespace for DBR test to allow parallelism (#380)
Browse files Browse the repository at this point in the history
Step 3 is no longer valid since we are using a test namespace
rather than "default".

Remove namespace integration test and add cases to a unit test instead
  • Loading branch information
lblackstone authored Jan 31, 2019
1 parent a9c1887 commit 0e5116e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 98 deletions.
46 changes: 36 additions & 10 deletions pkg/provider/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
type object map[string]interface{}
type list []interface{}

func TestFieldsChanged(t *testing.T) {
func TestPropertiesChanged(t *testing.T) {
tests := []struct {
name string
group string
version string
kind string
Expand All @@ -22,34 +23,59 @@ func TestFieldsChanged(t *testing.T) {
expected []string
}{
{
name: "Adding spec and nested field results in correct diffs.",
group: "core", version: "v1", kind: "PersistentVolumeClaim",
old: object{"spec": object{}},
new: object{"spec": object{"accessModes": object{}}},
expected: []string{".spec", ".spec.accessModes"},
},
{
name: "Changing image spec results in correct diff.",
group: "core", version: "v1", kind: "Pod",
old: object{"spec": object{"containers": list{object{"name": "nginx", "image": "nginx"}}}},
new: object{"spec": object{"containers": list{object{"name": "nginx", "image": "nginx:1.15-alpine"}}}},
expected: []string{".spec.containers[*].image"},
},
{
name: "Group unspecified and changing image spec results in correct diff.",
group: "", version: "v1", kind: "Pod",
old: object{"spec": object{"containers": list{object{"name": "nginx", "image": "nginx"}}}},
new: object{"spec": object{"containers": list{object{"name": "nginx", "image": "nginx:1.15-alpine"}}}},
expected: []string{".spec.containers[*].image"},
},
{
name: `Changing namespace from "" to "default" produces no diff.`,
group: "core", version: "v1", kind: "Pod",
old: object{"metadata": object{"namespace": ""}},
new: object{"metadata": object{"namespace": "default"}},
expected: []string{},
},
{
name: `Changing image spec results in correct diff and changing namespace from "" to "default" produces no diff.`,
group: "", version: "v1", kind: "Pod",
old: object{
"metadata": object{"namespace": ""},
"spec": object{"containers": list{object{"name": "nginx", "image": "nginx"}}},
},
new: object{
"metadata": object{"namespace": "default"},
"spec": object{"containers": list{object{"name": "nginx", "image": "nginx:1.15-alpine"}}},
},
expected: []string{".spec.containers[*].image"},
},
}

for _, test := range tests {
diff, err := openapi.PropertiesChanged(test.old, test.new,
forceNew[test.group][test.version][test.kind])
if err != nil {
t.Error(err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
diff, err := openapi.PropertiesChanged(tt.old, tt.new,
forceNew[tt.group][tt.version][tt.kind])
if err != nil {
t.Errorf("PropertiesChanged() error = %v, wantErr %v", err, nil)
}

if !reflect.DeepEqual(diff, test.expected) {
t.Errorf("Got '%v' expected '%v'", diff, test.expected)
}
if !reflect.DeepEqual(diff, tt.expected) {
t.Errorf("PropertiesChanged() = %v, want %v", diff, tt.expected)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ints

Expand Down Expand Up @@ -27,21 +39,21 @@ func TestPod(t *testing.T) {
Quick: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 3, len(stackInfo.Deployment.Resources))
assert.Equal(t, 4, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

stackRes := stackInfo.Deployment.Resources[2]
stackRes := stackInfo.Deployment.Resources[3]
assert.Equal(t, resource.RootStackType, stackRes.URN.Type())

provRes := stackInfo.Deployment.Resources[1]
provRes := stackInfo.Deployment.Resources[2]
assert.True(t, providers.IsProviderType(provRes.URN.Type()))

//
// Assert pod is successfully created.
//

pod := stackInfo.Deployment.Resources[0]
pod := stackInfo.Deployment.Resources[1]
name, _ := openapi.Pluck(pod.Outputs, "metadata", "name")
assert.Equal(t, name.(string), "pod-test")

Expand Down Expand Up @@ -75,14 +87,14 @@ func TestPod(t *testing.T) {
Additive: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 3, len(stackInfo.Deployment.Resources))
assert.Equal(t, 4, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

stackRes := stackInfo.Deployment.Resources[2]
stackRes := stackInfo.Deployment.Resources[3]
assert.Equal(t, resource.RootStackType, stackRes.URN.Type())

provRes := stackInfo.Deployment.Resources[1]
provRes := stackInfo.Deployment.Resources[2]
assert.True(t, providers.IsProviderType(provRes.URN.Type()))

//
Expand All @@ -94,7 +106,7 @@ func TestPod(t *testing.T) {
// with the same name.
//

pod := stackInfo.Deployment.Resources[0]
pod := stackInfo.Deployment.Resources[1]
name, _ := openapi.Pluck(pod.Outputs, "metadata", "name")
assert.Equal(t, name.(string), "pod-test")

Expand Down Expand Up @@ -123,57 +135,6 @@ func TestPod(t *testing.T) {
assert.Equal(t, "nginx:1.15-alpine", image)
},
},
{
Dir: "step3",
Additive: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 3, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

stackRes := stackInfo.Deployment.Resources[2]
assert.Equal(t, resource.RootStackType, stackRes.URN.Type())

provRes := stackInfo.Deployment.Resources[1]
assert.True(t, providers.IsProviderType(provRes.URN.Type()))

//
// Assert new Pod is deleted before being replaced with the new Pod, running
// nginx:1.13-alpine, EVEN WHEN we change the namespace from "" -> "default". This
// captures the case that we need to delete-before-replace if we're deploying to the same
// namespace, as measured by canonical name, rather than literal string equality.
//

pod := stackInfo.Deployment.Resources[0]
name, _ := openapi.Pluck(pod.Outputs, "metadata", "name")
assert.Equal(t, name.(string), "pod-test")

// Not autonamed.
_, autonamed := openapi.Pluck(pod.Outputs, "metadata", "annotations", "pulumi.com/autonamed")
assert.False(t, autonamed)

// Status is "Running"
phase, _ := openapi.Pluck(pod.Outputs, "status", "phase")
assert.Equal(t, "Running", phase)

// Status "Ready" is "True".
conditions, _ := openapi.Pluck(pod.Outputs, "status", "conditions")
ready := conditions.([]interface{})[1].(map[string]interface{})
readyType := ready["type"]
assert.Equal(t, "Ready", readyType)
readyStatus := ready["status"]
assert.Equal(t, "True", readyStatus)

// Container is called "nginx" and uses image "nginx:1.13-alpine".
containerStatuses, _ := openapi.Pluck(pod.Outputs, "status", "containerStatuses")
containerStatus := containerStatuses.([]interface{})[0].(map[string]interface{})
containerName := containerStatus["name"]
assert.Equal(t, "nginx", containerName)
image := containerStatus["image"]
assert.Equal(t, "nginx:1.13-alpine", image)
},
},
},
})
}
19 changes: 17 additions & 2 deletions tests/integration/delete-before-replace/step1/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as k8s from "@pulumi/kubernetes";

export const namespace = new k8s.core.v1.Namespace("test-namespace");

//
// Create a simple Pod.
//

const pod = new k8s.core.v1.Pod("pod-test", {
metadata: {
namespace: namespace.metadata.apply(ns => ns.name),
name: "pod-test",
},
spec: {
containers: [
{name: "nginx", image: "nginx:1.13-alpine"},
],
},
})
});
2 changes: 1 addition & 1 deletion tests/integration/delete-before-replace/step1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "steps",
"version": "0.1.0",
"dependencies": {
"@pulumi/pulumi": "^0.15.1"
"@pulumi/pulumi": "^0.16.2"
},
"devDependencies": {
"typescript": "^2.5.3"
Expand Down
19 changes: 17 additions & 2 deletions tests/integration/delete-before-replace/step2/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as k8s from "@pulumi/kubernetes";

export const namespace = new k8s.core.v1.Namespace("test-namespace");

//
// Cause hard delete-before-replace. Changing the Pod's container image tag, causes it to be
// replaced, and since we've manually specified a name, the engine has no choice but to delete it
Expand All @@ -10,11 +24,12 @@ import * as k8s from "@pulumi/kubernetes";

const pod = new k8s.core.v1.Pod("pod-test", {
metadata: {
namespace: namespace.metadata.apply(ns => ns.name),
name: "pod-test",
},
spec: {
containers: [
{name: "nginx", image: "nginx:1.15-alpine"},
],
},
})
});
23 changes: 0 additions & 23 deletions tests/integration/delete-before-replace/step3/index.ts

This file was deleted.

0 comments on commit 0e5116e

Please sign in to comment.