diff options
author | Ahmad Hassan <ahassan@gitlab.com> | 2018-11-26 13:07:05 +0300 |
---|---|---|
committer | Jacob Vosmaer <jacob@gitlab.com> | 2018-11-26 13:07:05 +0300 |
commit | fcf66810aed1385bfab387a9f5fbbfecb5b344d6 (patch) | |
tree | a8833e0312d7f0d104cf744dd07d88396075a8b7 | |
parent | 34d0e23d75aa297a56b1aa52b7ce61e10cee8358 (diff) |
Add connectivity tests
-rw-r--r-- | changelogs/unreleased/test-authentication.yml | 5 | ||||
-rw-r--r-- | cmd/gitaly-ssh/auth_test.go | 96 | ||||
-rw-r--r-- | ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb | 27 | ||||
-rw-r--r-- | ruby/spec/support/helpers/integration_helper.rb | 39 |
4 files changed, 165 insertions, 2 deletions
diff --git a/changelogs/unreleased/test-authentication.yml b/changelogs/unreleased/test-authentication.yml new file mode 100644 index 000000000..c68f1092c --- /dev/null +++ b/changelogs/unreleased/test-authentication.yml @@ -0,0 +1,5 @@ +--- +title: Add connectivity tests +merge_request: 968 +author: +type: other diff --git a/cmd/gitaly-ssh/auth_test.go b/cmd/gitaly-ssh/auth_test.go new file mode 100644 index 000000000..58ff89d9a --- /dev/null +++ b/cmd/gitaly-ssh/auth_test.go @@ -0,0 +1,96 @@ +package main + +import ( + "fmt" + "net" + "os" + "os/exec" + "path" + "strconv" + "strings" + "testing" + + "github.com/golang/protobuf/jsonpb" + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb" + "gitlab.com/gitlab-org/gitaly/internal/rubyserver" + "gitlab.com/gitlab-org/gitaly/internal/server" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" + "google.golang.org/grpc" +) + +func buildGitalySSH(t *testing.T) { + // Build the test-binary that we need + os.Remove("gitaly-ssh") + testhelper.MustRunCommand(nil, nil, "go", "build", "gitlab.com/gitlab-org/gitaly/cmd/gitaly-ssh") +} + +func TestConnectivity(t *testing.T) { + buildGitalySSH(t) + testRepo := testhelper.TestRepository() + + cwd, err := os.Getwd() + require.NoError(t, err) + gitalySSHPath := path.Join(cwd, "gitaly-ssh") + + socketPath := testhelper.GetTemporaryGitalySocketFileName() + + tcpServer, tcpPort := runServer(t, server.New, "tcp", "localhost:0") + defer tcpServer.Stop() + + unixServer, _ := runServer(t, server.New, "unix", socketPath) + defer unixServer.Stop() + + testCases := []struct { + addr string + }{ + { + addr: fmt.Sprintf("tcp://localhost:%d", tcpPort), + }, + { + addr: fmt.Sprintf("unix://%s", socketPath), + }, + } + + pbMarshaler := &jsonpb.Marshaler{} + payload, err := pbMarshaler.MarshalToString(&gitalypb.SSHUploadPackRequest{ + Repository: testRepo, + }) + + require.NoError(t, err) + for _, testcase := range testCases { + cmd := exec.Command("git", "ls-remote", "git@localhost:test/test.git", "refs/heads/master") + + cmd.Env = []string{ + fmt.Sprintf("GITALY_PAYLOAD=%s", payload), + fmt.Sprintf("GITALY_ADDRESS=%s", testcase.addr), + fmt.Sprintf("PATH=.:%s", os.Getenv("PATH")), + fmt.Sprintf("GIT_SSH_COMMAND=%s upload-pack", gitalySSHPath), + } + + output, err := cmd.Output() + + require.NoError(t, err) + require.True(t, strings.HasSuffix(strings.TrimSpace(string(output)), "refs/heads/master")) + } +} + +func runServer(t *testing.T, newServer func(rubyServer *rubyserver.Server) *grpc.Server, connectionType string, addr string) (*grpc.Server, int) { + srv := newServer(nil) + + listener, err := net.Listen(connectionType, addr) + require.NoError(t, err) + + go srv.Serve(listener) + + port := 0 + if connectionType != "unix" { + addrSplit := strings.Split(listener.Addr().String(), ":") + portString := addrSplit[len(addrSplit)-1] + + port, err = strconv.Atoi(portString) + require.NoError(t, err) + } + + return srv, port +} diff --git a/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb b/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb new file mode 100644 index 000000000..dddb21c00 --- /dev/null +++ b/ruby/spec/lib/gitlab/git/remote_repository_client_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe Gitlab::Git::GitalyRemoteRepository do + include TestRepo + include IntegrationClient + + let(:repository) { gitlab_git_from_gitaly_with_gitlab_projects(new_mutable_test_repo) } + describe 'Connectivity' do + context 'tcp' do + let(:client) do + get_client("tcp://localhost:#{GitalyConfig.dynamic_port}") + end + + it 'Should connect over tcp' do + expect(client).not_to be_empty + end + end + + context 'unix' do + let(:client) { get_client("unix:#{File.join(TMP_DIR_NAME, SOCKET_PATH)}") } + + it 'Should connect over unix' do + expect(client).not_to be_empty + end + end + end +end diff --git a/ruby/spec/support/helpers/integration_helper.rb b/ruby/spec/support/helpers/integration_helper.rb index 8402ee1e4..9c717261f 100644 --- a/ruby/spec/support/helpers/integration_helper.rb +++ b/ruby/spec/support/helpers/integration_helper.rb @@ -5,15 +5,49 @@ require 'spec_helper' SOCKET_PATH = 'gitaly.socket'.freeze +module GitalyConfig + def self.dynamic_port + @dynamic_port ||= begin + sock = Socket.new(:INET, :STREAM) + sock.bind(Addrinfo.tcp('127.0.0.1', 0)) + sock.local_address.ip_port + ensure + sock.close + end + end +end + module IntegrationClient - def gitaly_stub(service) + def gitaly_stub(service, type = 'unix') klass = Gitaly.const_get(service).const_get(:Stub) - klass.new("unix:#{File.join(TMP_DIR_NAME, SOCKET_PATH)}", :this_channel_is_insecure) + addr = case type + when 'unix' + "unix:#{File.join(TMP_DIR_NAME, SOCKET_PATH)}" + when 'tcp' + "tcp://localhost:#{GitalyConfig.dynamic_port}" + end + klass.new(addr, creds) + end + + def creds + :this_channel_is_insecure end def gitaly_repo(storage, relative_path) Gitaly::Repository.new(storage_name: storage, relative_path: relative_path) end + + def get_client(addr) + servers = Base64.strict_encode64({ + default: { + address: addr, + token: 'the-secret-token' + } + }.to_json) + + call = double(metadata: { 'gitaly-servers' => servers }) + Gitlab::Git::GitalyRemoteRepository.new(repository.gitaly_repository, call) + end end def start_gitaly @@ -22,6 +56,7 @@ def start_gitaly config_toml = <<~CONFIG socket_path = "#{SOCKET_PATH}" + listen_addr = "localhost:#{GitalyConfig.dynamic_port}" bin_dir = "#{build_dir}/bin" [gitlab-shell] |