github.com/robertojrojas/docker@v1.9.1/image/image_test.go (about)

     1  package image
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/docker/distribution/digest"
     9  )
    10  
    11  var fixtures = []string{
    12  	"fixtures/pre1.9",
    13  	"fixtures/post1.9",
    14  }
    15  
    16  func loadFixtureFile(t *testing.T, path string) []byte {
    17  	fileData, err := ioutil.ReadFile(path)
    18  	if err != nil {
    19  		t.Fatalf("error opening %s: %v", path, err)
    20  	}
    21  
    22  	return bytes.TrimSpace(fileData)
    23  }
    24  
    25  // TestMakeImageConfig makes sure that MakeImageConfig returns the expected
    26  // canonical JSON for a reference Image.
    27  func TestMakeImageConfig(t *testing.T) {
    28  	for _, fixture := range fixtures {
    29  		v1Compatibility := loadFixtureFile(t, fixture+"/v1compatibility")
    30  		expectedConfig := loadFixtureFile(t, fixture+"/expected_config")
    31  		layerID := digest.Digest(loadFixtureFile(t, fixture+"/layer_id"))
    32  		parentID := digest.Digest(loadFixtureFile(t, fixture+"/parent_id"))
    33  
    34  		json, err := MakeImageConfig(v1Compatibility, layerID, parentID)
    35  		if err != nil {
    36  			t.Fatalf("MakeImageConfig on %s returned error: %v", fixture, err)
    37  		}
    38  		if !bytes.Equal(json, expectedConfig) {
    39  			t.Fatalf("did not get expected JSON for %s\nexpected: %s\ngot: %s", fixture, expectedConfig, json)
    40  		}
    41  	}
    42  }
    43  
    44  // TestGetStrongID makes sure that GetConfigJSON returns the expected
    45  // hash for a reference Image.
    46  func TestGetStrongID(t *testing.T) {
    47  	for _, fixture := range fixtures {
    48  		expectedConfig := loadFixtureFile(t, fixture+"/expected_config")
    49  		expectedComputedID := digest.Digest(loadFixtureFile(t, fixture+"/expected_computed_id"))
    50  
    51  		if id, err := StrongID(expectedConfig); err != nil || id != expectedComputedID {
    52  			t.Fatalf("did not get expected ID for %s\nexpected: %s\ngot: %s\nerror: %v", fixture, expectedComputedID, id, err)
    53  		}
    54  	}
    55  }