github.com/quay/claircore@v1.5.28/indexer/controller/checkmanifest_test.go (about)

     1  package controller
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/quay/zlog"
     9  
    10  	"github.com/quay/claircore"
    11  	indexer "github.com/quay/claircore/test/mock/indexer"
    12  )
    13  
    14  // confirm checkManfest statefunc acts appropriately
    15  // when manifest has been seen
    16  func TestCheckManifest(t *testing.T) {
    17  	ctx := zlog.Test(context.Background(), t)
    18  	tt := []struct {
    19  		// the name of this test
    20  		name string
    21  		// the expected state returned
    22  		expectedState State
    23  		// a function to initialize any mocks
    24  		mock func(t *testing.T) *indexer.MockStore
    25  	}{
    26  		{
    27  			name:          "Seen",
    28  			expectedState: Terminal,
    29  			mock: func(t *testing.T) *indexer.MockStore {
    30  				ctrl := gomock.NewController(t)
    31  				m := indexer.NewMockStore(ctrl)
    32  				m.EXPECT().ManifestScanned(gomock.Any(), gomock.Any(), gomock.Any()).Return(true, nil)
    33  				m.EXPECT().IndexReport(gomock.Any(), gomock.Any()).Return(&claircore.IndexReport{}, true, nil)
    34  				return m
    35  			},
    36  		},
    37  		{
    38  			name:          "Unseen",
    39  			expectedState: FetchLayers,
    40  			mock: func(t *testing.T) *indexer.MockStore {
    41  				ctrl := gomock.NewController(t)
    42  				m := indexer.NewMockStore(ctrl)
    43  				m.EXPECT().ManifestScanned(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil)
    44  				m.EXPECT().PersistManifest(gomock.Any(), gomock.Any()).Return(nil)
    45  				return m
    46  			},
    47  		},
    48  	}
    49  
    50  	for _, table := range tt {
    51  		t.Run(table.name, func(t *testing.T) {
    52  			ctx := zlog.Test(ctx, t)
    53  			// get mock
    54  			m := table.mock(t)
    55  
    56  			// create indexer
    57  			opts := &indexer.Options{
    58  				Store: m,
    59  			}
    60  			s := New(opts)
    61  
    62  			// call state func
    63  			state, err := checkManifest(ctx, s)
    64  			if err != nil {
    65  				t.Error(err)
    66  			}
    67  			if got, want := state, table.expectedState; got != want {
    68  				t.Errorf("got: %v, want: %v", got, want)
    69  			}
    70  		})
    71  	}
    72  }