Files
pulumi-docker-build/provider/internal/cache_test.go
Alberto Pose 38e65a3f81 Remove URL and Token arguments from CacheFromGitHubActions. (#626)
Fixes #75

Arguments URL and Token have been removed but `$ACTIONS_CACHE_URL` and
`$ACTIONS_RUNTIME_TOKEN` env variables could be injected if desired.

---------

Co-authored-by: Bryce Lampe <bryce@pulumi.com>
2025-10-17 11:00:40 +01:00

173 lines
4.6 KiB
Go

// Copyright 2024, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"fmt"
"testing"
"github.com/docker/buildx/util/buildflags"
"github.com/stretchr/testify/assert"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
//nolint:paralleltest // We don't call t.Parallel here to prevent environment corruption.
func TestCacheString(t *testing.T) {
gzip := Gzip
tests := []struct {
name string
arrange func(t *testing.T)
given fmt.Stringer
want string
}{
{
name: "s3",
given: CacheTo{S3: &CacheToS3{
CacheFromS3: CacheFromS3{
Region: "us-west-2",
Bucket: "bucket-foo",
Name: "myname",
EndpointURL: "https://some.endpoint",
BlobsPrefix: "blob-prefix",
ManifestsPrefix: "manifest-prefix",
UsePathStyle: pulumi.BoolRef(true),
AccessKeyID: "access-key-id",
SecretAccessKey: "secret-key",
SessionToken: "session",
},
}},
//nolint:lll // Taken from AWS reference docs.
want: "type=s3,bucket=bucket-foo,name=myname,endpoint_url=https://some.endpoint,blobs_prefix=blob-prefix,manifests_prefix=manifest-prefix,use_path_type=true,access_key_id=access-key-id,secret_access_key=secret-key,session_token=session",
},
{
name: "gha",
given: CacheTo{GHA: &CacheToGitHubActions{}},
arrange: func(t *testing.T) {
t.Setenv("ACTIONS_CACHE_URL", "")
t.Setenv("ACTIONS_RUNTIME_TOKEN", "")
},
want: "type=gha",
},
{
name: "gha-default-envs",
arrange: func(t *testing.T) {
t.Setenv("ACTIONS_CACHE_URL", "https://example.com")
t.Setenv("ACTIONS_RUNTIME_TOKEN", "token")
},
given: CacheTo{GHA: &CacheToGitHubActions{
CacheFromGitHubActions: CacheFromGitHubActions{
Scope: "scope",
},
}},
want: "type=gha,scope=scope,token=token,url=https://example.com",
},
{
name: "gha-with-scope",
arrange: func(t *testing.T) {
t.Setenv("ACTIONS_CACHE_URL", "")
t.Setenv("ACTIONS_RUNTIME_TOKEN", "")
},
given: CacheTo{GHA: &CacheToGitHubActions{
CacheFromGitHubActions: CacheFromGitHubActions{
Scope: "scope",
},
}},
want: "type=gha,scope=scope",
},
{
name: "from-local",
given: CacheFrom{Local: &CacheFromLocal{Src: "/foo/bar"}},
want: "type=local,src=/foo/bar",
},
{
name: "to-local",
given: CacheTo{Local: &CacheToLocal{Dest: "/foo/bar"}},
want: "type=local,dest=/foo/bar",
},
{
name: "inline",
given: CacheTo{Inline: &CacheToInline{}},
want: "type=inline",
},
{
name: "raw",
given: CacheTo{Raw: Raw("type=gha")},
want: "type=gha",
},
{
name: "compression",
given: CacheTo{Local: &CacheToLocal{
Dest: "/foo",
CacheWithCompression: CacheWithCompression{
Compression: &gzip,
CompressionLevel: 100,
ForceCompression: pulumi.BoolRef(true),
},
}},
want: "type=local,dest=/foo,compression=gzip,compression-level=22,force-compression=true",
},
{
name: "ignore-error",
given: CacheTo{
AZBlob: &CacheToAzureBlob{
CacheWithIgnoreError: CacheWithIgnoreError{pulumi.BoolRef(true)},
},
},
want: "type=azblob,ignore-error=true",
},
{
name: "oci",
given: CacheTo{
Registry: &CacheToRegistry{
CacheFromRegistry: CacheFromRegistry{Ref: "docker.io/foo/bar:baz"},
CacheWithOCI: CacheWithOCI{
OCI: pulumi.BoolRef(true),
ImageManifest: pulumi.BoolRef(true),
},
},
},
want: "type=registry,ref=docker.io/foo/bar:baz,oci-mediatypes=true,image-manifest=true",
},
{
name: "disabled-to",
given: CacheTo{
Raw: Raw("type=gha"),
Disabled: true,
},
want: "",
},
}
//nolint:paralleltest // We don't call t.Parallel here to prevent environment corruption.
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.arrange != nil {
tt.arrange(t)
}
actual := tt.given.String()
assert.Equal(t, tt.want, actual)
if tt.want != "" {
// Our output should be parsable by Docker.
_, err := buildflags.ParseCacheEntry([]string{actual})
assert.NoError(t, err)
}
})
}
}