...

Source file src/edge-infra.dev/pkg/f8n/warehouse/oci/cmp/diff_test.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/oci/cmp

     1  package cmp_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"edge-infra.dev/pkg/f8n/warehouse/oci"
    10  	"edge-infra.dev/pkg/f8n/warehouse/oci/cmp"
    11  )
    12  
    13  func TestDiff(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	tcs := []struct {
    17  		a  oci.Artifact
    18  		bb []oci.Artifact
    19  	}{
    20  		{shoot, []oci.Artifact{shoot}},
    21  		{shoot, []oci.Artifact{
    22  			redpanda, certmgr, store, chirp, patchmanager, prometheus,
    23  		}},
    24  		{shoot, []oci.Artifact{
    25  			ctlfish, edgeIAM, novnc, couchdb, fluentbit, ksm, corednsctl,
    26  		}},
    27  		{shoot, []oci.Artifact{store}},
    28  		{shoot, []oci.Artifact{fluentbit, prometheus, certmgr, redpanda}},
    29  		{shoot, []oci.Artifact{
    30  			redpanda, certmgr, store, chirp, patchmanager, prometheus, ctlfish,
    31  			edgeIAM, novnc, couchdb, fluentbit, ksm, corednsctl,
    32  		}},
    33  		{shoot, []oci.Artifact{}},
    34  		{certmgr, []oci.Artifact{certmgr}},
    35  		{certmgr, []oci.Artifact{
    36  			redpanda, store, chirp, patchmanager, prometheus,
    37  		}},
    38  		{certmgr, []oci.Artifact{
    39  			ctlfish, edgeIAM, novnc, couchdb, fluentbit, ksm, corednsctl,
    40  		}},
    41  		{certmgr, []oci.Artifact{store}},
    42  		{certmgr, []oci.Artifact{}},
    43  		{certmgr, []oci.Artifact{fluentbit, prometheus, redpanda}},
    44  		{certmgr, []oci.Artifact{
    45  			redpanda, store, chirp, patchmanager, prometheus, ctlfish,
    46  			edgeIAM, novnc, couchdb, fluentbit, ksm, corednsctl,
    47  		}},
    48  		{store, []oci.Artifact{store}},
    49  		{certmgr, []oci.Artifact{}},
    50  		{certmgr, nil},
    51  		{store, []oci.Artifact{
    52  			redpanda, certmgr, store, chirp, patchmanager, prometheus,
    53  		}},
    54  		{store, []oci.Artifact{
    55  			ctlfish, edgeIAM, novnc, couchdb, fluentbit, ksm, corednsctl,
    56  		}},
    57  		{store, []oci.Artifact{shoot}},
    58  		{store, []oci.Artifact{fluentbit, prometheus, certmgr, redpanda}},
    59  		{store, []oci.Artifact{
    60  			redpanda, certmgr, store, chirp, patchmanager, prometheus, ctlfish,
    61  			edgeIAM, novnc, couchdb, fluentbit, ksm, corednsctl,
    62  		}},
    63  	}
    64  
    65  	for i, tc := range tcs {
    66  		tc := tc
    67  		i := i
    68  		t.Run(fmt.Sprintf("%s-%d", t.Name(), i), func(t *testing.T) {
    69  			t.Parallel()
    70  			a := assert.New(t)
    71  
    72  			hashes, err := cmp.Hashes(tc.a)
    73  			a.NoError(err)
    74  			t.Log("hashes in artifact", hashes)
    75  			diff, err := cmp.Diff(tc.a, tc.bb...)
    76  			a.NoError(err)
    77  			t.Log("calculated diff", diff)
    78  
    79  			// If we diff against ourselves, should be empty
    80  			if len(tc.bb) == 1 && tc.a == tc.bb[0] {
    81  				a.Len(diff, 0)
    82  			}
    83  
    84  			if len(tc.bb) == 0 {
    85  				t.Log("diffing against empty set of artifacts")
    86  				a.Len(diff, len(hashes))
    87  				// Every hash in a should be in result
    88  				for _, h := range hashes {
    89  					found := false
    90  					for _, d := range diff {
    91  						if d == h {
    92  							found = true
    93  							break
    94  						}
    95  					}
    96  					a.True(found, "digest %s not present in diff", h.Hex)
    97  				}
    98  			}
    99  
   100  			for _, d := range diff {
   101  				found, err := cmp.ContainsBlob(d, tc.bb...)
   102  				a.NoError(err)
   103  				a.False(found, "digest %s in diff but found in bb", d.Hex)
   104  
   105  				found, err = cmp.ContainsBlob(d, tc.a)
   106  				a.NoError(err)
   107  				a.True(found, "digest %s in diff but not found in a", d.Hex)
   108  			}
   109  		})
   110  	}
   111  }
   112  

View as plain text