|
| 1 | +/* |
| 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 | + |
| 15 | +package fake |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "sync" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 23 | + "github.com/aws/aws-sdk-go-v2/service/iam" |
| 24 | + iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types" |
| 25 | +) |
| 26 | + |
| 27 | +// TestIAMAPICreateInstanceProfileReturnsCopy ensures CreateInstanceProfile returns an object that is independent from |
| 28 | +// the one retained in the fake's internal map. Before the fix, the same pointer was returned, so a caller mutating the |
| 29 | +// returned profile (as the instanceprofile provider does) silently mutated the fake's internal state. |
| 30 | +func TestIAMAPICreateInstanceProfileReturnsCopy(t *testing.T) { |
| 31 | + api := NewIAMAPI() |
| 32 | + const name = "test-profile" |
| 33 | + |
| 34 | + out, err := api.CreateInstanceProfile(context.Background(), &iam.CreateInstanceProfileInput{ |
| 35 | + InstanceProfileName: aws.String(name), |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("CreateInstanceProfile: unexpected error: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Mutate the returned object the way the provider's Create does. |
| 42 | + out.InstanceProfile.Roles = []iamtypes.Role{{RoleName: aws.String("some-role")}} |
| 43 | + |
| 44 | + if got := len(api.InstanceProfiles[name].Roles); got != 0 { |
| 45 | + t.Fatalf("mutating the returned instance profile leaked into internal state: internal Roles len = %d, want 0", got) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// TestIAMAPIGetInstanceProfileReturnsCopy ensures GetInstanceProfile returns an object independent from the internally |
| 50 | +// stored one. |
| 51 | +func TestIAMAPIGetInstanceProfileReturnsCopy(t *testing.T) { |
| 52 | + api := NewIAMAPI() |
| 53 | + const name = "test-profile" |
| 54 | + |
| 55 | + if _, err := api.CreateInstanceProfile(context.Background(), &iam.CreateInstanceProfileInput{ |
| 56 | + InstanceProfileName: aws.String(name), |
| 57 | + }); err != nil { |
| 58 | + t.Fatalf("CreateInstanceProfile: unexpected error: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + out, err := api.GetInstanceProfile(context.Background(), &iam.GetInstanceProfileInput{ |
| 62 | + InstanceProfileName: aws.String(name), |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("GetInstanceProfile: unexpected error: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + out.InstanceProfile.Roles = append(out.InstanceProfile.Roles, iamtypes.Role{RoleName: aws.String("some-role")}) |
| 69 | + |
| 70 | + if got := len(api.InstanceProfiles[name].Roles); got != 0 { |
| 71 | + t.Fatalf("mutating the returned instance profile leaked into internal state: internal Roles len = %d, want 0", got) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestIAMAPIConcurrentResetAndAccess exercises Reset concurrently with the locked API methods. Before the fix, Reset |
| 76 | +// reassigned the InstanceProfiles/Roles maps without holding the mutex, racing with (and potentially panicking against) |
| 77 | +// the concurrent map access performed under lock by the other methods. Run with -race to observe the data race. |
| 78 | +func TestIAMAPIConcurrentResetAndAccess(t *testing.T) { |
| 79 | + api := NewIAMAPI() |
| 80 | + ctx := context.Background() |
| 81 | + const name = "p" |
| 82 | + |
| 83 | + var wg sync.WaitGroup |
| 84 | + for range 200 { |
| 85 | + wg.Add(5) |
| 86 | + go func() { defer wg.Done(); api.Reset() }() |
| 87 | + go func() { |
| 88 | + defer wg.Done() |
| 89 | + _, _ = api.CreateInstanceProfile(ctx, &iam.CreateInstanceProfileInput{InstanceProfileName: aws.String(name)}) |
| 90 | + }() |
| 91 | + go func() { |
| 92 | + defer wg.Done() |
| 93 | + _, _ = api.GetInstanceProfile(ctx, &iam.GetInstanceProfileInput{InstanceProfileName: aws.String(name)}) |
| 94 | + }() |
| 95 | + go func() { |
| 96 | + defer wg.Done() |
| 97 | + _, _ = api.AddRoleToInstanceProfile(ctx, &iam.AddRoleToInstanceProfileInput{ |
| 98 | + InstanceProfileName: aws.String(name), |
| 99 | + RoleName: aws.String("r"), |
| 100 | + }) |
| 101 | + }() |
| 102 | + go func() { |
| 103 | + defer wg.Done() |
| 104 | + _, _ = api.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{PathPrefix: aws.String("/")}) |
| 105 | + }() |
| 106 | + } |
| 107 | + wg.Wait() |
| 108 | +} |
0 commit comments