github.com/moby/docker@v26.1.3+incompatible/layer/migration_test.go (about)

     1  package layer // import "github.com/docker/docker/layer"
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/daemon/graphdriver"
    12  	"github.com/docker/docker/pkg/stringid"
    13  )
    14  
    15  func tarFromFilesInGraph(graph graphdriver.Driver, graphID, parentID string, files ...FileApplier) ([]byte, error) {
    16  	t, err := tarFromFiles(files...)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	if err := graph.Create(graphID, parentID, nil); err != nil {
    22  		return nil, err
    23  	}
    24  	if _, err := graph.ApplyDiff(graphID, parentID, bytes.NewReader(t)); err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	ar, err := graph.Diff(graphID, parentID)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	defer ar.Close()
    33  
    34  	return io.ReadAll(ar)
    35  }
    36  
    37  func TestLayerMigrationNoTarsplit(t *testing.T) {
    38  	// TODO Windows: Figure out why this is failing
    39  	if runtime.GOOS == "windows" {
    40  		t.Skip("Failing on Windows")
    41  	}
    42  	td, err := os.MkdirTemp("", "migration-test-")
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	defer os.RemoveAll(td)
    47  
    48  	layer1Files := []FileApplier{
    49  		newTestFile("/root/.bashrc", []byte("# Boring configuration"), 0o644),
    50  		newTestFile("/etc/profile", []byte("# Base configuration"), 0o644),
    51  	}
    52  
    53  	layer2Files := []FileApplier{
    54  		newTestFile("/root/.bashrc", []byte("# Updated configuration"), 0o644),
    55  	}
    56  
    57  	graph, err := newVFSGraphDriver(filepath.Join(td, "graphdriver-"))
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	graphID1 := stringid.GenerateRandomID()
    62  	graphID2 := stringid.GenerateRandomID()
    63  
    64  	tar1, err := tarFromFilesInGraph(graph, graphID1, "", layer1Files...)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	tar2, err := tarFromFilesInGraph(graph, graphID2, graphID1, layer2Files...)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	root := filepath.Join(td, "layers")
    75  	ls, err := newStoreFromGraphDriver(root, graph)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	newTarDataPath := filepath.Join(td, ".migration-tardata")
    81  	diffID, size, err := ls.(*layerStore).ChecksumForGraphID(graphID1, "", newTarDataPath)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	layer1a, err := ls.(*layerStore).RegisterByGraphID(graphID1, "", diffID, newTarDataPath, size)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	layer1b, err := ls.Register(bytes.NewReader(tar1), "")
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  
    96  	assertReferences(t, layer1a, layer1b)
    97  
    98  	// Attempt register, should be same
    99  	layer2a, err := ls.Register(bytes.NewReader(tar2), layer1a.ChainID())
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	diffID, size, err = ls.(*layerStore).ChecksumForGraphID(graphID2, graphID1, newTarDataPath)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  
   109  	layer2b, err := ls.(*layerStore).RegisterByGraphID(graphID2, layer1a.ChainID(), diffID, newTarDataPath, size)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	assertReferences(t, layer2a, layer2b)
   114  
   115  	if metadata, err := ls.Release(layer2a); err != nil {
   116  		t.Fatal(err)
   117  	} else if len(metadata) > 0 {
   118  		t.Fatalf("Unexpected layer removal after first release: %#v", metadata)
   119  	}
   120  
   121  	metadata, err := ls.Release(layer2b)
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  
   126  	assertMetadata(t, metadata, createMetadata(layer2a))
   127  }