github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/depsfile/locks_test.go (about)

     1  package depsfile
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/internal/addrs"
     7  	"github.com/hashicorp/terraform/internal/getproviders"
     8  )
     9  
    10  func TestLocksEqual(t *testing.T) {
    11  	boopProvider := addrs.NewDefaultProvider("boop")
    12  	v2 := getproviders.MustParseVersion("2.0.0")
    13  	v2LocalBuild := getproviders.MustParseVersion("2.0.0+awesomecorp.1")
    14  	v2GtConstraints := getproviders.MustParseVersionConstraints(">= 2.0.0")
    15  	v2EqConstraints := getproviders.MustParseVersionConstraints("2.0.0")
    16  	hash1 := getproviders.HashScheme("test").New("1")
    17  	hash2 := getproviders.HashScheme("test").New("2")
    18  	hash3 := getproviders.HashScheme("test").New("3")
    19  
    20  	equalBothWays := func(t *testing.T, a, b *Locks) {
    21  		t.Helper()
    22  		if !a.Equal(b) {
    23  			t.Errorf("a should be equal to b")
    24  		}
    25  		if !b.Equal(a) {
    26  			t.Errorf("b should be equal to a")
    27  		}
    28  	}
    29  	nonEqualBothWays := func(t *testing.T, a, b *Locks) {
    30  		t.Helper()
    31  		if a.Equal(b) {
    32  			t.Errorf("a should be equal to b")
    33  		}
    34  		if b.Equal(a) {
    35  			t.Errorf("b should be equal to a")
    36  		}
    37  	}
    38  
    39  	t.Run("both empty", func(t *testing.T) {
    40  		a := NewLocks()
    41  		b := NewLocks()
    42  		equalBothWays(t, a, b)
    43  	})
    44  	t.Run("an extra provider lock", func(t *testing.T) {
    45  		a := NewLocks()
    46  		b := NewLocks()
    47  		b.SetProvider(boopProvider, v2, v2GtConstraints, nil)
    48  		nonEqualBothWays(t, a, b)
    49  	})
    50  	t.Run("both have boop provider with same version", func(t *testing.T) {
    51  		a := NewLocks()
    52  		b := NewLocks()
    53  		// Note: the constraints are not part of the definition of "Equal", so they can differ
    54  		a.SetProvider(boopProvider, v2, v2GtConstraints, nil)
    55  		b.SetProvider(boopProvider, v2, v2EqConstraints, nil)
    56  		equalBothWays(t, a, b)
    57  	})
    58  	t.Run("both have boop provider with different versions", func(t *testing.T) {
    59  		a := NewLocks()
    60  		b := NewLocks()
    61  		a.SetProvider(boopProvider, v2, v2EqConstraints, nil)
    62  		b.SetProvider(boopProvider, v2LocalBuild, v2EqConstraints, nil)
    63  		nonEqualBothWays(t, a, b)
    64  	})
    65  	t.Run("both have boop provider with same version and same hashes", func(t *testing.T) {
    66  		a := NewLocks()
    67  		b := NewLocks()
    68  		hashes := []getproviders.Hash{hash1, hash2, hash3}
    69  		a.SetProvider(boopProvider, v2, v2EqConstraints, hashes)
    70  		b.SetProvider(boopProvider, v2, v2EqConstraints, hashes)
    71  		equalBothWays(t, a, b)
    72  	})
    73  	t.Run("both have boop provider with same version but different hashes", func(t *testing.T) {
    74  		a := NewLocks()
    75  		b := NewLocks()
    76  		hashesA := []getproviders.Hash{hash1, hash2}
    77  		hashesB := []getproviders.Hash{hash1, hash3}
    78  		a.SetProvider(boopProvider, v2, v2EqConstraints, hashesA)
    79  		b.SetProvider(boopProvider, v2, v2EqConstraints, hashesB)
    80  		nonEqualBothWays(t, a, b)
    81  	})
    82  }
    83  
    84  func TestLocksEqualProviderAddress(t *testing.T) {
    85  	boopProvider := addrs.NewDefaultProvider("boop")
    86  	v2 := getproviders.MustParseVersion("2.0.0")
    87  	v2LocalBuild := getproviders.MustParseVersion("2.0.0+awesomecorp.1")
    88  	v2GtConstraints := getproviders.MustParseVersionConstraints(">= 2.0.0")
    89  	v2EqConstraints := getproviders.MustParseVersionConstraints("2.0.0")
    90  	hash1 := getproviders.HashScheme("test").New("1")
    91  	hash2 := getproviders.HashScheme("test").New("2")
    92  	hash3 := getproviders.HashScheme("test").New("3")
    93  
    94  	equalProviderAddressBothWays := func(t *testing.T, a, b *Locks) {
    95  		t.Helper()
    96  		if !a.EqualProviderAddress(b) {
    97  			t.Errorf("a should be equal to b")
    98  		}
    99  		if !b.EqualProviderAddress(a) {
   100  			t.Errorf("b should be equal to a")
   101  		}
   102  	}
   103  	nonEqualProviderAddressBothWays := func(t *testing.T, a, b *Locks) {
   104  		t.Helper()
   105  		if a.EqualProviderAddress(b) {
   106  			t.Errorf("a should be equal to b")
   107  		}
   108  		if b.EqualProviderAddress(a) {
   109  			t.Errorf("b should be equal to a")
   110  		}
   111  	}
   112  
   113  	t.Run("both empty", func(t *testing.T) {
   114  		a := NewLocks()
   115  		b := NewLocks()
   116  		equalProviderAddressBothWays(t, a, b)
   117  	})
   118  	t.Run("an extra provider lock", func(t *testing.T) {
   119  		a := NewLocks()
   120  		b := NewLocks()
   121  		b.SetProvider(boopProvider, v2, v2GtConstraints, nil)
   122  		nonEqualProviderAddressBothWays(t, a, b)
   123  	})
   124  	t.Run("both have boop provider with different versions", func(t *testing.T) {
   125  		a := NewLocks()
   126  		b := NewLocks()
   127  		a.SetProvider(boopProvider, v2, v2EqConstraints, nil)
   128  		b.SetProvider(boopProvider, v2LocalBuild, v2EqConstraints, nil)
   129  		equalProviderAddressBothWays(t, a, b)
   130  	})
   131  	t.Run("both have boop provider with same version but different hashes", func(t *testing.T) {
   132  		a := NewLocks()
   133  		b := NewLocks()
   134  		hashesA := []getproviders.Hash{hash1, hash2}
   135  		hashesB := []getproviders.Hash{hash1, hash3}
   136  		a.SetProvider(boopProvider, v2, v2EqConstraints, hashesA)
   137  		b.SetProvider(boopProvider, v2, v2EqConstraints, hashesB)
   138  		equalProviderAddressBothWays(t, a, b)
   139  	})
   140  }