gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/osutil/disks/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package disks
    21  
    22  import "fmt"
    23  
    24  func MockUdevPropertiesForDevice(new func(string) (map[string]string, error)) (restore func()) {
    25  	old := udevadmProperties
    26  	// for better testing we mock the udevadm command output so that we still
    27  	// test the parsing
    28  	udevadmProperties = func(dev string) ([]byte, error) {
    29  		props, err := new(dev)
    30  		if err != nil {
    31  			return []byte(err.Error()), err
    32  		}
    33  		// put it into udevadm format output, i.e. "KEY=VALUE\n"
    34  		output := ""
    35  		for k, v := range props {
    36  			output += fmt.Sprintf("%s=%s\n", k, v)
    37  		}
    38  		return []byte(output), nil
    39  	}
    40  	return func() {
    41  		udevadmProperties = old
    42  	}
    43  }