github.com/khulnasoft-lab/tunnel-db@v0.0.0-20231117205118-74e1113bd007/pkg/db/advisory_detail_test.go (about)

     1  package db_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  	bolt "go.etcd.io/bbolt"
     9  
    10  	"github.com/khulnasoft-lab/tunnel-db/pkg/db"
    11  	"github.com/khulnasoft-lab/tunnel-db/pkg/dbtest"
    12  	"github.com/khulnasoft-lab/tunnel-db/pkg/types"
    13  )
    14  
    15  func TestConfig_SaveAdvisoryDetails(t *testing.T) {
    16  	type want struct {
    17  		key   []string
    18  		value types.Advisory
    19  	}
    20  	tests := []struct {
    21  		name     string
    22  		fixtures []string
    23  		vulnID   string
    24  		want     []want
    25  		wantErr  string
    26  	}{
    27  		{
    28  			name:     "happy path",
    29  			fixtures: []string{"testdata/fixtures/advisory-detail.yaml"},
    30  			vulnID:   "CVE-2019-14904",
    31  			want: []want{
    32  				{
    33  					key: []string{"alpine 3.14", "ansible", "CVE-2019-14904"},
    34  					value: types.Advisory{
    35  						FixedVersion: "2.9.3-r0",
    36  					},
    37  				},
    38  				{
    39  					key: []string{"debian 10", "ansible", "CVE-2019-14904"},
    40  					value: types.Advisory{
    41  						FixedVersion: "2.3.4",
    42  					},
    43  				},
    44  				{
    45  					key: []string{"Red Hat", "cpe:/o:redhat:enterprise_linux:6::server", "ansible", "CVE-2019-14904"},
    46  					value: types.Advisory{
    47  						FixedVersion: "3.4.5",
    48  					},
    49  				},
    50  			},
    51  		},
    52  		{
    53  			name:     "missing ID",
    54  			fixtures: []string{"testdata/fixtures/advisory-detail.yaml"},
    55  			vulnID:   "CVE-2019-9999",
    56  			want:     nil,
    57  		},
    58  	}
    59  	for _, tt := range tests {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			// Initialize DB for testing
    62  			tmpDir := dbtest.InitDB(t, tt.fixtures)
    63  			defer db.Close()
    64  
    65  			dbc := db.Config{}
    66  			err := dbc.BatchUpdate(func(tx *bolt.Tx) error {
    67  				return dbc.SaveAdvisoryDetails(tx, tt.vulnID)
    68  			})
    69  
    70  			if tt.wantErr != "" {
    71  				require.NotNil(t, err)
    72  				assert.Contains(t, err.Error(), tt.wantErr)
    73  				return
    74  			}
    75  
    76  			require.NoError(t, err)
    77  			require.NoError(t, db.Close()) // Need to close before dbtest.JSONEq is called
    78  			for _, w := range tt.want {
    79  				dbtest.JSONEq(t, db.Path(tmpDir), w.key, w.value)
    80  			}
    81  		})
    82  	}
    83  }