Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dave/stalwart 27 #6719

Merged
merged 8 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions components/infra-proxy-service/migrations/pipeline/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"archive/zip"
"context"
"encoding/json"

"errors"

"io"
"os"
"path"
Expand Down Expand Up @@ -342,6 +345,37 @@ func Unzip(ctx context.Context, mst storage.MigrationStorage, result pipeline.Re
return result, nil
}

func ValidateZip(ctx context.Context, st storage.Storage, mst storage.MigrationStorage, result pipeline.Result) (pipeline.Result, error) {
var err error
foundOrg := false
// Find under unzip folder where org folder exists
if err := filepath.Walk(result.Meta.UnzipFolder, func(path string, f os.FileInfo, err error) error {

// check if base path is `organizations`
if filepath.Base(path) == "organizations" {
// reassign unzipFolder
foundOrg = true
result.Meta.UnzipFolder = filepath.Dir(path)
}
return nil
}); err != nil {
log.Errorf("Failed to find organizations folder for migration id %s : %s", result.Meta.MigrationID, err.Error())
return result, err
}
if !foundOrg {
return result, errors.New("cannot find organizations folder")
}
// Check if the reassigned unzip folder contains key_dump.json
_, err = os.Stat(result.Meta.UnzipFolder + "/key_dump.json")
if err != nil {
log.Errorf("Failed to validate unzip folder for migration id %s : %s", result.Meta.MigrationID, err.Error())
return result, err
}

result.Meta.IsValid = true
return result, nil
}

// ParseOrgUserAssociation sync the automate org users with chef server org users
func ParseOrgUserAssociation(ctx context.Context, st storage.Storage, result pipeline.Result) (pipeline.Result, error) {
log.Info("Starting with the parsing org user association for migration id :", result.Meta.MigrationID)
Expand Down Expand Up @@ -534,4 +568,5 @@ func createMapForOrgAdminsInJson(adminsJson pipeline.AdminsJson) map[string]stri
orgAdminsMap[username] = ""
}
return orgAdminsMap

}
53 changes: 53 additions & 0 deletions components/infra-proxy-service/migrations/pipeline/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/chef/automate/components/infra-proxy-service/storage/testDB"
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

func TestStoreOrg(t *testing.T) {
Expand Down Expand Up @@ -130,6 +131,57 @@ func TestCreatePreview(t *testing.T) {
}
}

func TestValidateZip(t *testing.T) {
type args struct {
name string
ctx context.Context
st storage.Storage
mst storage.MigrationStorage
result pipeline.Result
requiredErr error
}

arg := []args{
{
name: "Validated",
ctx: context.Background(),
st: &testDB.TestDB{},
mst: &testDB.MigrationDB{},
result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/backup", ServerID: "server1", MigrationID: "mig1"}},
requiredErr: nil,
},
{
name: "Organization not found",
ctx: context.Background(),
st: &testDB.TestDB{},
mst: &testDB.MigrationDB{},
result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/orgnotfound", ServerID: "server1", MigrationID: "mig1"}},
requiredErr: errors.New("cannot find organizations folder"),
},
{
name: "KeyDump not found",
ctx: context.Background(),
st: &testDB.TestDB{},
mst: &testDB.MigrationDB{},
result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/keydumpnotfound", ServerID: "server1", MigrationID: "mig1"}},
requiredErr: errors.New("stat ../../testdata/keydumpnotfound/key_dump.json: no such file or directory"),
},
}

for _, ar := range arg {
t.Run(ar.name, func(t *testing.T) {
res, err := ValidateZip(ar.ctx, ar.st, ar.mst, ar.result)
if err != nil {
require.Equal(t, err.Error(), ar.requiredErr.Error())
} else {
require.NoError(t, err)
require.True(t, res.Meta.IsValid)
require.NotEmpty(t, res.Meta.UnzipFolder)
}
})
}
}

func TestUserOrgAssociation(t *testing.T) {
deleteBackUp := "../../testdata/deleteBackup/"
skipBackup := "../../testdata/skipBackup/"
Expand Down Expand Up @@ -160,6 +212,7 @@ func TestUserOrgAssociation(t *testing.T) {
if !reflect.DeepEqual(got.ParsedResult.OrgsUsers, tt.want1.ParsedResult.OrgsUsers) {
t.Errorf("ParseOrgUserAssociation() got = %v, want %v", got.ParsedResult.OrgsUsers, tt.want1.ParsedResult.OrgsUsers)
}

})
}
}
3 changes: 3 additions & 0 deletions components/infra-proxy-service/pipeline/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Meta struct {

// Migration ID
MigrationID string `json:"migration_id"`

// To check whether zip file extracted successfully
IsValid bool `json:"is_valid"`
}

type StageResult struct {
Expand Down
Empty file.