|
| 1 | +// Copyright 2026 New Relic Corporation. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package fs |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestReadFirstLine(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + content string |
| 20 | + expected string |
| 21 | + expectError bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "single line", |
| 25 | + content: "Hello World", |
| 26 | + expected: "Hello World", |
| 27 | + expectError: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "multiple lines returns first", |
| 31 | + content: "First Line\nSecond Line\nThird Line", |
| 32 | + expected: "First Line", |
| 33 | + expectError: false, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "trims whitespace", |
| 37 | + content: " Trimmed Content \nSecond Line", |
| 38 | + expected: "Trimmed Content", |
| 39 | + expectError: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "empty file", |
| 43 | + content: "", |
| 44 | + expected: "", |
| 45 | + expectError: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "only newline", |
| 49 | + content: "\n", |
| 50 | + expected: "", |
| 51 | + expectError: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "tabs and spaces", |
| 55 | + content: "\t Hello \t\nWorld", |
| 56 | + expected: "Hello", |
| 57 | + expectError: false, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tt := range tests { //nolint:varnamelen |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + tmpFile := createTempFile(t, tt.content) |
| 64 | + defer os.Remove(tmpFile) |
| 65 | + |
| 66 | + result, err := ReadFirstLine(tmpFile) |
| 67 | + if tt.expectError { |
| 68 | + require.Error(t, err) |
| 69 | + } else { |
| 70 | + require.NoError(t, err) |
| 71 | + assert.Equal(t, tt.expected, result) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestReadFirstLine_FileNotFound(t *testing.T) { |
| 78 | + result, err := ReadFirstLine("/nonexistent/path/file.txt") |
| 79 | + require.Error(t, err) |
| 80 | + assert.Equal(t, "unknown", result) |
| 81 | + assert.Contains(t, err.Error(), "cannot open file") |
| 82 | +} |
| 83 | + |
| 84 | +func TestReadFileFieldMatching(t *testing.T) { |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + content string |
| 88 | + pattern string |
| 89 | + expected string |
| 90 | + expectError bool |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "match on first line", |
| 94 | + content: "VERSION=1.2.3\nNAME=test", |
| 95 | + pattern: `VERSION=(.+)`, |
| 96 | + expected: "1.2.3", |
| 97 | + expectError: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "match on second line", |
| 101 | + content: "NAME=test\nVERSION=1.2.3", |
| 102 | + pattern: `VERSION=(.+)`, |
| 103 | + expected: "1.2.3", |
| 104 | + expectError: false, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "match with quotes", |
| 108 | + content: `NAME="Ubuntu"\nVERSION="20.04"`, |
| 109 | + pattern: `NAME="([^"]+)"`, |
| 110 | + expected: "Ubuntu", |
| 111 | + expectError: false, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "no match returns unknown", |
| 115 | + content: "FOO=bar\nBAZ=qux", |
| 116 | + pattern: `VERSION=(.+)`, |
| 117 | + expected: "unknown", |
| 118 | + expectError: false, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "complex regex", |
| 122 | + content: "Red Hat Enterprise Linux release 8.4 (Ootpa)", |
| 123 | + pattern: `release (\d+\.\d+)`, |
| 124 | + expected: "8.4", |
| 125 | + expectError: false, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "empty file returns unknown", |
| 129 | + content: "", |
| 130 | + pattern: `(.+)`, |
| 131 | + expected: "unknown", |
| 132 | + expectError: false, |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + for _, tt := range tests { //nolint:varnamelen |
| 137 | + t.Run(tt.name, func(t *testing.T) { |
| 138 | + tmpFile := createTempFile(t, tt.content) |
| 139 | + |
| 140 | + defer os.Remove(tmpFile) |
| 141 | + |
| 142 | + re := regexp.MustCompile(tt.pattern) |
| 143 | + |
| 144 | + result, err := ReadFileFieldMatching(tmpFile, re) |
| 145 | + if tt.expectError { |
| 146 | + assert.Error(t, err) |
| 147 | + } else { |
| 148 | + // Note: err may still be nil even when result is "unknown" |
| 149 | + assert.Equal(t, tt.expected, result) |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestReadFileFieldMatching_FileNotFound(t *testing.T) { |
| 156 | + re := regexp.MustCompile(`(.+)`) |
| 157 | + result, err := ReadFileFieldMatching("/nonexistent/path/file.txt", re) |
| 158 | + require.Error(t, err) |
| 159 | + assert.Equal(t, "unknown", result) |
| 160 | + assert.Contains(t, err.Error(), "cannot open file") |
| 161 | +} |
| 162 | + |
| 163 | +func TestReadFileFieldMatching_MultipleCaptures(t *testing.T) { |
| 164 | + content := "VERSION_ID=8.4" |
| 165 | + |
| 166 | + tmpFile := createTempFile(t, content) |
| 167 | + defer os.Remove(tmpFile) |
| 168 | + |
| 169 | + // Pattern with multiple capture groups - should return first capture |
| 170 | + re := regexp.MustCompile(`VERSION_ID=(\d+)\.(\d+)`) |
| 171 | + result, err := ReadFileFieldMatching(tmpFile, re) |
| 172 | + require.NoError(t, err) |
| 173 | + assert.Equal(t, "8", result) // First capture group |
| 174 | +} |
| 175 | + |
| 176 | +func TestReadFileFieldMatching_LinuxReleaseFiles(t *testing.T) { |
| 177 | + t.Run("os-release NAME", func(t *testing.T) { |
| 178 | + tmpFile := createTempFile(t, `NAME="Ubuntu"\nVERSION="20.04.3 LTS (Focal Fossa)"`) |
| 179 | + defer os.Remove(tmpFile) |
| 180 | + |
| 181 | + re := regexp.MustCompile(`NAME="?([^"\n]+)"?`) |
| 182 | + result, err := ReadFileFieldMatching(tmpFile, re) |
| 183 | + require.NoError(t, err) |
| 184 | + assert.Equal(t, "Ubuntu", result) |
| 185 | + }) |
| 186 | + |
| 187 | + t.Run("centos-release", func(t *testing.T) { |
| 188 | + tmpFile := createTempFile(t, "CentOS Linux release 7.9.2009 (Core)") |
| 189 | + defer os.Remove(tmpFile) |
| 190 | + |
| 191 | + re := regexp.MustCompile(`release (\d+\.\d+)`) |
| 192 | + result, err := ReadFileFieldMatching(tmpFile, re) |
| 193 | + require.NoError(t, err) |
| 194 | + assert.Equal(t, "7.9", result) |
| 195 | + }) |
| 196 | + |
| 197 | + t.Run("redhat-release", func(t *testing.T) { |
| 198 | + tmpFile := createTempFile(t, "Red Hat Enterprise Linux release 8.4 (Ootpa)") |
| 199 | + defer os.Remove(tmpFile) |
| 200 | + |
| 201 | + re := regexp.MustCompile(`release (\d+\.\d+)`) |
| 202 | + result, err := ReadFileFieldMatching(tmpFile, re) |
| 203 | + require.NoError(t, err) |
| 204 | + assert.Equal(t, "8.4", result) |
| 205 | + }) |
| 206 | +} |
| 207 | + |
| 208 | +// createTempFile creates a temporary file with the given content and returns its path. |
| 209 | +func createTempFile(t *testing.T, content string) string { |
| 210 | + t.Helper() |
| 211 | + tmpDir := t.TempDir() |
| 212 | + tmpFile := filepath.Join(tmpDir, "testfile") |
| 213 | + err := os.WriteFile(tmpFile, []byte(content), 0o600) |
| 214 | + require.NoError(t, err) |
| 215 | + |
| 216 | + return tmpFile |
| 217 | +} |
0 commit comments