github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/libinit/root_linux_test.go (about)

     1  // Copyright 2022 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package libinit
     6  
     7  import (
     8  	"bytes"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  	"github.com/mvdan/u-root-coreutils/pkg/cmdline"
    16  )
    17  
    18  func TestLoadModule(t *testing.T) {
    19  	var loadedModules []string
    20  	loader := &InitModuleLoader{
    21  		Cmdline: cmdline.NewCmdLine(),
    22  		Prober: func(name, params string) error {
    23  			loadedModules = append(loadedModules, name)
    24  			return nil
    25  		},
    26  	}
    27  
    28  	expectedModules := []string{"test", "something-test"}
    29  	InstallModules(loader, expectedModules)
    30  	if diff := cmp.Diff(expectedModules, loadedModules); diff != "" {
    31  		t.Fatalf("unexpected difference of loaded modules (-want, +got): %v", diff)
    32  	}
    33  }
    34  
    35  func TestModuleConf(t *testing.T) {
    36  	var toBytes = func(s string) []byte {
    37  		return bytes.NewBufferString(s).Bytes()
    38  	}
    39  	var files = []struct {
    40  		Name    string
    41  		Content string
    42  		Modules []string
    43  	}{
    44  		{
    45  			Name:    "test.conf",
    46  			Content: `something`,
    47  			Modules: []string{"something"},
    48  		},
    49  		{
    50  			Name: "test2.conf",
    51  			Content: `module1
    52  # not a module
    53  module2`,
    54  			Modules: []string{"module1", "module2"},
    55  		},
    56  	}
    57  
    58  	dir := t.TempDir()
    59  
    60  	var checkModules []string
    61  	for _, file := range files {
    62  		t.Run(file.Name, func(t *testing.T) {
    63  			p := filepath.Join(dir, file.Name)
    64  			if err := os.WriteFile(p, toBytes(file.Content), 0o644); err != nil {
    65  				t.Fatal(err)
    66  			}
    67  			checkModules = append(checkModules, file.Modules...)
    68  		})
    69  	}
    70  
    71  	moduleConfPattern := filepath.Join(dir, "*.conf")
    72  	modules, err := GetModulesFromConf(moduleConfPattern)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	if diff := cmp.Diff(checkModules, modules); diff != "" {
    78  		t.Fatalf("unexpected difference of loaded modules (-want, +got): %v", diff)
    79  	}
    80  }
    81  
    82  func TestCmdline(t *testing.T) {
    83  	cline := &cmdline.CmdLine{
    84  		AsMap: map[string]string{
    85  			"modules_load": "test",
    86  			"test.key1":    "value1",
    87  			"test.key2":    "value2",
    88  			"test.key3":    "value3",
    89  		},
    90  	}
    91  	var loadedModules []string
    92  	var moduleParams []string
    93  	loader := &InitModuleLoader{
    94  		Cmdline: cline,
    95  		Prober: func(name, params string) error {
    96  			loadedModules = append(loadedModules, name)
    97  			moduleParams = append(moduleParams, params)
    98  			return nil
    99  		},
   100  	}
   101  
   102  	mods, err := GetModulesFromCmdline(loader)
   103  	if err != nil {
   104  		t.Fail()
   105  	}
   106  	InstallModules(loader, mods)
   107  	expectedCmdLine := []string{"key1=value1", "key2=value2", "key3=value3"}
   108  	expectedModules := []string{"test"}
   109  
   110  	// Ordering of the parsed cmdline from the package isn't stable
   111  	for _, val := range expectedCmdLine {
   112  		if !strings.Contains(moduleParams[0], val) {
   113  			t.Fatalf("failed cmdline test. Did not find %+v\n", val)
   114  		}
   115  	}
   116  
   117  	if diff := cmp.Diff(expectedModules, loadedModules); diff != "" {
   118  		t.Fatalf("unexpected difference of loaded modules (-want, +got): %v", diff)
   119  	}
   120  }