knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/webhook/env_test.go (about)

     1  /*
     2  Copyright 2020 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 webhook
    18  
    19  import (
    20  	"crypto/tls"
    21  	"testing"
    22  )
    23  
    24  const (
    25  	testMissingInputName = "MissingInput"
    26  
    27  	testDefaultPort          = 8888
    28  	testDefaultSecretName    = "webhook-certs"
    29  	testDefaultTLSMinVersion = tls.VersionTLS13
    30  )
    31  
    32  type portTest struct {
    33  	name      string
    34  	in        string
    35  	want      int
    36  	wantPanic bool
    37  }
    38  
    39  type webhookNameTest struct {
    40  	name      string
    41  	in        string
    42  	want      string
    43  	wantPanic bool
    44  }
    45  
    46  type secretNameTest struct {
    47  	name      string
    48  	in        string
    49  	want      string
    50  	wantPanic bool
    51  }
    52  
    53  type tlsMinVersionTest struct {
    54  	name      string
    55  	in        string
    56  	want      uint16
    57  	wantPanic bool
    58  }
    59  
    60  func TestPort(t *testing.T) {
    61  	tests := []portTest{{
    62  		name: testMissingInputName,
    63  		want: testDefaultPort,
    64  	}, {
    65  		name: "EmptyInput",
    66  		in:   "",
    67  		want: testDefaultPort,
    68  	}, {
    69  		name:      "InvalidInputNonNumeric",
    70  		in:        "invalid",
    71  		wantPanic: true,
    72  	}, {
    73  		name:      "InvalidInputTrailingSpace",
    74  		in:        "8443 ",
    75  		wantPanic: true,
    76  	}, {
    77  		name:      "InvalidInputZero",
    78  		in:        "0",
    79  		wantPanic: true,
    80  	}, {
    81  		name: "ValidInput",
    82  		in:   "443",
    83  		want: 443,
    84  	}}
    85  
    86  	for _, tc := range tests {
    87  		t.Run(tc.name, func(t *testing.T) {
    88  			// portEnvKey is unset when testing missing input.
    89  			if tc.name != testMissingInputName {
    90  				t.Setenv(portEnvKey, tc.in)
    91  			}
    92  
    93  			defer func() {
    94  				if r := recover(); r == nil && tc.wantPanic {
    95  					t.Error("Did not panic")
    96  				} else if r != nil && !tc.wantPanic {
    97  					t.Error("Got unexpected panic")
    98  				}
    99  			}()
   100  
   101  			if got := PortFromEnv(testDefaultPort); got != tc.want {
   102  				t.Errorf("PortFromEnv = %d, want: %d", got, tc.want)
   103  			}
   104  		})
   105  	}
   106  }
   107  
   108  func TestWebhookName(t *testing.T) {
   109  	tests := []webhookNameTest{{
   110  		name:      "EmptyInput",
   111  		in:        "",
   112  		wantPanic: true,
   113  	}, {
   114  		name: "ValidInput",
   115  		in:   "mywebhook",
   116  		want: "mywebhook",
   117  	}}
   118  
   119  	for _, tc := range tests {
   120  		t.Run(tc.name, func(t *testing.T) {
   121  			// webhookNameEnv is unset when testing missing input.
   122  			if tc.name != testMissingInputName {
   123  				t.Setenv(webhookNameEnvKey, tc.in)
   124  			}
   125  
   126  			defer func() {
   127  				if r := recover(); r == nil && tc.wantPanic {
   128  					t.Error("Did not panic")
   129  				} else if r != nil && !tc.wantPanic {
   130  					t.Error("Got unexpected panic")
   131  				}
   132  			}()
   133  
   134  			if got := NameFromEnv(); got != tc.want {
   135  				t.Errorf("NameFromEnv = %s, want: %s", got, tc.want)
   136  			}
   137  		})
   138  	}
   139  }
   140  
   141  func TestSecretName(t *testing.T) {
   142  	tests := []secretNameTest{{
   143  		name: testMissingInputName,
   144  		want: testDefaultSecretName,
   145  	}, {
   146  		name: "EmptyInput",
   147  		in:   "",
   148  		want: testDefaultSecretName,
   149  	}, {
   150  		name: "ValidInput",
   151  		in:   "my-webhook-certs",
   152  		want: "my-webhook-certs",
   153  	}}
   154  
   155  	for _, tc := range tests {
   156  		t.Run(tc.name, func(t *testing.T) {
   157  			// secretNameEnvKey is unset when testing missing input.
   158  			if tc.name != testMissingInputName {
   159  				t.Setenv(secretNameEnvKey, tc.in)
   160  			}
   161  
   162  			defer func() {
   163  				if r := recover(); r == nil && tc.wantPanic {
   164  					t.Error("Did not panic")
   165  				} else if r != nil && !tc.wantPanic {
   166  					t.Error("Got unexpected panic")
   167  				}
   168  			}()
   169  
   170  			if got := SecretNameFromEnv(testDefaultSecretName); got != tc.want {
   171  				t.Errorf("SecretNameFromEnv = %s, want: %s", got, tc.want)
   172  			}
   173  		})
   174  	}
   175  }
   176  
   177  func TestTLSMinVersion(t *testing.T) {
   178  	tests := []tlsMinVersionTest{{
   179  		name: testMissingInputName,
   180  		want: testDefaultTLSMinVersion,
   181  	}, {
   182  		name: "EmptyInput",
   183  		in:   "",
   184  		want: testDefaultTLSMinVersion,
   185  	}, {
   186  		name:      "InvalidInputTrailingSpace",
   187  		in:        "1.2  ",
   188  		wantPanic: true,
   189  	}, {
   190  		name:      "InvalidInput",
   191  		in:        "1.0",
   192  		wantPanic: true,
   193  	}, {
   194  		name: "ValidInputTLS12",
   195  		in:   "1.2",
   196  		want: tls.VersionTLS12,
   197  	}, {
   198  		name: "ValidInputTLS13",
   199  		in:   "1.3",
   200  		want: tls.VersionTLS13,
   201  	}}
   202  
   203  	for _, tc := range tests {
   204  		t.Run(tc.name, func(t *testing.T) {
   205  			// tlsMinVersionEnvKey is unset when testing missing input.
   206  			if tc.name != testMissingInputName {
   207  				t.Setenv(tlsMinVersionEnvKey, tc.in)
   208  			}
   209  
   210  			defer func() {
   211  				if r := recover(); r == nil && tc.wantPanic {
   212  					t.Error("Did not panic")
   213  				} else if r != nil && !tc.wantPanic {
   214  					t.Error("Got unexpected panic")
   215  				}
   216  			}()
   217  
   218  			if got := TLSMinVersionFromEnv(testDefaultTLSMinVersion); got != tc.want {
   219  				t.Errorf("TLSMinVersionFromEnv = %d, want: %d", got, tc.want)
   220  			}
   221  		})
   222  	}
   223  }