sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/config/gangway_test.go (about) 1 /* 2 Copyright 2023 The Kubernetes 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 config 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 ) 24 25 func TestGangwayConfig(t *testing.T) { 26 testCases := []struct { 27 name string 28 gangwayConfig string 29 expectError bool 30 }{ 31 { 32 name: "basic valid case", 33 gangwayConfig: ` 34 gangway: 35 allowed_api_clients: 36 - gcp: 37 endpoint_api_consumer_type: "PROJECT" 38 endpoint_api_consumer_number: "123" 39 allowed_jobs_filters: 40 - tenant_id: "well-behaved-tenant-for-gangway" 41 `, 42 expectError: false, 43 }, 44 { 45 name: "missing allowed_jobs_filters", 46 gangwayConfig: ` 47 gangway: 48 allowed_api_clients: 49 - gcp: 50 endpoint_api_consumer_type: "PROJECT" 51 endpoint_api_consumer_number: "123" 52 `, 53 expectError: true, 54 }, 55 { 56 name: "tenant_id is an empty string", 57 gangwayConfig: ` 58 gangway: 59 allowed_api_clients: 60 - gcp: 61 endpoint_api_consumer_type: "PROJECT" 62 endpoint_api_consumer_number: "123" 63 - tenant_id: "" 64 `, 65 expectError: true, 66 }, 67 { 68 name: "missing cloud vendor", 69 gangwayConfig: ` 70 gangway: 71 allowed_api_clients: 72 - allowed_jobs_filters: 73 - tenant_id: "well-behaved-tenant-for-gangway" 74 `, 75 expectError: true, 76 }, 77 { 78 name: "missing project type", 79 gangwayConfig: ` 80 gangway: 81 allowed_api_clients: 82 - gcp: 83 endpoint_api_consumer_number: "123" 84 allowed_jobs_filters: 85 - tenant_id: "well-behaved-tenant-for-gangway" 86 `, 87 expectError: true, 88 }, 89 { 90 name: "missing project number", 91 gangwayConfig: ` 92 gangway: 93 allowed_api_clients: 94 - gcp: 95 endpoint_api_consumer_type: "PROJECT" 96 allowed_jobs_filters: 97 - tenant_id: "well-behaved-tenant-for-gangway" 98 `, 99 expectError: true, 100 }, 101 { 102 name: "multiple clients with the same endpoint_api_consumer_number", 103 gangwayConfig: ` 104 gangway: 105 allowed_api_clients: 106 - gcp: 107 endpoint_api_consumer_type: "PROJECT" 108 endpoint_api_consumer_number: "123" 109 allowed_jobs_filters: 110 - tenant_id: "well-behaved-tenant-for-gangway" 111 - gcp: 112 endpoint_api_consumer_type: "PROJECT" 113 endpoint_api_consumer_number: "123" 114 allowed_jobs_filters: 115 - tenant_id: "another-client" 116 `, 117 expectError: true, 118 }, 119 } 120 for _, tc := range testCases { 121 // save the config 122 gangwayConfigDir := t.TempDir() 123 124 gangwayConfig := filepath.Join(gangwayConfigDir, "config.yaml") 125 if err := os.WriteFile(gangwayConfig, []byte(tc.gangwayConfig), 0666); err != nil { 126 t.Fatalf("fail to write gangway config: %v", err) 127 } 128 129 _, err := Load(gangwayConfig, "", nil, "") 130 131 if (err != nil) != tc.expectError { 132 t.Fatalf("tc %s: expected error: %v, got: %v, error: %v", tc.name, tc.expectError, (err != nil), err) 133 } 134 } 135 }