github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/operations/resources_test.go (about)

     1  // Copyright 2016-2022, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package operations
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"io/ioutil"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"github.com/pulumi/pulumi/pkg/v3/resource/stack"
    26  	"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
    27  )
    28  
    29  func getPulumiResources(t *testing.T, path string) *Resource {
    30  	ctx := context.Background()
    31  	var checkpoint apitype.CheckpointV3
    32  	byts, err := ioutil.ReadFile(path)
    33  	assert.NoError(t, err)
    34  	err = json.Unmarshal(byts, &checkpoint)
    35  	assert.NoError(t, err)
    36  	snapshot, err := stack.DeserializeCheckpoint(ctx, &checkpoint)
    37  	assert.NoError(t, err)
    38  	resources := NewResourceTree(snapshot.Resources)
    39  	return resources
    40  }
    41  
    42  func TestTodo(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	components := getPulumiResources(t, "testdata/todo.json")
    46  	assert.Equal(t, 4, len(components.Children))
    47  
    48  	// Table child
    49  	table, ok := components.GetChild("cloud:table:Table", "todo")
    50  	assert.True(t, ok)
    51  	if !assert.NotNil(t, table) {
    52  		return
    53  	}
    54  	assert.Equal(t, 2, len(table.State.Inputs))
    55  	assert.Equal(t, "id", table.State.Inputs["primaryKey"].StringValue())
    56  	assert.Equal(t, 1, len(table.Children))
    57  	table, ok = table.GetChild("aws:dynamodb/table:Table", "todo")
    58  	assert.True(t, ok)
    59  	assert.NotNil(t, table)
    60  
    61  	// Endpoint child
    62  	endpoint, ok := components.GetChild("cloud:http:HttpEndpoint", "todo")
    63  	assert.True(t, ok)
    64  	if !assert.NotNil(t, endpoint) {
    65  		return
    66  	}
    67  	assert.Equal(t, 5, len(endpoint.State.Inputs))
    68  	assert.Equal(t,
    69  		"https://eupwl7wu4i.execute-api.us-east-2.amazonaws.com/", endpoint.State.Inputs["url"].StringValue())
    70  	assert.Equal(t, 14, len(endpoint.Children))
    71  	endpoint, ok = endpoint.GetChild("aws:apigateway/restApi:RestApi", "todo")
    72  	assert.True(t, ok)
    73  	assert.NotNil(t, endpoint)
    74  
    75  	// Nonexistant resource.
    76  	r, ok := endpoint.GetChild("garden:ornimentation/gnome", "stone")
    77  	assert.False(t, ok)
    78  	assert.Nil(t, r)
    79  }
    80  
    81  func TestCrawler(t *testing.T) {
    82  	t.Parallel()
    83  
    84  	components := getPulumiResources(t, "testdata/crawler.json")
    85  	assert.Equal(t, 7, len(components.Children))
    86  
    87  	// Topic child
    88  	topic, ok := components.GetChild("cloud:topic:Topic", "countDown")
    89  	assert.True(t, ok)
    90  	if !assert.NotNil(t, topic) {
    91  		return
    92  	}
    93  	assert.Equal(t, 0, len(topic.State.Inputs))
    94  	assert.Equal(t, 1, len(topic.Children))
    95  	topic, ok = topic.GetChild("aws:sns/topic:Topic", "countDown")
    96  	assert.True(t, ok)
    97  	assert.NotNil(t, topic)
    98  
    99  	// Timer child
   100  	heartbeat, ok := components.GetChild("cloud:timer:Timer", "heartbeat")
   101  	assert.True(t, ok)
   102  	if !assert.NotNil(t, heartbeat) {
   103  		return
   104  	}
   105  	assert.Equal(t, 1, len(heartbeat.State.Inputs))
   106  	assert.Equal(t, "rate(5 minutes)", heartbeat.State.Inputs["scheduleExpression"].StringValue())
   107  	assert.Equal(t, 4, len(heartbeat.Children))
   108  
   109  	// Function child of timer
   110  	function, ok := heartbeat.GetChild("cloud:function:Function", "heartbeat")
   111  	assert.True(t, ok)
   112  	if !assert.NotNil(t, function) {
   113  		return
   114  	}
   115  	assert.Equal(t, 1, len(function.State.Inputs))
   116  	assert.Equal(t, 3, len(function.Children))
   117  }