|
| 1 | +/* |
| 2 | +Copyright 2024 k0s 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 worker_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "runtime" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/k0sproject/k0s/pkg/component/worker" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestGetContainerRuntimeEndpoint_Defaults(t *testing.T) { |
| 28 | + runtimeEndpoint, err := worker.GetContainerRuntimeEndpoint("", "/run/user/999") |
| 29 | + assert.NoError(t, err) |
| 30 | + if assert.NotNil(t, runtimeEndpoint) { |
| 31 | + if runtime.GOOS == "windows" { |
| 32 | + assert.Equal(t, "npipe:////./pipe/containerd-containerd", runtimeEndpoint.String()) |
| 33 | + assert.Equal(t, "//./pipe/containerd-containerd", runtimeEndpoint.Path) |
| 34 | + } else { |
| 35 | + assert.Equal(t, "unix:///run/user/999/containerd.sock", runtimeEndpoint.String()) |
| 36 | + assert.Equal(t, "/run/user/999/containerd.sock", runtimeEndpoint.Path) |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestGetContainerRuntimeEndpoint_Flag(t *testing.T) { |
| 42 | + cases := []struct { |
| 43 | + name string |
| 44 | + flag string |
| 45 | + expEndpoint string |
| 46 | + expPath string |
| 47 | + err string |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "containerd-unix", |
| 51 | + flag: "remote:unix:///var/run/mke/containerd.sock", |
| 52 | + expEndpoint: "unix:///var/run/mke/containerd.sock", |
| 53 | + expPath: "/var/run/mke/containerd.sock", |
| 54 | + err: "", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "containerd-windows", |
| 58 | + flag: "remote:npipe:////./pipe/containerd-containerd", |
| 59 | + expEndpoint: "npipe:////./pipe/containerd-containerd", |
| 60 | + expPath: "//./pipe/containerd-containerd", |
| 61 | + err: "", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "no-colon-in-flag", |
| 65 | + flag: "no-colon-in-flag", |
| 66 | + expEndpoint: "", |
| 67 | + expPath: "", |
| 68 | + err: "CRI socket flag must be of the form <type>:<url>", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "invalid-url", |
| 72 | + flag: "remote:u<nix:///foo", |
| 73 | + expEndpoint: "", |
| 74 | + expPath: "", |
| 75 | + err: "failed to parse runtime endpoint: ", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "unknown-type", |
| 79 | + flag: "foobar:unix:///var/run/mke/containerd.sock", |
| 80 | + expEndpoint: "", |
| 81 | + expPath: "", |
| 82 | + err: `unknown runtime type "foobar", only "remote" is supported`, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tc := range cases { |
| 87 | + t.Run(tc.name, func(t *testing.T) { |
| 88 | + endpoint, err := worker.GetContainerRuntimeEndpoint(tc.flag, "y u use me?") |
| 89 | + if tc.err != "" { |
| 90 | + assert.ErrorContains(t, err, tc.err) |
| 91 | + assert.Nil(t, endpoint) |
| 92 | + } else { |
| 93 | + assert.NoError(t, err) |
| 94 | + if assert.NotNil(t, endpoint) { |
| 95 | + assert.Equal(t, tc.expEndpoint, endpoint.String()) |
| 96 | + assert.Equal(t, tc.expPath, endpoint.Path) |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments