github.com/jaypipes/ghw@v0.21.1/pkg/option/option_test.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package option_test
     8  
     9  import (
    10  	"runtime"
    11  	"testing"
    12  
    13  	"github.com/jaypipes/ghw/pkg/option"
    14  	"github.com/jaypipes/pcidb"
    15  )
    16  
    17  type optTestCase struct {
    18  	name   string
    19  	opts   []*option.Option
    20  	merged *option.Option
    21  }
    22  
    23  // nolint: gocyclo
    24  func TestOption(t *testing.T) {
    25  	var pciTest *optTestCase
    26  
    27  	if runtime.GOOS == "linux" {
    28  		pcidb, err := pcidb.New()
    29  		if err != nil {
    30  			t.Fatalf("error creating new pcidb: %v", err)
    31  		}
    32  		pciTest = &optTestCase{
    33  			name: "pcidb",
    34  			opts: []*option.Option{
    35  				option.WithPCIDB(pcidb),
    36  				option.WithChroot("/my/chroot/dir"),
    37  			},
    38  			merged: &option.Option{
    39  				Chroot: stringPtr("/my/chroot/dir"),
    40  				PCIDB:  pcidb,
    41  			},
    42  		}
    43  	}
    44  
    45  	optTCases := []optTestCase{
    46  		{
    47  			name: "multiple chroots",
    48  			opts: []*option.Option{
    49  				option.WithChroot("/my/chroot/dir"),
    50  				option.WithChroot("/my/chroot/dir/2"),
    51  			},
    52  			merged: &option.Option{
    53  				Chroot:      stringPtr("/my/chroot/dir/2"),
    54  				EnableTools: boolPtr(true),
    55  			},
    56  		},
    57  		{
    58  			name: "multiple chroots interleaved",
    59  			opts: []*option.Option{
    60  				option.WithChroot("/my/chroot/dir"),
    61  				option.WithSnapshot(option.SnapshotOptions{
    62  					Path: "/my/snapshot/dir",
    63  				}),
    64  				option.WithChroot("/my/chroot/dir/2"),
    65  			},
    66  			merged: &option.Option{
    67  				// latest seen takes precedence
    68  				Chroot: stringPtr("/my/chroot/dir/2"),
    69  				Snapshot: &option.SnapshotOptions{
    70  					Path: "/my/snapshot/dir",
    71  				},
    72  			},
    73  		},
    74  		{
    75  			name: "multiple snapshots overriding path",
    76  			opts: []*option.Option{
    77  				option.WithSnapshot(option.SnapshotOptions{
    78  					Path: "/my/snapshot/dir",
    79  				}),
    80  				option.WithSnapshot(option.SnapshotOptions{
    81  					Exclusive: true,
    82  				}),
    83  			},
    84  			merged: &option.Option{
    85  				Chroot: stringPtr("/"),
    86  				Snapshot: &option.SnapshotOptions{
    87  					// note Path is gone because the second instance
    88  					// has default (empty) path, and the latest always
    89  					// takes precedence.
    90  					Path:      "",
    91  					Exclusive: true,
    92  				},
    93  			},
    94  		},
    95  		{
    96  			name: "chroot and snapshot",
    97  			opts: []*option.Option{
    98  				option.WithChroot("/my/chroot/dir"),
    99  				option.WithSnapshot(option.SnapshotOptions{
   100  					Path:      "/my/snapshot/dir",
   101  					Exclusive: true,
   102  				}),
   103  			},
   104  			merged: &option.Option{
   105  				Chroot: stringPtr("/my/chroot/dir"),
   106  				Snapshot: &option.SnapshotOptions{
   107  					Path:      "/my/snapshot/dir",
   108  					Exclusive: true,
   109  				},
   110  			},
   111  		},
   112  		{
   113  			name: "chroot and snapshot with root",
   114  			opts: []*option.Option{
   115  				option.WithChroot("/my/chroot/dir"),
   116  				option.WithSnapshot(option.SnapshotOptions{
   117  					Path:      "/my/snapshot/dir",
   118  					Root:      stringPtr("/my/overridden/chroot/dir"),
   119  					Exclusive: true,
   120  				}),
   121  			},
   122  			merged: &option.Option{
   123  				// caveat! the option merge logic DOES NOT DO the override
   124  				Chroot: stringPtr("/my/chroot/dir"),
   125  				Snapshot: &option.SnapshotOptions{
   126  					Path:      "/my/snapshot/dir",
   127  					Root:      stringPtr("/my/overridden/chroot/dir"),
   128  					Exclusive: true,
   129  				},
   130  			},
   131  		},
   132  		{
   133  			name: "chroot and disabling tools",
   134  			opts: []*option.Option{
   135  				option.WithChroot("/my/chroot/dir"),
   136  				option.WithDisableTools(),
   137  			},
   138  			merged: &option.Option{
   139  				Chroot:      stringPtr("/my/chroot/dir"),
   140  				EnableTools: boolPtr(false),
   141  			},
   142  		},
   143  		{
   144  			name: "paths",
   145  			opts: []*option.Option{
   146  				option.WithPathOverrides(option.PathOverrides{
   147  					"/run": "/host-run",
   148  					"/var": "/host-var",
   149  				}),
   150  			},
   151  			merged: &option.Option{
   152  				PathOverrides: option.PathOverrides{
   153  					"/run": "/host-run",
   154  					"/var": "/host-var",
   155  				},
   156  			},
   157  		},
   158  		{
   159  			name: "chroot paths",
   160  			opts: []*option.Option{
   161  				option.WithChroot("/my/chroot/dir"),
   162  				option.WithPathOverrides(option.PathOverrides{
   163  					"/run": "/host-run",
   164  					"/var": "/host-var",
   165  				}),
   166  			},
   167  			merged: &option.Option{
   168  				Chroot: stringPtr("/my/chroot/dir"),
   169  				PathOverrides: option.PathOverrides{
   170  					"/run": "/host-run",
   171  					"/var": "/host-var",
   172  				},
   173  			},
   174  		},
   175  	}
   176  	if pciTest != nil {
   177  		optTCases = append(optTCases, *pciTest)
   178  	}
   179  	for _, optTCase := range optTCases {
   180  		t.Run(optTCase.name, func(t *testing.T) {
   181  			opt := option.Merge(optTCase.opts...)
   182  			if what, ok := optionEqual(optTCase.merged, opt); !ok {
   183  				t.Errorf("expected %#v got %#v - difference on %s", optTCase.merged, opt, what)
   184  			}
   185  		})
   186  	}
   187  }
   188  
   189  func stringPtr(s string) *string {
   190  	return &s
   191  }
   192  
   193  func boolPtr(b bool) *bool {
   194  	return &b
   195  }
   196  
   197  func optionEqual(a, b *option.Option) (string, bool) {
   198  	if a == nil || b == nil {
   199  		return "top-level", false
   200  	}
   201  	if a.Chroot != nil {
   202  		if b.Chroot == nil {
   203  			return "chroot ptr", false
   204  		}
   205  		if *a.Chroot != *b.Chroot {
   206  			return "chroot value", false
   207  		}
   208  	}
   209  	if a.Snapshot != nil {
   210  		if b.Snapshot == nil {
   211  			return "snapshot ptr", false
   212  		}
   213  		return optionSnapshotEqual(a.Snapshot, b.Snapshot)
   214  	}
   215  	if a.EnableTools != nil {
   216  		if b.EnableTools == nil {
   217  			return "enabletools ptr", false
   218  		}
   219  		if *a.EnableTools != *b.EnableTools {
   220  			return "enabletools value", false
   221  		}
   222  	}
   223  	return "", true
   224  }
   225  
   226  func optionSnapshotEqual(a, b *option.SnapshotOptions) (string, bool) {
   227  	if a.Path != b.Path {
   228  		return "snapshot path", false
   229  	}
   230  	if a.Exclusive != b.Exclusive {
   231  		return "snapshot exclusive flag", false
   232  	}
   233  	if a.Root != nil {
   234  		if b.Root == nil {
   235  			return "snapshot root ptr", false
   236  		}
   237  		if *a.Root != *b.Root {
   238  			return "snapshot root value", false
   239  		}
   240  	}
   241  	return "", true
   242  }