...

Source file src/github.com/containerd/continuity/fs/fstest/compare.go

Documentation: github.com/containerd/continuity/fs/fstest

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package fstest
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/containerd/continuity"
    24  )
    25  
    26  // CheckDirectoryEqual compares two directory paths to make sure that
    27  // the content of the directories is the same.
    28  func CheckDirectoryEqual(d1, d2 string) error {
    29  	c1, err := continuity.NewContext(d1)
    30  	if err != nil {
    31  		return fmt.Errorf("failed to build context: %w", err)
    32  	}
    33  
    34  	c2, err := continuity.NewContext(d2)
    35  	if err != nil {
    36  		return fmt.Errorf("failed to build context: %w", err)
    37  	}
    38  
    39  	m1, err := continuity.BuildManifest(c1)
    40  	if err != nil {
    41  		return fmt.Errorf("failed to build manifest: %w", err)
    42  	}
    43  
    44  	m2, err := continuity.BuildManifest(c2)
    45  	if err != nil {
    46  		return fmt.Errorf("failed to build manifest: %w", err)
    47  	}
    48  
    49  	diff := diffResourceList(m1.Resources, m2.Resources)
    50  	if diff.HasDiff() {
    51  		return fmt.Errorf("directory diff between %s and %s\n%s", d1, d2, diff.String())
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // CheckDirectoryEqualWithApplier compares directory against applier
    58  func CheckDirectoryEqualWithApplier(root string, a Applier) error {
    59  	applied, err := os.MkdirTemp("", "fstest")
    60  	if err != nil {
    61  		return err
    62  	}
    63  	defer os.RemoveAll(applied)
    64  	if err := a.Apply(applied); err != nil {
    65  		return err
    66  	}
    67  	return CheckDirectoryEqual(applied, root)
    68  }
    69  

View as plain text