|
1 | 1 | package system_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
4 | 5 | "runtime" |
5 | 6 | "slices" |
6 | 7 | "strings" |
@@ -33,3 +34,177 @@ func TestGetMachine(t *testing.T) { |
33 | 34 | t.Error(arch) |
34 | 35 | } |
35 | 36 | } |
| 37 | + |
| 38 | +func createOsReleaseFile(t *testing.T, content string) string { |
| 39 | + t.Helper() |
| 40 | + |
| 41 | + tmpfile, err := os.CreateTemp("", "os-release-test-") |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("failed to create temp file: %v", err) |
| 44 | + } |
| 45 | + t.Cleanup(func() { |
| 46 | + if err := os.Remove(tmpfile.Name()); err != nil { |
| 47 | + t.Fatalf("failed to remove temp file: %v", err) |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + if _, err := tmpfile.WriteString(content); err != nil { |
| 52 | + t.Fatalf("failed to write to temp file: %v", err) |
| 53 | + } |
| 54 | + if err := tmpfile.Close(); err != nil { |
| 55 | + t.Fatalf("failed to close temp file: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + return tmpfile.Name() |
| 59 | +} |
| 60 | + |
| 61 | +func TestGetOSID(t *testing.T) { |
| 62 | + testCases := []struct { |
| 63 | + name string |
| 64 | + content string |
| 65 | + expectedOSID string |
| 66 | + }{ |
| 67 | + { |
| 68 | + name: "should return ID from simple file", |
| 69 | + content: ` |
| 70 | +NAME="Test OS" |
| 71 | +VERSION="1.0" |
| 72 | +ID=testos |
| 73 | +ID_LIKE=anotheros |
| 74 | +`, |
| 75 | + expectedOSID: "testos", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "should return ID from quoted syntac with whitespaces", |
| 79 | + content: ` |
| 80 | +NAME="Test OS" |
| 81 | +VERSION="1.0" |
| 82 | + ID = "testos" |
| 83 | +ID_LIKE=anotheros |
| 84 | +`, |
| 85 | + expectedOSID: "testos", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "should return ID with whitespaces", |
| 89 | + content: ` |
| 90 | +NAME="Test OS" |
| 91 | +VERSION="1.0" |
| 92 | + ID = testos |
| 93 | +ID_LIKE=anotheros |
| 94 | +`, |
| 95 | + expectedOSID: "testos", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "should return ID from quoted syntac with whitespaces", |
| 99 | + content: ` |
| 100 | +NAME="Test OS" |
| 101 | +VERSION="1.0" |
| 102 | +ID='testos' |
| 103 | +ID_LIKE=anotheros |
| 104 | +`, |
| 105 | + expectedOSID: "testos", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "should return the last ID when duplicates exist", |
| 109 | + content: ` |
| 110 | +ID=firstid |
| 111 | +NAME="Test OS" |
| 112 | +ID=secondid |
| 113 | +VERSION="1.0" |
| 114 | +ID=lastid |
| 115 | +`, |
| 116 | + expectedOSID: "lastid", |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "should return default string for empty file", |
| 120 | + content: "", |
| 121 | + expectedOSID: runtime.GOOS, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "should return default string for file with no ID", |
| 125 | + content: ` |
| 126 | +NAME="Test OS" |
| 127 | +VERSION="1.0" |
| 128 | +`, |
| 129 | + expectedOSID: runtime.GOOS, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tc := range testCases { |
| 134 | + t.Run(tc.name, func(t *testing.T) { |
| 135 | + osReleaseFile := createOsReleaseFile(t, tc.content) |
| 136 | + sys := system.NewSystem() |
| 137 | + osID, err := sys.GetOSID(osReleaseFile) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("unexpected error getting OSID: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + if osID != tc.expectedOSID { |
| 143 | + t.Errorf("expected OSID %q, got %q", tc.expectedOSID, osID) |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestGetOSIDMultipleFiles(t *testing.T) { |
| 150 | + t.Run("should return default GOOS when no path is provided", func(t *testing.T) { |
| 151 | + sys := system.NewSystem() |
| 152 | + osID, err := sys.GetOSID() |
| 153 | + if err != nil { |
| 154 | + t.Fatalf("unexpected error getting OSID: %v", err) |
| 155 | + } |
| 156 | + |
| 157 | + _, err1 := os.Stat("/etc/os-release") |
| 158 | + _, err2 := os.Stat("/usr/lib/os-release") |
| 159 | + |
| 160 | + if os.IsNotExist(err1) && os.IsNotExist(err2) { |
| 161 | + // Neither file exists, so we should get the default GOOS |
| 162 | + if osID != runtime.GOOS { |
| 163 | + t.Errorf("expected OSID %q, got %q", runtime.GOOS, osID) |
| 164 | + } |
| 165 | + } else { |
| 166 | + // At least one file exists, so we should get a non-empty string |
| 167 | + if osID == "" { |
| 168 | + t.Error("expected a non-empty OSID, but it was empty") |
| 169 | + } |
| 170 | + } |
| 171 | + }) |
| 172 | + |
| 173 | + t.Run("should return ID from the second file if the first does not exist", func(t *testing.T) { |
| 174 | + osReleaseFile := createOsReleaseFile(t, "ID=second") |
| 175 | + sys := system.NewSystem() |
| 176 | + osID, err := sys.GetOSID("/non/existent/file", osReleaseFile) |
| 177 | + if err != nil { |
| 178 | + t.Fatalf("unexpected error getting OSID: %v", err) |
| 179 | + } |
| 180 | + if osID != "second" { |
| 181 | + t.Errorf("expected OSID %q, got %q", "second", osID) |
| 182 | + } |
| 183 | + }) |
| 184 | + |
| 185 | + t.Run("should return default GOOS if the first file has no ID", func(t *testing.T) { |
| 186 | + firstFile := createOsReleaseFile(t, "NAME=No ID here") |
| 187 | + secondFile := createOsReleaseFile(t, "ID=second") |
| 188 | + sys := system.NewSystem() |
| 189 | + osID, err := sys.GetOSID(firstFile, secondFile) |
| 190 | + if err != nil { |
| 191 | + t.Fatalf("unexpected error getting OSID: %v", err) |
| 192 | + } |
| 193 | + if osID != runtime.GOOS { |
| 194 | + t.Errorf("expected OSID %q, got %q", runtime.GOOS, osID) |
| 195 | + } |
| 196 | + }) |
| 197 | + |
| 198 | + t.Run("should return ID from the first file if both exist", func(t *testing.T) { |
| 199 | + firstFile := createOsReleaseFile(t, "ID=first") |
| 200 | + secondFile := createOsReleaseFile(t, "ID=second") |
| 201 | + sys := system.NewSystem() |
| 202 | + osID, err := sys.GetOSID(firstFile, secondFile) |
| 203 | + if err != nil { |
| 204 | + t.Fatalf("unexpected error getting OSID: %v", err) |
| 205 | + } |
| 206 | + if osID != "first" { |
| 207 | + t.Errorf("expected OSID %q, got %q", "first", osID) |
| 208 | + } |
| 209 | + }) |
| 210 | +} |
0 commit comments