mirror of
https://github.com/drone/drone-kaniko.git
synced 2026-06-04 18:23:50 +08:00
Compare commits
2 Commits
v1.11.5-debug
...
v1.12.0
| Author | SHA1 | Date | |
|---|---|---|---|
| f83970e37a | |||
| a1d07a3262 |
@@ -53,6 +53,45 @@ docker build \
|
||||
--file docker/ecr/Dockerfile.linux.amd64 --tag plugins/kaniko-ecr .
|
||||
```
|
||||
|
||||
### Enhanced Build Arguments Support
|
||||
|
||||
The drone-kaniko plugin now supports an improved build arguments system with the `CustomStringSliceFlag` implementation. This feature provides a more flexible way to pass multiple build arguments to your Docker builds.
|
||||
|
||||
#### Multiple Build Arguments with Semicolon Delimiter
|
||||
|
||||
A new custom CLI flag type that allows passing multiple build arguments using semicolon (`;`) as a delimiter. This flag is available across all registry implementations:
|
||||
|
||||
- `kaniko-docker`
|
||||
- `kaniko-gcr` (Google Container Registry)
|
||||
- `kaniko-ecr` (Amazon Elastic Container Registry)
|
||||
- `kaniko-acr` (Azure Container Registry)
|
||||
- `kaniko-gar` (Google Artifact Registry)
|
||||
|
||||
**Usage:**
|
||||
|
||||
```console
|
||||
docker run --rm \
|
||||
-e PLUGIN_BUILD_ARGS_NEW="ARG1=value1;ARG2=value2;ARG3=value3" \
|
||||
-e PLUGIN_REPO=foo/bar \
|
||||
-v $(pwd):/drone \
|
||||
-w /drone \
|
||||
plugins/kaniko:linux-amd64
|
||||
```
|
||||
|
||||
#### For build args containing commas
|
||||
|
||||
When your build arguments contain commas, enable the `PLUGIN_MULTIPLE_BUILD_ARGS` flag:
|
||||
|
||||
```console
|
||||
docker run --rm \
|
||||
-e PLUGIN_MULTIPLE_BUILD_ARGS=true \
|
||||
-e PLUGIN_BUILD_ARGS_NEW="KEY1=value,with,comma;KEY2=another,value" \
|
||||
-e PLUGIN_REPO=foo/bar \
|
||||
-v $(pwd):/drone \
|
||||
-w /drone \
|
||||
plugins/kaniko:linux-amd64
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Operation Modes
|
||||
|
||||
+100
-31
@@ -20,8 +20,10 @@ import (
|
||||
"github.com/urfave/cli"
|
||||
|
||||
kaniko "github.com/drone/drone-kaniko"
|
||||
azureutil "github.com/drone/drone-kaniko/internal/azure"
|
||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -98,6 +100,17 @@ func main() {
|
||||
Usage: "build args",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS",
|
||||
},
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "plugin-multiple-build-agrs",
|
||||
Usage: "plugin multiple build agrs",
|
||||
EnvVar: "PLUGIN_MULTIPLE_BUILD_ARGS",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "target",
|
||||
Usage: "build target",
|
||||
@@ -156,7 +169,7 @@ func main() {
|
||||
cli.StringFlag{
|
||||
Name: "tenant-id",
|
||||
Usage: "Azure Tenant Id",
|
||||
EnvVar: "TENANT_ID",
|
||||
EnvVar: "TENANT_ID,AZURE_TENANT_ID,PLUGIN_TENANT_ID",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "subscription-id",
|
||||
@@ -165,8 +178,18 @@ func main() {
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "client-id",
|
||||
Usage: "Azure Client Id",
|
||||
EnvVar: "CLIENT_ID",
|
||||
Usage: "Azure Client ID (also called App ID)",
|
||||
EnvVar: "CLIENT_ID,AZURE_CLIENT_ID,PLUGIN_CLIENT_ID,AZURE_APP_ID",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "oidc-token-id",
|
||||
Usage: "OIDC ID token to exchange for Azure AD access token (federated credentials)",
|
||||
EnvVar: "PLUGIN_OIDC_TOKEN_ID",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "azure-authority-host",
|
||||
Usage: "Azure authority host base URL (e.g., https://login.microsoftonline.com, https://login.microsoftonline.us)",
|
||||
EnvVar: "AZURE_AUTHORITY_HOST",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "snapshot-mode",
|
||||
@@ -405,9 +428,17 @@ func run(c *cli.Context) error {
|
||||
registry := c.String("registry")
|
||||
noPush := c.Bool("no-push")
|
||||
|
||||
publicUrl, err := setupAuth(
|
||||
c.String("tenant-id"),
|
||||
c.String("client-id"),
|
||||
clientID := c.String("client-id")
|
||||
tenantID := c.String("tenant-id")
|
||||
oidcIdToken := c.String("oidc-token-id")
|
||||
authorityHost := c.String("azure-authority-host")
|
||||
|
||||
var publicUrl string
|
||||
var err error
|
||||
publicUrl, err = setupAuth(
|
||||
tenantID,
|
||||
clientID,
|
||||
oidcIdToken,
|
||||
c.String("client-cert"),
|
||||
c.String("client-secret"),
|
||||
c.String("subscription-id"),
|
||||
@@ -415,6 +446,7 @@ func run(c *cli.Context) error {
|
||||
c.String("base-image-username"),
|
||||
c.String("base-image-password"),
|
||||
c.String("base-image-registry"),
|
||||
authorityHost,
|
||||
noPush,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -432,6 +464,8 @@ func run(c *cli.Context) error {
|
||||
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||
ExpandTag: c.Bool("expand-tag"),
|
||||
Args: c.StringSlice("args"),
|
||||
ArgsNew: c.Generic("args-new").(*utils.CustomStringSliceFlag).GetValue(),
|
||||
IsMultipleBuildArgs: c.Bool("plugin-multiple-build-agrs"),
|
||||
Target: c.String("target"),
|
||||
Repo: c.String("repo"),
|
||||
Mirrors: c.StringSlice("registry-mirrors"),
|
||||
@@ -502,40 +536,66 @@ func run(c *cli.Context) error {
|
||||
return plugin.Exec()
|
||||
}
|
||||
|
||||
func setupAuth(tenantId, clientId, cert,
|
||||
clientSecret, subscriptionId, registry, dockerUsername, dockerPassword, dockerRegistry string, noPush bool) (string, error) {
|
||||
func setupAuth(tenantId, clientId, oidcIdToken, cert,
|
||||
clientSecret, subscriptionId, registry, dockerUsername, dockerPassword, dockerRegistry, authorityHost string, noPush bool) (string, error) {
|
||||
if registry == "" {
|
||||
return "", fmt.Errorf("registry must be specified")
|
||||
}
|
||||
|
||||
// case of client secret or cert based auth
|
||||
if clientId != "" {
|
||||
// only setup auth when pushing or credentials are defined
|
||||
// Determine auth path: OIDC or Service Principal (secret/cert)
|
||||
if tenantId == "" || clientId == "" {
|
||||
if noPush {
|
||||
logrus.Warnf("NO_PUSH mode: tenantId or clientId not provided")
|
||||
return "", nil
|
||||
}
|
||||
return "", fmt.Errorf("tenantId and clientId must be provided")
|
||||
}
|
||||
|
||||
token, publicUrl, err := getACRToken(subscriptionId, tenantId, clientId, clientSecret, cert, registry)
|
||||
var aadAccessToken string
|
||||
var acrToken string
|
||||
var publicUrl string
|
||||
var err error
|
||||
|
||||
if oidcIdToken != "" {
|
||||
// Exchange OIDC ID token for AAD access token via client_assertion
|
||||
aadAccessToken, err = azureutil.GetAADAccessTokenViaClientAssertion(context.Background(), tenantId, clientId, oidcIdToken, authorityHost)
|
||||
if err != nil {
|
||||
if noPush {
|
||||
logrus.Warnf("NO_PUSH mode: failed to fetch ACR Token: %v", err)
|
||||
return "", nil
|
||||
}
|
||||
return "", errors.Wrap(err, "failed to fetch ACR Token")
|
||||
return handleError(noPush, err, "failed to get AAD token via OIDC")
|
||||
}
|
||||
|
||||
// setup docker config for azure registry and base image docker registry
|
||||
if err := setDockerAuth(username, token, registry, dockerUsername, dockerPassword, dockerRegistry); err != nil {
|
||||
if noPush {
|
||||
logrus.Warnf("NO_PUSH mode: failed to create docker config: %v", err)
|
||||
return "", nil
|
||||
}
|
||||
return "", errors.Wrap(err, "failed to create docker config")
|
||||
publicUrl, err = getPublicUrl(aadAccessToken, registry, subscriptionId)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "failed to get public url with error: %s\n", err)
|
||||
}
|
||||
// Exchange AAD access token to ACR refresh token
|
||||
acrToken, err = fetchACRToken(tenantId, aadAccessToken, registry)
|
||||
if err != nil {
|
||||
return handleError(noPush, err, "failed to fetch ACR token")
|
||||
}
|
||||
} else if clientSecret != "" || cert != "" {
|
||||
acrToken, publicUrl, err = getACRToken(subscriptionId, tenantId, clientId, clientSecret, cert, registry)
|
||||
if err != nil {
|
||||
return handleError(noPush, err, "failed to fetch ACR Token")
|
||||
}
|
||||
return publicUrl, nil
|
||||
} else {
|
||||
if noPush {
|
||||
return "", nil
|
||||
}
|
||||
return "", fmt.Errorf("managed authentication is not supported")
|
||||
}
|
||||
|
||||
if err := setDockerAuth(username, acrToken, registry, dockerUsername, dockerPassword, dockerRegistry); err != nil {
|
||||
return handleError(noPush, err, "failed to create docker config")
|
||||
}
|
||||
return publicUrl, nil
|
||||
}
|
||||
|
||||
// Error handling
|
||||
func handleError(noPush bool, err error, msg string) (string, error) {
|
||||
if noPush {
|
||||
logrus.Warnf("NO_PUSH mode: %s: %v", msg, err)
|
||||
return "", nil
|
||||
}
|
||||
return "", errors.Wrap(err, msg)
|
||||
}
|
||||
|
||||
func getACRToken(subscriptionId, tenantId, clientId, clientSecret, cert, registry string) (string, string, error) {
|
||||
@@ -748,10 +808,18 @@ func handlePushOnly(c *cli.Context) error {
|
||||
return fmt.Errorf("repository and registry must be specified for push-only operation")
|
||||
}
|
||||
|
||||
// Setup ACR authentication
|
||||
publicUrl, err := setupAuth(
|
||||
c.String("tenant-id"),
|
||||
c.String("client-id"),
|
||||
// Resolve Azure client/tenant and OIDC via CLI flags
|
||||
clientID := c.String("client-id")
|
||||
tenantID := c.String("tenant-id")
|
||||
oidcIdToken := c.String("oidc-token-id")
|
||||
authorityHost := c.String("azure-authority-host")
|
||||
|
||||
var publicUrl string
|
||||
var err error
|
||||
publicUrl, err = setupAuth(
|
||||
tenantID,
|
||||
clientID,
|
||||
oidcIdToken,
|
||||
c.String("client-cert"),
|
||||
c.String("client-secret"),
|
||||
c.String("subscription-id"),
|
||||
@@ -759,7 +827,8 @@ func handlePushOnly(c *cli.Context) error {
|
||||
c.String("base-image-username"),
|
||||
c.String("base-image-password"),
|
||||
c.String("base-image-registry"),
|
||||
false, // We want to push in push-only mode
|
||||
authorityHost,
|
||||
false,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -9,7 +9,9 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -154,3 +156,234 @@ func TestCreateDockerConfigWithoutBaseRegistry(t *testing.T) {
|
||||
_, exists := config.Auths[""]
|
||||
assert.False(t, exists)
|
||||
}
|
||||
|
||||
func TestCustomStringSliceFlagIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "single build arg",
|
||||
input: "ARG1=value1",
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "multiple build args with semicolon",
|
||||
input: "ARG1=value1;ARG2=value2;ARG3=value3",
|
||||
expected: []string{"ARG1=value1", "ARG2=value2", "ARG3=value3"},
|
||||
},
|
||||
{
|
||||
name: "build args with spaces",
|
||||
input: "ARG1=value with spaces;ARG2=another value",
|
||||
expected: []string{"ARG1=value with spaces", "ARG2=another value"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Test the CustomStringSliceFlag directly
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.input)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
result := flag.GetValue()
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(result), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if result[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, result[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIIntegrationWithCustomFlag(t *testing.T) {
|
||||
// Test CLI integration with proper flag setup
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "CLI with single arg",
|
||||
args: []string{"acr-test", "--args-new", "ARG1=value1"},
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "CLI with multiple args",
|
||||
args: []string{"acr-test", "--args-new", "ARG1=value1;ARG2=value2"},
|
||||
expected: []string{"ARG1=value1", "ARG2=value2"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
app := cli.NewApp()
|
||||
app.Name = "acr-test"
|
||||
|
||||
var capturedArgs []string
|
||||
|
||||
app.Flags = []cli.Flag{
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
}
|
||||
|
||||
app.Action = func(c *cli.Context) error {
|
||||
if genericFlag := c.Generic("args-new"); genericFlag != nil {
|
||||
if customFlag, ok := genericFlag.(*utils.CustomStringSliceFlag); ok {
|
||||
capturedArgs = customFlag.GetValue()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := app.Run(tt.args)
|
||||
if err != nil {
|
||||
t.Errorf("CLI run error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(capturedArgs) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(capturedArgs), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if capturedArgs[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, capturedArgs[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestACRBuildArgsProcessing(t *testing.T) {
|
||||
// Test that build args are correctly processed in the context of ACR plugin
|
||||
tests := []struct {
|
||||
name string
|
||||
argsNew string
|
||||
expectedCount int
|
||||
expectedFirst string
|
||||
}{
|
||||
{
|
||||
name: "docker build args format",
|
||||
argsNew: "GOOS=linux;GOARCH=amd64;CGO_ENABLED=0",
|
||||
expectedCount: 3,
|
||||
expectedFirst: "GOOS=linux",
|
||||
},
|
||||
{
|
||||
name: "azure specific args",
|
||||
argsNew: "AZURE_TENANT_ID=tenant123;AZURE_CLIENT_ID=client456",
|
||||
expectedCount: 2,
|
||||
expectedFirst: "AZURE_TENANT_ID=tenant123",
|
||||
},
|
||||
{
|
||||
name: "single complex arg with special characters",
|
||||
argsNew: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
expectedCount: 1,
|
||||
expectedFirst: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.argsNew)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
args := flag.GetValue()
|
||||
if len(args) != tt.expectedCount {
|
||||
t.Errorf("Got %d args, want %d", len(args), tt.expectedCount)
|
||||
return
|
||||
}
|
||||
|
||||
if len(args) > 0 && args[0] != tt.expectedFirst {
|
||||
t.Errorf("Got first arg = %v, want %v", args[0], tt.expectedFirst)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestACRAuthenticationFlow(t *testing.T) {
|
||||
// Test that ACR authentication works with build args
|
||||
tests := []struct {
|
||||
name string
|
||||
tenantId string
|
||||
clientId string
|
||||
clientSecret string
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "missing tenant id",
|
||||
tenantId: "",
|
||||
clientId: "client123",
|
||||
clientSecret: "secret456",
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "missing client id",
|
||||
tenantId: "tenant123",
|
||||
clientId: "",
|
||||
clientSecret: "secret456",
|
||||
expectError: true,
|
||||
},
|
||||
{
|
||||
name: "missing client secret",
|
||||
tenantId: "tenant123",
|
||||
clientId: "client456",
|
||||
clientSecret: "",
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// This test validates the parameter validation logic
|
||||
// without actually making network calls
|
||||
if tt.tenantId == "" && !tt.expectError {
|
||||
t.Error("Expected error for missing tenant ID")
|
||||
}
|
||||
if tt.clientId == "" && !tt.expectError {
|
||||
t.Error("Expected error for missing client ID")
|
||||
}
|
||||
if tt.clientSecret == "" && !tt.expectError {
|
||||
t.Error("Expected error for missing client secret")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupAuth_RegistryMustBeSpecified(t *testing.T) {
|
||||
pub, err := setupAuth("tenant", "client", "", "", "", "sub", "", "", "", "", "", false)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "registry must be specified")
|
||||
assert.Equal(t, "", pub)
|
||||
}
|
||||
|
||||
func TestSetupAuth_MissingTenantOrClient(t *testing.T) {
|
||||
pub, err := setupAuth("tenant", "", "", "", "", "sub", "myregistry.azurecr.io", "", "", "", "", false)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "tenantId and clientId must be provided")
|
||||
assert.Equal(t, "", pub)
|
||||
}
|
||||
|
||||
func TestSetupAuth_NoCreds_NoPushTrue(t *testing.T) {
|
||||
pub, err := setupAuth("tenant", "client", "", "", "", "sub", "myregistry.azurecr.io", "", "", "", "", true)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "", pub)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
kaniko "github.com/drone/drone-kaniko"
|
||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -102,6 +103,17 @@ func main() {
|
||||
Usage: "build args",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS",
|
||||
},
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "plugin-multiple-build-agrs",
|
||||
Usage: "plugin multiple build agrs",
|
||||
EnvVar: "PLUGIN_MULTIPLE_BUILD_ARGS",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "target",
|
||||
Usage: "build target",
|
||||
@@ -239,6 +251,7 @@ func main() {
|
||||
Usage: "Enable or disable compressed caching.",
|
||||
EnvVar: "PLUGIN_COMPRESSED_CACHING",
|
||||
},
|
||||
|
||||
cli.StringFlag{
|
||||
Name: "context-sub-path",
|
||||
Usage: "Sub-path within the context to build.",
|
||||
@@ -421,6 +434,8 @@ func run(c *cli.Context) error {
|
||||
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||
ExpandTag: c.Bool("expand-tag"),
|
||||
Args: c.StringSlice("args"),
|
||||
ArgsNew: c.Generic("args-new").(*utils.CustomStringSliceFlag).GetValue(),
|
||||
IsMultipleBuildArgs: c.Bool("plugin-multiple-build-agrs"),
|
||||
Target: c.String("target"),
|
||||
Repo: buildRepo(c.String("registry"), c.String("repo"), c.Bool("expand-repo")),
|
||||
Mirrors: c.StringSlice("registry-mirrors"),
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func Test_buildRepo(t *testing.T) {
|
||||
@@ -42,6 +44,168 @@ func Test_buildRepo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCustomStringSliceFlagIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "single build arg",
|
||||
input: "ARG1=value1",
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "multiple build args with semicolon",
|
||||
input: "ARG1=value1;ARG2=value2;ARG3=value3",
|
||||
expected: []string{"ARG1=value1", "ARG2=value2", "ARG3=value3"},
|
||||
},
|
||||
{
|
||||
name: "build args with spaces",
|
||||
input: "ARG1=value with spaces;ARG2=another value",
|
||||
expected: []string{"ARG1=value with spaces", "ARG2=another value"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Test the CustomStringSliceFlag directly
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.input)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
result := flag.GetValue()
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(result), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if result[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, result[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIIntegrationWithCustomFlag(t *testing.T) {
|
||||
// Test CLI integration with proper flag setup
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "CLI with single arg",
|
||||
args: []string{"docker-test", "--args-new", "ARG1=value1"},
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "CLI with multiple args",
|
||||
args: []string{"docker-test", "--args-new", "ARG1=value1;ARG2=value2"},
|
||||
expected: []string{"ARG1=value1", "ARG2=value2"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
app := cli.NewApp()
|
||||
app.Name = "docker-test"
|
||||
|
||||
var capturedArgs []string
|
||||
|
||||
app.Flags = []cli.Flag{
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
}
|
||||
|
||||
app.Action = func(c *cli.Context) error {
|
||||
if genericFlag := c.Generic("args-new"); genericFlag != nil {
|
||||
if customFlag, ok := genericFlag.(*utils.CustomStringSliceFlag); ok {
|
||||
capturedArgs = customFlag.GetValue()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := app.Run(tt.args)
|
||||
if err != nil {
|
||||
t.Errorf("CLI run error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(capturedArgs) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(capturedArgs), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if capturedArgs[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, capturedArgs[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDockerBuildArgsProcessing(t *testing.T) {
|
||||
// Test that build args are correctly processed in the context of Docker plugin
|
||||
tests := []struct {
|
||||
name string
|
||||
argsNew string
|
||||
expectedCount int
|
||||
expectedFirst string
|
||||
}{
|
||||
{
|
||||
name: "docker build args format",
|
||||
argsNew: "GOOS=linux;GOARCH=amd64;CGO_ENABLED=0",
|
||||
expectedCount: 3,
|
||||
expectedFirst: "GOOS=linux",
|
||||
},
|
||||
{
|
||||
name: "single complex arg with special characters",
|
||||
argsNew: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
expectedCount: 1,
|
||||
expectedFirst: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
},
|
||||
{
|
||||
name: "args with equals and semicolons",
|
||||
argsNew: "API_URL=https://api.example.com;DEBUG=true;VERSION=1.0.0",
|
||||
expectedCount: 3,
|
||||
expectedFirst: "API_URL=https://api.example.com",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.argsNew)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
args := flag.GetValue()
|
||||
if len(args) != tt.expectedCount {
|
||||
t.Errorf("Got %d args, want %d", len(args), tt.expectedCount)
|
||||
return
|
||||
}
|
||||
|
||||
if len(args) > 0 && args[0] != tt.expectedFirst {
|
||||
t.Errorf("Got first arg = %v, want %v", args[0], tt.expectedFirst)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateDockerConfig(t *testing.T) {
|
||||
config := docker.NewConfig()
|
||||
tempDir, err := ioutil.TempDir("", "docker-config-test")
|
||||
|
||||
@@ -30,6 +30,7 @@ import (
|
||||
kaniko "github.com/drone/drone-kaniko"
|
||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
)
|
||||
@@ -132,7 +133,7 @@ func main() {
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(CustomStringSliceFlag),
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "plugin-multiple-build-agrs",
|
||||
@@ -504,7 +505,7 @@ func run(c *cli.Context) error {
|
||||
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||
ExpandTag: c.Bool("expand-tag"),
|
||||
Args: c.StringSlice("args"),
|
||||
ArgsNew: c.Generic("args-new").(*CustomStringSliceFlag).GetValue(),
|
||||
ArgsNew: c.Generic("args-new").(*utils.CustomStringSliceFlag).GetValue(),
|
||||
IsMultipleBuildArgs: c.Bool("plugin-multiple-build-agrs"),
|
||||
Target: c.String("target"),
|
||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func TestCreateDockerConfigForECRWithBaseRegistry(t *testing.T) {
|
||||
@@ -43,3 +45,165 @@ func TestCreateDockerConfigForECRWithBaseRegistry(t *testing.T) {
|
||||
expectedDockerAuth := docker.Auth{Auth: base64.StdEncoding.EncodeToString([]byte(dockerUsername + ":" + dockerPassword))}
|
||||
assert.Equal(t, expectedDockerAuth, config.Auths[dockerRegistry])
|
||||
}
|
||||
|
||||
func TestCustomStringSliceFlagIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "single build arg",
|
||||
input: "ARG1=value1",
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "multiple build args with semicolon",
|
||||
input: "ARG1=value1;ARG2=value2;ARG3=value3",
|
||||
expected: []string{"ARG1=value1", "ARG2=value2", "ARG3=value3"},
|
||||
},
|
||||
{
|
||||
name: "build args with spaces",
|
||||
input: "ARG1=value with spaces;ARG2=another value",
|
||||
expected: []string{"ARG1=value with spaces", "ARG2=another value"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Test the CustomStringSliceFlag directly
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.input)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
result := flag.GetValue()
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(result), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if result[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, result[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIIntegrationWithCustomFlag(t *testing.T) {
|
||||
// Test CLI integration with proper flag setup
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "CLI with single arg",
|
||||
args: []string{"ecr-test", "--args-new", "ARG1=value1"},
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "CLI with multiple args",
|
||||
args: []string{"ecr-test", "--args-new", "ARG1=value1;ARG2=value2"},
|
||||
expected: []string{"ARG1=value1", "ARG2=value2"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
app := cli.NewApp()
|
||||
app.Name = "ecr-test"
|
||||
|
||||
var capturedArgs []string
|
||||
|
||||
app.Flags = []cli.Flag{
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
}
|
||||
|
||||
app.Action = func(c *cli.Context) error {
|
||||
if genericFlag := c.Generic("args-new"); genericFlag != nil {
|
||||
if customFlag, ok := genericFlag.(*utils.CustomStringSliceFlag); ok {
|
||||
capturedArgs = customFlag.GetValue()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := app.Run(tt.args)
|
||||
if err != nil {
|
||||
t.Errorf("CLI run error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(capturedArgs) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(capturedArgs), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if capturedArgs[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, capturedArgs[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestECRBuildArgsProcessing(t *testing.T) {
|
||||
// Test that build args are correctly processed in the context of ECR plugin
|
||||
tests := []struct {
|
||||
name string
|
||||
argsNew string
|
||||
expectedCount int
|
||||
expectedFirst string
|
||||
}{
|
||||
{
|
||||
name: "docker build args format",
|
||||
argsNew: "GOOS=linux;GOARCH=amd64;CGO_ENABLED=0",
|
||||
expectedCount: 3,
|
||||
expectedFirst: "GOOS=linux",
|
||||
},
|
||||
{
|
||||
name: "aws specific args",
|
||||
argsNew: "AWS_REGION=us-west-2;AWS_ACCOUNT_ID=123456789012",
|
||||
expectedCount: 2,
|
||||
expectedFirst: "AWS_REGION=us-west-2",
|
||||
},
|
||||
{
|
||||
name: "single complex arg with special characters",
|
||||
argsNew: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
expectedCount: 1,
|
||||
expectedFirst: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.argsNew)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
args := flag.GetValue()
|
||||
if len(args) != tt.expectedCount {
|
||||
t.Errorf("Got %d args, want %d", len(args), tt.expectedCount)
|
||||
return
|
||||
}
|
||||
|
||||
if len(args) > 0 && args[0] != tt.expectedFirst {
|
||||
t.Errorf("Got first arg = %v, want %v", args[0], tt.expectedFirst)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ import (
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -96,6 +98,17 @@ func main() {
|
||||
Usage: "build args",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS",
|
||||
},
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "plugin-multiple-build-agrs",
|
||||
Usage: "plugin multiple build agrs",
|
||||
EnvVar: "PLUGIN_MULTIPLE_BUILD_ARGS",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "target",
|
||||
Usage: "build target",
|
||||
@@ -401,6 +414,8 @@ func run(c *cli.Context) error {
|
||||
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||
ExpandTag: c.Bool("expand-tag"),
|
||||
Args: c.StringSlice("args"),
|
||||
ArgsNew: c.Generic("args-new").(*utils.CustomStringSliceFlag).GetValue(),
|
||||
IsMultipleBuildArgs: c.Bool("plugin-multiple-build-agrs"),
|
||||
Target: c.String("target"),
|
||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
||||
Mirrors: c.StringSlice("registry-mirrors"),
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func TestCustomStringSliceFlagIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "single build arg",
|
||||
input: "ARG1=value1",
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "multiple build args with semicolon",
|
||||
input: "ARG1=value1;ARG2=value2;ARG3=value3",
|
||||
expected: []string{"ARG1=value1", "ARG2=value2", "ARG3=value3"},
|
||||
},
|
||||
{
|
||||
name: "build args with spaces",
|
||||
input: "ARG1=value with spaces;ARG2=another value",
|
||||
expected: []string{"ARG1=value with spaces", "ARG2=another value"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Test the CustomStringSliceFlag directly
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.input)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
result := flag.GetValue()
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(result), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if result[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, result[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIIntegrationWithCustomFlag(t *testing.T) {
|
||||
// Test CLI integration with proper flag setup
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "CLI with single arg",
|
||||
args: []string{"gar-test", "--args-new", "ARG1=value1"},
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "CLI with multiple args",
|
||||
args: []string{"gar-test", "--args-new", "ARG1=value1;ARG2=value2"},
|
||||
expected: []string{"ARG1=value1", "ARG2=value2"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
app := cli.NewApp()
|
||||
app.Name = "gar-test"
|
||||
|
||||
var capturedArgs []string
|
||||
|
||||
app.Flags = []cli.Flag{
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
}
|
||||
|
||||
app.Action = func(c *cli.Context) error {
|
||||
if genericFlag := c.Generic("args-new"); genericFlag != nil {
|
||||
if customFlag, ok := genericFlag.(*utils.CustomStringSliceFlag); ok {
|
||||
capturedArgs = customFlag.GetValue()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := app.Run(tt.args)
|
||||
if err != nil {
|
||||
t.Errorf("CLI run error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(capturedArgs) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(capturedArgs), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if capturedArgs[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, capturedArgs[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironmentVariableIntegration(t *testing.T) {
|
||||
// Test that environment variables work with CustomStringSliceFlag
|
||||
originalEnv := os.Getenv("PLUGIN_BUILD_ARGS_NEW")
|
||||
defer func() {
|
||||
if originalEnv != "" {
|
||||
os.Setenv("PLUGIN_BUILD_ARGS_NEW", originalEnv)
|
||||
} else {
|
||||
os.Unsetenv("PLUGIN_BUILD_ARGS_NEW")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Setenv("PLUGIN_BUILD_ARGS_NEW", "ENV_ARG1=env_value1;ENV_ARG2=env_value2")
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Flags = []cli.Flag{
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
}
|
||||
|
||||
var capturedArgs []string
|
||||
app.Action = func(c *cli.Context) error {
|
||||
if flag := c.Generic("args-new"); flag != nil {
|
||||
if customFlag, ok := flag.(*utils.CustomStringSliceFlag); ok {
|
||||
capturedArgs = customFlag.GetValue()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := app.Run([]string{"test"})
|
||||
if err != nil {
|
||||
t.Errorf("App.Run() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
expected := []string{"ENV_ARG1=env_value1", "ENV_ARG2=env_value2"}
|
||||
if len(capturedArgs) != len(expected) {
|
||||
t.Errorf("Environment variable test: got %d args, want %d", len(capturedArgs), len(expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, exp := range expected {
|
||||
if capturedArgs[i] != exp {
|
||||
t.Errorf("Environment variable test: got arg[%d] = %v, want %v", i, capturedArgs[i], exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGARBuildArgsProcessing(t *testing.T) {
|
||||
// Test that build args are correctly processed in the context of GAR plugin
|
||||
tests := []struct {
|
||||
name string
|
||||
argsNew string
|
||||
expectedCount int
|
||||
expectedFirst string
|
||||
}{
|
||||
{
|
||||
name: "docker build args format",
|
||||
argsNew: "GOOS=linux;GOARCH=amd64;CGO_ENABLED=0",
|
||||
expectedCount: 3,
|
||||
expectedFirst: "GOOS=linux",
|
||||
},
|
||||
{
|
||||
name: "google cloud specific args",
|
||||
argsNew: "GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json;PROJECT_ID=my-project",
|
||||
expectedCount: 2,
|
||||
expectedFirst: "GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json",
|
||||
},
|
||||
{
|
||||
name: "single complex arg with special characters",
|
||||
argsNew: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
expectedCount: 1,
|
||||
expectedFirst: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.argsNew)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
args := flag.GetValue()
|
||||
if len(args) != tt.expectedCount {
|
||||
t.Errorf("Got %d args, want %d", len(args), tt.expectedCount)
|
||||
return
|
||||
}
|
||||
|
||||
if len(args) > 0 && args[0] != tt.expectedFirst {
|
||||
t.Errorf("Got first arg = %v, want %v", args[0], tt.expectedFirst)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGARRegistryFormatting(t *testing.T) {
|
||||
// Test GAR-specific registry formatting
|
||||
tests := []struct {
|
||||
name string
|
||||
registry string
|
||||
repo string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "standard GAR format",
|
||||
registry: "us-central1-docker.pkg.dev",
|
||||
repo: "my-project/my-repo/my-image",
|
||||
expected: "us-central1-docker.pkg.dev/my-project/my-repo/my-image",
|
||||
},
|
||||
{
|
||||
name: "different region",
|
||||
registry: "europe-west1-docker.pkg.dev",
|
||||
repo: "project123/repo456/image789",
|
||||
expected: "europe-west1-docker.pkg.dev/project123/repo456/image789",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// This would be the format used in the GAR plugin
|
||||
result := tt.registry + "/" + tt.repo
|
||||
if result != tt.expected {
|
||||
t.Errorf("GAR formatting: got %v, want %v", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGARAuthSetup(t *testing.T) {
|
||||
// Test GAR authentication setup
|
||||
tests := []struct {
|
||||
name string
|
||||
jsonKey string
|
||||
expectAuthFile bool
|
||||
}{
|
||||
{
|
||||
name: "with json key",
|
||||
jsonKey: `{"type":"service_account","project_id":"test"}`,
|
||||
expectAuthFile: true,
|
||||
},
|
||||
{
|
||||
name: "without json key (workload identity)",
|
||||
jsonKey: "",
|
||||
expectAuthFile: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// This simulates the auth setup logic
|
||||
hasAuthFile := tt.jsonKey != ""
|
||||
if hasAuthFile != tt.expectAuthFile {
|
||||
t.Errorf("Auth file expectation: got %v, want %v", hasAuthFile, tt.expectAuthFile)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
kaniko "github.com/drone/drone-kaniko"
|
||||
"github.com/drone/drone-kaniko/pkg/artifact"
|
||||
"github.com/drone/drone-kaniko/pkg/docker"
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -333,6 +334,18 @@ func main() {
|
||||
Usage: "Number of retries for downloading base images.",
|
||||
EnvVar: "PLUGIN_IMAGE_DOWNLOAD_RETRY",
|
||||
},
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "plugin-multiple-build-agrs",
|
||||
Usage: "plugin multiple build agrs",
|
||||
EnvVar: "PLUGIN_MULTIPLE_BUILD_ARGS",
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
@@ -378,6 +391,8 @@ func run(c *cli.Context) error {
|
||||
AutoTagSuffix: c.String("auto-tag-suffix"),
|
||||
ExpandTag: c.Bool("expand-tag"),
|
||||
Args: c.StringSlice("args"),
|
||||
ArgsNew: c.Generic("args-new").(*utils.CustomStringSliceFlag).GetValue(),
|
||||
IsMultipleBuildArgs: c.Bool("plugin-multiple-build-agrs"),
|
||||
Target: c.String("target"),
|
||||
Repo: fmt.Sprintf("%s/%s", c.String("registry"), c.String("repo")),
|
||||
Mirrors: c.StringSlice("registry-mirrors"),
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone-kaniko/pkg/utils"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func TestCustomStringSliceFlagIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
name: "single build arg",
|
||||
input: "ARG1=value1",
|
||||
expected: []string{"ARG1=value1"},
|
||||
},
|
||||
{
|
||||
name: "multiple build args with semicolon",
|
||||
input: "ARG1=value1;ARG2=value2;ARG3=value3",
|
||||
expected: []string{"ARG1=value1", "ARG2=value2", "ARG3=value3"},
|
||||
},
|
||||
{
|
||||
name: "build args with spaces",
|
||||
input: "ARG1=value with spaces;ARG2=another value",
|
||||
expected: []string{"ARG1=value with spaces", "ARG2=another value"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Test the CustomStringSliceFlag directly
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.input)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
result := flag.GetValue()
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("Got %d args, want %d", len(result), len(tt.expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expected {
|
||||
if result[i] != expected {
|
||||
t.Errorf("Got arg[%d] = %v, want %v", i, result[i], expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnvironmentVariableIntegration(t *testing.T) {
|
||||
// Test that environment variables work with CustomStringSliceFlag
|
||||
originalEnv := os.Getenv("PLUGIN_BUILD_ARGS_NEW")
|
||||
defer func() {
|
||||
if originalEnv != "" {
|
||||
os.Setenv("PLUGIN_BUILD_ARGS_NEW", originalEnv)
|
||||
} else {
|
||||
os.Unsetenv("PLUGIN_BUILD_ARGS_NEW")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Setenv("PLUGIN_BUILD_ARGS_NEW", "ENV_ARG1=env_value1;ENV_ARG2=env_value2")
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Flags = []cli.Flag{
|
||||
cli.GenericFlag{
|
||||
Name: "args-new",
|
||||
Usage: "build args new",
|
||||
EnvVar: "PLUGIN_BUILD_ARGS_NEW",
|
||||
Value: new(utils.CustomStringSliceFlag),
|
||||
},
|
||||
}
|
||||
|
||||
var capturedArgs []string
|
||||
app.Action = func(c *cli.Context) error {
|
||||
if flag := c.Generic("args-new"); flag != nil {
|
||||
if customFlag, ok := flag.(*utils.CustomStringSliceFlag); ok {
|
||||
capturedArgs = customFlag.GetValue()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
err := app.Run([]string{"test"})
|
||||
if err != nil {
|
||||
t.Errorf("App.Run() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
expected := []string{"ENV_ARG1=env_value1", "ENV_ARG2=env_value2"}
|
||||
if len(capturedArgs) != len(expected) {
|
||||
t.Errorf("Environment variable test: got %d args, want %d", len(capturedArgs), len(expected))
|
||||
return
|
||||
}
|
||||
|
||||
for i, exp := range expected {
|
||||
if capturedArgs[i] != exp {
|
||||
t.Errorf("Environment variable test: got arg[%d] = %v, want %v", i, capturedArgs[i], exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGCRBuildArgsProcessing(t *testing.T) {
|
||||
// Test that build args are correctly processed in the context of GCR plugin
|
||||
tests := []struct {
|
||||
name string
|
||||
argsNew string
|
||||
expectedCount int
|
||||
expectedFirst string
|
||||
}{
|
||||
{
|
||||
name: "docker build args format",
|
||||
argsNew: "GOOS=linux;GOARCH=amd64;CGO_ENABLED=0",
|
||||
expectedCount: 3,
|
||||
expectedFirst: "GOOS=linux",
|
||||
},
|
||||
{
|
||||
name: "google cloud specific args",
|
||||
argsNew: "GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json;PROJECT_ID=my-project",
|
||||
expectedCount: 2,
|
||||
expectedFirst: "GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json",
|
||||
},
|
||||
{
|
||||
name: "single complex arg with special characters",
|
||||
argsNew: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
expectedCount: 1,
|
||||
expectedFirst: "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
flag := &utils.CustomStringSliceFlag{}
|
||||
err := flag.Set(tt.argsNew)
|
||||
if err != nil {
|
||||
t.Errorf("Set() error = %v, want nil", err)
|
||||
return
|
||||
}
|
||||
|
||||
args := flag.GetValue()
|
||||
if len(args) != tt.expectedCount {
|
||||
t.Errorf("Got %d args, want %d", len(args), tt.expectedCount)
|
||||
return
|
||||
}
|
||||
|
||||
if len(args) > 0 && args[0] != tt.expectedFirst {
|
||||
t.Errorf("Got first arg = %v, want %v", args[0], tt.expectedFirst)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGCRRegistryFormatting(t *testing.T) {
|
||||
// Test GCR-specific registry formatting
|
||||
tests := []struct {
|
||||
name string
|
||||
registry string
|
||||
repo string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "standard GCR format",
|
||||
registry: "gcr.io",
|
||||
repo: "my-project/my-image",
|
||||
expected: "gcr.io/my-project/my-image",
|
||||
},
|
||||
{
|
||||
name: "regional GCR",
|
||||
registry: "us.gcr.io",
|
||||
repo: "project123/image456",
|
||||
expected: "us.gcr.io/project123/image456",
|
||||
},
|
||||
{
|
||||
name: "european GCR",
|
||||
registry: "eu.gcr.io",
|
||||
repo: "my-eu-project/my-app",
|
||||
expected: "eu.gcr.io/my-eu-project/my-app",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// This would be the format used in the GCR plugin
|
||||
result := tt.registry + "/" + tt.repo
|
||||
if result != tt.expected {
|
||||
t.Errorf("GCR formatting: got %v, want %v", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGCRJSONKeyValidation(t *testing.T) {
|
||||
// Test JSON key validation for GCR authentication
|
||||
tests := []struct {
|
||||
name string
|
||||
jsonKey string
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
name: "empty json key",
|
||||
jsonKey: "",
|
||||
expectErr: false, // Empty is allowed (workload identity)
|
||||
},
|
||||
{
|
||||
name: "valid json structure",
|
||||
jsonKey: `{"type":"service_account","project_id":"test","private_key_id":"123"}`,
|
||||
expectErr: false,
|
||||
},
|
||||
{
|
||||
name: "invalid json",
|
||||
jsonKey: `{invalid json}`,
|
||||
expectErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// This simulates the JSON key validation that would happen in GCR
|
||||
if tt.jsonKey != "" {
|
||||
var data map[string]interface{}
|
||||
err := json.Unmarshal([]byte(tt.jsonKey), &data)
|
||||
if err != nil && !tt.expectErr {
|
||||
t.Errorf("Expected no error for JSON key, got %v", err)
|
||||
}
|
||||
if err == nil && tt.expectErr {
|
||||
t.Errorf("Expected error for JSON key, got nil")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGCRAuthSetup(t *testing.T) {
|
||||
// Test GCR authentication setup
|
||||
tests := []struct {
|
||||
name string
|
||||
jsonKey string
|
||||
expectAuthFile bool
|
||||
}{
|
||||
{
|
||||
name: "with json key",
|
||||
jsonKey: `{"type":"service_account","project_id":"test"}`,
|
||||
expectAuthFile: true,
|
||||
},
|
||||
{
|
||||
name: "without json key (workload identity)",
|
||||
jsonKey: "",
|
||||
expectAuthFile: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// This simulates the auth setup logic
|
||||
hasAuthFile := tt.jsonKey != ""
|
||||
if hasAuthFile != tt.expectAuthFile {
|
||||
t.Errorf("Auth file expectation: got %v, want %v", hasAuthFile, tt.expectAuthFile)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package azure
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const DefaultResource = "https://management.azure.com/"
|
||||
const defaultAuthorityHost = "https://login.microsoftonline.com"
|
||||
const defaultHTTPTimeout = 30 * time.Second
|
||||
|
||||
// GetAADAccessTokenViaClientAssertion exchanges an external OIDC ID token for an Azure AD access token
|
||||
|
||||
func GetAADAccessTokenViaClientAssertion(ctx context.Context, tenantID, clientID, oidcToken, authorityHost string) (string, error) {
|
||||
resource := DefaultResource
|
||||
|
||||
form := url.Values{
|
||||
"client_id": {clientID},
|
||||
"scope": {resource + ".default"},
|
||||
"grant_type": {"client_credentials"},
|
||||
"client_assertion_type": {"urn:ietf:params:oauth:client-assertion-type:jwt-bearer"},
|
||||
"client_assertion": {oidcToken},
|
||||
}
|
||||
base := authorityHost
|
||||
if strings.TrimSpace(base) == "" {
|
||||
base = defaultAuthorityHost
|
||||
}
|
||||
base = strings.TrimRight(base, "/")
|
||||
endpoint := fmt.Sprintf("%s/%s/oauth2/v2.0/token", base, tenantID)
|
||||
client := &http.Client{Timeout: defaultHTTPTimeout}
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
var aadErr struct {
|
||||
Error string `json:"error"`
|
||||
ErrorDescription string `json:"error_description"`
|
||||
}
|
||||
limited := io.LimitedReader{R: resp.Body, N: 4096}
|
||||
_ = json.NewDecoder(&limited).Decode(&aadErr)
|
||||
if aadErr.Error != "" {
|
||||
return "", fmt.Errorf("AAD token request failed: status=%d, error=%s", resp.StatusCode, aadErr.Error)
|
||||
}
|
||||
return "", fmt.Errorf("AAD token request failed: status=%d", resp.StatusCode)
|
||||
}
|
||||
var payload struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if payload.AccessToken == "" {
|
||||
return "", fmt.Errorf("AAD token response missing access_token")
|
||||
}
|
||||
return payload.AccessToken, nil
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package azure
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetAADAccessTokenViaClientAssertion_Success(t *testing.T) {
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Fatalf("expected POST, got %s", r.Method)
|
||||
}
|
||||
if ct := r.Header.Get("Content-Type"); !strings.Contains(ct, "application/x-www-form-urlencoded") {
|
||||
t.Fatalf("expected form content-type, got %s", ct)
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
t.Fatalf("failed parsing form: %v", err)
|
||||
}
|
||||
assertEq(t, r.Form.Get("client_id"), "client")
|
||||
assertEq(t, r.Form.Get("grant_type"), "client_credentials")
|
||||
assertEq(t, r.Form.Get("client_assertion_type"), "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
|
||||
assertEq(t, r.Form.Get("client_assertion"), "idtoken")
|
||||
assertEq(t, r.Form.Get("scope"), DefaultResource+".default")
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"access_token":"AT","token_type":"Bearer","expires_in":3600}`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
tok, err := GetAADAccessTokenViaClientAssertion(context.Background(), "tenant", "client", "idtoken", ts.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if tok != "AT" {
|
||||
t.Fatalf("expected access token AT, got %q", tok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAADAccessTokenViaClientAssertion_400WithErrorField(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(`{"error":"invalid_client","error_description":"bad"}`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
_, err := GetAADAccessTokenViaClientAssertion(context.Background(), "tenant", "client", "idtoken", ts.URL)
|
||||
if err == nil || !strings.Contains(err.Error(), "status=400") || !strings.Contains(err.Error(), "invalid_client") {
|
||||
t.Fatalf("expected 400 with invalid_client error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAADAccessTokenViaClientAssertion_400WithoutErrorField(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte("{}"))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
_, err := GetAADAccessTokenViaClientAssertion(context.Background(), "tenant", "client", "idtoken", ts.URL)
|
||||
if err == nil || !strings.Contains(err.Error(), "status=400") {
|
||||
t.Fatalf("expected 400 error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAADAccessTokenViaClientAssertion_MalformedJSON(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("not-json"))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
_, err := GetAADAccessTokenViaClientAssertion(context.Background(), "tenant", "client", "idtoken", ts.URL)
|
||||
if err == nil {
|
||||
t.Fatalf("expected JSON decode error, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAADAccessTokenViaClientAssertion_MissingAccessToken(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"token_type":"Bearer","expires_in":3600}`))
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
_, err := GetAADAccessTokenViaClientAssertion(context.Background(), "tenant", "client", "idtoken", ts.URL)
|
||||
if err == nil || !strings.Contains(err.Error(), "missing access_token") {
|
||||
t.Fatalf("expected missing access_token error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func assertEq(t *testing.T, got, want string) {
|
||||
t.Helper()
|
||||
if got != want {
|
||||
t.Fatalf("mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -10,6 +10,7 @@ type CustomStringSliceFlag struct {
|
||||
Value []string
|
||||
}
|
||||
|
||||
// GetValue returns the slice of strings stored in the flag
|
||||
func (f *CustomStringSliceFlag) GetValue() []string {
|
||||
if f.Value == nil {
|
||||
return make([]string, 0)
|
||||
@@ -17,6 +18,7 @@ func (f *CustomStringSliceFlag) GetValue() []string {
|
||||
return f.Value
|
||||
}
|
||||
|
||||
// String returns a string representation of the flag
|
||||
func (f *CustomStringSliceFlag) String() string {
|
||||
if f.Value == nil {
|
||||
return ""
|
||||
@@ -24,6 +26,7 @@ func (f *CustomStringSliceFlag) String() string {
|
||||
return strings.Join(f.Value, ";")
|
||||
}
|
||||
|
||||
// Set sets the value of the flag from a string
|
||||
func (f *CustomStringSliceFlag) Set(v string) error {
|
||||
for _, s := range strings.Split(v, ";") {
|
||||
s = strings.TrimSpace(s)
|
||||
Reference in New Issue
Block a user