github.com/google/cadvisor@v0.49.1/devicemapper/fake/dmsetup_client_fake.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     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 fake
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  type DmsetupCommand struct {
    22  	Name   string
    23  	Result string
    24  	Err    error
    25  }
    26  
    27  // NewFakeDmsetupClient returns a new fake DmsetupClient.
    28  func NewFakeDmsetupClient(t *testing.T, commands ...DmsetupCommand) *FakeDmsetupClient {
    29  	if len(commands) == 0 {
    30  		commands = make([]DmsetupCommand, 0)
    31  	}
    32  	return &FakeDmsetupClient{t: t, commands: commands}
    33  }
    34  
    35  // FakeDmsetupClient is a thread-unsafe fake implementation of the
    36  // DmsetupClient interface
    37  type FakeDmsetupClient struct {
    38  	t        *testing.T
    39  	commands []DmsetupCommand
    40  }
    41  
    42  func (c *FakeDmsetupClient) Table(deviceName string) ([]byte, error) {
    43  	return c.dmsetup("table")
    44  }
    45  
    46  func (c *FakeDmsetupClient) Message(deviceName string, sector int, message string) ([]byte, error) {
    47  	return c.dmsetup("message")
    48  }
    49  
    50  func (c *FakeDmsetupClient) Status(deviceName string) ([]byte, error) {
    51  	return c.dmsetup("status")
    52  }
    53  
    54  func (c *FakeDmsetupClient) AddCommand(name string, result string, err error) {
    55  	c.commands = append(c.commands, DmsetupCommand{name, result, err})
    56  }
    57  
    58  func (c *FakeDmsetupClient) dmsetup(inputCommand string) ([]byte, error) {
    59  	var nextCommand DmsetupCommand
    60  	nextCommand, c.commands = c.commands[0], c.commands[1:]
    61  	if nextCommand.Name != inputCommand {
    62  		c.t.Fatalf("unexpected dmsetup command; expected: %q, got %q", nextCommand.Name, inputCommand)
    63  		// should be unreachable in a test context.
    64  	}
    65  
    66  	return []byte(nextCommand.Result), nextCommand.Err
    67  }