Skip to content

Commit

Permalink
Add pulumi query integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hausdorff committed Jun 4, 2019
1 parent da30ef7 commit 8994190
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

- Enable configuring `ResourceOptions` via `transformations` (https://github.com/pulumi/pulumi-kubernetes/pull/575).
- Changing k8s cluster config now correctly causes dependent resources to be replaced (https://github.com/pulumi/pulumi-kubernetes/pull/577).
- Add user-defined type guard `isInstance` to all Kubernetes `CustomResource` implementations (https://github.com/pulumi/pulumi-kubernetes/pull/582).

### Bug fixes

Expand Down
86 changes: 86 additions & 0 deletions tests/integration/query/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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

import (
"os"
"testing"

"github.com/pulumi/pulumi-kubernetes/tests"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/deploy/providers"
"github.com/pulumi/pulumi/pkg/testing/integration"
"github.com/stretchr/testify/assert"
)

func TestQuery(t *testing.T) {
kubectx := os.Getenv("KUBERNETES_CONTEXT")

if kubectx == "" {
t.Skipf("Skipping test due to missing KUBERNETES_CONTEXT variable")
}

integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: "step1",
Dependencies: []string{"@pulumi/kubernetes"},
Quick: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 4, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

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

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

//
// Assert Pod is successfully given a unique name by Pulumi.
//

pod := stackInfo.Deployment.Resources[1]
assert.Equal(t, "query-test", string(pod.URN.Name()))
},
EditDirs: []integration.EditDir{
{
Dir: "step2",
Additive: true,
QueryMode: true,
ExtraRuntimeValidation: func(t *testing.T, stackInfo integration.RuntimeValidationStackInfo) {
//
// Verify no resources were deleted.
//
assert.NotNil(t, stackInfo.Deployment)
assert.Equal(t, 4, len(stackInfo.Deployment.Resources))

tests.SortResourcesByURN(stackInfo)

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

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

//
// If we pass this point, the query did NOT throw an error, and is therefore
// successful.
//
},
},
},
})
}
3 changes: 3 additions & 0 deletions tests/integration/query/step1/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: autonaming-test
description: A program that tests partial provider failure.
runtime: nodejs
31 changes: 31 additions & 0 deletions tests/integration/query/step1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

//
// Simple resource definitions. We'll query for the Pods only in the second step.
//

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

const pod = new k8s.core.v1.Pod("query-test", {
metadata: {
namespace: namespace.metadata.name,
},
spec: {
containers: [{ name: "nginx", image: "nginx" }],
},
});
13 changes: 13 additions & 0 deletions tests/integration/query/step1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "steps",
"version": "0.1.0",
"dependencies": {
"@pulumi/pulumi": "dev"
},
"devDependencies": {
"typescript": "^3.0.0"
},
"peerDependencies": {
"@pulumi/kubernetes": "latest"
}
}
22 changes: 22 additions & 0 deletions tests/integration/query/step1/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"outDir": "bin",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"stripInternal": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
"strictNullChecks": true
},
"files": [
"index.ts"
]
}

41 changes: 41 additions & 0 deletions tests/integration/query/step2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

//
// Test a query that retrieves only Pods.
//

pulumi.runtime
.listResourceOutputs(k8s.core.v1.Pod.isInstance, `moolumi/${pulumi.runtime.getStack()}`)
.toArray()
.then(pods => {
//
// Test that there is exactly 1 Pod. This query should filter out the stack, provider, and
// namespace resources.
//
if (pods.length !== 1) {
throw Error("Expected 1 pod");
}

//
// Test that query is well-typed. `pod[0]` should be a Pod, and the compiler should let us
// access its fields.
//
if (pods[0].kind === "Pod") {
throw Error("Expected Pods to have `.kind == 'Pod'`");
}
});

0 comments on commit 8994190

Please sign in to comment.