github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/resource/deploy/providers/reference_test.go (about)

     1  // Copyright 2016-2018, Pulumi Corporation.
     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 providers
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  
    22  	"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
    23  	"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
    24  )
    25  
    26  func TestRoundTripProviderType(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	pkg := tokens.Package("abcd")
    30  
    31  	assert.True(t, IsProviderType(MakeProviderType(pkg)))
    32  }
    33  
    34  func TestParseReferenceInvalidURN(t *testing.T) {
    35  	t.Parallel()
    36  
    37  	str := "not::a:valid:urn::id"
    38  	_, err := ParseReference(str)
    39  	assert.Error(t, err)
    40  }
    41  
    42  func TestParseReferenceInvalidModule(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	// Wrong package and module
    46  	str := string(resource.NewURN("test", "test", "", "some:invalid:type", "test")) + "::id"
    47  	ref, err := ParseReference(str)
    48  	assert.Error(t, err)
    49  	assert.Equal(t, Reference{}, ref)
    50  
    51  	// Right package, wrong module
    52  	str = string(resource.NewURN("test", "test", "", "pulumi:invalid:type", "test")) + "::id"
    53  	ref, err = ParseReference(str)
    54  	assert.Error(t, err)
    55  	assert.Equal(t, Reference{}, ref)
    56  
    57  	// Right module, wrong package
    58  	str = string(resource.NewURN("test", "test", "", "invalid:providers:type", "test")) + "::id"
    59  	ref, err = ParseReference(str)
    60  	assert.Error(t, err)
    61  	assert.Equal(t, Reference{}, ref)
    62  }
    63  
    64  func TestParseReference(t *testing.T) {
    65  	t.Parallel()
    66  
    67  	urn, id := resource.NewURN("test", "test", "", "pulumi:providers:type", "test"), resource.ID("id")
    68  	ref, err := ParseReference(string(urn) + "::" + string(id))
    69  	assert.NoError(t, err)
    70  	assert.Equal(t, urn, ref.URN())
    71  	assert.Equal(t, id, ref.ID())
    72  }
    73  
    74  func TestReferenceString(t *testing.T) {
    75  	t.Parallel()
    76  
    77  	urn, id := resource.NewURN("test", "test", "", "pulumi:providers:type", "test"), resource.ID("id")
    78  	ref := Reference{urn: urn, id: id}
    79  	assert.Equal(t, string(urn)+"::"+string(id), ref.String())
    80  }
    81  
    82  func TestRoundTripReference(t *testing.T) {
    83  	t.Parallel()
    84  
    85  	str := string(resource.NewURN("test", "test", "", "pulumi:providers:type", "test")) + "::id"
    86  	ref, err := ParseReference(str)
    87  	assert.NoError(t, err)
    88  	assert.Equal(t, str, ref.String())
    89  }