github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/os/osrelease/osrelease_test.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package osrelease_test
    16  
    17  import (
    18  	"errors"
    19  	"os"
    20  	"path/filepath"
    21  	"runtime"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"github.com/google/osv-scalibr/extractor/filesystem/os/osrelease"
    26  	scalibrfs "github.com/google/osv-scalibr/fs"
    27  )
    28  
    29  func TestGetOSRelease(t *testing.T) {
    30  	tests := []struct {
    31  		name          string
    32  		path          string
    33  		content       string
    34  		mode          os.FileMode
    35  		want          map[string]string
    36  		wantErr       error
    37  		skipOnWindows bool
    38  	}{
    39  		{
    40  			name:    "location: etc/os-release",
    41  			path:    "etc/os-release",
    42  			content: `ID=ubuntu`,
    43  			mode:    0666,
    44  			want:    map[string]string{"ID": "ubuntu"},
    45  		},
    46  		{
    47  			name:    "location: usr/lib/os-release",
    48  			path:    "usr/lib/os-release",
    49  			content: `ID=ubuntu`,
    50  			mode:    0666,
    51  			want:    map[string]string{"ID": "ubuntu"},
    52  		},
    53  		{
    54  			name:    "not found",
    55  			path:    "foo",
    56  			content: `ID=ubuntu`,
    57  			mode:    0666,
    58  			wantErr: os.ErrNotExist,
    59  		},
    60  		{
    61  			name:          "permission error",
    62  			path:          "etc/os-release",
    63  			content:       `ID=ubuntu`,
    64  			mode:          0,
    65  			wantErr:       os.ErrPermission,
    66  			skipOnWindows: true,
    67  		},
    68  		{
    69  			name: "ignore_comments",
    70  			path: "etc/os-release",
    71  			content: `#ID=foo
    72  			ID=ubuntu`,
    73  			mode: 0666,
    74  			want: map[string]string{"ID": "ubuntu"},
    75  		},
    76  		{
    77  			name: "ignore_random_stuff",
    78  			path: "etc/os-release",
    79  			content: `random stuff
    80  			ID=ubuntu`,
    81  			mode: 0666,
    82  			want: map[string]string{"ID": "ubuntu"},
    83  		},
    84  		{
    85  			name:    "resolve quotes",
    86  			path:    "etc/os-release",
    87  			content: `ID="ubuntu"`,
    88  			mode:    0666,
    89  			want:    map[string]string{"ID": "ubuntu"},
    90  		},
    91  		{
    92  			name:    "don't resolve partial quotes",
    93  			path:    "etc/os-release",
    94  			content: `ID="ubuntu`,
    95  			mode:    0666,
    96  			want:    map[string]string{"ID": "\"ubuntu"},
    97  		},
    98  	}
    99  
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			if tt.skipOnWindows && runtime.GOOS == "windows" {
   103  				t.Skip()
   104  			}
   105  
   106  			d := t.TempDir()
   107  			_ = os.Mkdir(filepath.Join(d, "etc"), 0744)
   108  			_ = os.MkdirAll(filepath.Join(d, "usr/lib"), 0744)
   109  			p := filepath.Join(d, tt.path)
   110  			err := os.WriteFile(p, []byte(tt.content), tt.mode)
   111  			if err != nil {
   112  				t.Fatalf("WriteFile(%s): %v", tt.path, err)
   113  			}
   114  
   115  			got, err := osrelease.GetOSRelease(scalibrfs.DirFS(d))
   116  			if !errors.Is(err, tt.wantErr) {
   117  				t.Fatalf("FileRequired(%s) error: got %v, want %v", tt.path, err, tt.wantErr)
   118  			}
   119  			if diff := cmp.Diff(got, tt.want); diff != "" {
   120  				t.Fatalf("FileRequired(%s): got %v, want %v", tt.path, got, tt.want)
   121  			}
   122  		})
   123  	}
   124  }