knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/contexts_test.go (about) 1 /* 2 Copyright 2019 The Knative 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 apis 18 19 import ( 20 "context" 21 "net/http" 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 authenticationv1 "k8s.io/api/authentication/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 ) 28 29 func TestContexts(t *testing.T) { 30 ctx := context.Background() 31 32 tests := []struct { 33 name string 34 ctx context.Context 35 check func(context.Context) bool 36 want bool 37 }{{ 38 name: "is in create", 39 ctx: WithinCreate(ctx), 40 check: IsInCreate, 41 want: true, 42 }, { 43 name: "not in create (bare)", 44 ctx: ctx, 45 check: IsInCreate, 46 want: false, 47 }, { 48 name: "not in create (update)", 49 ctx: WithinUpdate(ctx, struct{}{}), 50 check: IsInCreate, 51 want: false, 52 }, { 53 name: "is in delete", 54 ctx: WithinDelete(ctx), 55 check: IsInDelete, 56 want: true, 57 }, { 58 name: "not in delete (bare)", 59 ctx: ctx, 60 check: IsInDelete, 61 want: false, 62 }, { 63 name: "not in delete (create)", 64 ctx: WithinCreate(ctx), 65 check: IsInDelete, 66 want: false, 67 }, { 68 name: "is in update", 69 ctx: WithinUpdate(ctx, struct{}{}), 70 check: IsInUpdate, 71 want: true, 72 }, { 73 name: "is in update", 74 ctx: WithinUpdate(ctx, struct{}{}), 75 check: IsInStatusUpdate, 76 want: false, 77 }, { 78 name: "is in update (subresource)", 79 ctx: WithinSubResourceUpdate(ctx, struct{}{}, "scale"), 80 check: IsInUpdate, 81 want: true, 82 }, { 83 name: "is not in status update", 84 ctx: WithinSubResourceUpdate(ctx, struct{}{}, "scale"), 85 check: IsInStatusUpdate, 86 want: false, 87 }, { 88 name: "is in status update", 89 ctx: WithinSubResourceUpdate(ctx, struct{}{}, "status"), 90 check: IsInStatusUpdate, 91 want: true, 92 }, { 93 name: "not in update (bare)", 94 ctx: ctx, 95 check: IsInUpdate, 96 want: false, 97 }, { 98 name: "not in status update (bare)", 99 ctx: ctx, 100 check: IsInStatusUpdate, 101 want: false, 102 }, { 103 name: "not in update (create)", 104 ctx: WithinCreate(ctx), 105 check: IsInUpdate, 106 want: false, 107 }, { 108 name: "in spec", 109 ctx: WithinSpec(ctx), 110 check: IsInSpec, 111 want: true, 112 }, { 113 name: "not in spec", 114 ctx: WithinStatus(ctx), 115 check: IsInSpec, 116 want: false, 117 }, { 118 name: "in status", 119 ctx: WithinStatus(ctx), 120 check: IsInStatus, 121 want: true, 122 }, { 123 name: "not in status", 124 ctx: WithinSpec(ctx), 125 check: IsInStatus, 126 want: false, 127 }, { 128 name: "disallow deprecated", 129 ctx: DisallowDeprecated(ctx), 130 check: IsDeprecatedAllowed, 131 want: false, 132 }, { 133 name: "allow deprecated", 134 ctx: ctx, 135 check: IsDeprecatedAllowed, 136 want: true, 137 }, { 138 name: "allow different namespace", 139 ctx: AllowDifferentNamespace(ctx), 140 check: IsDifferentNamespaceAllowed, 141 want: true, 142 }, { 143 name: "don't allow different namespace", 144 ctx: ctx, 145 check: IsDifferentNamespaceAllowed, 146 want: false, 147 }, { 148 name: "not in dry run", 149 ctx: ctx, 150 check: IsDryRun, 151 want: false, 152 }, { 153 name: "in dry run", 154 ctx: WithDryRun(ctx), 155 check: IsDryRun, 156 want: true, 157 }, { 158 name: "within parent", 159 ctx: WithDryRun(ctx), 160 check: IsWithinParent, 161 want: false, 162 }, { 163 name: "in dry run", 164 ctx: WithinParent(ctx, metav1.ObjectMeta{Name: "jack"}), 165 check: IsWithinParent, 166 want: true, 167 }} 168 169 for _, tc := range tests { 170 t.Run(tc.name, func(t *testing.T) { 171 got := tc.check(tc.ctx) 172 if tc.want != got { 173 t.Errorf("check() = %v, wanted %v", got, tc.want) 174 } 175 }) 176 } 177 } 178 179 func TestGetBaseline(t *testing.T) { 180 ctx := context.Background() 181 182 if got := GetBaseline(ctx); got != nil { 183 t.Errorf("GetBaseline() = %v, wanted %v", got, nil) 184 } 185 186 var foo interface{} = "this is the object" 187 ctx = WithinUpdate(ctx, foo) 188 189 if want, got := foo, GetBaseline(ctx); got != want { 190 t.Errorf("GetBaseline() = %v, wanted %v", got, want) 191 } 192 } 193 194 func TestGetUserInfo(t *testing.T) { 195 ctx := context.Background() 196 197 if got := GetUserInfo(ctx); got != nil { 198 t.Errorf("GetUserInfo() = %v, wanted %v", got, nil) 199 } 200 201 bob := &authenticationv1.UserInfo{Username: "bob"} 202 ctx = WithUserInfo(ctx, bob) 203 204 if want, got := bob, GetUserInfo(ctx); got != want { 205 t.Errorf("GetUserInfo() = %v, wanted %v", got, want) 206 } 207 } 208 209 func TestParentMeta(t *testing.T) { 210 ctx := context.Background() 211 212 if got, want := ParentMeta(ctx), (metav1.ObjectMeta{}); !cmp.Equal(want, got) { 213 t.Errorf("ParentMeta() = %v, wanted %v", got, want) 214 } 215 216 want := metav1.ObjectMeta{ 217 Name: "foo", 218 Namespace: "bar", 219 } 220 ctx = WithinParent(ctx, want) 221 222 if got := ParentMeta(ctx); !cmp.Equal(want, got) { 223 t.Errorf("ParentMeta() = %v, wanted %v", got, want) 224 } 225 } 226 227 func TestGetHTTPRequest(t *testing.T) { 228 ctx := context.Background() 229 230 if got := GetHTTPRequest(ctx); got != nil { 231 t.Errorf("GetHTTPRequest() = %v, wanted %v", got, nil) 232 } 233 234 req, err := http.NewRequest(http.MethodGet, "https://google.com", nil) 235 if err != nil { 236 t.Fatalf("NewRequest() = %v", err) 237 } 238 ctx = WithHTTPRequest(ctx, req) 239 240 if want, got := req, GetHTTPRequest(ctx); got != want { 241 t.Errorf("GetHTTPRequest() = %v, wanted %v", got, want) 242 } 243 }