github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/ssh/executor_ssh_test.go (about)

     1  package ssh
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"gitlab.com/gitlab-org/gitlab-runner/common"
    11  	"gitlab.com/gitlab-org/gitlab-runner/executors"
    12  	sshHelpers "gitlab.com/gitlab-org/gitlab-runner/helpers/ssh"
    13  )
    14  
    15  var (
    16  	executorOptions = executors.ExecutorOptions{
    17  		SharedBuildsDir:  false,
    18  		DefaultBuildsDir: "builds",
    19  		DefaultCacheDir:  "cache",
    20  		Shell: common.ShellScriptInfo{
    21  			Shell:         "bash",
    22  			Type:          common.NormalShell,
    23  			RunnerCommand: "/usr/bin/gitlab-runner-helper",
    24  		},
    25  		ShowHostname: true,
    26  	}
    27  )
    28  
    29  const SSH_SERVER_PRIVATE_KEY = "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIJCgNhsvCiKATDBXmRYHQfXIatKKOXGrmBLEVtGZtVv7oAoGCCqGSM49\nAwEHoUQDQgAE+36GvpVV34STGaV+YU4HHXCtJjburfo8IQDVTgLRwAkoLqLIl1cO\nduKDmdmeG/n66BNH1rJUkXFfEr4OYbZH5g==\n-----END EC PRIVATE KEY-----"
    30  
    31  func TestPrepare(t *testing.T) {
    32  	runnerConfig := &common.RunnerConfig{
    33  		RunnerSettings: common.RunnerSettings{
    34  			Executor: "ssh",
    35  			SSH:      &sshHelpers.Config{User: "user", Password: "pass", Host: "127.0.0.1"},
    36  		},
    37  	}
    38  	build := &common.Build{
    39  		JobResponse: common.JobResponse{
    40  			GitInfo: common.GitInfo{
    41  				Sha: "1234567890",
    42  			},
    43  		},
    44  		Runner: &common.RunnerConfig{},
    45  	}
    46  
    47  	sshConfig := runnerConfig.RunnerSettings.SSH
    48  	server, err := NewStubServer(sshConfig.User, sshConfig.Password, []byte(SSH_SERVER_PRIVATE_KEY))
    49  	assert.NoError(t, err)
    50  
    51  	port, err := server.Start()
    52  	assert.NoError(t, err)
    53  	defer server.Stop()
    54  
    55  	sshConfig.Port = strconv.Itoa(port)
    56  
    57  	e := &executor{
    58  		AbstractExecutor: executors.AbstractExecutor{
    59  			ExecutorOptions: executorOptions,
    60  		},
    61  	}
    62  
    63  	prepareOptions := common.ExecutorPrepareOptions{
    64  		Config:  runnerConfig,
    65  		Build:   build,
    66  		Context: context.TODO(),
    67  	}
    68  
    69  	err = e.Prepare(prepareOptions)
    70  	assert.NoError(t, err)
    71  }
    72  
    73  func TestSharedEnv(t *testing.T) {
    74  	provider := common.GetExecutor("ssh")
    75  	features := &common.FeaturesInfo{}
    76  
    77  	provider.GetFeatures(features)
    78  	assert.True(t, features.Shared)
    79  }