|
| 1 | +/* |
| 2 | +Copyright 2017 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 server |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/containerd/cri/pkg/config" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | +) |
| 25 | + |
| 26 | +func TestValidateStreamServer(t *testing.T) { |
| 27 | + for desc, test := range map[string]struct { |
| 28 | + *criService |
| 29 | + tlsMode streamListenerMode |
| 30 | + expectErr bool |
| 31 | + }{ |
| 32 | + "should pass with default withoutTLS": { |
| 33 | + criService: &criService{ |
| 34 | + config: config.Config{ |
| 35 | + PluginConfig: config.DefaultConfig(), |
| 36 | + }, |
| 37 | + }, |
| 38 | + tlsMode: withoutTLS, |
| 39 | + expectErr: false, |
| 40 | + }, |
| 41 | + "should pass with x509KeyPairTLS": { |
| 42 | + criService: &criService{ |
| 43 | + config: config.Config{ |
| 44 | + PluginConfig: config.PluginConfig{ |
| 45 | + EnableTLSStreaming: true, |
| 46 | + X509KeyPairStreaming: config.X509KeyPairStreaming{ |
| 47 | + TLSKeyFile: "non-empty", |
| 48 | + TLSCertFile: "non-empty", |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + tlsMode: x509KeyPairTLS, |
| 54 | + expectErr: false, |
| 55 | + }, |
| 56 | + "should pass with selfSign": { |
| 57 | + criService: &criService{ |
| 58 | + config: config.Config{ |
| 59 | + PluginConfig: config.PluginConfig{ |
| 60 | + EnableTLSStreaming: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + tlsMode: selfSignTLS, |
| 65 | + expectErr: false, |
| 66 | + }, |
| 67 | + "should return error with X509 keypair but not EnableTLSStreaming": { |
| 68 | + criService: &criService{ |
| 69 | + config: config.Config{ |
| 70 | + PluginConfig: config.PluginConfig{ |
| 71 | + EnableTLSStreaming: false, |
| 72 | + X509KeyPairStreaming: config.X509KeyPairStreaming{ |
| 73 | + TLSKeyFile: "non-empty", |
| 74 | + TLSCertFile: "non-empty", |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + tlsMode: -1, |
| 80 | + expectErr: true, |
| 81 | + }, |
| 82 | + "should return error with X509 TLSCertFile empty": { |
| 83 | + criService: &criService{ |
| 84 | + config: config.Config{ |
| 85 | + PluginConfig: config.PluginConfig{ |
| 86 | + EnableTLSStreaming: true, |
| 87 | + X509KeyPairStreaming: config.X509KeyPairStreaming{ |
| 88 | + TLSKeyFile: "non-empty", |
| 89 | + TLSCertFile: "", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + tlsMode: -1, |
| 95 | + expectErr: true, |
| 96 | + }, |
| 97 | + "should return error with X509 TLSKeyFile empty": { |
| 98 | + criService: &criService{ |
| 99 | + config: config.Config{ |
| 100 | + PluginConfig: config.PluginConfig{ |
| 101 | + EnableTLSStreaming: true, |
| 102 | + X509KeyPairStreaming: config.X509KeyPairStreaming{ |
| 103 | + TLSKeyFile: "", |
| 104 | + TLSCertFile: "non-empty", |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + tlsMode: -1, |
| 110 | + expectErr: true, |
| 111 | + }, |
| 112 | + "should return error without EnableTLSStreaming and only TLSCertFile set": { |
| 113 | + criService: &criService{ |
| 114 | + config: config.Config{ |
| 115 | + PluginConfig: config.PluginConfig{ |
| 116 | + EnableTLSStreaming: false, |
| 117 | + X509KeyPairStreaming: config.X509KeyPairStreaming{ |
| 118 | + TLSKeyFile: "", |
| 119 | + TLSCertFile: "non-empty", |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + tlsMode: -1, |
| 125 | + expectErr: true, |
| 126 | + }, |
| 127 | + "should return error without EnableTLSStreaming and only TLSKeyFile set": { |
| 128 | + criService: &criService{ |
| 129 | + config: config.Config{ |
| 130 | + PluginConfig: config.PluginConfig{ |
| 131 | + EnableTLSStreaming: false, |
| 132 | + X509KeyPairStreaming: config.X509KeyPairStreaming{ |
| 133 | + TLSKeyFile: "non-empty", |
| 134 | + TLSCertFile: "", |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + tlsMode: -1, |
| 140 | + expectErr: true, |
| 141 | + }, |
| 142 | + } { |
| 143 | + t.Run(desc, func(t *testing.T) { |
| 144 | + tlsMode, err := getStreamListenerMode(test.criService) |
| 145 | + if test.expectErr { |
| 146 | + assert.Error(t, err) |
| 147 | + return |
| 148 | + } |
| 149 | + assert.NoError(t, err) |
| 150 | + assert.Equal(t, test.tlsMode, tlsMode) |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments