...

Source file src/edge-infra.dev/pkg/edge/api/services/compatibility_service_test.go

Documentation: edge-infra.dev/pkg/edge/api/services

     1  package services
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"edge-infra.dev/pkg/edge/api/graph/model"
     8  	sqlquery "edge-infra.dev/pkg/edge/api/sql"
     9  	"edge-infra.dev/pkg/edge/api/testutils/seededpostgres"
    10  	"edge-infra.dev/pkg/edge/constants/api/fleet"
    11  
    12  	"github.com/DATA-DOG/go-sqlmock"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  var (
    17  	artifactVersionA = model.ArtifactVersion{
    18  		Name:    fleet.Store,
    19  		Version: "0.15",
    20  	}
    21  	artifactVersionB = model.ArtifactVersion{
    22  		Name:    fleet.Store,
    23  		Version: "0.13",
    24  	}
    25  	compatibleArtifactVersionA = model.ArtifactVersion{
    26  		Name:    "edge-os",
    27  		Version: "1.9",
    28  	}
    29  	compatibleArtifactVersionB = model.ArtifactVersion{
    30  		Name:    "edge-os",
    31  		Version: "1.8",
    32  	}
    33  	compatibleArtifactVersionC = model.ArtifactVersion{
    34  		Name:    "edge-os",
    35  		Version: "2.0",
    36  	}
    37  )
    38  
    39  func TestGetArtifactVersionCompatibility(t *testing.T) {
    40  	db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
    41  	if err != nil {
    42  		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    43  	}
    44  	defer db.Close()
    45  
    46  	mock.ExpectQuery(sqlquery.GetArtifactVersionsAndCompatibility).WithArgs(artifactVersionA.Name, artifactVersionA.Version).
    47  		WillReturnRows(mock.NewRows([]string{"artifact_name", "artifact_version", "nth_index", "compatible_artifact_name", "compatible_artifact_version"}).
    48  			AddRow(artifactVersionA.Name, artifactVersionA.Version, 1, compatibleArtifactVersionA.Name, compatibleArtifactVersionA.Version).
    49  			AddRow(artifactVersionA.Name, artifactVersionA.Version, 1, compatibleArtifactVersionB.Name, compatibleArtifactVersionB.Version))
    50  
    51  	svc := NewCompatibilityService(db)
    52  	result, err := svc.GetArtifactVersionCompatibility(context.Background(), artifactVersionA, nil)
    53  	assert.NoError(t, err)
    54  	assert.Len(t, result.CompatibleArtifacts, 2)
    55  	assert.NoError(t, mock.ExpectationsWereMet())
    56  }
    57  
    58  func TestIsCompatible(t *testing.T) {
    59  	db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
    60  	if err != nil {
    61  		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    62  	}
    63  	defer db.Close()
    64  
    65  	mock.ExpectQuery(sqlquery.GetArtifactVersionsAndCompatibility).WithArgs(artifactVersionA.Name, artifactVersionA.Version).
    66  		WillReturnRows(mock.NewRows([]string{"artifact_name", "artifact_version", "nth_index", "compatible_artifact_name", "compatible_artifact_version"}).
    67  			AddRow(artifactVersionA.Name, artifactVersionA.Version, 1, compatibleArtifactVersionA.Name, compatibleArtifactVersionA.Version).
    68  			AddRow(artifactVersionA.Name, artifactVersionA.Version, 1, compatibleArtifactVersionB.Name, compatibleArtifactVersionB.Version))
    69  
    70  	svc := NewCompatibilityService(db)
    71  
    72  	// test compatible artifact
    73  	result, err := svc.IsCompatible(context.Background(), artifactVersionA, compatibleArtifactVersionA)
    74  	assert.NoError(t, err)
    75  	assert.True(t, result)
    76  	assert.NoError(t, mock.ExpectationsWereMet())
    77  
    78  	mock.ExpectQuery(sqlquery.GetArtifactVersionsAndCompatibility).WithArgs(artifactVersionA.Name, artifactVersionA.Version).
    79  		WillReturnRows(mock.NewRows([]string{"artifact_name", "artifact_version", "nth_index", "compatible_artifact_name", "compatible_artifact_version"}).
    80  			AddRow(artifactVersionA.Name, artifactVersionA.Version, 1, compatibleArtifactVersionA.Name, compatibleArtifactVersionA.Version).
    81  			AddRow(artifactVersionA.Name, artifactVersionA.Version, 1, compatibleArtifactVersionB.Name, compatibleArtifactVersionB.Version))
    82  
    83  	// test incompatible artifact
    84  	result, err = svc.IsCompatible(context.Background(), artifactVersionA, compatibleArtifactVersionC)
    85  	assert.NoError(t, err)
    86  	assert.False(t, result)
    87  	assert.NoError(t, mock.ExpectationsWereMet())
    88  
    89  	mock.ExpectQuery(sqlquery.GetArtifactVersionsAndCompatibility).WithArgs(artifactVersionA.Name, artifactVersionA.Version).
    90  		WillReturnRows(mock.NewRows([]string{"artifact_name", "artifact_version", "nth_index", "compatible_artifact_name", "compatible_artifact_version"}).
    91  			AddRow(artifactVersionA.Name, artifactVersionA.Version, 2, compatibleArtifactVersionA.Name, compatibleArtifactVersionA.Version))
    92  	//test supported artifact
    93  	result, err = svc.IsCompatible(context.Background(), artifactVersionA, artifactVersionB)
    94  	assert.NoError(t, err)
    95  	assert.True(t, result)
    96  	assert.NoError(t, mock.ExpectationsWereMet())
    97  
    98  	mock.ExpectQuery(sqlquery.GetArtifactVersionsAndCompatibility).WithArgs(artifactVersionA.Name, artifactVersionA.Version).
    99  		WillReturnRows(mock.NewRows([]string{"artifact_name", "artifact_version", "nth_index", "compatible_artifact_name", "compatible_artifact_version"}).
   100  			AddRow(artifactVersionA.Name, artifactVersionA.Version, 1, compatibleArtifactVersionA.Name, compatibleArtifactVersionA.Version))
   101  
   102  	//test unsupported artifact
   103  	result, err = svc.IsCompatible(context.Background(), artifactVersionA, artifactVersionB)
   104  	assert.NoError(t, err)
   105  	assert.False(t, result)
   106  	assert.NoError(t, mock.ExpectationsWereMet())
   107  }
   108  
   109  func TestCompatibilityService(t *testing.T) {
   110  	ctx := context.Background()
   111  	sp, err := seededpostgres.New()
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	defer sp.Close()
   116  	db, err := sp.DB()
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  	defer db.Close()
   121  
   122  	svc := NewCompatibilityService(db)
   123  
   124  	compatibility := model.ArtifactCompatibilityPayload{
   125  		Artifact: &model.ArtifactInput{
   126  			Name:    artifactVersionA.Name,
   127  			Version: artifactVersionA.Version,
   128  		},
   129  		CompatibleArtifacts: []*model.ArtifactInput{
   130  			{
   131  				Name:    compatibleArtifactVersionC.Name,
   132  				Version: compatibleArtifactVersionC.Version,
   133  			},
   134  		},
   135  	}
   136  
   137  	added, err := svc.AddArtifactCompatibility(ctx, compatibility)
   138  	assert.NoError(t, err)
   139  	assert.True(t, added)
   140  
   141  	addedCompatibleArtifacts, err := svc.GetArtifactVersionCompatibility(ctx, artifactVersionA, nil)
   142  	assert.NoError(t, err)
   143  	assert.Len(t, addedCompatibleArtifacts.CompatibleArtifacts, 3)
   144  
   145  	deleted, err := svc.DeleteArtifactCompatibility(ctx, compatibility)
   146  	assert.NoError(t, err)
   147  	assert.True(t, deleted)
   148  
   149  	deletedCompatibleArtifacts, err := svc.GetArtifactVersionCompatibility(ctx, artifactVersionA, nil)
   150  	assert.NoError(t, err)
   151  	assert.Len(t, deletedCompatibleArtifacts.CompatibleArtifacts, 2)
   152  }
   153  

View as plain text