|
| 1 | +/* |
| 2 | +Copyright 2022 The KCP 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 shared |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/kcp-dev/logicalcluster" |
| 25 | +) |
| 26 | + |
| 27 | +func TestLocatorFromAnnotations(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + annotations map[string]string |
| 31 | + want *NamespaceLocator |
| 32 | + wantFound bool |
| 33 | + wantErrs []string |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "no annotation", |
| 37 | + wantFound: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "garbage", |
| 41 | + annotations: map[string]string{ |
| 42 | + NamespaceLocatorAnnotation: "garbage", |
| 43 | + }, |
| 44 | + wantErrs: []string{"invalid character"}, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "happy case", |
| 48 | + annotations: map[string]string{ |
| 49 | + NamespaceLocatorAnnotation: `{"syncTarget":{"workspace":"test-workspace","name":"test-name","uid":"test-uid"},"workspace":"test-workspace","namespace":"test-namespace"}`, |
| 50 | + }, |
| 51 | + want: &NamespaceLocator{ |
| 52 | + SyncTarget: SyncTargetLocator{ |
| 53 | + Workspace: "test-workspace", |
| 54 | + Name: "test-name", |
| 55 | + UID: "test-uid", |
| 56 | + }, |
| 57 | + Workspace: logicalcluster.New("test-workspace"), |
| 58 | + Namespace: "test-namespace", |
| 59 | + }, |
| 60 | + wantFound: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "format up to v0.6.0", |
| 64 | + annotations: map[string]string{ |
| 65 | + NamespaceLocatorAnnotation: `{"syncTarget":{"path":"test-workspace","name":"test-name","uid":"test-uid"},"workspace":"test-workspace","namespace":"test-namespace"}`, |
| 66 | + }, |
| 67 | + want: &NamespaceLocator{ |
| 68 | + SyncTarget: SyncTargetLocator{ |
| 69 | + Workspace: "test-workspace", |
| 70 | + Name: "test-name", |
| 71 | + UID: "test-uid", |
| 72 | + }, |
| 73 | + Workspace: logicalcluster.New("test-workspace"), |
| 74 | + Namespace: "test-namespace", |
| 75 | + }, |
| 76 | + wantFound: true, |
| 77 | + }, |
| 78 | + } |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + got, gotFound, err := LocatorFromAnnotations(tt.annotations) |
| 82 | + if (err != nil) != (len(tt.wantErrs) > 0) { |
| 83 | + t.Errorf("LocatorFromAnnotations() error = %q, wantErrs %v", err.Error(), tt.wantErrs) |
| 84 | + return |
| 85 | + } else if err != nil { |
| 86 | + for _, wantErr := range tt.wantErrs { |
| 87 | + if !strings.Contains(err.Error(), wantErr) { |
| 88 | + t.Errorf("LocatorFromAnnotations() error = %q, wantErrs %q", err.Error(), wantErr) |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + if !reflect.DeepEqual(got, tt.want) { |
| 94 | + t.Errorf("LocatorFromAnnotations() got = %v, want %v", got, tt.want) |
| 95 | + } |
| 96 | + if gotFound != tt.wantFound { |
| 97 | + t.Errorf("LocatorFromAnnotations() gotFound = %v, want %v", gotFound, tt.wantFound) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments