Skip to content

Commit

Permalink
Stalwart 25 pipeline function to store parsed stage file (#6683)
Browse files Browse the repository at this point in the history
* Create preview pipeline function added

Signed-off-by: sonali wale <sonali.wale@progress.com>

* Test cases added

Signed-off-by: sonali wale <sonali.wale@progress.com>

* Updated test cases

Signed-off-by: sonali wale <sonali.wale@progress.com>

* Resolved conflicts

Signed-off-by: sonali wale <sonali.wale@progress.com>
  • Loading branch information
sonali523 authored and vinay033 committed Apr 19, 2022
1 parent fe0bcd6 commit acc6bd2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
27 changes: 26 additions & 1 deletion components/infra-proxy-service/migrations/pipeline/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"path/filepath"

"github.com/chef/automate/api/interservice/authz"

"github.com/chef/automate/components/infra-proxy-service/pipeline"

"github.com/chef/automate/components/infra-proxy-service/storage"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -144,6 +144,31 @@ func ParseOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationSto

}

//CreatePreview Stores the staged data in db
func CreatePreview(ctx context.Context, st storage.Storage, mst storage.MigrationStorage, result pipeline.Result) (pipeline.Result, error) {
log.Info("Starting with create preview phase for migration id: ", result.Meta.MigrationID)

_, err := mst.StartCreatePreview(ctx, result.Meta.MigrationID, result.Meta.ServerID)
if err != nil {
log.Errorf("Failed to update the status for create preview for the migration id %s : %s", result.Meta.MigrationID, err.Error())
return result, err
}
_, err = mst.StoreMigrationStage(ctx, result.Meta.MigrationID, result)
if err != nil {
log.Errorf("Failed to store the staged data %s : %s ", result.Meta.MigrationID, err.Error())
_, _ = mst.FailedCreatePreview(ctx, result.Meta.MigrationID, result.Meta.ServerID, err.Error(), 0, 0, 0)
return result, err
}
_, err = mst.CompleteCreatePreview(ctx, result.Meta.MigrationID, result.Meta.ServerID, 0, 0, 0)
if err != nil {
log.Errorf("Failed to update the complete status while creating preview for migration id %s : %s", result.Meta.MigrationID, err.Error())
return result, err
}
log.Info("Successfully completed the create preview phase for migration id: ", result.Meta.MigrationID)

return result, nil
}

func createDatabaseOrgsMap(orgs []storage.Org) map[string]string {
orgMap := make(map[string]string)
for _, s := range orgs {
Expand Down
33 changes: 33 additions & 0 deletions components/infra-proxy-service/migrations/pipeline/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func TestStoreOrg(t *testing.T) {
{name: "Test Store Org", errorFromProject: false, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: pipeline.Org{Name: "org2", FullName: "Org 2", ActionOps: pipeline.Insert}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: nil, want1: pipeline.Insert},
{name: "Test Edit Org", errorFromProject: false, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: pipeline.Org{Name: "org3", FullName: "Org 3", ActionOps: pipeline.Update}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: nil, want1: pipeline.Update},
{name: "Test Create Project Error", errorFromProject: true, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: pipeline.Org{Name: "org3", FullName: "Org 3", ActionOps: pipeline.Insert}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: errors.New("Project already exists"), want1: 0},
{name: "Test Error from Delete Org", errorFromProject: false, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: pipeline.Org{Name: "org3", FullName: "Org 1", ActionOps: pipeline.Delete}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: errors.New("failed to delete org"), want1: pipeline.Delete},
{name: "Test Error from Store Org", errorFromProject: false, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: pipeline.Org{Name: "org2", FullName: "Org 2", ActionOps: pipeline.Insert}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: errors.New("failed to store org"), want1: pipeline.Insert},
{name: "Test Error from Edit Org", errorFromProject: false, args: args{ctx: context.Background(), st: &testDB.TestDB{NeedError: true}, org: pipeline.Org{Name: "org3", FullName: "Org 3", ActionOps: pipeline.Update}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: errors.New("failed to edit org"), want1: pipeline.Update},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -96,3 +99,33 @@ func TestParseOrg(t *testing.T) {

}
}

func TestCreatePreview(t *testing.T) {
type args struct {
ctx context.Context
st storage.Storage
mst storage.MigrationStorage
result pipeline.Result
}
tests := []struct {
name string
args args
wantError error
want1 pipeline.Result
}{
{name: "Test Create preview", args: args{ctx: context.Background(), st: &testDB.TestDB{}, mst: &testDB.MigrationDB{}, result: pipeline.Result{Meta: pipeline.Meta{ServerID: "server1", MigrationID: "mig1"}, ParsedResult: pipeline.ParsedResult{Orgs: []pipeline.Org{{Name: "Org1", FullName: "Org1_infra", ActionOps: pipeline.Insert}}}}}, wantError: nil, want1: pipeline.Result{Meta: pipeline.Meta{ServerID: "server1", MigrationID: "mig1"}, ParsedResult: pipeline.ParsedResult{Orgs: []pipeline.Org{{Name: "Org1", FullName: "Org1_infra", ActionOps: pipeline.Insert}}}}},
{name: "Test Error from staged database", args: args{ctx: context.Background(), st: &testDB.TestDB{NeedError: true}, mst: &testDB.MigrationDB{}, result: pipeline.Result{Meta: pipeline.Meta{ServerID: "server1", MigrationID: "mig1"}, ParsedResult: pipeline.ParsedResult{Orgs: []pipeline.Org{{Name: "Org1", FullName: "Org1_infra", ActionOps: pipeline.Insert}}}}}, wantError: errors.New("Failed to insert staged data"), want1: pipeline.Result{Meta: pipeline.Meta{ServerID: "server1", MigrationID: "mig1"}, ParsedResult: pipeline.ParsedResult{Orgs: []pipeline.Org{{Name: "Org1", FullName: "Org1_infra", ActionOps: pipeline.Insert}}}}},
{name: "Test Error from Status database", args: args{ctx: context.Background(), st: &testDB.TestDB{}, mst: &testDB.MigrationDB{NeedError: true}, result: pipeline.Result{Meta: pipeline.Meta{ServerID: "server1", MigrationID: "mig1"}, ParsedResult: pipeline.ParsedResult{Orgs: []pipeline.Org{{Name: "Org1", FullName: "Org1_infra", ActionOps: pipeline.Insert}}}}}, wantError: errors.New("Failed to update status"), want1: pipeline.Result{Meta: pipeline.Meta{ServerID: "server1", MigrationID: "mig1"}, ParsedResult: pipeline.ParsedResult{Orgs: []pipeline.Org{{Name: "Org1", FullName: "Org1_infra", ActionOps: pipeline.Insert}}}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreatePreview(tt.args.ctx, tt.args.st, tt.args.mst, tt.args.result)
if err != nil && err.Error() != tt.wantError.Error() {
t.Errorf("CreatePreview() err = %v, want %v", err, tt.wantError)
}
if !reflect.DeepEqual(got, tt.want1) {
t.Errorf("CreatePreview() got = %v, want %v", got, tt.want1)
}
})
}
}
37 changes: 33 additions & 4 deletions components/infra-proxy-service/storage/testDB/testMigrationsDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testDB

import (
"context"

"github.com/chef/automate/components/infra-proxy-service/storage"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -109,15 +110,36 @@ func (m MigrationDB) FailedPermissionParsing(ctx context.Context, migrationId, s
}

func (m MigrationDB) StartCreatePreview(ctx context.Context, migrationId, serverId string) (storage.Migration, error) {
panic("implement me")
if m.NeedError {
return storage.Migration{}, errors.New("Failed to update status")
}
return storage.Migration{
ID: "mig1",
ServerID: "serverId",
TypeID: 100,
}, nil
}

func (m MigrationDB) CompleteCreatePreview(ctx context.Context, migrationId, serverId string, totalSucceeded, totalSkipped, totalFailed int64) (storage.Migration, error) {
panic("implement me")
if m.NeedError {
return storage.Migration{}, errors.New("Failed to update status")
}
return storage.Migration{
ID: "mig1",
ServerID: "serverId",
TypeID: 100,
}, nil
}

func (m MigrationDB) FailedCreatePreview(ctx context.Context, migrationId, serverId, message string, totalSucceeded, totalSkipped, totalFailed int64) (storage.Migration, error) {
panic("implement me")
if m.NeedError {
return storage.Migration{}, errors.New("Failed to update status")
}
return storage.Migration{
ID: "mig1",
ServerID: "serverId",
TypeID: 100,
}, nil
}

func (m MigrationDB) StartOrgMigration(ctx context.Context, migrationId, serverId string) (storage.Migration, error) {
Expand Down Expand Up @@ -192,7 +214,14 @@ func (m MigrationDB) FailedCancelMigration(ctx context.Context, migrationId, ser
}

func (m MigrationDB) StoreMigrationStage(ctx context.Context, migrationId string, parsedData interface{}) (storage.MigrationStage, error) {
panic("implement me")
if m.NeedError {
return storage.MigrationStage{}, errors.New("Failed to insert staged data")
}
return storage.MigrationStage{
ID: "1",
MigrationID: "mig1",
}, nil

}

func (m MigrationDB) GetMigrationStage(ctx context.Context, migrationId string) (storage.MigrationStage, error) {
Expand Down

0 comments on commit acc6bd2

Please sign in to comment.