|
| 1 | +/* |
| 2 | +Copyright 2025 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package aws_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + |
| 24 | + "github.com/fluxcd/pkg/auth/aws" |
| 25 | +) |
| 26 | + |
| 27 | +func TestParseRegistry(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + registry string |
| 30 | + wantAccountID string |
| 31 | + wantRegion string |
| 32 | + wantOK bool |
| 33 | + }{ |
| 34 | + { |
| 35 | + registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com/foo:v1", |
| 36 | + wantAccountID: "012345678901", |
| 37 | + wantRegion: "us-east-1", |
| 38 | + wantOK: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com/foo", |
| 42 | + wantAccountID: "012345678901", |
| 43 | + wantRegion: "us-east-1", |
| 44 | + wantOK: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com", |
| 48 | + wantAccountID: "012345678901", |
| 49 | + wantRegion: "us-east-1", |
| 50 | + wantOK: true, |
| 51 | + }, |
| 52 | + { |
| 53 | + registry: "https://012345678901.dkr.ecr.us-east-1.amazonaws.com/v2/part/part", |
| 54 | + wantAccountID: "012345678901", |
| 55 | + wantRegion: "us-east-1", |
| 56 | + wantOK: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + registry: "012345678901.dkr.ecr.cn-north-1.amazonaws.com.cn/foo", |
| 60 | + wantAccountID: "012345678901", |
| 61 | + wantRegion: "cn-north-1", |
| 62 | + wantOK: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + registry: "012345678901.dkr.ecr-fips.us-gov-west-1.amazonaws.com", |
| 66 | + wantAccountID: "012345678901", |
| 67 | + wantRegion: "us-gov-west-1", |
| 68 | + wantOK: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + registry: "012345678901.dkr.ecr.us-secret-region.sc2s.sgov.gov", |
| 72 | + wantAccountID: "012345678901", |
| 73 | + wantRegion: "us-secret-region", |
| 74 | + wantOK: true, |
| 75 | + }, |
| 76 | + { |
| 77 | + registry: "012345678901.dkr.ecr-fips.us-ts-region.c2s.ic.gov", |
| 78 | + wantAccountID: "012345678901", |
| 79 | + wantRegion: "us-ts-region", |
| 80 | + wantOK: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + registry: "012345678901.dkr.ecr.uk-region.cloud.adc-e.uk", |
| 84 | + wantAccountID: "012345678901", |
| 85 | + wantRegion: "uk-region", |
| 86 | + wantOK: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + registry: "012345678901.dkr.ecr.us-ts-region.csp.hci.ic.gov", |
| 90 | + wantAccountID: "012345678901", |
| 91 | + wantRegion: "us-ts-region", |
| 92 | + wantOK: true, |
| 93 | + }, |
| 94 | + // TODO: Fix: this invalid registry is allowed by the regex. |
| 95 | + // { |
| 96 | + // registry: ".dkr.ecr.error.amazonaws.com", |
| 97 | + // wantOK: false, |
| 98 | + // }, |
| 99 | + { |
| 100 | + registry: "gcr.io/foo/bar:baz", |
| 101 | + wantOK: false, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.registry, func(t *testing.T) { |
| 107 | + g := NewWithT(t) |
| 108 | + |
| 109 | + accId, region, ok := aws.ParseRegistry(tt.registry) |
| 110 | + g.Expect(ok).To(Equal(tt.wantOK), "unexpected OK") |
| 111 | + g.Expect(accId).To(Equal(tt.wantAccountID), "unexpected account IDs") |
| 112 | + g.Expect(region).To(Equal(tt.wantRegion), "unexpected regions") |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments