pulumi/pkg/resource/deploy/source_refresh_test.go
Alex Clemmer a172f1a048 Implement partial Read
Some time ago, we introduced the concept of the initialization error to
Pulumi (i.e., an error where the resource was successfully created but
failed to fully initialize). This was originally implemented in `Create`
and `Update`  methods of the resource provider interface; when we
detected an initialization failure, we'd pack the live version of the
object into the error, and return that to the engine.

Omitted from this initial implementation was a similar semantics for
`Read`. There are many implications of this, but one of them is that a
`pulumi refresh` will erase any initialization errors that had
previously been observed, even if the initialization errors still exist
in the resource.

This commit will introduce the initialization error semantics to `Read`,
fixing this issue.
2018-08-10 15:10:14 -07:00

146 lines
4.4 KiB
Go

// Copyright 2016-2018, 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 deploy
import (
"context"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/deploy/deploytest"
"github.com/pulumi/pulumi/pkg/resource/deploy/providers"
"github.com/pulumi/pulumi/pkg/resource/plugin"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/workspace"
)
func TestRefresh(t *testing.T) {
proj := &workspace.Project{Name: "test"}
target := &Target{Name: "test"}
newURN := func(t tokens.Type, name string, parent resource.URN) resource.URN {
var pt tokens.Type
if parent != "" {
pt = parent.Type()
}
return resource.NewURN(target.Name, proj.Name, pt, t, tokens.QName(name))
}
newProviderURN := func(pkg tokens.Package, name string, parent resource.URN) resource.URN {
return newURN(providers.MakeProviderType(pkg), name, parent)
}
componentURN := newURN("component", "component", "")
resAURN := newURN("pkgA:m:typA", "resA", "")
resBURN := newURN("pkgA:m:typB", "resB", "")
resCURN := newURN("pkgC:m:typC", "resC", "")
providerARef, err := providers.NewReference(newProviderURN("pkgA", "providerA", ""), "id1")
assert.NoError(t, err)
providerBRef, err := providers.NewReference(newProviderURN("pkgA", "providerB", componentURN), "id2")
assert.NoError(t, err)
providerCRef, err := providers.NewReference(newProviderURN("pkgC", "providerC", ""), "id1")
assert.NoError(t, err)
newProviderState := func(ref providers.Reference) *resource.State {
return &resource.State{
Type: ref.URN().Type(),
URN: ref.URN(),
Custom: true,
ID: ref.ID(),
Inputs: resource.PropertyMap{},
}
}
newState := func(urn resource.URN, id resource.ID, provider string) *resource.State {
custom := id != ""
return &resource.State{
Type: urn.Type(),
URN: urn,
Custom: custom,
ID: id,
Provider: provider,
Inputs: resource.PropertyMap{},
}
}
reads := int32(0)
noopProvider := &deploytest.Provider{
ReadF: func(resource.URN, resource.ID, resource.PropertyMap) (resource.PropertyMap, resource.Status, error) {
atomic.AddInt32(&reads, 1)
return resource.PropertyMap{}, resource.StatusUnknown, nil
},
}
providerSource := &testProviderSource{
providers: map[providers.Reference]plugin.Provider{
providerARef: noopProvider,
providerBRef: noopProvider,
providerCRef: noopProvider,
},
}
olds := []*resource.State{
// One top-level provider from package A
newProviderState(providerARef),
// One component resource
newState(componentURN, "", ""),
// One nested provider from package A
newProviderState(providerBRef),
// One resource referencing provider A
newState(resAURN, "id1", providerARef.String()),
// One resource referencing provider B
newState(resBURN, "id2", providerBRef.String()),
// A top-level provider from package C
newProviderState(providerCRef),
// One resource refernecing provider C
newState(resCURN, "id3", providerCRef.String()),
}
target.Snapshot = &Snapshot{Resources: olds}
// Create and iterate a source.
iter, err := NewRefreshSource(nil, proj, target, false).Iterate(context.Background(), Options{}, providerSource)
assert.NoError(t, err)
processed := 0
for {
event, err := iter.Next()
assert.NoError(t, err)
if event == nil {
break
}
regEvent, ok := event.(RegisterResourceEvent)
assert.True(t, ok)
// The real step executor executes this on a different goroutine. Since this involves a send on a channel
// that iter.Next() is reading from, we must write to the channel on another goroutine.
go regEvent.Done(nil)
processed++
}
assert.Equal(t, len(olds), processed)
expectedRead := 0
for _, s := range olds {
if s.Custom && !providers.IsProviderType(s.Type) {
expectedRead++
}
}
assert.Equal(t, expectedRead, int(reads))
}