1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-02-03 13:11:48 +02:00
goreleaser/internal/client/gitlab_test.go
Steve Azzopardi b0a6bf4705
fix: panic on gitlab create release (#2473)
Background
---
In 5bcfa9042d0efd419b660a3b63c815a13d919e30 the condition was changed to
that it can support the new status code returned by GitLab. However, the
condition is now checked `resp != nil` and not `resp == nil` which causes
the condition not to trigger when the resp is nil and only returns an
error. We can reproduce this issue by running
`TestGitLabCreateReleaseUknownHost` test added in this commit.

Solution
---
If we look at other error handling
91a8abb2e6/internal/client/gitlab.go (L106)
we can see that we check if the `resp` is nil as well before continuing.

Added tests for the following use cases:
- When there is an error and no response was returned.
- When there is a 403 or 404 error so we expect to create a release
  first.
- When there is an unknown error (not 404, 403) we just bail.
2021-09-10 16:35:42 -03:00

326 lines
7.4 KiB
Go

package client
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"text/template"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestGitLabReleaseURLTemplate(t *testing.T) {
tests := []struct {
name string
downloadURL string
wantDownloadURL string
wantErr bool
}{
{
name: "default_download_url",
downloadURL: DefaultGitLabDownloadURL,
wantDownloadURL: "https://gitlab.com/owner/name/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}",
},
{
name: "download_url_template",
downloadURL: "{{ .Env.GORELEASER_TEST_GITLAB_URLS_DOWNLOAD }}",
wantDownloadURL: "https://gitlab.mycompany.com/owner/name/-/releases/{{ .Tag }}/downloads/{{ .ArtifactName }}",
},
{
name: "download_url_template_invalid_value",
downloadURL: "{{ .Env.GORELEASER_NOT_EXISTS }}",
wantErr: true,
},
{
name: "download_url_template_invalid",
downloadURL: "{{.dddddddddd",
wantErr: true,
},
}
for _, tt := range tests {
ctx := context.New(config.Project{
Env: []string{
"GORELEASER_TEST_GITLAB_URLS_DOWNLOAD=https://gitlab.mycompany.com",
},
GitLabURLs: config.GitLabURLs{
Download: tt.downloadURL,
},
Release: config.Release{
GitLab: config.Repo{
Owner: "owner",
Name: "name",
},
},
})
client, err := NewGitLab(ctx, ctx.Token)
require.NoError(t, err)
urlTpl, err := client.ReleaseURLTemplate(ctx)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
require.Equal(t, tt.wantDownloadURL, urlTpl)
}
}
func TestGitLabURLsAPITemplate(t *testing.T) {
tests := []struct {
name string
apiURL string
wantHost string
}{
{
name: "default_values",
wantHost: "gitlab.com",
},
{
name: "speicifed_api_env_key",
apiURL: "https://gitlab.mycompany.com",
wantHost: "gitlab.mycompany.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
envs := []string{}
gitlabURLs := config.GitLabURLs{}
if tt.apiURL != "" {
envs = append(envs, fmt.Sprintf("GORELEASER_TEST_GITLAB_URLS_API=%s", tt.apiURL))
gitlabURLs.API = "{{ .Env.GORELEASER_TEST_GITLAB_URLS_API }}"
}
ctx := context.New(config.Project{
Env: envs,
GitLabURLs: gitlabURLs,
})
client, err := NewGitLab(ctx, ctx.Token)
require.NoError(t, err)
gitlabClient, ok := client.(*gitlabClient)
require.True(t, ok)
require.Equal(t, tt.wantHost, gitlabClient.client.BaseURL().Host)
})
}
t.Run("no_env_specified", func(t *testing.T) {
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: "{{ .Env.GORELEASER_NOT_EXISTS }}",
},
})
_, err := NewGitLab(ctx, ctx.Token)
require.ErrorAs(t, err, &template.ExecError{})
})
t.Run("invalid_template", func(t *testing.T) {
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: "{{.dddddddddd",
},
})
_, err := NewGitLab(ctx, ctx.Token)
require.Error(t, err)
})
}
func TestGitLabURLsDownloadTemplate(t *testing.T) {
tests := []struct {
name string
downloadURL string
wantURL string
wantErr bool
}{
{
name: "empty_download_url",
wantURL: "/",
},
{
name: "download_url_template",
downloadURL: "{{ .Env.GORELEASER_TEST_GITLAB_URLS_DOWNLOAD }}",
wantURL: "https://gitlab.mycompany.com/",
},
{
name: "download_url_template_invalid_value",
downloadURL: "{{ .Eenv.GORELEASER_NOT_EXISTS }}",
wantErr: true,
},
{
name: "download_url_template_invalid",
downloadURL: "{{.dddddddddd",
wantErr: true,
},
{
name: "download_url_string",
downloadURL: "https://gitlab.mycompany.com",
wantURL: "https://gitlab.mycompany.com/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer fmt.Fprint(w, "{}")
defer w.WriteHeader(http.StatusOK)
defer r.Body.Close()
if !strings.Contains(r.URL.Path, "assets/links") {
_, _ = io.Copy(io.Discard, r.Body)
return
}
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
reqBody := map[string]interface{}{}
err = json.Unmarshal(b, &reqBody)
require.NoError(t, err)
require.Equal(t, tt.wantURL, reqBody["url"])
}))
defer srv.Close()
ctx := context.New(config.Project{
Env: []string{
"GORELEASER_TEST_GITLAB_URLS_DOWNLOAD=https://gitlab.mycompany.com",
},
Release: config.Release{
GitLab: config.Repo{
Owner: "test",
Name: "test",
},
},
GitLabURLs: config.GitLabURLs{
API: srv.URL,
Download: tt.downloadURL,
},
})
tmpFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)
client, err := NewGitLab(ctx, ctx.Token)
require.NoError(t, err)
err = client.Upload(ctx, "1234", &artifact.Artifact{Name: "test", Path: "some-path"}, tmpFile)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
}
func TestGitLabCreateReleaseUknownHost(t *testing.T) {
ctx := context.New(config.Project{
Release: config.Release{
GitLab: config.Repo{
Owner: "owner",
Name: "name",
},
},
GitLabURLs: config.GitLabURLs{
API: "http://goreleaser-notexists",
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
_, err = client.CreateRelease(ctx, "body")
require.Error(t, err)
}
func TestGitLabCreateReleaseReleaseNotExists(t *testing.T) {
notExistsStatusCodes := []int{http.StatusNotFound, http.StatusForbidden}
for _, tt := range notExistsStatusCodes {
t.Run(strconv.Itoa(tt), func(t *testing.T) {
totalRequests := 0
createdRelease := false
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
totalRequests++
if !strings.Contains(r.URL.Path, "releases") {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
return
}
// Check if release exists
if r.Method == http.MethodGet {
w.WriteHeader(tt)
fmt.Fprint(w, "{}")
return
}
// Create release if it doens't exists
if r.Method == http.MethodPost {
createdRelease = true
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
return
}
require.FailNow(t, "should not reach here")
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
_, err = client.CreateRelease(ctx, "body")
require.NoError(t, err)
require.True(t, createdRelease)
require.Equal(t, 3, totalRequests)
})
}
}
func TestGitLabCreateReleaseUnkownHTTPError(t *testing.T) {
totalRequests := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
totalRequests++
defer r.Body.Close()
w.WriteHeader(http.StatusUnprocessableEntity)
fmt.Fprint(w, "{}")
}))
defer srv.Close()
ctx := context.New(config.Project{
GitLabURLs: config.GitLabURLs{
API: srv.URL,
},
})
client, err := NewGitLab(ctx, "test-token")
require.NoError(t, err)
_, err = client.CreateRelease(ctx, "body")
require.Error(t, err)
require.Equal(t, 2, totalRequests)
}