github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/core/chaincode/platforms/util/utils_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package util
     8  
     9  import (
    10  	"archive/tar"
    11  	"bytes"
    12  	"fmt"
    13  	"io"
    14  	"os"
    15  	"runtime"
    16  	"strings"
    17  	"testing"
    18  
    19  	docker "github.com/fsouza/go-dockerclient"
    20  	"github.com/hyperledger/fabric/common/metadata"
    21  	"github.com/hyperledger/fabric/core/config/configtest"
    22  	"github.com/spf13/viper"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestDockerPull(t *testing.T) {
    27  	codepackage, output := io.Pipe()
    28  	go func() {
    29  		tw := tar.NewWriter(output)
    30  
    31  		tw.Close()
    32  		output.Close()
    33  	}()
    34  
    35  	binpackage := bytes.NewBuffer(nil)
    36  
    37  	// Perform a nop operation within a fixed target.  We choose 1.1.0 because we know it's
    38  	// published and available.  Ideally we could choose something that we know is both multi-arch
    39  	// and ok to delete prior to executing DockerBuild.  This would ensure that we exercise the
    40  	// image pull logic.  However, no suitable target exists that meets all the criteria.  Therefore
    41  	// we settle on using a known released image.  We don't know if the image is already
    42  	// downloaded per se, and we don't want to explicitly delete this particular image first since
    43  	// it could be in use legitimately elsewhere.  Instead, we just know that this should always
    44  	// work and call that "close enough".
    45  	//
    46  	// Future considerations: publish a known dummy image that is multi-arch and free to randomly
    47  	// delete, and use that here instead.
    48  	image := fmt.Sprintf("hyperledger/fabric-ccenv:%s-1.1.0", runtime.GOARCH)
    49  	client, err := docker.NewClientFromEnv()
    50  	if err != nil {
    51  		t.Errorf("failed to get docker client: %s", err)
    52  	}
    53  
    54  	err = DockerBuild(
    55  		DockerBuildOptions{
    56  			Image:        image,
    57  			Cmd:          "/bin/true",
    58  			InputStream:  codepackage,
    59  			OutputStream: binpackage,
    60  		},
    61  		client,
    62  	)
    63  	if err != nil {
    64  		t.Errorf("Error during build: %s", err)
    65  	}
    66  }
    67  
    68  func TestUtil_GetDockerImageFromConfig(t *testing.T) {
    69  
    70  	path := "dt"
    71  
    72  	expected := "FROM " + metadata.DockerNamespace + ":" + runtime.GOARCH + "-" + metadata.Version
    73  	viper.Set(path, "FROM $(DOCKER_NS):$(ARCH)-$(PROJECT_VERSION)")
    74  	actual := GetDockerImageFromConfig(path)
    75  	assert.Equal(t, expected, actual, `Error parsing Dockerfile Template. Expected "%s", got "%s"`, expected, actual)
    76  
    77  	expected = "FROM " + metadata.DockerNamespace + ":" + runtime.GOARCH + "-" + twoDigitVersion(metadata.Version)
    78  	viper.Set(path, "FROM $(DOCKER_NS):$(ARCH)-$(TWO_DIGIT_VERSION)")
    79  	actual = GetDockerImageFromConfig(path)
    80  	assert.Equal(t, expected, actual, `Error parsing Dockerfile Template. Expected "%s", got "%s"`, expected, actual)
    81  
    82  }
    83  
    84  func TestMain(m *testing.M) {
    85  	viper.SetConfigName("core")
    86  	viper.SetEnvPrefix("CORE")
    87  	configtest.AddDevConfigPath(nil)
    88  	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
    89  	viper.AutomaticEnv()
    90  	if err := viper.ReadInConfig(); err != nil {
    91  		fmt.Printf("could not read config %s\n", err)
    92  		os.Exit(-1)
    93  	}
    94  	os.Exit(m.Run())
    95  }
    96  
    97  func TestTwoDigitVersion(t *testing.T) {
    98  	version := "2.0.0"
    99  	expected := "2.0"
   100  	actual := twoDigitVersion(version)
   101  	assert.Equal(t, expected, actual, `Error parsing two digit version. Expected "%s", got "%s"`, expected, actual)
   102  
   103  	version = "latest"
   104  	expected = "latest"
   105  	actual = twoDigitVersion(version)
   106  	assert.Equal(t, expected, actual, `Error parsing two digit version. Expected "%s", got "%s"`, expected, actual)
   107  }