github.com/afbjorklund/moby@v20.10.5+incompatible/layer/filestore_test.go (about)

     1  package layer // import "github.com/docker/docker/layer"
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"math/rand"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"syscall"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/pkg/stringid"
    14  	digest "github.com/opencontainers/go-digest"
    15  )
    16  
    17  func randomLayerID(seed int64) ChainID {
    18  	r := rand.New(rand.NewSource(seed))
    19  
    20  	return ChainID(digest.FromBytes([]byte(fmt.Sprintf("%d", r.Int63()))))
    21  }
    22  
    23  func newFileMetadataStore(t *testing.T) (*fileMetadataStore, string, func()) {
    24  	td, err := ioutil.TempDir("", "layers-")
    25  	if err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	fms, err := newFSMetadataStore(td)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	return fms, td, func() {
    34  		if err := os.RemoveAll(td); err != nil {
    35  			t.Logf("Failed to cleanup %q: %s", td, err)
    36  		}
    37  	}
    38  }
    39  
    40  func assertNotDirectoryError(t *testing.T, err error) {
    41  	perr, ok := err.(*os.PathError)
    42  	if !ok {
    43  		t.Fatalf("Unexpected error %#v, expected path error", err)
    44  	}
    45  
    46  	if perr.Err != syscall.ENOTDIR {
    47  		t.Fatalf("Unexpected error %s, expected %s", perr.Err, syscall.ENOTDIR)
    48  	}
    49  }
    50  
    51  func TestCommitFailure(t *testing.T) {
    52  	fms, td, cleanup := newFileMetadataStore(t)
    53  	defer cleanup()
    54  
    55  	if err := ioutil.WriteFile(filepath.Join(td, "sha256"), []byte("was here first!"), 0644); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	tx, err := fms.StartTransaction()
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	if err := tx.SetSize(0); err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	err = tx.Commit(randomLayerID(5))
    69  	if err == nil {
    70  		t.Fatalf("Expected error committing with invalid layer parent directory")
    71  	}
    72  	assertNotDirectoryError(t, err)
    73  }
    74  
    75  func TestStartTransactionFailure(t *testing.T) {
    76  	fms, td, cleanup := newFileMetadataStore(t)
    77  	defer cleanup()
    78  
    79  	if err := ioutil.WriteFile(filepath.Join(td, "tmp"), []byte("was here first!"), 0644); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  
    83  	_, err := fms.StartTransaction()
    84  	if err == nil {
    85  		t.Fatalf("Expected error starting transaction with invalid layer parent directory")
    86  	}
    87  	assertNotDirectoryError(t, err)
    88  
    89  	if err := os.Remove(filepath.Join(td, "tmp")); err != nil {
    90  		t.Fatal(err)
    91  	}
    92  
    93  	tx, err := fms.StartTransaction()
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	if expected := filepath.Join(td, "tmp"); strings.HasPrefix(expected, tx.String()) {
    99  		t.Fatalf("Unexpected transaction string %q, expected prefix %q", tx.String(), expected)
   100  	}
   101  
   102  	if err := tx.Cancel(); err != nil {
   103  		t.Fatal(err)
   104  	}
   105  }
   106  
   107  func TestGetOrphan(t *testing.T) {
   108  	fms, td, cleanup := newFileMetadataStore(t)
   109  	defer cleanup()
   110  
   111  	layerRoot := filepath.Join(td, "sha256")
   112  	if err := os.MkdirAll(layerRoot, 0755); err != nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	tx, err := fms.StartTransaction()
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  
   121  	layerid := randomLayerID(5)
   122  	err = tx.Commit(layerid)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	layerPath := fms.getLayerDirectory(layerid)
   127  	if err := ioutil.WriteFile(filepath.Join(layerPath, "cache-id"), []byte(stringid.GenerateRandomID()), 0644); err != nil {
   128  		t.Fatal(err)
   129  	}
   130  
   131  	orphanLayers, err := fms.getOrphan()
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	if len(orphanLayers) != 0 {
   136  		t.Fatalf("Expected to have zero orphan layers")
   137  	}
   138  
   139  	layeridSplit := strings.Split(layerid.String(), ":")
   140  	newPath := filepath.Join(layerRoot, fmt.Sprintf("%s-%s-removing", layeridSplit[1], stringid.GenerateRandomID()))
   141  	err = os.Rename(layerPath, newPath)
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	orphanLayers, err = fms.getOrphan()
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	if len(orphanLayers) != 1 {
   150  		t.Fatalf("Expected to have one orphan layer")
   151  	}
   152  }