|
| 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 reset |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + testifysuite "github.com/stretchr/testify/suite" |
| 23 | + |
| 24 | + "github.com/k0sproject/k0s/inttest/common" |
| 25 | +) |
| 26 | + |
| 27 | +type suite struct { |
| 28 | + common.BootlooseSuite |
| 29 | +} |
| 30 | + |
| 31 | +func (s *suite) TestReset() { |
| 32 | + ctx := s.Context() |
| 33 | + workerNode := s.WorkerNode(0) |
| 34 | + |
| 35 | + if ok := s.Run("k0s gets up", func() { |
| 36 | + s.Require().NoError(s.InitController(0, "--disable-components=konnectivity-server,metrics-server")) |
| 37 | + s.Require().NoError(s.RunWorkers()) |
| 38 | + |
| 39 | + kc, err := s.KubeClient(s.ControllerNode(0)) |
| 40 | + s.Require().NoError(err) |
| 41 | + |
| 42 | + err = s.WaitForNodeReady(workerNode, kc) |
| 43 | + s.NoError(err) |
| 44 | + |
| 45 | + s.T().Log("waiting to see CNI pods ready") |
| 46 | + s.NoError(common.WaitForKubeRouterReady(ctx, kc), "CNI did not start") |
| 47 | + }); !ok { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + s.Run("k0s reset", func() { |
| 52 | + ssh, err := s.SSH(ctx, workerNode) |
| 53 | + s.Require().NoError(err) |
| 54 | + defer ssh.Disconnect() |
| 55 | + |
| 56 | + s.NoError(ssh.Exec(ctx, "test -d /var/lib/k0s", common.SSHStreams{}), "/var/lib/k0s is not a directory") |
| 57 | + s.NoError(ssh.Exec(ctx, "test -d /run/k0s", common.SSHStreams{}), "/run/k0s is not a directory") |
| 58 | + |
| 59 | + s.NoError(ssh.Exec(ctx, "pidof containerd-shim-runc-v2 >&2", common.SSHStreams{}), "Expected some running containerd shims") |
| 60 | + |
| 61 | + s.NoError(s.StopWorker(workerNode), "Failed to stop k0s") |
| 62 | + |
| 63 | + streams, flushStreams := common.TestLogStreams(s.T(), "reset") |
| 64 | + err = ssh.Exec(ctx, "k0s reset --debug", streams) |
| 65 | + flushStreams() |
| 66 | + s.NoError(err, "k0s reset didn't exit cleanly") |
| 67 | + |
| 68 | + // /var/lib/k0s is a mount point in the Docker container and can't be deleted, so it must be empty |
| 69 | + s.NoError(ssh.Exec(ctx, `x="$(ls -A /var/lib/k0s)" && echo "$x" >&2 && [ -z "$x" ]`, common.SSHStreams{}), "/var/lib/k0s is not empty") |
| 70 | + s.NoError(ssh.Exec(ctx, "! test -e /run/k0s", common.SSHStreams{}), "/run/k0s still exists") |
| 71 | + s.NoError(ssh.Exec(ctx, "! pidof containerd-shim-runc-v2 >&2", common.SSHStreams{}), "Expected no running containerd shims") |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func TestResetSuite(t *testing.T) { |
| 76 | + testifysuite.Run(t, &suite{ |
| 77 | + common.BootlooseSuite{ |
| 78 | + ControllerCount: 1, |
| 79 | + WorkerCount: 1, |
| 80 | + }, |
| 81 | + }) |
| 82 | +} |
0 commit comments