github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/cmdline/filters_test.go (about)

     1  // Copyright 2018 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 cmdline
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestRemoveFilter(t *testing.T) {
    13  	toRemove := []string{"remove-1", "remove_2"}
    14  
    15  	cl := `keep=5 remove_1=wontbethere remove-2=nomore keep2`
    16  	want := `keep=5 keep2`
    17  
    18  	got := removeFilter(cl, toRemove)
    19  	if got != want {
    20  		t.Errorf("removeFilter(%v,%v) = %v, want %v", cl, toRemove, got, want)
    21  	}
    22  }
    23  
    24  func TestUpdateFilter(t *testing.T) {
    25  	exampleCmdLine := `BOOT_IMAGE=/vmlinuz-4.11.2 ro ` +
    26  		`test-flag test2-flag=8 uroot.uinitflags="a=3 skipfork test2-flag" ` +
    27  		`uroot.initflags="systemd test-flag=3  test2-flag runlevel=2" ` +
    28  		`root=LABEL=/ biosdevname=0 net.ifnames=0 fsck.repair=yes ` +
    29  		`ipv6.autoconf=0 erst_disable nox2apic crashkernel=128M ` +
    30  		`systemd.unified_cgroup_hierarchy=1 cgroup_no_v1=all console=tty0 ` +
    31  		`console=ttyS0,115200 security=selinux selinux=1 enforcing=0`
    32  
    33  	// Do this once, we'll over-write soon
    34  	once.Do(cmdLineOpener)
    35  	cmdLineReader := strings.NewReader(exampleCmdLine)
    36  	procCmdLine = parse(cmdLineReader)
    37  
    38  	if procCmdLine.Err != nil {
    39  		t.Errorf("procCmdLine threw an error: %v", procCmdLine.Err)
    40  	}
    41  
    42  	toRemove := []string{"console", "earlyconsole"}
    43  	toReuse := []string{"console", "not-present"}
    44  	toAppend := "append=me"
    45  
    46  	cl := `keep=5 console=ttyS1 keep2 earlyconsole=ttyS1`
    47  	want := `keep=5 keep2 append=me console=ttyS0,115200`
    48  
    49  	filter := NewUpdateFilter(toAppend, toRemove, toReuse)
    50  	got := filter.Update(cl)
    51  	if got != want {
    52  		t.Errorf("Update(%v) = %v, want %v", cl, got, want)
    53  	}
    54  }