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

     1  package controller
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/quay/zlog"
    10  
    11  	indexer "github.com/quay/claircore/test/mock/indexer"
    12  )
    13  
    14  func TestIndexManifest(t *testing.T) {
    15  	ctx := context.Background()
    16  	tt := []struct {
    17  		name          string
    18  		expectedState State
    19  		err           bool
    20  		mock          func(t *testing.T) indexer.Store
    21  	}{
    22  		{
    23  			name:          "Success",
    24  			expectedState: IndexFinished,
    25  			mock: func(t *testing.T) indexer.Store {
    26  				ctrl := gomock.NewController(t)
    27  				s := indexer.NewMockStore(ctrl)
    28  				s.EXPECT().IndexManifest(gomock.Any(), gomock.Any()).Return(nil)
    29  				return s
    30  			},
    31  		},
    32  		{
    33  			name:          "Failure",
    34  			expectedState: Terminal,
    35  			err:           true,
    36  			mock: func(t *testing.T) indexer.Store {
    37  				ctrl := gomock.NewController(t)
    38  				s := indexer.NewMockStore(ctrl)
    39  				s.EXPECT().IndexManifest(gomock.Any(), gomock.Any()).Return(fmt.Errorf("failure"))
    40  				return s
    41  			},
    42  		},
    43  	}
    44  
    45  	for _, table := range tt {
    46  		t.Run(table.name, func(t *testing.T) {
    47  			ctx := zlog.Test(ctx, t)
    48  			s := table.mock(t)
    49  			indexer := New(&indexer.Options{
    50  				Store: s,
    51  			})
    52  
    53  			state, err := indexManifest(ctx, indexer)
    54  			if (err == nil) == table.err {
    55  				t.Fatalf("did not expect error: %v", err)
    56  			}
    57  			if table.expectedState != state {
    58  				t.Fatalf("got: %v, want: %v", state, table.expectedState)
    59  			}
    60  		})
    61  	}
    62  }