Skip to content

Commit a194e69

Browse files
committed
fix(secretmanager): Typo/Formatting and flakiness for the existing global samples
1 parent dbd0da1 commit a194e69

10 files changed

+72
-33
lines changed

secretmanager/api/SecretManager.Samples.Tests/AccessSecretVersionTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ public AccessSecretVersionTests(SecretManagerFixture fixture)
3232
[Fact]
3333
public void AddsSecretVersions()
3434
{
35+
// Get the SecretVersionName.
3536
SecretVersionName secretVersionName = _fixture.SecretVersion.SecretVersionName;
37+
38+
// Run the code sample.
3639
string result = _sample.AccessSecretVersion(
3740
projectId: secretVersionName.ProjectId, secretId: secretVersionName.SecretId, secretVersionId: secretVersionName.SecretVersionId);
41+
42+
// Assert that expected result is observed.
3843
Assert.Equal("my super secret data", result);
3944
}
4045
}

secretmanager/api/SecretManager.Samples.Tests/AddSecretVersionTests.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,26 @@ public AddSecretVersionTests(SecretManagerFixture fixture)
3232
[Fact]
3333
public void AddsSecretVersions()
3434
{
35+
// Get the payload data.
3536
string data = "my secret data";
36-
SecretName secretName = _fixture.Secret.SecretName;
37+
38+
// Create the secret resource.
39+
Secret secret = _fixture.CreateSecret(_fixture.RandomId());
40+
41+
// Get the SecretName.
42+
SecretName secretName = secret.SecretName;
43+
44+
// Run the code sample.
3745
SecretVersion secretVersion = _sample.AddSecretVersion(
3846
projectId: secretName.ProjectId, secretId: secretName.SecretId,
3947
data: data);
4048

41-
SecretManagerServiceClient client = SecretManagerServiceClient.Create();
49+
// Assert expected result is observed.
50+
SecretManagerServiceClient client = _fixture.Client;
4251
AccessSecretVersionResponse result = client.AccessSecretVersion(secretVersion.SecretVersionName);
4352
Assert.Equal(data, result.Payload.Data.ToStringUtf8());
53+
54+
// Cleanup the created resource.
55+
_fixture.DeleteSecret(secretName);
4456
}
4557
}

secretmanager/api/SecretManager.Samples.Tests/CreateSecretTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
using Xunit;
1817
using Google.Cloud.SecretManager.V1;
18+
using Xunit;
1919

2020
[Collection(nameof(SecretManagerFixture))]
2121
public class CreateSecretTests
@@ -32,9 +32,17 @@ public CreateSecretTests(SecretManagerFixture fixture)
3232
[Fact]
3333
public void CreatesSecrets()
3434
{
35-
SecretName secretName = _fixture.SecretToCreateName;
35+
// Get the SecretName.
36+
SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId());
37+
38+
// Run the code sample.
3639
Secret result = _sample.CreateSecret(
3740
projectId: secretName.ProjectId, secretId: secretName.SecretId);
41+
42+
// Assert expected result is observed.
3843
Assert.Equal(result.SecretName.SecretId, secretName.SecretId);
44+
45+
// Cleanup the created resource.
46+
_fixture.DeleteSecret(secretName);
3947
}
4048
}

secretmanager/api/SecretManager.Samples.Tests/CreateUserManagedReplicationSecretTests.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
using Xunit;
1817
using Google.Cloud.SecretManager.V1;
18+
using Xunit;
1919

2020
[Collection(nameof(SecretManagerFixture))]
2121
public class CreateUserManagedReplicationSecretTests
@@ -32,13 +32,20 @@ public CreateUserManagedReplicationSecretTests(SecretManagerFixture fixture)
3232
[Fact]
3333
public void CreatesUmmrSecrets()
3434
{
35-
SecretName secretName = _fixture.UserManagedReplicationSecretName;
36-
string[] locations = {"us-east1", "us-east4", "us-west1"};
35+
// Get the SecretName.
36+
SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId());
37+
38+
// Set the locations list for replications.
39+
string[] locations = { "us-east1", "us-east4", "us-west1" };
40+
41+
// Run the code sample.
3742
Secret result = _sample.CreateUserManagedReplicationSecret(
3843
projectId: secretName.ProjectId, secretId: secretName.SecretId, locations: locations);
3944
Assert.Equal(result.SecretName.SecretId, secretName.SecretId);
4045
Assert.Contains(result.Replication.UserManaged.Replicas, r => r.Location == "us-east1");
4146
Assert.Contains(result.Replication.UserManaged.Replicas, r => r.Location == "us-east4");
4247
Assert.Contains(result.Replication.UserManaged.Replicas, r => r.Location == "us-west1");
48+
49+
_fixture.DeleteSecret(secretName);
4350
}
4451
}

secretmanager/api/SecretManager.Samples.Tests/DeleteSecretTests.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
using Xunit;
1817
using Google.Cloud.SecretManager.V1;
18+
using Xunit;
1919

2020
[Collection(nameof(SecretManagerFixture))]
2121
public class DeleteSecretTests
@@ -32,10 +32,17 @@ public DeleteSecretTests(SecretManagerFixture fixture)
3232
[Fact]
3333
public void DeletesSecrets()
3434
{
35-
SecretName secretName = _fixture.SecretToDelete.SecretName;
35+
// Create the secret.
36+
Secret secret = _fixture.CreateSecret(_fixture.RandomId());
37+
38+
// Get the secretName from the created secret.
39+
SecretName secretName = secret.SecretName;
40+
41+
// Call the code sample function.
3642
_sample.DeleteSecret(projectId: secretName.ProjectId, secretId: secretName.SecretId);
3743

38-
SecretManagerServiceClient client = SecretManagerServiceClient.Create();
44+
// Assert expected behavior is seen.
45+
SecretManagerServiceClient client = _fixture.Client;
3946
Assert.Throws<Grpc.Core.RpcException>(() => client.GetSecret(secretName));
4047
}
4148
}

secretmanager/api/SecretManager.Samples.Tests/EditSecretAnnotationsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void EditSecretAnnotations()
4444
string oldAnnotationKey = _fixture.AnnotationKey;
4545
string oldAnnotationValue = _fixture.AnnotationValue;
4646

47-
// Create the secret with the specificed fields.
47+
// Call the code sample function.
4848
Secret result = _sample.EditSecretAnnotations(
4949
projectId: secretName.ProjectId, secretId: secretName.SecretId, annotationKey: newAnnotationKey, annotationValue: newAnnotationValue);
5050

secretmanager/api/SecretManager.Samples.Tests/QuickstartTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
using Xunit;
1817
using Google.Cloud.SecretManager.V1;
18+
using Xunit;
1919

2020
[Collection(nameof(SecretManagerFixture))]
2121
public class QuickstartTests
@@ -32,7 +32,10 @@ public QuickstartTests(SecretManagerFixture fixture)
3232
[Fact]
3333
public void Runs()
3434
{
35-
SecretName secretName = _fixture.SecretForQuickstartName;
35+
SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId());
36+
3637
_sample.Quickstart(projectId: secretName.ProjectId, secretId: secretName.SecretId);
38+
39+
_fixture.DeleteSecret(secretName);
3740
}
3841
}

secretmanager/api/SecretManager.Samples.Tests/SecretManagerFixture.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public class SecretManagerFixture : IDisposable, ICollectionFixture<SecretManage
2828
public string ProjectId { get; }
2929
public ProjectName ProjectName { get; }
3030
public Secret Secret { get; }
31-
public Secret SecretToDelete { get; }
32-
public Secret SecretWithVersions { get; }
33-
public SecretName SecretForQuickstartName { get; }
34-
public SecretName SecretToCreateName { get; }
35-
public SecretName UserManagedReplicationSecretName { get; }
3631
public SecretVersion SecretVersion { get; }
3732

3833
public string AnnotationKey { get; }
@@ -64,23 +59,12 @@ public SecretManagerFixture()
6459
LabelValue = "my-label-value";
6560

6661
Secret = CreateSecret(RandomId());
67-
SecretToDelete = CreateSecret(RandomId());
68-
SecretWithVersions = CreateSecret(RandomId());
69-
SecretForQuickstartName = new SecretName(ProjectId, RandomId());
70-
SecretToCreateName = new SecretName(ProjectId, RandomId());
71-
UserManagedReplicationSecretName = new SecretName(ProjectId, RandomId());
72-
73-
SecretVersion = AddSecretVersion(SecretWithVersions);
62+
SecretVersion = AddSecretVersion(Secret);
7463
}
7564

7665
public void Dispose()
7766
{
7867
DeleteSecret(Secret.SecretName);
79-
DeleteSecret(SecretForQuickstartName);
80-
DeleteSecret(SecretToCreateName);
81-
DeleteSecret(UserManagedReplicationSecretName);
82-
DeleteSecret(SecretToDelete.SecretName);
83-
DeleteSecret(SecretWithVersions.SecretName);
8468
}
8569

8670
public String RandomId()

secretmanager/api/SecretManager.Samples.Tests/UpdateSecretTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ public UpdateSecretTests(SecretManagerFixture fixture)
3232
[Fact]
3333
public void UpdatesSecrets()
3434
{
35-
SecretName secretName = _fixture.Secret.SecretName;
35+
// Get the secret name.
36+
SecretName secretName = _fixture.CreateSecret(_fixture.RandomId()).SecretName;
37+
38+
// Run the code sample.
3639
Secret result = _sample.UpdateSecret(projectId: secretName.ProjectId, secretId: secretName.SecretId);
40+
41+
// Verify the result.
3742
Assert.Equal("rocks", result.Labels["secretmanager"]);
43+
44+
// Cleanup the created resource.
45+
_fixture.DeleteSecret(secretName);
3846
}
3947
}

secretmanager/api/SecretManager.Samples.Tests/UpdateSecretWithAliasTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
using Xunit;
1817
using Google.Cloud.SecretManager.V1;
18+
using Xunit;
1919

2020
[Collection(nameof(SecretManagerFixture))]
2121
public class UpdateSecretWithAliasTests
@@ -32,8 +32,13 @@ public UpdateSecretWithAliasTests(SecretManagerFixture fixture)
3232
[Fact]
3333
public void UpdatesSecrets()
3434
{
35-
SecretName secretName = _fixture.SecretWithVersions.SecretName;
35+
// Get the SecretName.
36+
SecretName secretName = _fixture.Secret.SecretName;
37+
38+
// Run the code sample.
3639
Secret result = _sample.UpdateSecret(projectId: secretName.ProjectId, secretId: secretName.SecretId);
40+
41+
// Assert that the expected behaviour is seen.
3742
Assert.Equal(1, result.VersionAliases["test"]);
3843
}
3944
}

0 commit comments

Comments
 (0)