github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/bddtests/docker.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package bddtests
    18  
    19  import (
    20  	"fmt"
    21  	"os/exec"
    22  	"strings"
    23  )
    24  
    25  // DockerHelper helper for docker specific functions
    26  type DockerHelper interface {
    27  	GetIPAddress(containerID string) (string, error)
    28  	RemoveContainersWithNamePrefix(namePrefix string) error
    29  }
    30  
    31  // NewDockerCmdlineHelper returns a new command line DockerHelper instance
    32  func NewDockerCmdlineHelper() (DockerHelper, error) {
    33  	dockerCmdlineHelper := &dockerCmdlineHelper{}
    34  	return dockerCmdlineHelper, nil
    35  }
    36  
    37  type dockerCmdlineHelper struct {
    38  }
    39  
    40  func splitDockerCommandResults(cmdOutput string) (linesToReturn []string) {
    41  	lines := strings.Split(string(cmdOutput), "\n")
    42  	for _, line := range lines {
    43  		if len(line) > 0 {
    44  			linesToReturn = append(linesToReturn, line)
    45  		}
    46  	}
    47  	return linesToReturn
    48  }
    49  
    50  func (d *dockerCmdlineHelper) issueDockerCommand(cmdArgs []string) (string, error) {
    51  	var cmdOut []byte
    52  	var err error
    53  	cmd := exec.Command("docker", cmdArgs...)
    54  	//cmd.Env = append(cmd.Env, c.getEnv()...)
    55  	cmdOut, err = cmd.CombinedOutput()
    56  	return string(cmdOut), err
    57  }
    58  
    59  func (d *dockerCmdlineHelper) getContainerIDsWithNamePrefix(namePrefix string) ([]string, error) {
    60  	cmdOutput, err := d.issueDockerCommand([]string{"ps", "--filter", fmt.Sprintf("name=%s", namePrefix), "-qa"})
    61  	if err != nil {
    62  		return nil, fmt.Errorf("Error getting containers with name prefix '%s':  %s", namePrefix, err)
    63  	}
    64  	containerIDs := splitDockerCommandResults(cmdOutput)
    65  	return containerIDs, err
    66  }
    67  
    68  func (d *dockerCmdlineHelper) GetIPAddress(containerID string) (ipAddress string, err error) {
    69  	var (
    70  		cmdOutput string
    71  		lines     []string
    72  	)
    73  	errRetFunc := func() error {
    74  		return fmt.Errorf("Error getting IPAddress for container '%s':  %s", containerID, err)
    75  	}
    76  	if cmdOutput, err = d.issueDockerCommand([]string{"inspect", "--format", "{{ .NetworkSettings.IPAddress }}", containerID}); err != nil {
    77  		return "", errRetFunc()
    78  	}
    79  
    80  	if lines = splitDockerCommandResults(cmdOutput); len(lines) != 1 {
    81  		err = fmt.Errorf("unexpected length on inspect output")
    82  		return "", errRetFunc()
    83  	}
    84  	ipAddress = lines[0]
    85  	return ipAddress, nil
    86  }
    87  
    88  func (d *dockerCmdlineHelper) RemoveContainersWithNamePrefix(namePrefix string) error {
    89  	containers, err := d.getContainerIDsWithNamePrefix(namePrefix)
    90  	if err != nil {
    91  		return fmt.Errorf("Error removing containers with name prefix (%s):  %s", namePrefix, err)
    92  	}
    93  	for _, id := range containers {
    94  		fmt.Printf("container: %s", id)
    95  		_, _ = d.issueDockerCommand([]string{"rm", "-f", id})
    96  	}
    97  	return nil
    98  }