github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/os/ubuntu/esm_test.go (about)

     1  package ubuntu
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/devseccon/trivy/pkg/fanal/analyzer"
    12  	"github.com/devseccon/trivy/pkg/fanal/types"
    13  )
    14  
    15  func Test_ubuntuESMAnalyzer_Analyze(t *testing.T) {
    16  	tests := []struct {
    17  		name     string
    18  		filePath string
    19  		testFile string
    20  		want     *analyzer.AnalysisResult
    21  		wantErr  string
    22  	}{
    23  		{
    24  			name:     "happy path. Parse status.json file(ESM enabled)",
    25  			filePath: "var/lib/ubuntu-advantage/status.json",
    26  			testFile: "testdata/esm_enabled_status.json",
    27  			want: &analyzer.AnalysisResult{
    28  				OS: types.OS{Family: "ubuntu", Extended: true},
    29  			},
    30  		},
    31  		{
    32  			name:     "happy path. Parse status.json file(ESM disabled)",
    33  			filePath: "var/lib/ubuntu-advantage/status.json",
    34  			testFile: "testdata/esm_disabled_status.json",
    35  			want:     nil,
    36  		},
    37  		{
    38  			name:     "sad path",
    39  			filePath: "var/lib/ubuntu-advantage/status.json",
    40  			testFile: "testdata/invalid",
    41  			wantErr:  "ubuntu ESM analyze error",
    42  		},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			a := ubuntuESMAnalyzer{}
    47  			f, err := os.Open(tt.testFile)
    48  			require.NoError(t, err)
    49  			defer f.Close()
    50  
    51  			ctx := context.Background()
    52  			got, err := a.Analyze(ctx, analyzer.AnalysisInput{
    53  				FilePath: tt.filePath,
    54  				Content:  f,
    55  			})
    56  			if tt.wantErr != "" {
    57  				assert.ErrorContains(t, err, tt.wantErr)
    58  				return
    59  			}
    60  			require.NoError(t, err)
    61  			assert.Equal(t, tt.want, got)
    62  		})
    63  	}
    64  }
    65  
    66  func Test_ubuntuESMAnalyzer_Required(t *testing.T) {
    67  	tests := []struct {
    68  		name     string
    69  		filePath string
    70  		want     bool
    71  	}{
    72  		{
    73  			name:     "happy path status.json",
    74  			filePath: "var/lib/ubuntu-advantage/status.json",
    75  			want:     true,
    76  		},
    77  		{
    78  			name:     "sad path",
    79  			filePath: "etc/invalid",
    80  			want:     false,
    81  		},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			a := ubuntuESMAnalyzer{}
    86  			got := a.Required(tt.filePath, nil)
    87  			assert.Equal(t, tt.want, got)
    88  		})
    89  	}
    90  }