Skip to content

Commit

Permalink
Two tests for .NET aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhailshilkov committed Nov 11, 2019
1 parent 1a6897b commit 07c0f86
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 5 deletions.
2 changes: 1 addition & 1 deletion sdk/dotnet/Pulumi/Core/Urn.cs
Expand Up @@ -36,7 +36,7 @@ internal static class Urn
}
else
{
parentPrefix = Output.Create($"urn:pulumi:{stack ?? Deployment.Instance.StackName}::{project ?? Deployment.Instance.ProjectName}::");
parentPrefix = Output.Format($"urn:pulumi:{stack ?? Deployment.Instance.StackName}::{project ?? Deployment.Instance.ProjectName}::");
}

return Output.Format($"{parentPrefix}{type}::{name}");
Expand Down
3 changes: 2 additions & 1 deletion sdk/dotnet/Pulumi/Deployment/Deployment_Prepare.cs
@@ -1,5 +1,6 @@
// Copyright 2016-2019, Pulumi Corporation

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand Down Expand Up @@ -75,7 +76,7 @@ public partial class Deployment
foreach (var alias in res._aliases)
{
var aliasVal = await alias.ToOutput().GetValueAsync().ConfigureAwait(false);
if (!uniqueAliases.Add(aliasVal))
if (uniqueAliases.Add(aliasVal))
{
aliases.Add(aliasVal);
}
Expand Down
25 changes: 22 additions & 3 deletions tests/integration/aliases/aliases_test.go
Expand Up @@ -13,9 +13,9 @@ import (
var dirs = []string{
"rename",
"adopt_into_component",
"rename_component_and_child",
"retype_component",
"rename_component",
// "rename_component_and_child",
// "retype_component",
// "rename_component",
}

// TestNodejsAliases tests a case where a resource's name changes but it provides an `alias`
Expand Down Expand Up @@ -61,3 +61,22 @@ func TestPythonAliases(t *testing.T) {
})
}
}

func TestDotNetAliases(t *testing.T) {
for _, dir := range dirs {
d := path.Join("dotnet", dir)
t.Run(d, func(t *testing.T) {
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: path.Join(d, "step1"),
Quick: true,
EditDirs: []integration.EditDir{
{
Dir: path.Join(d, "step2"),
Additive: true,
ExpectNoChanges: true,
},
},
})
})
}
}
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

</Project>
@@ -0,0 +1,72 @@
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.

using System.Threading.Tasks;
using Pulumi;

class Resource : ComponentResource
{
public Resource(string name, ComponentResourceOptions options = null)
: base("my:module:Resource", name, options)
{
}
}

// Scenario #2 - adopt a resource into a component
class Component : ComponentResource
{
public Component(string name, ComponentResourceOptions options = null)
: base("my:module:Component", name, options)
{
}
}

// Scenario 3: adopt this resource into a new parent.
class Component2 : ComponentResource
{
public Component2(string name, ComponentResourceOptions options = null)
: base("my:module:Component2", name, options)
{
}
}

// Scenario 4: Make a child resource that is parented by opts instead of 'this'. Fix
// in the next step to be parented by this. Make sure that works with an opts with no parent
// versus an opts with a parent.

class Component3 : ComponentResource
{
public Component3(string name, ComponentResourceOptions options = null)
: base("my:module:Component3", name, options)
{
new Component2(name + "-child", options);
}
}

// Scenario 5: Allow multiple aliases to the same resource.
class Component4 : ComponentResource
{
public Component4(string name, ComponentResourceOptions options = null)
: base("my:module:Component4", name, options)
{
}
}


class Program
{
static Task<int> Main(string[] args)
{
return Deployment.RunAsync(() =>
{
var res2 = new Resource("res2");
var comp2 = new Component("comp2");
new Component2("unparented");
new Component3("parentedbystack");
new Component3("parentedbycomponent", new ComponentResourceOptions { Parent = comp2 });
new Component4("duplicateAliases", new ComponentResourceOptions { Parent = comp2 });
});
}
}
@@ -0,0 +1,3 @@
name: aliases_adopt_into_component
description: A program that replaces a resource with a new name and alias.
runtime: dotnet
114 changes: 114 additions & 0 deletions tests/integration/aliases/dotnet/adopt_into_component/step2/Program.cs
@@ -0,0 +1,114 @@
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.

using System;
using System.Threading.Tasks;
using Pulumi;

class Resource : ComponentResource
{
public Resource(string name, ComponentResourceOptions options = null)
: base("my:module:Resource", name, options)
{
}
}

// Scenario #2 - adopt a resource into a component. The component author is the same as the component user, and changes
// the component to be able to adopt the resource that was previously defined separately...
class Component : ComponentResource
{
private Resource resource;

public Component(string name, ComponentResourceOptions options = null)
: base("my:module:Component", name, options)
{
// The resource creation was moved from top level to inside the component.
this.resource = new Resource($"{name}-child",
new ComponentResourceOptions
{
// With a new parent
Parent = this,
// But with an alias provided based on knowing where the resource existing before - in this case at top
// level. We use an absolute URN instead of a relative `Alias` because we are referencing a fixed resource
// that was in some arbitrary other location in the hierarchy prior to being adopted into this component.
Aliases = { new Alias { Urn = "my:module:Resource::res2" } },
});
}
}

// Scenario 3: adopt this resource into a new parent.
class Component2 : ComponentResource
{
public Component2(string name, ComponentResourceOptions options = null)
: base("my:module:Component2", name, options)
{
}
}


// Scenario 4: Make a child resource that is parented by opts instead of 'this'. Fix
// in the next step to be parented by this. Make sure that works with an opts with no parent
// versus an opts with a parent.

class Component3 : ComponentResource
{
public Component3(string name, ComponentResourceOptions options = null)
: base("my:module:Component3", name, options)
{
new Component2(name + "-child",
new ComponentResourceOptions
{
Aliases = { new Alias { Parent = options?.Parent, NoParent = options?.Parent == null } },
Parent = this
});
}
}

// Scenario 5: Allow multiple aliases to the same resource.
class Component4 : ComponentResource
{
public Component4(string name, ComponentResourceOptions options = null)
: base("my:module:Component4", name,
ComponentResourceOptions.Merge(
new ComponentResourceOptions
{
Aliases =
{
new Alias { NoParent = true },
new Alias { NoParent = true }
},
},
options))
{
}
}

class Program
{
static Task<int> Main(string[] args)
{
return Deployment.RunAsync(() =>
{
// The creation of the component is unchanged.
var comp2 = new Component("comp2");
// validate that "parent: undefined" means "i didn't have a parent previously"
new Component2("unparented",
new ComponentResourceOptions
{
Aliases = { new Alias { NoParent = true } },
Parent = comp2,
});
new Component3("parentedbystack");
new Component3("parentedbycomponent", new ComponentResourceOptions { Parent = comp2 });
new Component4("duplicateAliases", new ComponentResourceOptions { Parent = comp2 });
});
}
}




//new Component4("duplicateAliases", { parent: comp2 });
8 changes: 8 additions & 0 deletions tests/integration/aliases/dotnet/rename/step1/Aliases.csproj
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>

</Project>
24 changes: 24 additions & 0 deletions tests/integration/aliases/dotnet/rename/step1/Program.cs
@@ -0,0 +1,24 @@
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.

using System.Threading.Tasks;
using Pulumi;

class Resource : ComponentResource
{
public Resource(string name, ComponentResourceOptions options = null)
: base("my:module:Resource", name, options)
{
}
}

class Program
{
static Task<int> Main(string[] args)
{
return Deployment.RunAsync(() =>
{
// Scenario #1 - rename a resource
var res1 = new Resource("res1");
});
}
}
3 changes: 3 additions & 0 deletions tests/integration/aliases/dotnet/rename/step1/Pulumi.yaml
@@ -0,0 +1,3 @@
name: aliases_rename
description: A program that replaces a resource with a new name and alias.
runtime: dotnet
29 changes: 29 additions & 0 deletions tests/integration/aliases/dotnet/rename/step2/Program.cs
@@ -0,0 +1,29 @@
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.

using System.Threading.Tasks;
using Pulumi;

class Resource : ComponentResource
{
public Resource(string name, ComponentResourceOptions options = null)
: base("my:module:Resource", name, options)
{
}
}

class Program
{
static Task<int> Main(string[] args)
{
return Deployment.RunAsync(() =>
{
// Scenario #1 - rename a resource
// This resource was previously named `res1`, we'll alias to the old name.
var res1 = new Resource("newres1",
new ComponentResourceOptions
{
Aliases = { new Alias { Name = "res1" } },
});
});
}
}

0 comments on commit 07c0f86

Please sign in to comment.