github.com/hashicorp/packer@v1.14.3/packer/plugin-getter/release/getter_test.go (about)

     1  package release
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/packer/hcl2template/addrs"
     7  	plugingetter "github.com/hashicorp/packer/packer/plugin-getter"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestInit(t *testing.T) {
    12  
    13  	tests := []struct {
    14  		name            string
    15  		entry           *plugingetter.ChecksumFileEntry
    16  		binVersion      string
    17  		protocolVersion string
    18  		os              string
    19  		arch            string
    20  		wantErr         bool
    21  	}{
    22  		{
    23  			name: "valid format parses",
    24  			entry: &plugingetter.ChecksumFileEntry{
    25  				Filename: "packer-plugin-v0.2.12_freebsd_amd64.zip",
    26  			},
    27  			binVersion:      "v0.2.12",
    28  			protocolVersion: "x5.0",
    29  			os:              "freebsd",
    30  			arch:            "amd64",
    31  			wantErr:         false,
    32  		},
    33  		{
    34  			name: "malformed filename returns error",
    35  			entry: &plugingetter.ChecksumFileEntry{
    36  				Filename: "packer-plugin-v0.2.12.zip",
    37  			},
    38  			binVersion: "v0.2.12",
    39  			wantErr:    true,
    40  		},
    41  	}
    42  
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			req := &plugingetter.Requirement{}
    46  
    47  			getter := &Getter{}
    48  			err := getter.Init(req, tt.entry)
    49  
    50  			if err != nil && !tt.wantErr {
    51  				t.Fatalf("unexpected error: %s", err)
    52  			}
    53  
    54  			if err == nil && tt.wantErr {
    55  				t.Fatal("expected error but got nil")
    56  			}
    57  
    58  			if !tt.wantErr && (tt.entry.BinVersion != "0.2.12" || tt.entry.Os != "freebsd" || tt.entry.Arch != "amd64") {
    59  				t.Fatalf("unexpected parsed values: %+v", tt.entry)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestValidate(t *testing.T) {
    66  	tests := []struct {
    67  		name        string
    68  		option      plugingetter.GetOptions
    69  		installOpts plugingetter.BinaryInstallationOptions
    70  		entry       *plugingetter.ChecksumFileEntry
    71  		version     string
    72  		wantErr     bool
    73  	}{
    74  		{
    75  			name: "invalid bin version",
    76  			installOpts: plugingetter.BinaryInstallationOptions{
    77  				OS:   "linux",
    78  				ARCH: "amd64",
    79  			},
    80  			entry: &plugingetter.ChecksumFileEntry{
    81  				BinVersion: "1.2.3",
    82  				Os:         "linux",
    83  				Arch:       "amd64",
    84  			},
    85  			version: "1.2.4",
    86  			wantErr: true,
    87  		},
    88  		{
    89  			name: "wrong OS",
    90  			installOpts: plugingetter.BinaryInstallationOptions{
    91  				OS:   "linux",
    92  				ARCH: "amd64",
    93  			},
    94  			entry: &plugingetter.ChecksumFileEntry{
    95  				BinVersion: "1.2.3",
    96  				Os:         "darwin",
    97  				Arch:       "amd64",
    98  			},
    99  			version: "1.2.3",
   100  			wantErr: true,
   101  		},
   102  		{
   103  			name: "wrong Arch",
   104  			installOpts: plugingetter.BinaryInstallationOptions{
   105  				OS:   "linux",
   106  				ARCH: "amd64",
   107  			},
   108  			entry: &plugingetter.ChecksumFileEntry{
   109  				BinVersion:  "1.2.3",
   110  				Os:          "linux",
   111  				Arch:        "arm64",
   112  				ProtVersion: "x5.0",
   113  			},
   114  			version: "1.2.3",
   115  			wantErr: true,
   116  		},
   117  	}
   118  
   119  	for _, tt := range tests {
   120  		t.Run(tt.name, func(t *testing.T) {
   121  
   122  			getter := &Getter{}
   123  			err := getter.Validate(plugingetter.GetOptions{}, tt.version, tt.installOpts, tt.entry)
   124  
   125  			if err != nil && !tt.wantErr {
   126  				t.Fatalf("unexpected error: %s", err)
   127  			}
   128  
   129  			if err == nil && tt.wantErr {
   130  				t.Fatal("expected error but got nil")
   131  			}
   132  		})
   133  	}
   134  }
   135  
   136  func TestExpectedFileName(t *testing.T) {
   137  	getter := &Getter{
   138  		APIMajor: "5",
   139  		APIMinor: "0",
   140  	}
   141  	pr := plugingetter.Requirement{
   142  		Identifier: &addrs.Plugin{
   143  			Source: "github.com/hashicorp/docker",
   144  		},
   145  	}
   146  
   147  	entry := &plugingetter.ChecksumFileEntry{
   148  		Os:   "linux",
   149  		Arch: "amd64",
   150  	}
   151  	fileName := getter.ExpectedFileName(&pr, "1.2.3", entry, "packer-plugin-docker_v1.2.3_x5.0_linux_amd64.zip")
   152  	assert.Equal(t, "packer-plugin-docker_v1.2.3_x5.0_linux_amd64.zip", fileName)
   153  }