github.com/grange74/docker@v1.6.0-rc3/graph/manifest_test.go (about) 1 package graph 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "testing" 10 11 "github.com/docker/docker/image" 12 "github.com/docker/docker/pkg/tarsum" 13 "github.com/docker/docker/registry" 14 "github.com/docker/docker/runconfig" 15 "github.com/docker/docker/utils" 16 ) 17 18 const ( 19 testManifestImageName = "testapp" 20 testManifestImageID = "d821b739e8834ec89ac4469266c3d11515da88fdcbcbdddcbcddb636f54fdde9" 21 testManifestImageIDShort = "d821b739e883" 22 testManifestTag = "manifesttest" 23 ) 24 25 func (s *TagStore) newManifest(localName, remoteName, tag string) ([]byte, error) { 26 manifest := ®istry.ManifestData{ 27 Name: remoteName, 28 Tag: tag, 29 SchemaVersion: 1, 30 } 31 localRepo, err := s.Get(localName) 32 if err != nil { 33 return nil, err 34 } 35 if localRepo == nil { 36 return nil, fmt.Errorf("Repo does not exist: %s", localName) 37 } 38 39 // Get the top-most layer id which the tag points to 40 layerId, exists := localRepo[tag] 41 if !exists { 42 return nil, fmt.Errorf("Tag does not exist for %s: %s", localName, tag) 43 } 44 layersSeen := make(map[string]bool) 45 46 layer, err := s.graph.Get(layerId) 47 if err != nil { 48 return nil, err 49 } 50 manifest.Architecture = layer.Architecture 51 manifest.FSLayers = make([]*registry.FSLayer, 0, 4) 52 manifest.History = make([]*registry.ManifestHistory, 0, 4) 53 var metadata runconfig.Config 54 if layer.Config != nil { 55 metadata = *layer.Config 56 } 57 58 for ; layer != nil; layer, err = layer.GetParent() { 59 if err != nil { 60 return nil, err 61 } 62 63 if layersSeen[layer.ID] { 64 break 65 } 66 if layer.Config != nil && metadata.Image != layer.ID { 67 err = runconfig.Merge(&metadata, layer.Config) 68 if err != nil { 69 return nil, err 70 } 71 } 72 73 checksum, err := layer.GetCheckSum(s.graph.ImageRoot(layer.ID)) 74 if err != nil { 75 return nil, fmt.Errorf("Error getting image checksum: %s", err) 76 } 77 if tarsum.VersionLabelForChecksum(checksum) != tarsum.Version1.String() { 78 archive, err := layer.TarLayer() 79 if err != nil { 80 return nil, err 81 } 82 83 defer archive.Close() 84 85 tarSum, err := tarsum.NewTarSum(archive, true, tarsum.Version1) 86 if err != nil { 87 return nil, err 88 } 89 if _, err := io.Copy(ioutil.Discard, tarSum); err != nil { 90 return nil, err 91 } 92 93 checksum = tarSum.Sum(nil) 94 95 // Save checksum value 96 if err := layer.SaveCheckSum(s.graph.ImageRoot(layer.ID), checksum); err != nil { 97 return nil, err 98 } 99 } 100 101 jsonData, err := layer.RawJson() 102 if err != nil { 103 return nil, fmt.Errorf("Cannot retrieve the path for {%s}: %s", layer.ID, err) 104 } 105 106 manifest.FSLayers = append(manifest.FSLayers, ®istry.FSLayer{BlobSum: checksum}) 107 108 layersSeen[layer.ID] = true 109 110 manifest.History = append(manifest.History, ®istry.ManifestHistory{V1Compatibility: string(jsonData)}) 111 } 112 113 manifestBytes, err := json.MarshalIndent(manifest, "", " ") 114 if err != nil { 115 return nil, err 116 } 117 118 return manifestBytes, nil 119 } 120 121 func TestManifestTarsumCache(t *testing.T) { 122 tmp, err := utils.TestDirectory("") 123 if err != nil { 124 t.Fatal(err) 125 } 126 defer os.RemoveAll(tmp) 127 store := mkTestTagStore(tmp, t) 128 defer store.graph.driver.Cleanup() 129 130 archive, err := fakeTar() 131 if err != nil { 132 t.Fatal(err) 133 } 134 img := &image.Image{ID: testManifestImageID} 135 if err := store.graph.Register(img, archive); err != nil { 136 t.Fatal(err) 137 } 138 if err := store.Set(testManifestImageName, testManifestTag, testManifestImageID, false); err != nil { 139 t.Fatal(err) 140 } 141 142 if cs, err := img.GetCheckSum(store.graph.ImageRoot(testManifestImageID)); err != nil { 143 t.Fatal(err) 144 } else if cs != "" { 145 t.Fatalf("Non-empty checksum file after register") 146 } 147 148 // Generate manifest 149 payload, err := store.newManifest(testManifestImageName, testManifestImageName, testManifestTag) 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 manifestChecksum, err := img.GetCheckSum(store.graph.ImageRoot(testManifestImageID)) 155 if err != nil { 156 t.Fatal(err) 157 } 158 159 var manifest registry.ManifestData 160 if err := json.Unmarshal(payload, &manifest); err != nil { 161 t.Fatalf("error unmarshalling manifest: %s", err) 162 } 163 164 if len(manifest.FSLayers) != 1 { 165 t.Fatalf("Unexpected number of layers, expecting 1: %d", len(manifest.FSLayers)) 166 } 167 168 if manifest.FSLayers[0].BlobSum != manifestChecksum { 169 t.Fatalf("Unexpected blob sum, expecting %q, got %q", manifestChecksum, manifest.FSLayers[0].BlobSum) 170 } 171 172 if len(manifest.History) != 1 { 173 t.Fatalf("Unexpected number of layer history, expecting 1: %d", len(manifest.History)) 174 } 175 176 v1compat, err := img.RawJson() 177 if err != nil { 178 t.Fatal(err) 179 } 180 if manifest.History[0].V1Compatibility != string(v1compat) { 181 t.Fatalf("Unexpected json value\nExpected:\n%s\nActual:\n%s", v1compat, manifest.History[0].V1Compatibility) 182 } 183 }