github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/plugin/discovery/requirements_test.go (about)

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