|
| 1 | +// Copyright (c) arkade author(s) 2022. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +package oci |
| 5 | + |
| 6 | +import ( |
| 7 | + "reflect" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/google/go-containerregistry/pkg/crane" |
| 13 | + v1 "github.com/google/go-containerregistry/pkg/v1" |
| 14 | +) |
| 15 | + |
| 16 | +func TestResolveShortcutImage(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + input string |
| 20 | + wantImage string |
| 21 | + wantAnonymousAuth bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "vmmeter shortcut uses anonymous auth", |
| 25 | + input: "vmmeter", |
| 26 | + wantImage: "ghcr.io/openfaasltd/vmmeter", |
| 27 | + wantAnonymousAuth: true, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "slicer shortcut uses anonymous auth", |
| 31 | + input: "slicer", |
| 32 | + wantImage: "ghcr.io/openfaasltd/slicer", |
| 33 | + wantAnonymousAuth: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "superterm shortcut uses anonymous auth", |
| 37 | + input: "superterm", |
| 38 | + wantImage: "ghcr.io/openfaasltd/superterm", |
| 39 | + wantAnonymousAuth: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "k3sup-pro shortcut uses anonymous auth", |
| 43 | + input: "k3sup-pro", |
| 44 | + wantImage: "ghcr.io/openfaasltd/k3sup-pro", |
| 45 | + wantAnonymousAuth: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "fully qualified image is unchanged", |
| 49 | + input: "ghcr.io/openfaasltd/custom-tool", |
| 50 | + wantImage: "ghcr.io/openfaasltd/custom-tool", |
| 51 | + wantAnonymousAuth: false, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tc := range tests { |
| 56 | + t.Run(tc.name, func(t *testing.T) { |
| 57 | + gotImage, gotAnonymousAuth := resolveShortcutImage(tc.input) |
| 58 | + if gotImage != tc.wantImage { |
| 59 | + t.Fatalf("want image %q, got %q", tc.wantImage, gotImage) |
| 60 | + } |
| 61 | + if gotAnonymousAuth != tc.wantAnonymousAuth { |
| 62 | + t.Fatalf("want anonymous auth %v, got %v", tc.wantAnonymousAuth, gotAnonymousAuth) |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestBuildPullOptions(t *testing.T) { |
| 69 | + platform := &v1.Platform{Architecture: "amd64", OS: "linux"} |
| 70 | + |
| 71 | + t.Run("default uses keychain auth", func(t *testing.T) { |
| 72 | + opts := crane.GetOptions(buildPullOptions(platform, false)...) |
| 73 | + if opts.Platform != platform { |
| 74 | + t.Fatalf("want platform %p, got %p", platform, opts.Platform) |
| 75 | + } |
| 76 | + |
| 77 | + authOptionName := runtime.FuncForPC(reflect.ValueOf(opts.Remote[0]).Pointer()).Name() |
| 78 | + if !strings.Contains(authOptionName, "WithAuthFromKeychain") { |
| 79 | + t.Fatalf("want keychain auth option, got %q", authOptionName) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("anonymous auth overrides keychain", func(t *testing.T) { |
| 84 | + opts := crane.GetOptions(buildPullOptions(platform, true)...) |
| 85 | + if opts.Platform != platform { |
| 86 | + t.Fatalf("want platform %p, got %p", platform, opts.Platform) |
| 87 | + } |
| 88 | + |
| 89 | + authOptionName := runtime.FuncForPC(reflect.ValueOf(opts.Remote[0]).Pointer()).Name() |
| 90 | + if !strings.Contains(authOptionName, "WithAuth") || strings.Contains(authOptionName, "WithAuthFromKeychain") { |
| 91 | + t.Fatalf("want anonymous auth option, got %q", authOptionName) |
| 92 | + } |
| 93 | + }) |
| 94 | +} |
0 commit comments