1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-30 04:50:45 +02:00
goreleaser/internal/pipe/ko/ko_test.go
dependabot[bot] 43731e252c
feat(deps): bump github.com/google/ko from 0.12.0 to 0.13.0 (#3880)
Bumps [github.com/google/ko](https://github.com/google/ko) from 0.12.0
to 0.13.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/google/ko/releases">github.com/google/ko's
releases</a>.</em></p>
<blockquote>
<h2>v0.13.0</h2>
<h2>What's Changed</h2>
<ul>
<li>SPDX: Fix package manager label by <a
href="https://github.com/puerco"><code>@​puerco</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/801">ko-build/ko#801</a></li>
<li>SPDX 2.3 support by <a
href="https://github.com/puerco"><code>@​puerco</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/803">ko-build/ko#803</a></li>
<li>ci: build and test using 1.18 and 1.19 (drop 1.17) by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/812">ko-build/ko#812</a></li>
<li>removes repo move message by <a
href="https://github.com/mchmarny"><code>@​mchmarny</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/814">ko-build/ko#814</a></li>
<li>feat: write sbom result to disk by <a
href="https://github.com/developer-guy"><code>@​developer-guy</code></a>
in <a
href="https://redirect.github.com/ko-build/ko/pull/822">ko-build/ko#822</a></li>
<li>feat: adding support for using multiple keychain for sending sbom
results to a different repository by <a
href="https://github.com/developer-guy"><code>@​developer-guy</code></a>
in <a
href="https://redirect.github.com/ko-build/ko/pull/821">ko-build/ko#821</a></li>
<li>Move docs to ko.build by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/749">ko-build/ko#749</a></li>
<li>Update setup-ko version by <a
href="https://github.com/ianlewis"><code>@​ianlewis</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/836">ko-build/ko#836</a></li>
<li>Add -- usage in readme by <a
href="https://github.com/jwcesign"><code>@​jwcesign</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/840">ko-build/ko#840</a></li>
<li>add CONTRIBUTING, code of conduct, roadmap by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/837">ko-build/ko#837</a></li>
<li>attempt to fix GH Pages publishing by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/843">ko-build/ko#843</a></li>
<li>doc: fix link to Installation page in Getting Started by <a
href="https://github.com/antoineco"><code>@​antoineco</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/846">ko-build/ko#846</a></li>
<li>.ko.yaml: bump golang 1.18 -&gt; 1.19 by <a
href="https://github.com/srenatus"><code>@​srenatus</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/848">ko-build/ko#848</a></li>
<li>truncate -image-refs file by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/855">ko-build/ko#855</a></li>
<li>update docs: fix broken links, align with README by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/854">ko-build/ko#854</a></li>
<li>Handle KO_DOCKER_REPO=ko.local/repo and --bare correctly by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/820">ko-build/ko#820</a></li>
<li>another docs update by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/856">ko-build/ko#856</a></li>
<li>ko.build: support some common shortlinks by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/872">ko-build/ko#872</a></li>
<li>install: fail with 404 instead of gzip error when url was wrong by
<a href="https://github.com/grosser"><code>@​grosser</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/879">ko-build/ko#879</a></li>
<li>feat: deduplicate tags by <a
href="https://github.com/bluebrown"><code>@​bluebrown</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/884">ko-build/ko#884</a></li>
<li>install mkdocs-redirect when publishing site by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/873">ko-build/ko#873</a></li>
<li>nit: replace one-item slice with const by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/885">ko-build/ko#885</a></li>
<li>Temp fix for SLSA generators by <a
href="https://github.com/laurentsimon"><code>@​laurentsimon</code></a>
in <a
href="https://redirect.github.com/ko-build/ko/pull/886">ko-build/ko#886</a></li>
<li>Fix verifier by <a
href="https://github.com/laurentsimon"><code>@​laurentsimon</code></a>
in <a
href="https://redirect.github.com/ko-build/ko/pull/891">ko-build/ko#891</a></li>
<li>Fix link in static-assets.md by <a
href="https://github.com/yuryu"><code>@​yuryu</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/893">ko-build/ko#893</a></li>
<li>add KO_DEFAULTBASEIMAGE usage to docs by <a
href="https://github.com/developer-guy"><code>@​developer-guy</code></a>
in <a
href="https://redirect.github.com/ko-build/ko/pull/895">ko-build/ko#895</a></li>
<li>Publish an tagged image on release by <a
href="https://github.com/vdemeester"><code>@​vdemeester</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/868">ko-build/ko#868</a></li>
<li>Add option to configure default platforms by <a
href="https://github.com/ReToCode"><code>@​ReToCode</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/897">ko-build/ko#897</a></li>
<li>Fix broken SLSA link by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/899">ko-build/ko#899</a></li>
<li>add MAINTAINERS.md by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/905">ko-build/ko#905</a></li>
<li>fix: possible race condition when applying templates to
flags/ldflags by <a
href="https://github.com/caarlos0"><code>@​caarlos0</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/913">ko-build/ko#913</a></li>
<li>update docs to reflect actual default base image by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/903">ko-build/ko#903</a></li>
<li>remove repeated error message on failure by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/921">ko-build/ko#921</a></li>
<li>website: update CNCF announcement by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/920">ko-build/ko#920</a></li>
<li>fix KO_CONFIG_PATH pointing to a file by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/923">ko-build/ko#923</a></li>
<li>upgrade to cosign v2.0.0-rc.0 by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/933">ko-build/ko#933</a></li>
<li>Feature: Add ECR presubmit testing. by <a
href="https://github.com/mattmoor"><code>@​mattmoor</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/934">ko-build/ko#934</a></li>
<li>remove 'ko deps' by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/937">ko-build/ko#937</a></li>
<li>feat: Add KO_GO_PATH env var by <a
href="https://github.com/embano1"><code>@​embano1</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/930">ko-build/ko#930</a></li>
<li>add ko.build/slack short link by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/945">ko-build/ko#945</a></li>
<li>update link to ko goreleaser docs by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/936">ko-build/ko#936</a></li>
<li>add ko community meeting details by <a
href="https://github.com/developer-guy"><code>@​developer-guy</code></a>
in <a
href="https://redirect.github.com/ko-build/ko/pull/938">ko-build/ko#938</a></li>
<li>fix cosign by adding --yes by <a
href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/973">ko-build/ko#973</a></li>
<li>fix: handle docker's unknown/unknown platform in index manifests by
<a href="https://github.com/imjasonh"><code>@​imjasonh</code></a> in <a
href="https://redirect.github.com/ko-build/ko/pull/975">ko-build/ko#975</a></li>
<li>fix file extension for cyclonedx by <a
href="https://github.com/developer-guy"><code>@​developer-guy</code></a>
in <a
href="https://redirect.github.com/ko-build/ko/pull/974">ko-build/ko#974</a></li>
</ul>
<h2>New Contributors</h2>
<ul>
<li><a href="https://github.com/ianlewis"><code>@​ianlewis</code></a>
made their first contribution in <a
href="https://redirect.github.com/ko-build/ko/pull/836">ko-build/ko#836</a></li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="e22e7a15ff"><code>e22e7a1</code></a>
bump ggcr dep to <a
href="https://github.com/main"><code>@​main</code></a> (<a
href="https://redirect.github.com/google/ko/issues/976">#976</a>)</li>
<li><a
href="8e075ae1f1"><code>8e075ae</code></a>
fix file extension for cyclonedx (<a
href="https://redirect.github.com/google/ko/issues/974">#974</a>)</li>
<li><a
href="11670b7498"><code>11670b7</code></a>
fix: handle docker's unknown/unknown platform in index manifests (<a
href="https://redirect.github.com/google/ko/issues/975">#975</a>)</li>
<li><a
href="7ce947817e"><code>7ce9478</code></a>
fix cosign by adding --yes (<a
href="https://redirect.github.com/google/ko/issues/973">#973</a>)</li>
<li><a
href="9302da78dc"><code>9302da7</code></a>
Bump k8s.io/apimachinery from 0.26.1 to 0.26.2 (<a
href="https://redirect.github.com/google/ko/issues/972">#972</a>)</li>
<li><a
href="a1588838ba"><code>a158883</code></a>
Bump sigstore/cosign-installer from 2.8.1 to 3.0.1 (<a
href="https://redirect.github.com/google/ko/issues/971">#971</a>)</li>
<li><a
href="86b6c2854f"><code>86b6c28</code></a>
Bump actions/checkout from 2 to 3 (<a
href="https://redirect.github.com/google/ko/issues/966">#966</a>)</li>
<li><a
href="0bd12fb106"><code>0bd12fb</code></a>
Bump slsa-framework/slsa-github-generator from 1.2.1 to 1.5.0 (<a
href="https://redirect.github.com/google/ko/issues/967">#967</a>)</li>
<li><a
href="d5125daacd"><code>d5125da</code></a>
Bump github.com/sigstore/cosign/v2 from 2.0.0-rc.2 to 2.0.0 (<a
href="https://redirect.github.com/google/ko/issues/965">#965</a>)</li>
<li><a
href="03f4aed682"><code>03f4aed</code></a>
add ko community meeting details (<a
href="https://redirect.github.com/google/ko/issues/938">#938</a>)</li>
<li>Additional commits viewable in <a
href="https://github.com/google/ko/compare/v0.12.0...v0.13.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=github.com/google/ko&package-manager=go_modules&previous-version=0.12.0&new-version=0.13.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2023-03-29 14:38:57 -03:00

434 lines
11 KiB
Go

package ko
import (
"fmt"
"strconv"
"strings"
"testing"
"time"
_ "github.com/distribution/distribution/v3/registry/auth/htpasswd"
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/goreleaser/goreleaser/internal/testctx"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
const (
registryPort = "5052"
registry = "localhost:5052/"
)
func TestDefault(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Env: []string{
"KO_DOCKER_REPO=" + registry,
"COSIGN_REPOSITORY=" + registry,
"LDFLAGS=foobar",
"FLAGS=barfoo",
"LE_ENV=test",
},
ProjectName: "test",
Builds: []config.Build{
{
ID: "test",
Dir: ".",
BuildDetails: config.BuildDetails{
Ldflags: []string{"{{.Env.LDFLAGS}}"},
Flags: []string{"{{.Env.FLAGS}}"},
Env: []string{"SOME_ENV={{.Env.LE_ENV}}"},
},
},
},
Kos: []config.Ko{
{},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.Equal(t, config.Ko{
ID: "test",
Build: "test",
BaseImage: chainguardStatic,
Repository: registry,
Platforms: []string{"linux/amd64"},
SBOM: "spdx",
Tags: []string{"latest"},
WorkingDir: ".",
Ldflags: []string{"{{.Env.LDFLAGS}}"},
Flags: []string{"{{.Env.FLAGS}}"},
Env: []string{"SOME_ENV={{.Env.LE_ENV}}"},
}, ctx.Config.Kos[0])
}
func TestDefaultNoImage(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "test",
Builds: []config.Build{
{
ID: "test",
},
},
Kos: []config.Ko{
{},
},
})
require.ErrorIs(t, Pipe{}.Default(ctx), errNoRepository)
}
func TestDescription(t *testing.T) {
require.NotEmpty(t, Pipe{}.String())
}
func TestSkip(t *testing.T) {
t.Run("skip ko set", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Kos: []config.Ko{{}},
})
ctx.SkipKo = true
require.True(t, Pipe{}.Skip(ctx))
})
t.Run("skip no kos", func(t *testing.T) {
ctx := testctx.New()
require.True(t, Pipe{}.Skip(ctx))
})
t.Run("dont skip", func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Kos: []config.Ko{{}},
})
require.False(t, Pipe{}.Skip(ctx))
})
}
func TestPublishPipeNoMatchingBuild(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "doesnt matter",
},
},
Kos: []config.Ko{
{
ID: "default",
Build: "wont match nothing",
},
},
})
require.EqualError(t, Pipe{}.Default(ctx), `no builds with id "wont match nothing"`)
}
func TestPublishPipeSuccess(t *testing.T) {
testlib.StartRegistry(t, "ko_registry", registryPort)
table := []struct {
Name string
SBOM string
BaseImage string
Labels map[string]string
ExpectedLabels map[string]string
Platforms []string
CreationTime string
KoDataCreationTime string
}{
{
// Must be first as others add an SBOM for the same image
Name: "sbom-none",
SBOM: "none",
},
{
Name: "sbom-spdx",
SBOM: "spdx",
},
{
Name: "sbom-cyclonedx",
SBOM: "cyclonedx",
},
{
Name: "sbom-go.version-m",
SBOM: "go.version-m",
},
{
Name: "base-image-is-not-index",
BaseImage: "alpine:latest@sha256:c0d488a800e4127c334ad20d61d7bc21b4097540327217dfab52262adc02380c",
},
{
Name: "multiple-platforms",
Platforms: []string{"linux/amd64", "linux/arm64"},
},
{
Name: "labels",
Labels: map[string]string{"foo": "bar", "project": "{{.ProjectName}}"},
ExpectedLabels: map[string]string{"foo": "bar", "project": "test"},
},
{
Name: "creation-time",
CreationTime: "1672531200",
},
{
Name: "kodata-creation-time",
KoDataCreationTime: "1672531200",
},
}
repository := fmt.Sprintf("%sgoreleasertest/testapp", registry)
for _, table := range table {
t.Run(table.Name, func(t *testing.T) {
ctx := testctx.NewWithCfg(config.Project{
ProjectName: "test",
Builds: []config.Build{
{
ID: "foo",
BuildDetails: config.BuildDetails{
Ldflags: []string{"-s", "-w"},
Flags: []string{"-tags", "netgo"},
Env: []string{"GOCACHE=" + t.TempDir()},
},
},
},
Kos: []config.Ko{
{
ID: "default",
Build: "foo",
WorkingDir: "./testdata/app/",
BaseImage: table.BaseImage,
Repository: repository,
Labels: table.Labels,
Platforms: table.Platforms,
Tags: []string{table.Name},
CreationTime: table.CreationTime,
KoDataCreationTime: table.KoDataCreationTime,
SBOM: table.SBOM,
Bare: true,
},
},
})
require.NoError(t, Pipe{}.Default(ctx))
require.NoError(t, Pipe{}.Publish(ctx))
ref, err := name.ParseReference(
fmt.Sprintf("%s:%s", repository, table.Name),
name.Insecure,
)
require.NoError(t, err)
index, err := remote.Index(ref)
if len(table.Platforms) > 1 {
require.NoError(t, err)
imf, err := index.IndexManifest()
require.NoError(t, err)
platforms := make([]string, 0, len(imf.Manifests))
for _, mf := range imf.Manifests {
platforms = append(platforms, mf.Platform.String())
}
require.ElementsMatch(t, table.Platforms, platforms)
} else {
require.Error(t, err)
}
image, err := remote.Image(ref)
require.NoError(t, err)
digest, err := image.Digest()
require.NoError(t, err)
sbomRef, err := name.ParseReference(
fmt.Sprintf(
"%s:%s.sbom",
repository,
strings.Replace(digest.String(), ":", "-", 1),
),
name.Insecure,
)
require.NoError(t, err)
sbom, err := remote.Image(sbomRef)
if table.SBOM == "none" {
require.Error(t, err)
} else {
require.NoError(t, err)
layers, err := sbom.Layers()
require.NoError(t, err)
require.NotEmpty(t, layers)
mediaType, err := layers[0].MediaType()
require.NoError(t, err)
switch table.SBOM {
case "spdx", "":
require.Equal(t, "text/spdx+json", string(mediaType))
case "cyclonedx":
require.Equal(t, "application/vnd.cyclonedx+json", string(mediaType))
case "go.version-m":
require.Equal(t, "application/vnd.go.version-m", string(mediaType))
default:
require.Fail(t, "unknown SBOM type", table.SBOM)
}
}
configFile, err := image.ConfigFile()
require.NoError(t, err)
require.GreaterOrEqual(t, len(configFile.History), 3)
require.Equal(t, table.ExpectedLabels, configFile.Config.Labels)
var creationTime time.Time
if table.CreationTime != "" {
ct, err := strconv.ParseInt(table.CreationTime, 10, 64)
require.NoError(t, err)
creationTime = time.Unix(ct, 0).UTC()
require.Equal(t, creationTime, configFile.Created.Time.UTC())
}
require.Equal(t, creationTime, configFile.History[len(configFile.History)-1].Created.Time.UTC())
var koDataCreationTime time.Time
if table.KoDataCreationTime != "" {
kdct, err := strconv.ParseInt(table.KoDataCreationTime, 10, 64)
require.NoError(t, err)
koDataCreationTime = time.Unix(kdct, 0).UTC()
}
require.Equal(t, koDataCreationTime, configFile.History[len(configFile.History)-2].Created.Time.UTC())
})
}
}
func TestPublishPipeError(t *testing.T) {
makeCtx := func() *context.Context {
return testctx.NewWithCfg(config.Project{
Builds: []config.Build{
{
ID: "foo",
Main: "./...",
},
},
Kos: []config.Ko{
{
ID: "default",
Build: "foo",
WorkingDir: "./testdata/app/",
Repository: "fakerepo:8080/",
Tags: []string{"latest", "{{.Tag}}"},
},
},
}, testctx.WithCurrentTag("v1.0.0"))
}
t.Run("invalid base image", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].BaseImage = "not a valid image hopefully"
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Publish(ctx), `build: could not parse reference: not a valid image hopefully`)
})
t.Run("invalid label tmpl", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].Labels = map[string]string{"nope": "{{.Nope}}"}
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Publish(ctx))
})
t.Run("invalid sbom", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].SBOM = "nope"
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Publish(ctx), `makeBuilder: unknown sbom type: "nope"`)
})
t.Run("invalid build", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].WorkingDir = t.TempDir()
require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Publish(ctx), `build: exit status 1`)
})
t.Run("invalid tags tmpl", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].Tags = []string{"{{.Nope}}"}
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Publish(ctx))
})
t.Run("invalid creation time", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].CreationTime = "nope"
require.NoError(t, Pipe{}.Default(ctx))
err := Pipe{}.Publish(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), `strconv.ParseInt: parsing "nope": invalid syntax`)
})
t.Run("invalid creation time tmpl", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].CreationTime = "{{.Nope}}"
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Publish(ctx))
})
t.Run("invalid kodata creation time", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].KoDataCreationTime = "nope"
require.NoError(t, Pipe{}.Default(ctx))
err := Pipe{}.Publish(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), `strconv.ParseInt: parsing "nope": invalid syntax`)
})
t.Run("invalid kodata creation time tmpl", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Kos[0].KoDataCreationTime = "{{.Nope}}"
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Publish(ctx))
})
t.Run("invalid env tmpl", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Builds[0].Env = []string{"{{.Nope}}"}
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Publish(ctx))
})
t.Run("invalid ldflags tmpl", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Builds[0].Ldflags = []string{"{{.Nope}}"}
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Publish(ctx))
})
t.Run("invalid flags tmpl", func(t *testing.T) {
ctx := makeCtx()
ctx.Config.Builds[0].Flags = []string{"{{.Nope}}"}
require.NoError(t, Pipe{}.Default(ctx))
testlib.RequireTemplateError(t, Pipe{}.Publish(ctx))
})
t.Run("publish fail", func(t *testing.T) {
ctx := makeCtx()
require.NoError(t, Pipe{}.Default(ctx))
err := Pipe{}.Publish(ctx)
require.Error(t, err)
require.Contains(t, err.Error(), `publish: writing sbom: Get "https://fakerepo:8080/v2/": dial tcp:`)
})
}
func TestApplyTemplate(t *testing.T) {
t.Run("success", func(t *testing.T) {
foo, err := applyTemplate(testctx.NewWithCfg(config.Project{
Env: []string{"FOO=bar"},
}), []string{"{{ .Env.FOO }}"})
require.NoError(t, err)
require.Equal(t, []string{"bar"}, foo)
})
t.Run("error", func(t *testing.T) {
_, err := applyTemplate(testctx.New(), []string{"{{ .Nope}}"})
require.Error(t, err)
})
}