github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/allocrunner/taskrunner/getter/util_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/go-getter"
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/hashicorp/nomad/client/testutil"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  	"github.com/shoenig/test/must"
    12  )
    13  
    14  func TestUtil_getURL(t *testing.T) {
    15  	ci.Parallel(t)
    16  
    17  	cases := []struct {
    18  		name     string
    19  		artifact *structs.TaskArtifact
    20  		expURL   string
    21  		expErr   *Error
    22  	}{{
    23  		name:     "basic http",
    24  		artifact: &structs.TaskArtifact{GetterSource: "example.com"},
    25  		expURL:   "example.com",
    26  		expErr:   nil,
    27  	}, {
    28  		name:     "bad url",
    29  		artifact: &structs.TaskArtifact{GetterSource: "::example.com"},
    30  		expURL:   "",
    31  		expErr: &Error{
    32  			URL:         "::example.com",
    33  			Err:         errors.New(`failed to parse source URL "::example.com": parse "::example.com": missing protocol scheme`),
    34  			Recoverable: false,
    35  		},
    36  	}, {
    37  		name: "option",
    38  		artifact: &structs.TaskArtifact{
    39  			GetterSource:  "git::github.com/hashicorp/nomad",
    40  			GetterOptions: map[string]string{"sshkey": "abc123"},
    41  		},
    42  		expURL: "git::github.com/hashicorp/nomad?sshkey=abc123",
    43  		expErr: nil,
    44  	}, {
    45  		name: "github case",
    46  		artifact: &structs.TaskArtifact{
    47  			GetterSource:  "git@github.com:hashicorp/nomad.git",
    48  			GetterOptions: map[string]string{"sshkey": "abc123"},
    49  		},
    50  		expURL: "git@github.com:hashicorp/nomad.git?sshkey=abc123",
    51  		expErr: nil,
    52  	}}
    53  
    54  	env := noopTaskEnv("/path/to/task")
    55  
    56  	for _, tc := range cases {
    57  		t.Run(tc.name, func(t *testing.T) {
    58  			result, err := getURL(env, tc.artifact)
    59  			err2, _ := err.(*Error)
    60  			must.Equal(t, tc.expErr, err2)
    61  			must.Eq(t, tc.expURL, result)
    62  		})
    63  	}
    64  }
    65  
    66  func TestUtil_getDestination(t *testing.T) {
    67  	ci.Parallel(t)
    68  
    69  	env := noopTaskEnv("/path/to/task")
    70  	t.Run("ok", func(t *testing.T) {
    71  		result, err := getDestination(env, &structs.TaskArtifact{
    72  			RelativeDest: "local/downloads",
    73  		})
    74  		must.NoError(t, err)
    75  		must.Eq(t, "/path/to/task/local/downloads", result)
    76  	})
    77  
    78  	t.Run("escapes", func(t *testing.T) {
    79  		result, err := getDestination(env, &structs.TaskArtifact{
    80  			RelativeDest: "../../../../../../../etc",
    81  		})
    82  		must.EqError(t, err, "artifact destination path escapes alloc directory")
    83  		must.Eq(t, "", result)
    84  	})
    85  }
    86  
    87  func TestUtil_getMode(t *testing.T) {
    88  	ci.Parallel(t)
    89  
    90  	cases := []struct {
    91  		mode string
    92  		exp  getter.ClientMode
    93  	}{
    94  		{mode: structs.GetterModeFile, exp: getter.ClientModeFile},
    95  		{mode: structs.GetterModeDir, exp: getter.ClientModeDir},
    96  		{mode: structs.GetterModeAny, exp: getter.ClientModeAny},
    97  	}
    98  
    99  	for _, tc := range cases {
   100  		t.Run(tc.mode, func(t *testing.T) {
   101  			artifact := &structs.TaskArtifact{GetterMode: tc.mode}
   102  			result := getMode(artifact)
   103  			must.Eq(t, tc.exp, result)
   104  		})
   105  	}
   106  }
   107  
   108  func TestUtil_getHeaders(t *testing.T) {
   109  	ci.Parallel(t)
   110  
   111  	env := upTaskEnv("/path/to/task")
   112  
   113  	t.Run("empty", func(t *testing.T) {
   114  		result := getHeaders(env, &structs.TaskArtifact{
   115  			GetterHeaders: nil,
   116  		})
   117  		must.Nil(t, result)
   118  	})
   119  
   120  	t.Run("replacments", func(t *testing.T) {
   121  		result := getHeaders(env, &structs.TaskArtifact{
   122  			GetterHeaders: map[string]string{
   123  				"color":  "red",
   124  				"number": "six",
   125  			},
   126  		})
   127  		must.MapEq(t, map[string][]string{
   128  			"Color":  {"RED"},
   129  			"Number": {"SIX"},
   130  		}, result)
   131  	})
   132  }
   133  
   134  func TestUtil_getTaskDir(t *testing.T) {
   135  	ci.Parallel(t)
   136  
   137  	env := noopTaskEnv("/path/to/task")
   138  	result := getTaskDir(env)
   139  	must.Eq(t, "/path/to/task", result)
   140  }
   141  
   142  func TestUtil_environment(t *testing.T) {
   143  	// not parallel
   144  	testutil.RequireLinux(t)
   145  
   146  	t.Run("default", func(t *testing.T) {
   147  		result := environment("/a/b/c", "")
   148  		must.Eq(t, []string{
   149  			"PATH=/usr/local/bin:/usr/bin:/bin",
   150  			"TMPDIR=/a/b/c/tmp",
   151  		}, result)
   152  	})
   153  
   154  	t.Run("append", func(t *testing.T) {
   155  		t.Setenv("ONE", "1")
   156  		t.Setenv("TWO", "2")
   157  		result := environment("/a/b/c", "ONE,TWO")
   158  		must.Eq(t, []string{
   159  			"ONE=1",
   160  			"PATH=/usr/local/bin:/usr/bin:/bin",
   161  			"TMPDIR=/a/b/c/tmp",
   162  			"TWO=2",
   163  		}, result)
   164  	})
   165  
   166  	t.Run("override", func(t *testing.T) {
   167  		t.Setenv("PATH", "/opt/bin")
   168  		t.Setenv("TMPDIR", "/scratch")
   169  		result := environment("/a/b/c", "PATH,TMPDIR")
   170  		must.Eq(t, []string{
   171  			"PATH=/opt/bin",
   172  			"TMPDIR=/scratch",
   173  		}, result)
   174  	})
   175  
   176  	t.Run("missing", func(t *testing.T) {
   177  		result := environment("/a/b/c", "DOES_NOT_EXIST")
   178  		must.Eq(t, []string{
   179  			"DOES_NOT_EXIST=",
   180  			"PATH=/usr/local/bin:/usr/bin:/bin",
   181  			"TMPDIR=/a/b/c/tmp",
   182  		}, result)
   183  	})
   184  }