github.com/jhixson74/hashicorp-terraform@v0.11.12-beta1/plugin/discovery/requirements_test.go (about)

     1  package discovery
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestPluginConstraintsAllows(t *testing.T) {
     9  	tests := []struct {
    10  		Constraints *PluginConstraints
    11  		Version     string
    12  		Want        bool
    13  	}{
    14  		{
    15  			&PluginConstraints{
    16  				Versions: AllVersions,
    17  			},
    18  			"1.0.0",
    19  			true,
    20  		},
    21  		{
    22  			&PluginConstraints{
    23  				Versions: ConstraintStr(">1.0.0").MustParse(),
    24  			},
    25  			"1.0.0",
    26  			false,
    27  		},
    28  		// This is not an exhaustive test because the callees
    29  		// already have plentiful tests of their own.
    30  	}
    31  
    32  	for i, test := range tests {
    33  		t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
    34  			version := VersionStr(test.Version).MustParse()
    35  			got := test.Constraints.Allows(version)
    36  			if got != test.Want {
    37  				t.Logf("looking for %s in %#v", test.Version, test.Constraints)
    38  				t.Errorf("wrong result %#v; want %#v", got, test.Want)
    39  			}
    40  		})
    41  	}
    42  }
    43  
    44  func TestPluginConstraintsAcceptsSHA256(t *testing.T) {
    45  	mustUnhex := func(hex string) (ret []byte) {
    46  		_, err := fmt.Sscanf(hex, "%x", &ret)
    47  		if err != nil {
    48  			panic(err)
    49  		}
    50  		return ret
    51  	}
    52  
    53  	tests := []struct {
    54  		Constraints *PluginConstraints
    55  		Digest      []byte
    56  		Want        bool
    57  	}{
    58  		{
    59  			&PluginConstraints{
    60  				Versions: AllVersions,
    61  				SHA256:   mustUnhex("0123456789abcdef"),
    62  			},
    63  			mustUnhex("0123456789abcdef"),
    64  			true,
    65  		},
    66  		{
    67  			&PluginConstraints{
    68  				Versions: AllVersions,
    69  				SHA256:   mustUnhex("0123456789abcdef"),
    70  			},
    71  			mustUnhex("f00dface"),
    72  			false,
    73  		},
    74  		{
    75  			&PluginConstraints{
    76  				Versions: AllVersions,
    77  				SHA256:   nil,
    78  			},
    79  			mustUnhex("f00dface"),
    80  			true,
    81  		},
    82  	}
    83  
    84  	for i, test := range tests {
    85  		t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
    86  			got := test.Constraints.AcceptsSHA256(test.Digest)
    87  			if got != test.Want {
    88  				t.Logf("%#v.AcceptsSHA256(%#v)", test.Constraints, test.Digest)
    89  				t.Errorf("wrong result %#v; want %#v", got, test.Want)
    90  			}
    91  		})
    92  	}
    93  }