|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors All rights reserved. |
| 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 virtiofs_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/google/uuid" |
| 26 | + |
| 27 | + "k8s.io/minikube/pkg/drivers/common/virtiofs" |
| 28 | +) |
| 29 | + |
| 30 | +func TestVirtiofsValidateEmptyMountString(t *testing.T) { |
| 31 | + mounts, err := virtiofs.ValidateMountString("") |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("failed to parse empty mount string: %s", err) |
| 34 | + } |
| 35 | + if mounts != nil { |
| 36 | + t.Fatalf("expected nil mounts, got %v", mounts) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestVirtiofsValidateMountString(t *testing.T) { |
| 41 | + hostPath := t.TempDir() |
| 42 | + guestPath := "/mnt/models" |
| 43 | + mountString := fmt.Sprintf("%s:%s", hostPath, guestPath) |
| 44 | + |
| 45 | + mounts, err := virtiofs.ValidateMountString(mountString) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("failed to parse mountString %q: %s", mountString, err) |
| 48 | + } |
| 49 | + if len(mounts) != 1 { |
| 50 | + t.Fatalf("expected a single mount, got %v", mounts) |
| 51 | + } |
| 52 | + |
| 53 | + mount := mounts[0] |
| 54 | + if mount.HostPath != hostPath { |
| 55 | + t.Fatalf("expected host path %q, got %q", hostPath, mount.HostPath) |
| 56 | + } |
| 57 | + if mount.GuestPath != guestPath { |
| 58 | + t.Fatalf("expected guest path %q, got %q", guestPath, mount.GuestPath) |
| 59 | + } |
| 60 | + |
| 61 | + tag, err := uuid.Parse(mount.Tag) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("failed to parse UUID from mount tag: %s", err) |
| 64 | + } |
| 65 | + if tag.Version() != 4 { |
| 66 | + t.Fatalf("mount tag is not a random UUID") |
| 67 | + } |
| 68 | + |
| 69 | + if err := mount.Validate(); err != nil { |
| 70 | + t.Fatalf("mount is not valid: %s", err) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestVirtiofsParseInvalidMountString(t *testing.T) { |
| 75 | + for _, tt := range []struct { |
| 76 | + name string |
| 77 | + mountString string |
| 78 | + }{ |
| 79 | + { |
| 80 | + name: "empty", |
| 81 | + mountString: "", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "guest path is missing", |
| 85 | + mountString: "host-path", |
| 86 | + }, |
| 87 | + } { |
| 88 | + t.Run(tt.name, func(t *testing.T) { |
| 89 | + mount, err := virtiofs.ParseMount(tt.mountString) |
| 90 | + if err == nil { |
| 91 | + t.Fatalf("invalid mount string %q did not fail to parse", tt.mountString) |
| 92 | + } |
| 93 | + if mount != nil { |
| 94 | + t.Fatalf("expected nil mount for %q, got %v", tt.mountString, mount) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestVirtiofsValidateInvalidMount(t *testing.T) { |
| 101 | + dir := t.TempDir() |
| 102 | + missing := filepath.Join(dir, "missing") |
| 103 | + file := filepath.Join(dir, "file") |
| 104 | + |
| 105 | + f, err := os.Create(file) |
| 106 | + if err != nil { |
| 107 | + t.Fatal(err) |
| 108 | + } |
| 109 | + f.Close() |
| 110 | + |
| 111 | + for _, tt := range []struct { |
| 112 | + name string |
| 113 | + mountString string |
| 114 | + }{ |
| 115 | + { |
| 116 | + name: "host path contains virtiofs config separator", |
| 117 | + mountString: "/host,path:/guest-path", |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "host path is relative", |
| 121 | + mountString: "host-path:/guest-path", |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "guest path is relative", |
| 125 | + mountString: fmt.Sprintf("%s:guest-path", dir), |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "host path is missing", |
| 129 | + mountString: fmt.Sprintf("%s:/guest-path", missing), |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "host path is not a directory", |
| 133 | + mountString: fmt.Sprintf("%s:/guest-path", file), |
| 134 | + }, |
| 135 | + } { |
| 136 | + t.Run(tt.name, func(t *testing.T) { |
| 137 | + mount, err := virtiofs.ParseMount(tt.mountString) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("failed to parse mount string %q: %s", tt.mountString, err) |
| 140 | + } |
| 141 | + if err := mount.Validate(); err == nil { |
| 142 | + t.Fatalf("invalid mount %q did not failed validation", tt.mountString) |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments