|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | +"os" |
| 8 | +"reflect" |
| 9 | +"testing" |
| 10 | + |
| 11 | +"go.amzn.com/lambda/rapidcore/env" |
| 12 | + |
| 13 | +"github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestSimpleBootstrap(t *testing.T) { |
| 17 | +tmpFile, err := os.CreateTemp("", "oci-test-bootstrap") |
| 18 | +assert.NoError(t, err) |
| 19 | +defer os.Remove(tmpFile.Name()) |
| 20 | + |
| 21 | +// Setup single cmd candidate |
| 22 | +file := []string{tmpFile.Name(), "--arg1 s", "foo"} |
| 23 | +cmdCandidate := file |
| 24 | + |
| 25 | +// Setup working dir |
| 26 | +cwd, err := os.Getwd() |
| 27 | +assert.NoError(t, err) |
| 28 | + |
| 29 | +// Setup environment |
| 30 | +environment := env.NewEnvironment() |
| 31 | +environment.StoreRuntimeAPIEnvironmentVariable("host:port") |
| 32 | +environment.StoreEnvironmentVariablesFromInit(map[string]string{}, "", "", "", "", "", "") |
| 33 | + |
| 34 | +// Test |
| 35 | +b := NewSimpleBootstrap(cmdCandidate, cwd) |
| 36 | +bCwd, err := b.Cwd() |
| 37 | +assert.NoError(t, err) |
| 38 | +assert.Equal(t, cwd, bCwd) |
| 39 | +assert.True(t, reflect.DeepEqual(environment.RuntimeExecEnv(), b.Env(environment))) |
| 40 | + |
| 41 | +cmd, err := b.Cmd() |
| 42 | +assert.NoError(t, err) |
| 43 | +assert.Equal(t, file, cmd) |
| 44 | +} |
| 45 | + |
| 46 | +func TestSimpleBootstrapCmdNonExistingCandidate(t *testing.T) { |
| 47 | +// Setup inexistent single cmd candidate |
| 48 | +file := []string{"/foo/bar", "--arg1 s", "foo"} |
| 49 | +cmdCandidate := file |
| 50 | + |
| 51 | +// Setup working dir |
| 52 | +cwd, err := os.Getwd() |
| 53 | +assert.NoError(t, err) |
| 54 | + |
| 55 | +// Setup environment |
| 56 | +environment := env.NewEnvironment() |
| 57 | +environment.StoreRuntimeAPIEnvironmentVariable("host:port") |
| 58 | +environment.StoreEnvironmentVariablesFromInit(map[string]string{}, "", "", "", "", "", "") |
| 59 | + |
| 60 | +// Test |
| 61 | +b := NewSimpleBootstrap(cmdCandidate, cwd) |
| 62 | +bCwd, err := b.Cwd() |
| 63 | +assert.NoError(t, err) |
| 64 | +assert.Equal(t, cwd, bCwd) |
| 65 | +assert.True(t, reflect.DeepEqual(environment.RuntimeExecEnv(), b.Env(environment))) |
| 66 | + |
| 67 | +// No validations run against single candidates |
| 68 | +cmd, err := b.Cmd() |
| 69 | +assert.NoError(t, err) |
| 70 | +assert.Equal(t, file, cmd) |
| 71 | +} |
| 72 | + |
| 73 | +func TestSimpleBootstrapCmdDefaultWorkingDir(t *testing.T) { |
| 74 | +b := NewSimpleBootstrap([]string{}, "") |
| 75 | +bCwd, err := b.Cwd() |
| 76 | +assert.NoError(t, err) |
| 77 | +assert.Equal(t, "/", bCwd) |
| 78 | +} |
0 commit comments