|
| 1 | +/* |
| 2 | +Copyright 2022 The Tekton 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | +) |
| 24 | + |
| 25 | +func TestSelectCommandForPlatform(t *testing.T) { |
| 26 | + for _, c := range []struct { |
| 27 | + desc string |
| 28 | + m map[string][]string |
| 29 | + plat string |
| 30 | + want []string |
| 31 | + wantErr bool |
| 32 | + }{{ |
| 33 | + desc: "platform exists", |
| 34 | + m: map[string][]string{ |
| 35 | + "linux/amd64": {"my", "command"}, |
| 36 | + "linux/s390x": {"other", "one"}, |
| 37 | + }, |
| 38 | + plat: "linux/amd64", |
| 39 | + want: []string{"my", "command"}, |
| 40 | + }, { |
| 41 | + desc: "platform not found", |
| 42 | + m: map[string][]string{ |
| 43 | + "linux/amd64": {"my", "command"}, |
| 44 | + "linux/s390x": {"other", "one"}, |
| 45 | + }, |
| 46 | + plat: "linux/ppc64le", |
| 47 | + wantErr: true, |
| 48 | + }, { |
| 49 | + desc: "platform fallback", |
| 50 | + m: map[string][]string{ |
| 51 | + "linux/amd64": {"my", "command"}, |
| 52 | + "linux/s390x": {"other", "one"}, |
| 53 | + }, |
| 54 | + plat: "linux/amd64/v8", |
| 55 | + want: []string{"my", "command"}, |
| 56 | + }, { |
| 57 | + desc: "platform fallback not needed", |
| 58 | + m: map[string][]string{ |
| 59 | + "linux/amd64": {"other", "one"}, |
| 60 | + "linux/amd64/v8": {"my", "command"}, |
| 61 | + }, |
| 62 | + plat: "linux/amd64/v8", |
| 63 | + want: []string{"my", "command"}, |
| 64 | + }} { |
| 65 | + t.Run(c.desc, func(t *testing.T) { |
| 66 | + got, err := selectCommandForPlatform(c.m, c.plat) |
| 67 | + if err != nil { |
| 68 | + if c.wantErr { |
| 69 | + return |
| 70 | + } |
| 71 | + t.Fatalf("Unexpected error: %v", err) |
| 72 | + } |
| 73 | + if d := cmp.Diff(c.want, got); d != "" { |
| 74 | + t.Fatalf("Diff(-want,+got):\n%s", d) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments