github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/sg_test.go (about) 1 // Copyright 2019 The Terraformer Authors. 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 aws 16 17 import ( 18 "reflect" 19 "testing" 20 21 "github.com/aws/aws-sdk-go-v2/aws" 22 "github.com/aws/aws-sdk-go-v2/service/ec2/types" 23 ) 24 25 func TestEmptySgs(t *testing.T) { 26 var securityGroups []types.SecurityGroup 27 28 rulesToMoveOut := findSgsToMoveOut(securityGroups) 29 30 if !reflect.DeepEqual(rulesToMoveOut, []string{}) { 31 t.Errorf("failed to calculate rules to move out %v", rulesToMoveOut) 32 } 33 } 34 35 func Test1CycleReference(t *testing.T) { 36 sgA := types.SecurityGroup{ 37 GroupId: aws.String("aaaa"), 38 IpPermissions: []types.IpPermission{ 39 { 40 UserIdGroupPairs: []types.UserIdGroupPair{ 41 { 42 GroupId: aws.String("aaaa"), 43 }, 44 }, 45 }, 46 {}, 47 }, 48 } 49 securityGroups := []types.SecurityGroup{ 50 sgA, 51 } 52 53 rulesToMoveOut := findSgsToMoveOut(securityGroups) 54 55 if !reflect.DeepEqual(rulesToMoveOut, []string{}) { 56 t.Errorf("failed to calculate rules to move out %v", rulesToMoveOut) 57 } 58 } 59 60 func Test2CycleReference(t *testing.T) { 61 sgA := types.SecurityGroup{ 62 GroupId: aws.String("aaaa"), 63 IpPermissions: []types.IpPermission{ 64 { 65 UserIdGroupPairs: []types.UserIdGroupPair{ 66 { 67 GroupId: aws.String("bbbb"), 68 }, 69 }, 70 }, 71 }, 72 } 73 securityGroups := []types.SecurityGroup{ 74 { 75 GroupId: aws.String("bbbb"), 76 IpPermissions: []types.IpPermission{ 77 { 78 UserIdGroupPairs: []types.UserIdGroupPair{ 79 { 80 GroupId: aws.String("aaaa"), 81 }, 82 }, 83 }, 84 {}, 85 }, 86 }, 87 sgA, 88 } 89 90 rulesToMoveOut := findSgsToMoveOut(securityGroups) 91 92 if !reflect.DeepEqual(rulesToMoveOut[0], *sgA.GroupId) { 93 t.Errorf("failed to calculate rules to move out %v", rulesToMoveOut) 94 } 95 } 96 97 func TestNoCycleReference(t *testing.T) { 98 sgA := types.SecurityGroup{ 99 GroupId: aws.String("aaaa"), 100 IpPermissions: []types.IpPermission{ 101 { 102 UserIdGroupPairs: []types.UserIdGroupPair{ 103 { 104 GroupId: aws.String("bbbb"), 105 }, 106 }, 107 }, 108 }, 109 } 110 securityGroups := []types.SecurityGroup{ 111 { 112 GroupId: aws.String("bbbb"), 113 IpPermissions: []types.IpPermission{ 114 {}, 115 {}, 116 }, 117 }, 118 sgA, 119 } 120 121 rulesToMoveOut := findSgsToMoveOut(securityGroups) 122 123 if len(rulesToMoveOut) != 0 { 124 t.Errorf("failed to calculate rules to move out %v", rulesToMoveOut) 125 } 126 } 127 128 func Test3Cycle1CycleReference(t *testing.T) { 129 sgA := types.SecurityGroup{ 130 GroupId: aws.String("aaaa"), 131 IpPermissions: []types.IpPermission{ 132 { 133 UserIdGroupPairs: []types.UserIdGroupPair{ 134 { 135 GroupId: aws.String("aaaa"), 136 }, 137 }, 138 }, 139 { 140 UserIdGroupPairs: []types.UserIdGroupPair{ 141 { 142 GroupId: aws.String("bbbb"), 143 }, 144 }, 145 }, 146 }, 147 } 148 securityGroups := []types.SecurityGroup{ 149 sgA, 150 { 151 GroupId: aws.String("bbbb"), 152 IpPermissions: []types.IpPermission{ 153 { 154 UserIdGroupPairs: []types.UserIdGroupPair{ 155 { 156 GroupId: aws.String("cccc"), 157 }, 158 }, 159 }, 160 {}, 161 }, 162 }, 163 { 164 GroupId: aws.String("cccc"), 165 IpPermissions: []types.IpPermission{ 166 { 167 UserIdGroupPairs: []types.UserIdGroupPair{ 168 { 169 GroupId: aws.String("aaaa"), 170 }, 171 }, 172 }, 173 {}, 174 }, 175 }, 176 { 177 GroupId: aws.String("dddd"), 178 IpPermissions: []types.IpPermission{ 179 { 180 UserIdGroupPairs: []types.UserIdGroupPair{ 181 { 182 GroupId: aws.String("aaaa"), 183 }, 184 }, 185 }, 186 {}, 187 }, 188 }, 189 } 190 191 rulesToMoveOut := findSgsToMoveOut(securityGroups) 192 193 if !reflect.DeepEqual(rulesToMoveOut[0], *sgA.GroupId) { 194 t.Errorf("failed to calculate rules to move out %v", rulesToMoveOut) 195 } 196 }