github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/resource/deploy/deployment_executor_test.go (about) 1 // Copyright 2016-2022, Pulumi Corporation. 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package deploy 15 16 import ( 17 "testing" 18 19 "github.com/pulumi/pulumi/sdk/v3/go/common/resource" 20 "github.com/stretchr/testify/assert" 21 ) 22 23 // Note: the only valid way to add a resource to the node list is via the `add` method. 24 // This ensures that the `sorted` method works correctly. 25 type nodeList []*resource.State 26 27 func (nl *nodeList) Add(urn string, delete bool, children ...*resource.State) *resource.State { 28 n := &resource.State{URN: resource.URN(urn), Delete: delete} 29 for _, child := range children { 30 child.Parent = n.URN 31 } 32 *nl = append(*nl, n) 33 return n 34 } 35 36 func (nl *nodeList) AsRefreshSteps() map[*resource.State]Step { 37 m := make(map[*resource.State]Step, len(*nl)) 38 for _, r := range *nl { 39 m[r] = &RefreshStep{ 40 old: r, 41 new: r, 42 } 43 } 44 return m 45 } 46 47 func (nl *nodeList) sorted() []*resource.State { 48 // Since we add elements before we add their parents, we are guaranteed a reverse 49 // topological sort. We can retrieve a topological sort by reversing the list. 50 l := make([]*resource.State, 0, len(*nl)) 51 for i := len(*nl) - 1; i >= 0; i-- { 52 l = append(l, (*nl)[i]) 53 } 54 return l 55 } 56 57 func (nl *nodeList) Executor() *deploymentExecutor { 58 return &deploymentExecutor{ 59 deployment: &Deployment{ 60 prev: &Snapshot{ 61 Resources: nl.sorted(), 62 }, 63 }, 64 } 65 } 66 67 func TestRebuildBaseState(t *testing.T) { 68 t.Parallel() 69 70 t.Run("simple-deps", func(t *testing.T) { 71 t.Parallel() 72 nl := &nodeList{} 73 nl.Add("A", true, nl.Add("B", false)) 74 75 ex := nl.Executor() 76 77 ex.rebuildBaseState(nl.AsRefreshSteps(), true) 78 79 assert.EqualValues(t, map[resource.URN]*resource.State{ 80 "B": {URN: "B"}, 81 }, ex.deployment.olds) 82 }) 83 84 t.Run("tree", func(t *testing.T) { 85 t.Parallel() 86 nl := &nodeList{} 87 nl.Add("A", false, 88 nl.Add("C", true, 89 nl.Add("F", false)), 90 nl.Add("D", false, 91 nl.Add("G", false), 92 nl.Add("H", true))) 93 nl.Add("B", true, 94 nl.Add("E", true, 95 nl.Add("I", false))) 96 97 ex := nl.Executor() 98 99 ex.rebuildBaseState(nl.AsRefreshSteps(), true) 100 101 assert.EqualValues(t, map[resource.URN]*resource.State{ 102 "A": {URN: "A"}, 103 "I": {URN: "I"}, 104 "F": {URN: "F", Parent: "A"}, 105 "G": {URN: "G", Parent: "D"}, 106 "D": {URN: "D", Parent: "A"}, 107 }, ex.deployment.olds) 108 109 }) 110 }