diff --git a/components/infra-proxy-service/migrations/migrations.go b/components/infra-proxy-service/migrations/migrations.go index c4a6c9864db..1ff3b45873a 100644 --- a/components/infra-proxy-service/migrations/migrations.go +++ b/components/infra-proxy-service/migrations/migrations.go @@ -8,11 +8,12 @@ import ( "os" "path" + "github.com/chef/automate/components/infra-proxy-service/pipeline" + "github.com/chef/automate/api/interservice/infra_proxy/migrations/request" "github.com/chef/automate/api/interservice/infra_proxy/migrations/response" "github.com/chef/automate/api/interservice/infra_proxy/migrations/service" "github.com/chef/automate/components/infra-proxy-service/constants" - "github.com/chef/automate/components/infra-proxy-service/migrations/pipeline" pipeline_model "github.com/chef/automate/components/infra-proxy-service/pipeline" "github.com/chef/automate/components/infra-proxy-service/validation" diff --git a/components/infra-proxy-service/migrations/pipeline/models.go b/components/infra-proxy-service/migrations/pipeline/models.go deleted file mode 100644 index b730e76df52..00000000000 --- a/components/infra-proxy-service/migrations/pipeline/models.go +++ /dev/null @@ -1,127 +0,0 @@ -package pipeline - -type ActionOps int - -const ( - Insert ActionOps = 1 + iota - Skip - Delete - Update -) - -// -type Result struct { - // Meta for Zip file info - Meta Meta `json:"meta"` - - // ParsedResult for Orgs, Users - ParsedResult ParsedResult `json:"parsed_result"` -} - -type Meta struct { - // StageResults holds Skipped instances - StageResults []StageResult `json:"stage_results"` - - // ZipFile for zip file location - ZipFile string `json:"zip_file"` - - // UnzipFolder for unzipped folder's location - UnzipFolder string `json:"unzip_folder"` - - // Chef Infra Server ID - ServerID string `json:"server_id"` - - // Migration ID - MigrationID string `json:"migration_id"` -} - -type StageResult struct { - StageName string `json:"stage_name"` - IsSuccess bool `json:"is_success"` - Failure error `json:"failure"` -} - -type ParsedResult struct { - // Orgs array - Orgs []Org `json:"orgs"` - - // Users array - Users []User `json:"users"` - - // OrgsUsers for Orgs and Users associations - OrgsUsers []OrgsUsersAssociations `json:"orgs_users_associations"` -} - -// OrgsUsersAssociations -type OrgsUsersAssociations struct { - // OrgName - OrgName Org `json:"org_name"` - - // Users UserAssociation slice - Users []UserAssociation `json:"user_association"` -} -type UserAssociation struct { - // Username - Username string `json:"username"` - - // IsAdmin - IsAdmin bool `json:"is_admin"` -} - -type KeyDump struct { - ID string `json:"id"` - AuthzID string `json:"authz_id"` - Username string `json:"username"` - Email string `json:"email"` - PubkeyVersion int `json:"pubkey_version"` - PublicKey interface{} `json:"public_key"` - SerializedObject string `json:"serialized_object"` - LastUpdatedBy string `json:"last_updated_by"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - ExternalAuthenticationUID interface{} `json:"external_authentication_uid"` - RecoveryAuthenticationEnabled interface{} `json:"recovery_authentication_enabled"` - Admin bool `json:"admin"` - HashedPassword interface{} `json:"hashed_password"` - Salt interface{} `json:"salt"` - HashType interface{} `json:"hash_type"` -} - -type Org struct { - - // Org Name - Name string `json:"name"` - - // FullName - FullName string `json:"full_name"` - - // ActionOps for Insert Skip Update and Delete - ActionOps ActionOps `json:"action_ops"` -} - -type User struct { - Username string `json:"username"` - Email string `json:"email"` - DisplayName string `json:"display_name"` - FirstName string `json:"first_name"` - LastName string `json:"last_name"` - MiddleName string `json:"middle_name"` - - // AutomateUsername is ldap username - AutomateUsername string `json:"automate_username"` - - // Connector ldap user - Connector string `json:"connector"` - - // IsConflicting for user's existance in db - IsConflicting bool `json:"is_conflicting"` - - // IsAdmin (user is admin or not) - IsAdmin bool `json:"is_admin"` -} - -type OrgJson struct { - Name string `json:"name"` - FullName string `json:"full_name"` - Guid string `json:"guid"` -} diff --git a/components/infra-proxy-service/migrations/pipeline/phaseonemigration.go b/components/infra-proxy-service/migrations/pipeline/phaseonemigration.go index a79089dd8f7..eeb5ca405ea 100644 --- a/components/infra-proxy-service/migrations/pipeline/phaseonemigration.go +++ b/components/infra-proxy-service/migrations/pipeline/phaseonemigration.go @@ -3,10 +3,20 @@ package pipeline import ( "context" "fmt" + + log "github.com/sirupsen/logrus" + + "github.com/chef/automate/components/infra-proxy-service/pipeline" + "github.com/chef/automate/components/infra-proxy-service/storage" +) + +var ( + Storage storage.Storage + Mig storage.MigrationStorage ) type PipelineData struct { - Result Result + Result pipeline.Result Done chan<- error Ctx context.Context } @@ -189,7 +199,7 @@ func adminUsers(result <-chan PipelineData) <-chan PipelineData { func migrationPipeline(source <-chan PipelineData, pipes ...PhaseOnePipelineProcessor) { fmt.Println("Pipeline started...") - + status := make(chan string) go func() { for _, pipe := range pipes { source = pipe(source) @@ -198,7 +208,9 @@ func migrationPipeline(source <-chan PipelineData, pipes ...PhaseOnePipelineProc for s := range source { s.Done <- nil } + status <- "Done" }() + <-status } func SetupPhaseOnePipeline() PhaseOnePipleine { @@ -214,7 +226,8 @@ func SetupPhaseOnePipeline() PhaseOnePipleine { return PhaseOnePipleine{in: c} } -func (p *PhaseOnePipleine) Run(result Result) { +func (p *PhaseOnePipleine) Run(result pipeline.Result) { + status := make(chan string) go func() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -224,8 +237,25 @@ func (p *PhaseOnePipleine) Run(result Result) { } err := <-done if err != nil { - fmt.Println("received error") + MigrationError(err, Mig, ctx, result.Meta.MigrationID, result.Meta.ServerID) + log.Errorf("Phase one pipeline received error for migration %s: %s", result.Meta.MigrationID, err) } - fmt.Println("received done") + log.Println("received done") + status <- "Done" }() + <-status +} + +func MigrationError(err error, st storage.MigrationStorage, ctx context.Context, migrationId, serviceId string) { + _, err = st.FailedMigration(ctx, migrationId, serviceId, err.Error(), 0, 0, 0) + if err != nil { + log.Errorf("received error while updating for migration id %s: %s", migrationId, err) + } +} + +func MigrationSuccess(st storage.MigrationStorage, ctx context.Context, migrationId, serviceId string) { + _, err := st.CompleteMigration(ctx, migrationId, serviceId, 0, 0, 0) + if err != nil { + log.Errorf("received error while updating for migration id %s: %s", migrationId, err) + } } diff --git a/components/infra-proxy-service/migrations/pipeline/phasetwomigration.go b/components/infra-proxy-service/migrations/pipeline/phasetwomigration.go index e504284c2d5..2b1512d8e67 100644 --- a/components/infra-proxy-service/migrations/pipeline/phasetwomigration.go +++ b/components/infra-proxy-service/migrations/pipeline/phasetwomigration.go @@ -3,6 +3,10 @@ package pipeline import ( "context" "fmt" + + log "github.com/sirupsen/logrus" + + "github.com/chef/automate/components/infra-proxy-service/pipeline" ) type PhaseTwoPipleine struct { @@ -143,7 +147,7 @@ func populateMembersPolicy(result <-chan PipelineData) <-chan PipelineData { func migrationTwoPipeline(source <-chan PipelineData, pipes ...PhaseTwoPipelineProcessor) { fmt.Println("Pipeline started...") - + status := make(chan string) go func() { for _, pipe := range pipes { source = pipe(source) @@ -152,7 +156,9 @@ func migrationTwoPipeline(source <-chan PipelineData, pipes ...PhaseTwoPipelineP for s := range source { s.Done <- nil } + status <- "Done" }() + <-status } func SetupPhaseTwoPipeline() PhaseTwoPipleine { @@ -167,7 +173,7 @@ func SetupPhaseTwoPipeline() PhaseTwoPipleine { return PhaseTwoPipleine{in: c} } -func (p *PhaseTwoPipleine) Run(result Result) { +func (p *PhaseTwoPipleine) Run(result pipeline.Result) { status := make(chan string) go func() { ctx, cancel := context.WithCancel(context.Background()) @@ -178,9 +184,11 @@ func (p *PhaseTwoPipleine) Run(result Result) { } err := <-done if err != nil { - fmt.Println("received error") + MigrationError(err, Mig, ctx, result.Meta.MigrationID, result.Meta.ServerID) + log.Errorf("Phase two pipeline received error for migration %s: %s", result.Meta.MigrationID, err) } - fmt.Println("received done") + MigrationSuccess(Mig, ctx, result.Meta.MigrationID, result.Meta.ServerID) + log.Println("received done") status <- "Done" }() <-status diff --git a/components/infra-proxy-service/migrations/pipeline/utility.go b/components/infra-proxy-service/migrations/pipeline/utility.go index dbcc8411345..1cd942534c0 100644 --- a/components/infra-proxy-service/migrations/pipeline/utility.go +++ b/components/infra-proxy-service/migrations/pipeline/utility.go @@ -1,20 +1,25 @@ package pipeline import ( + "archive/zip" "context" "encoding/json" "fmt" - "github.com/chef/automate/api/interservice/authz" + "io" "io/ioutil" "os" "path" + "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" ) // StoreOrgs reads the Result struct and populate the orgs table -func StoreOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationStorage, authzProjectClient authz.ProjectsServiceClient, res Result) (Result, error) { +func StoreOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationStorage, authzProjectClient authz.ProjectsServiceClient, res pipeline.Result) (pipeline.Result, error) { var err error var msg string var totalSucceeded, totalSkipped, totalFailed int64 @@ -30,7 +35,7 @@ func StoreOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationSto msg = err.Error() continue } - if org.ActionOps == Skip { + if org.ActionOps == pipeline.Skip { totalSkipped++ continue } @@ -51,24 +56,24 @@ func StoreOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationSto } // StoreOrg stores a single Org into DB -func StoreOrg(ctx context.Context, st storage.Storage, org Org, serverID string, authzProjectClient authz.ProjectsServiceClient) (error, ActionOps) { - var actionTaken ActionOps +func StoreOrg(ctx context.Context, st storage.Storage, org pipeline.Org, serverID string, authzProjectClient authz.ProjectsServiceClient) (error, pipeline.ActionOps) { + var actionTaken pipeline.ActionOps var err error switch org.ActionOps { - case Insert: + case pipeline.Insert: projects, err := createProjectFromOrgIdAndServerID(ctx, serverID, org.Name, authzProjectClient) if err != nil { log.Errorf("Unable to create project for serverid: %s", serverID) return err, actionTaken } _, err = st.StoreOrg(ctx, org.Name, org.FullName, "", "", serverID, projects) - actionTaken = Insert - case Delete: + actionTaken = pipeline.Insert + case pipeline.Delete: _, err = st.DeleteOrg(ctx, org.Name, serverID) - actionTaken = Delete - case Update: + actionTaken = pipeline.Delete + case pipeline.Update: _, err = st.EditOrg(ctx, org.Name, org.FullName, "", serverID, nil) - actionTaken = Update + actionTaken = pipeline.Update default: } return err, actionTaken @@ -91,7 +96,7 @@ func createProjectFromOrgIdAndServerID(ctx context.Context, serverId string, org return []string{projectID.Project.Name}, nil } -func ParseOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationStorage, result Result) (Result, error) { +func ParseOrgs(ctx context.Context, st storage.Storage, mst storage.MigrationStorage, result pipeline.Result) (pipeline.Result, error) { var err error log.Info("Starting with organisation parsing phase for migration id: ", result.Meta.MigrationID) _, err = mst.StartOrgParsing(ctx, result.Meta.MigrationID, result.Meta.ServerID) @@ -156,10 +161,10 @@ func createFileOrgsMap(orgs []os.FileInfo) map[string]string { return orgMap } -func insertOrUpdateOrg(orgsInFiles []os.FileInfo, orgsInDB []storage.Org, orgPath string) []Org { - var orgList []Org +func insertOrUpdateOrg(orgsInFiles []os.FileInfo, orgsInDB []storage.Org, orgPath string) []pipeline.Org { + var orgList []pipeline.Org orgDatabaseMap := createDatabaseOrgsMap(orgsInDB) - var orgJson OrgJson + var orgJson pipeline.OrgJson log.Info("Comparing the organisations from database and backup file for insert,update and skip action") //For insert, update and skip action for _, org := range orgsInFiles { @@ -169,15 +174,15 @@ func insertOrUpdateOrg(orgsInFiles []os.FileInfo, orgsInDB []storage.Org, orgPat if valuePresent { if orgJson.FullName != orgInfo { //Update org in the result actions - orgList = append(orgList, createOrgStructForAction(orgJson.Name, orgJson.FullName, Update)) + orgList = append(orgList, createOrgStructForAction(orgJson.Name, orgJson.FullName, pipeline.Update)) } else { //Skip org action if full names are not equal - orgList = append(orgList, createOrgStructForAction(orgJson.Name, orgJson.FullName, Skip)) + orgList = append(orgList, createOrgStructForAction(orgJson.Name, orgJson.FullName, pipeline.Skip)) } } else { //Insert org action if not present in database - orgList = append(orgList, createOrgStructForAction(orgJson.Name, orgJson.FullName, Insert)) + orgList = append(orgList, createOrgStructForAction(orgJson.Name, orgJson.FullName, pipeline.Insert)) } } } @@ -185,23 +190,23 @@ func insertOrUpdateOrg(orgsInFiles []os.FileInfo, orgsInDB []storage.Org, orgPat return orgList } -func deleteOrgsIfNotPresentInCurrentFile(orgsInFiles []os.FileInfo, orgsInDB []storage.Org) []Org { - var orgList []Org +func deleteOrgsIfNotPresentInCurrentFile(orgsInFiles []os.FileInfo, orgsInDB []storage.Org) []pipeline.Org { + var orgList []pipeline.Org orgFilesMap := createFileOrgsMap(orgsInFiles) log.Info("Comparing the organisations from database and backup file for delete action") //For delete action by comparing database orgs with file orgs for _, org := range orgsInDB { _, valuePresent := orgFilesMap[org.ID] if !valuePresent { - orgList = append(orgList, createOrgStructForAction(org.ID, org.Name, Delete)) + orgList = append(orgList, createOrgStructForAction(org.ID, org.Name, pipeline.Delete)) } } log.Info("Completed comparing the organisations from database and backup file for delete action") return orgList } -func openOrgFolder(org os.FileInfo, fileLocation string) OrgJson { - var orgJson OrgJson +func openOrgFolder(org os.FileInfo, fileLocation string) pipeline.OrgJson { + var orgJson pipeline.OrgJson jsonPath := path.Join(fileLocation, org.Name(), "org.json") jsonFile, err := os.Open(jsonPath) // if we os.Open returns an error then handle it @@ -217,10 +222,77 @@ func openOrgFolder(org os.FileInfo, fileLocation string) OrgJson { return orgJson } -func createOrgStructForAction(orgId string, orgName string, ops ActionOps) Org { - return Org{ +func createOrgStructForAction(orgId string, orgName string, ops pipeline.ActionOps) pipeline.Org { + return pipeline.Org{ Name: orgId, FullName: orgName, ActionOps: ops, } } + +// Unzip will decompress a zip file and sets the UnzipFolder +func Unzip(ctx context.Context, mst storage.MigrationStorage, result pipeline.Result) (pipeline.Result, error) { + var fpath string + + reader, err := zip.OpenReader(result.Meta.ZipFile) + if err != nil { + log.Errorf("cannot open reader: %s.", err) + mst.FailedUnzip(ctx, result.Meta.MigrationID, result.Meta.ServerID, "cannot open zipfile", 0, 0, 0) + return result, err + } + + for _, file := range reader.File { + + fpath = filepath.Join(filepath.Dir(result.Meta.ZipFile), file.Name) + + if file.FileInfo().IsDir() { + os.MkdirAll(fpath, os.ModePerm) + continue + } + + // Creating the files in the target directory + if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil { + log.Errorf("cannot create directory: %s. ", err) + mst.FailedUnzip(ctx, result.Meta.MigrationID, result.Meta.ServerID, "cannot create directory", 0, 0, 0) + return result, err + } + + // The created file will be stored in + // outFile with permissions to write &/or truncate + outFile, err := os.OpenFile(fpath, + os.O_WRONLY|os.O_CREATE|os.O_TRUNC, + file.Mode()) + if err != nil { + log.Errorf("cannot create a file: %s.", err) + mst.FailedUnzip(ctx, result.Meta.MigrationID, result.Meta.ServerID, "cannot create a file", 0, 0, 0) + return result, err + } + + readClose, err := file.Open() + if err != nil { + log.Errorf("cannot open file") + mst.FailedUnzip(ctx, result.Meta.MigrationID, result.Meta.ServerID, "cannot open file", 0, 0, 0) + return result, err + } + + _, err = io.Copy(outFile, readClose) + if err != nil { + log.Errorf("cannot copy file") + mst.FailedUnzip(ctx, result.Meta.MigrationID, result.Meta.ServerID, "cannot copy file", 0, 0, 0) + return result, err + } + + outFile.Close() + readClose.Close() + + if err != nil { + log.Errorf("cannot copy a file") + mst.FailedUnzip(ctx, result.Meta.MigrationID, result.Meta.ServerID, "cannot copy a file", 0, 0, 0) + return result, err + } + } + + result.Meta.UnzipFolder = filepath.Dir(fpath) + reader.Close() + return result, nil +} diff --git a/components/infra-proxy-service/migrations/pipeline/utility_test.go b/components/infra-proxy-service/migrations/pipeline/utility_test.go index c0941874ffb..5069e49606e 100644 --- a/components/infra-proxy-service/migrations/pipeline/utility_test.go +++ b/components/infra-proxy-service/migrations/pipeline/utility_test.go @@ -2,20 +2,22 @@ package pipeline import ( "context" + "reflect" + "testing" + "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" "github.com/chef/automate/components/infra-proxy-service/storage/testDB" "github.com/golang/mock/gomock" "github.com/pkg/errors" - "reflect" - "testing" ) func TestStoreOrg(t *testing.T) { type args struct { ctx context.Context st storage.Storage - org Org + org pipeline.Org serverID string authzMock *authz.MockProjectsServiceClient } @@ -24,12 +26,12 @@ func TestStoreOrg(t *testing.T) { errorFromProject bool args args want error - want1 ActionOps + want1 pipeline.ActionOps }{ - {name: "Test Delete Org", errorFromProject: false, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: Org{Name: "org3", FullName: "Org 1", ActionOps: Delete}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: nil, want1: Delete}, - {name: "Test Store Org", errorFromProject: false, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: Org{Name: "org2", FullName: "Org 2", ActionOps: Insert}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: nil, want1: Insert}, - {name: "Test Edit Org", errorFromProject: false, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: Org{Name: "org3", FullName: "Org 3", ActionOps: Update}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: nil, want1: Update}, - {name: "Test Create Project Error", errorFromProject: true, args: args{ctx: context.Background(), st: &testDB.TestDB{}, org: Org{Name: "org3", FullName: "Org 3", ActionOps: Insert}, serverID: "server1", authzMock: authz.NewMockProjectsServiceClient(gomock.NewController(t))}, want: errors.New("Project already exists"), want1: 0}, + {name: "Test 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: nil, want1: pipeline.Delete}, + {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}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -63,20 +65,20 @@ func TestParseOrg(t *testing.T) { ctx context.Context st storage.Storage mst storage.MigrationStorage - result Result + result pipeline.Result } tests := []struct { name string args args wantError error - want1 ActionOps + want1 pipeline.ActionOps }{ - {name: "Test Insert Org", args: args{ctx: context.Background(), st: &testDB.TestDB{Type: "Insert"}, mst: &testDB.MigrationDB{}, result: Result{Meta: Meta{UnzipFolder: "../../testdata/insertBackup/", ServerID: "server1", MigrationID: "mig1"}}}, wantError: nil, want1: Insert}, - {name: "Test Update Org", args: args{ctx: context.Background(), st: &testDB.TestDB{Type: "Update"}, mst: &testDB.MigrationDB{}, result: Result{Meta: Meta{UnzipFolder: "../../testdata/updateBackup", ServerID: "server1", MigrationID: "mig1"}}}, wantError: nil, want1: Update}, - {name: "Test Delete Org", args: args{ctx: context.Background(), st: &testDB.TestDB{Type: "Delete"}, mst: &testDB.MigrationDB{}, result: Result{Meta: Meta{UnzipFolder: "../../testdata/deleteBackup", ServerID: "server1", MigrationID: "mig1"}}}, wantError: nil, want1: Delete}, - {name: "Test Skip Org", args: args{ctx: context.Background(), st: &testDB.TestDB{Type: "Skip"}, mst: &testDB.MigrationDB{}, result: Result{Meta: Meta{UnzipFolder: backupFolderDefault, ServerID: "server1", MigrationID: "mig1"}}}, wantError: nil, want1: Skip}, - {name: "Test Error from Org database", args: args{ctx: context.Background(), st: &testDB.TestDB{NeedError: true}, mst: &testDB.MigrationDB{}, result: Result{Meta: Meta{UnzipFolder: backupFolderDefault, ServerID: "server1", MigrationID: "mig1"}}}, wantError: errors.New("failed to fetch Orgs"), want1: Skip}, - {name: "Test Error from Status database", args: args{ctx: context.Background(), st: &testDB.TestDB{}, mst: &testDB.MigrationDB{NeedError: true}, result: Result{Meta: Meta{UnzipFolder: backupFolderDefault, ServerID: "server1", MigrationID: "mig1"}}}, wantError: errors.New("Failed to update status"), want1: Skip}, + {name: "Test Insert Org", args: args{ctx: context.Background(), st: &testDB.TestDB{Type: "Insert"}, mst: &testDB.MigrationDB{}, result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/insertBackup/", ServerID: "server1", MigrationID: "mig1"}}}, wantError: nil, want1: pipeline.Insert}, + {name: "Test Update Org", args: args{ctx: context.Background(), st: &testDB.TestDB{Type: "Update"}, mst: &testDB.MigrationDB{}, result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/updateBackup", ServerID: "server1", MigrationID: "mig1"}}}, wantError: nil, want1: pipeline.Update}, + {name: "Test Delete Org", args: args{ctx: context.Background(), st: &testDB.TestDB{Type: "Delete"}, mst: &testDB.MigrationDB{}, result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/deleteBackup", ServerID: "server1", MigrationID: "mig1"}}}, wantError: nil, want1: pipeline.Delete}, + {name: "Test Skip Org", args: args{ctx: context.Background(), st: &testDB.TestDB{Type: "Skip"}, mst: &testDB.MigrationDB{}, result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: backupFolderDefault, ServerID: "server1", MigrationID: "mig1"}}}, wantError: nil, want1: pipeline.Skip}, + {name: "Test Error from Org database", args: args{ctx: context.Background(), st: &testDB.TestDB{NeedError: true}, mst: &testDB.MigrationDB{}, result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: backupFolderDefault, ServerID: "server1", MigrationID: "mig1"}}}, wantError: errors.New("failed to fetch Orgs"), want1: pipeline.Skip}, + {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{UnzipFolder: backupFolderDefault, ServerID: "server1", MigrationID: "mig1"}}}, wantError: errors.New("Failed to update status"), want1: pipeline.Skip}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/components/infra-proxy-service/service/service.go b/components/infra-proxy-service/service/service.go index b017ece5246..e52902d0af2 100644 --- a/components/infra-proxy-service/service/service.go +++ b/components/infra-proxy-service/service/service.go @@ -9,6 +9,8 @@ import ( secrets "github.com/chef/automate/api/external/secrets" "github.com/chef/automate/api/interservice/authz" + + "github.com/chef/automate/components/infra-proxy-service/migrations/pipeline" "github.com/chef/automate/components/infra-proxy-service/storage" "github.com/chef/automate/components/infra-proxy-service/storage/postgres" "github.com/chef/automate/components/infra-proxy-service/storage/postgres/migration" @@ -32,6 +34,8 @@ func Start(l logger.Logger, migrationsConfig migration.Config, connFactory *secu if err != nil { return nil, err } + pipeline.Storage = p + pipeline.Mig = pObj return &Service{ Logger: l, ConnFactory: connFactory,