opentofu/internal/depsfile/locks_test.go
Martin Atkins b9a93a0fe7 Move addrs/ to internal/addrs/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

141 lines
4.5 KiB
Go

package depsfile
import (
"testing"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/getproviders"
)
func TestLocksEqual(t *testing.T) {
boopProvider := addrs.NewDefaultProvider("boop")
v2 := getproviders.MustParseVersion("2.0.0")
v2LocalBuild := getproviders.MustParseVersion("2.0.0+awesomecorp.1")
v2GtConstraints := getproviders.MustParseVersionConstraints(">= 2.0.0")
v2EqConstraints := getproviders.MustParseVersionConstraints("2.0.0")
hash1 := getproviders.HashScheme("test").New("1")
hash2 := getproviders.HashScheme("test").New("2")
hash3 := getproviders.HashScheme("test").New("3")
equalBothWays := func(t *testing.T, a, b *Locks) {
t.Helper()
if !a.Equal(b) {
t.Errorf("a should be equal to b")
}
if !b.Equal(a) {
t.Errorf("b should be equal to a")
}
}
nonEqualBothWays := func(t *testing.T, a, b *Locks) {
t.Helper()
if a.Equal(b) {
t.Errorf("a should be equal to b")
}
if b.Equal(a) {
t.Errorf("b should be equal to a")
}
}
t.Run("both empty", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
equalBothWays(t, a, b)
})
t.Run("an extra provider lock", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
b.SetProvider(boopProvider, v2, v2GtConstraints, nil)
nonEqualBothWays(t, a, b)
})
t.Run("both have boop provider with same version", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
// Note: the constraints are not part of the definition of "Equal", so they can differ
a.SetProvider(boopProvider, v2, v2GtConstraints, nil)
b.SetProvider(boopProvider, v2, v2EqConstraints, nil)
equalBothWays(t, a, b)
})
t.Run("both have boop provider with different versions", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
a.SetProvider(boopProvider, v2, v2EqConstraints, nil)
b.SetProvider(boopProvider, v2LocalBuild, v2EqConstraints, nil)
nonEqualBothWays(t, a, b)
})
t.Run("both have boop provider with same version and same hashes", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
hashes := []getproviders.Hash{hash1, hash2, hash3}
a.SetProvider(boopProvider, v2, v2EqConstraints, hashes)
b.SetProvider(boopProvider, v2, v2EqConstraints, hashes)
equalBothWays(t, a, b)
})
t.Run("both have boop provider with same version but different hashes", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
hashesA := []getproviders.Hash{hash1, hash2}
hashesB := []getproviders.Hash{hash1, hash3}
a.SetProvider(boopProvider, v2, v2EqConstraints, hashesA)
b.SetProvider(boopProvider, v2, v2EqConstraints, hashesB)
nonEqualBothWays(t, a, b)
})
}
func TestLocksEqualProviderAddress(t *testing.T) {
boopProvider := addrs.NewDefaultProvider("boop")
v2 := getproviders.MustParseVersion("2.0.0")
v2LocalBuild := getproviders.MustParseVersion("2.0.0+awesomecorp.1")
v2GtConstraints := getproviders.MustParseVersionConstraints(">= 2.0.0")
v2EqConstraints := getproviders.MustParseVersionConstraints("2.0.0")
hash1 := getproviders.HashScheme("test").New("1")
hash2 := getproviders.HashScheme("test").New("2")
hash3 := getproviders.HashScheme("test").New("3")
equalProviderAddressBothWays := func(t *testing.T, a, b *Locks) {
t.Helper()
if !a.EqualProviderAddress(b) {
t.Errorf("a should be equal to b")
}
if !b.EqualProviderAddress(a) {
t.Errorf("b should be equal to a")
}
}
nonEqualProviderAddressBothWays := func(t *testing.T, a, b *Locks) {
t.Helper()
if a.EqualProviderAddress(b) {
t.Errorf("a should be equal to b")
}
if b.EqualProviderAddress(a) {
t.Errorf("b should be equal to a")
}
}
t.Run("both empty", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
equalProviderAddressBothWays(t, a, b)
})
t.Run("an extra provider lock", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
b.SetProvider(boopProvider, v2, v2GtConstraints, nil)
nonEqualProviderAddressBothWays(t, a, b)
})
t.Run("both have boop provider with different versions", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
a.SetProvider(boopProvider, v2, v2EqConstraints, nil)
b.SetProvider(boopProvider, v2LocalBuild, v2EqConstraints, nil)
equalProviderAddressBothWays(t, a, b)
})
t.Run("both have boop provider with same version but different hashes", func(t *testing.T) {
a := NewLocks()
b := NewLocks()
hashesA := []getproviders.Hash{hash1, hash2}
hashesB := []getproviders.Hash{hash1, hash3}
a.SetProvider(boopProvider, v2, v2EqConstraints, hashesA)
b.SetProvider(boopProvider, v2, v2EqConstraints, hashesB)
equalProviderAddressBothWays(t, a, b)
})
}